Craft Anything: understand how any software works, and craft your own.
Rust
Start the window on Macs whose Metal compiler rejects the OS's shader version (#744)
Fábio Soaresbbc06cb9c12d
diff --git a/Cargo.lock b/Cargo.lockindex b640c700..28acd1e9 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -7327,8 +7327,6 @@ dependencies = [[[package]]name = "wgpu-hal"version = "30.0.1"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "b6b7fb58561a792bc237628ba0792e332de418fefe145f13b5ed8201e6d52f58"dependencies = ["android_system_properties","arrayvec",diff --git a/Cargo.toml b/Cargo.tomlindex 856a84ba..c9b68fe1 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -92,3 +92,8 @@ codegen-units = 1debug = falsestrip = truepanic = "abort"++# wgpu-hal with a Metal shader-language probe (Craft Anything #742; upstream wgpu #10687).+# Remove this once the upstream wgpu release picks a Metal version the compiler accepts. See vendor/wgpu-hal/CRAFT_ANYTHING.md.+[patch.crates-io]+wgpu-hal = { path = "vendor/wgpu-hal" }diff --git a/apps/craft-anything/src/main.rs b/apps/craft-anything/src/main.rsindex 62be2cdf..87ec9602 100644--- a/apps/craft-anything/src/main.rs+++ b/apps/craft-anything/src/main.rs@@ -135,11 +135,16 @@ fn wants_software(setting: Option<&str>) -> bool {setting.is_some_and(|s| s.trim().eq_ignore_ascii_case("software"))}-/// What to try when the window can't start: a lost graphics device gets the way to the software-/// renderer, and anything else the usual driver advice.+/// What to try when the window can't start. A lost graphics device on Windows gets the way to the+/// software renderer; on macOS that setting does nothing (only Windows picks its adapter), so it+/// gets the update advice and the report path. Anything else gets the usual driver advice.fn graphics_advice(error: &str) -> &'static str {if error.to_lowercase().contains("device is lost") {- "The graphics driver stopped while the window opened. Update it, then try again. If it keeps happening, set the environment variable CRAFT_ANYTHING_RENDERER=software to draw without the graphics card (slower), and start the app again."+ if cfg!(target_os = "macos") {+ "The Mac's graphics stopped while the window opened. Install the latest macOS update, then try again. If it keeps happening, please report it with the log file (Craft Anything issue #742 covers the Metal shader case)."+ } else {+ "The graphics driver stopped while the window opened. Update it, then try again. If it keeps happening, set the environment variable CRAFT_ANYTHING_RENDERER=software to draw without the graphics card (slower), and start the app again."+ }} else {"Updating your graphics driver often fixes this."}@@ -282,10 +287,27 @@ mod graphics_tests {assert!(matches!(dx12_compiler(Some(Dx12Compiler::StaticDxc)), Dx12Compiler::StaticDxc));}+ #[cfg(windows)]#[test]fn a_lost_device_says_how_to_draw_without_the_graphics_card() {assert!(graphics_advice("WGPU error: Parent device is lost").contains("CRAFT_ANYTHING_RENDERER=software"));assert!(!graphics_advice("No adapter").contains("software"));assert!(wants_software(Some(" Software ")) && !wants_software(Some("gpu")) && !wants_software(None));}++ /// #742: on macOS `CRAFT_ANYTHING_RENDERER` does nothing, so the advice must not name it.+ #[cfg(target_os = "macos")]+ #[test]+ fn a_lost_device_on_a_mac_does_not_point_at_the_windows_only_setting() {+ let advice = graphics_advice("WGPU error: Parent device is lost");+ assert!(!advice.contains("CRAFT_ANYTHING_RENDERER"), "{advice}");+ assert!(advice.contains("#742"));+ assert!(!graphics_advice("No adapter").contains("CRAFT_ANYTHING_RENDERER"));+ }++ #[cfg(not(windows))]+ #[test]+ fn wants_software_reads_the_setting() {+ assert!(wants_software(Some(" Software ")) && !wants_software(Some("gpu")) && !wants_software(None));+ }}diff --git a/docs/pitfalls.md b/docs/pitfalls.mdindex b25e526c..b876da21 100644--- a/docs/pitfalls.md+++ b/docs/pitfalls.md@@ -258,6 +258,13 @@ instead breaks desktops whose monitors are on the discrete card. Draw on the GPUscreen, read from the connected displays, and keep wgpu's environment variables as the override;when a window is slow to open, check which adapter `desktop.log` says it drew with.+**A Metal version taken from the OS, not from the compiler.** wgpu-hal picks the Metal shading+language from the macOS version alone; on a 2012 iMac under OpenCore Legacy Patcher, macOS 15 asked+for `metal3.2`, the compiler stopped at 3.1, every shader failed and the window never opened (#742,+upstream #10687). Our `[patch]` of wgpu-hal probes the compiler (`vendor/wgpu-hal/CRAFT_ANYTHING.md`).+The advice on a lost device named `CRAFT_ANYTHING_RENDERER`, which only Windows reads: check that+advice names only settings the platform actually reads. Not run on the reporter's Mac.+**A Mac app that couldn't find Claude Code, and offered a GitHub key instead.** An app opened fromthe Dock gets the system's short `PATH`, not the shell's, so `claude` in `~/.local/bin` wasn'tfound; "Built by" then fell back to "Claude Code on github.com-work", an `~/.ssh/config` aliasdiff --git a/vendor/wgpu-hal/CRAFT_ANYTHING.md b/vendor/wgpu-hal/CRAFT_ANYTHING.mdnew file mode 100644index 00000000..069a3985--- /dev/null+++ b/vendor/wgpu-hal/CRAFT_ANYTHING.md@@ -0,0 +1,25 @@+# wgpu-hal 30.0.1, patched for Craft Anything++This is a copy of `wgpu-hal` 30.0.1 (MIT OR Apache-2.0, see `LICENSE.*`) with one change, used through+`[patch.crates-io]` in the workspace `Cargo.toml`.++## The change++`src/metal/adapter.rs`: the Metal shader language version is no longer taken from the macOS version+alone. `accepted_msl_version` compiles a tiny probe kernel with each version up to the OS's, highest+first, and keeps the first one the system's Metal compiler accepts. If none is accepted it keeps the+OS's version, as upstream does.++## Why++On some Macs the OS expects a Metal language version its compiler does not accept. On iMac 2012 Macs+(GT 640M, macOS 15 under OpenCore Legacy Patcher) macOS 15 selects `metal3.2` and the compiler stops at+`metal3.1`. Every shader fails, the device is lost and the app's window never opens.++- Craft Anything issue #742.+- Upstream: gfx-rs/wgpu #10687 (closed as a duplicate, no fix on trunk), #10592 (open).++## Removing it++Delete this directory and the `[patch.crates-io]` entry once a released wgpu picks a Metal version the+compiler accepts (check that the 2012 iMac case in #742 works on that release first).diff --git a/vendor/wgpu-hal/Cargo.toml b/vendor/wgpu-hal/Cargo.tomlnew file mode 100644index 00000000..67f65415--- /dev/null+++ b/vendor/wgpu-hal/Cargo.toml@@ -0,0 +1,542 @@+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO+#+# When uploading crates to the registry Cargo will automatically+# "normalize" Cargo.toml files for maximal compatibility+# with all versions of Cargo and also rewrite `path` dependencies+# to registry (e.g., crates.io) dependencies.+#+# If you are reading this file be aware that the original Cargo.toml+# will likely look very different (and much more reasonable).+# See Cargo.toml.orig for the original contents.++[package]+edition = "2021"+rust-version = "1.87"+name = "wgpu-hal"+version = "30.0.1"+authors = ["gfx-rs developers"]+build = "build.rs"+autolib = false+autobins = false+autoexamples = false+autotests = false+autobenches = false+description = "Hardware abstraction layer for wgpu, the cross-platform, safe, pure-rust graphics API"+homepage = "https://wgpu.rs/"+readme = "README.md"+keywords = ["graphics"]+license = "MIT OR Apache-2.0"+repository = "https://github.com/gfx-rs/wgpu"++[package.metadata.docs.rs]+features = [+ "metal",+ "vulkan",+ "gles",+ "renderdoc",+]+rustdoc-args = [+ "--cfg",+ "docsrs",+]+targets = [+ "x86_64-unknown-linux-gnu",+ "x86_64-apple-darwin",+ "x86_64-pc-windows-msvc",+ "wasm32-unknown-unknown",+]++[package.metadata.cargo-machete]+ignored = ["cfg_aliases"]++[features]+device_lost_panic = []+dx12 = [+ "dep:arrayvec",+ "dep:bit-set",+ "dep:bytemuck",+ "dep:gpu-allocator",+ "dep:libloading",+ "dep:once_cell",+ "dep:ordered-float",+ "dep:parking_lot",+ "dep:profiling",+ "dep:range-alloc",+ "dep:windows-core",+ "gpu-allocator/d3d12",+ "naga/hlsl-out",+ "once_cell/std",+ "windows/Win32_Devices_DeviceAndDriverInstallation",+ "windows/Win32_Devices_Display",+ "windows/Win32_Graphics_Direct3D_Dxc",+ "windows/Win32_Graphics_Direct3D_Fxc",+ "windows/Win32_Graphics_Direct3D",+ "windows/Win32_Graphics_Direct3D12",+ "windows/Win32_Graphics_DirectComposition",+ "windows/Win32_Graphics_Dxgi_Common",+ "windows/Win32_Graphics_Gdi",+ "windows/Win32_Security",+ "windows/Win32_System_Diagnostics_Debug",+ "windows/Win32_System_Kernel",+ "windows/Win32_System_Performance",+ "windows/Win32_System_Threading",+ "windows/Win32_UI_WindowsAndMessaging",+]+fragile-send-sync-non-atomic-wasm = ["wgpu-types/fragile-send-sync-non-atomic-wasm"]+gles = [+ "dep:arrayvec",+ "dep:bytemuck",+ "dep:glow",+ "dep:glutin_wgl_sys",+ "dep:js-sys",+ "dep:khronos-egl",+ "dep:libloading",+ "dep:ndk-sys",+ "dep:objc2",+ "dep:parking_lot",+ "dep:profiling",+ "dep:wasm-bindgen",+ "dep:wayland-sys",+ "dep:web-sys",+ "dep:windows-result",+ "naga/glsl-out",+ "wgpu-types/web",+ "windows-result/std",+ "windows/Win32_Graphics_Gdi",+ "windows/Win32_Graphics_OpenGL",+ "windows/Win32_System_LibraryLoader",+ "windows/Win32_UI_WindowsAndMessaging",+]+internal_error_panic = []+metal = [+ "naga/msl-out",+ "dep:arrayvec",+ "dep:block2",+ "dep:bytemuck",+ "dep:libloading",+ "dep:objc2",+ "dep:objc2-core-foundation",+ "dep:objc2-core-graphics",+ "dep:objc2-foundation",+ "dep:objc2-metal",+ "dep:objc2-quartz-core",+ "dep:parking_lot",+ "dep:profiling",+ "dep:smallvec",+ "dep:raw-window-metal",+]+portable-atomic = [+ "dep:portable-atomic",+ "dep:portable-atomic-util",+]+renderdoc = [+ "dep:libloading",+ "dep:renderdoc-sys",+]+static-dxc = ["dep:mach-dxcompiler-rs"]+validation_canary = ["dep:parking_lot"]+vulkan = [+ "naga/spv-out",+ "dep:android_system_properties",+ "dep:arrayvec",+ "dep:ash",+ "dep:bytemuck",+ "dep:libc",+ "dep:libloading",+ "dep:ordered-float",+ "dep:parking_lot",+ "dep:profiling",+ "dep:raw-window-metal",+ "dep:smallvec",+ "dep:windows",+ "gpu-allocator/vulkan",+ "windows/Win32",+ "windows/Win32_Devices_Display",+ "windows/Win32_Foundation",+ "windows/Win32_Graphics_Dxgi",+ "windows/Win32_Graphics_Dxgi_Common",+ "windows/Win32_Graphics_Gdi",+]++[lib]+name = "wgpu_hal"+path = "src/lib.rs"++[[example]]+name = "halmark"+path = "examples/halmark/main.rs"++[[example]]+name = "raw-gles"+path = "examples/raw-gles.rs"+required-features = ["gles"]++[[example]]+name = "ray-traced-triangle"+path = "examples/ray-traced-triangle/main.rs"++[dependencies.arrayvec]+version = "0.7.1"+optional = true+default-features = false++[dependencies.bitflags]+version = "2.9"++[dependencies.bytemuck]+version = "1.22"+features = [+ "extern_crate_alloc",+ "min_const_generics",+ "derive",+]+optional = true++[dependencies.cfg-if]+version = "1"++[dependencies.glow]+version = "0.17"+optional = true++[dependencies.hashbrown]+version = "0.17"+features = [+ "default-hasher",+ "inline-more",+]+default-features = false++[dependencies.log]+version = "0.4.29"++[dependencies.naga]+version = "30.0.0"++[dependencies.naga-types]+version = "30.0.0"++[dependencies.ordered-float]+version = ">=3, <6.0"+optional = true+default-features = false++[dependencies.parking_lot]+version = "0.12.3"+optional = true++[dependencies.profiling]+version = "1.0.1"+optional = true+default-features = false++[dependencies.raw-window-handle]+version = "0.6.2"+default-features = false++[dependencies.static_assertions]+version = "1.1"++[dependencies.thiserror]+version = "2.0.12"+default-features = false++[dependencies.wgpu-naga-bridge]+version = "30.0.0"++[dependencies.wgpu-types]+version = "30.0.0"+default-features = false++[dev-dependencies.env_logger]+version = "0.11"+default-features = false++[dev-dependencies.glam]+version = "0.33.0"++[dev-dependencies.naga]+version = "30.0.0"+features = [+ "wgsl-in",+ "termcolor",+]++[dev-dependencies.winit]+version = "0.30.8"+features = ["android-native-activity"]++[build-dependencies.cfg_aliases]+version = "0.2.2"++[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies.js-sys]+version = "0.3.104"+optional = true+default-features = true++[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies.wasm-bindgen]+version = "0.2.127"+optional = true+default-features = false++[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies.web-sys]+version = "0.3.104"+features = [+ "default",+ "Window",+ "HtmlCanvasElement",+ "WebGl2RenderingContext",+ "OffscreenCanvas",+ "VideoFrame",+]+optional = true+default-features = false++[target.'cfg(all(unix, not(target_vendor = "apple"), not(target_family = "wasm")))'.dependencies.drm]+version = "0.15"+optional = true++[target.'cfg(all(windows, not(target_arch = "aarch64"), target_env = "msvc"))'.dependencies.mach-dxcompiler-rs]+version = "0.1.4"+optional = true+default-features = false++[target.'cfg(any(not(target_has_atomic = "64"), not(target_has_atomic = "ptr")))'.dependencies.portable-atomic]+version = "1.10"+optional = true++[target.'cfg(not(any(target_arch = "wasm32", target_os = "ios", target_os = "visionos", target_env = "ohos")))'.dev-dependencies.glutin]+version = "0.32"+features = [+ "egl",+ "wgl",+ "wayland",+ "x11",+]+default-features = false++[target.'cfg(not(any(target_arch = "wasm32", target_os = "ios", target_os = "visionos", target_env = "ohos")))'.dev-dependencies.glutin-winit]+version = "0.5"+features = [+ "egl",+ "wgl",+ "wayland",+ "x11",+]+default-features = false++[target.'cfg(not(target_arch = "wasm32"))'.dependencies.ash]+version = "0.38"+optional = true++[target.'cfg(not(target_arch = "wasm32"))'.dependencies.gpu-allocator]+version = "0.28"+features = ["hashbrown"]+optional = true+default-features = false++[target.'cfg(not(target_arch = "wasm32"))'.dependencies.khronos-egl]+version = "6"+features = ["dynamic"]+optional = true++[target.'cfg(not(target_arch = "wasm32"))'.dependencies.libloading]+version = "0.8"+optional = true++[target.'cfg(not(target_arch = "wasm32"))'.dependencies.renderdoc-sys]+version = "1"+optional = true++[target.'cfg(not(target_arch = "wasm32"))'.dependencies.smallvec]+version = "1.14"+features = ["union"]+optional = true++[target.'cfg(not(target_has_atomic = "ptr"))'.dependencies.portable-atomic-util]+version = "0.2.4"+features = ["alloc"]+optional = true++[target.'cfg(target_os = "android")'.dependencies.android_system_properties]+version = "0.1.1"+optional = true++[target.'cfg(target_os = "android")'.dependencies.ndk-sys]+version = "0.6"+optional = true++[target.'cfg(target_os = "emscripten")'.dependencies.khronos-egl]+version = "6"+features = [+ "static",+ "no-pkg-config",+]+optional = true++[target.'cfg(target_os = "emscripten")'.dependencies.libloading]+version = "0.8"+optional = true++[target.'cfg(target_vendor = "apple")'.dependencies.block2]+version = "0.6.2"+optional = true++[target.'cfg(target_vendor = "apple")'.dependencies.bytemuck]+version = "1.22"+features = [+ "extern_crate_alloc",+ "min_const_generics",+]+optional = true++[target.'cfg(target_vendor = "apple")'.dependencies.objc2]+version = "0.6.3"+optional = true++[target.'cfg(target_vendor = "apple")'.dependencies.objc2-core-foundation]+version = "0.3.2"+features = [+ "std",+ "CFCGTypes",+]+optional = true+default-features = false++[target.'cfg(target_vendor = "apple")'.dependencies.objc2-core-graphics]+version = "0.3.2"+features = [+ "std",+ "CGColorSpace",+]+optional = true+default-features = false++[target.'cfg(target_vendor = "apple")'.dependencies.objc2-foundation]+version = "0.3.2"+features = [+ "std",+ "NSError",+ "NSProcessInfo",+ "NSRange",+ "NSString",+]+optional = true+default-features = false++[target.'cfg(target_vendor = "apple")'.dependencies.objc2-metal]+version = "0.3.2"+features = [+ "std",+ "block2",+ "MTLAllocation",+ "MTLBlitCommandEncoder",+ "MTLBlitPass",+ "MTLBuffer",+ "MTLCaptureManager",+ "MTLCaptureScope",+ "MTLCommandBuffer",+ "MTLCommandEncoder",+ "MTLCommandQueue",+ "MTLComputeCommandEncoder",+ "MTLComputePass",+ "MTLComputePipeline",+ "MTLCounters",+ "MTLDepthStencil",+ "MTLDevice",+ "MTLDrawable",+ "MTLEvent",+ "MTLLibrary",+ "MTLPipeline",+ "MTLPixelFormat",+ "MTLRenderCommandEncoder",+ "MTLRenderPass",+ "MTLRenderPipeline",+ "MTLResource",+ "MTLSampler",+ "MTLStageInputOutputDescriptor",+ "MTLTexture",+ "MTLTypes",+ "MTLVertexDescriptor",+ "MTLAccelerationStructure",+ "MTLAccelerationStructureTypes",+ "MTLAccelerationStructureCommandEncoder",+ "MTLResidencySet",+]+optional = true+default-features = false++[target.'cfg(target_vendor = "apple")'.dependencies.objc2-quartz-core]+version = "0.3.2"+features = [+ "std",+ "objc2-core-foundation",+ "objc2-core-graphics",+ "CALayer",+ "CAMetalLayer",+ "objc2-metal",+]+optional = true+default-features = false++[target.'cfg(target_vendor = "apple")'.dependencies.raw-window-metal]+version = "1.0"+optional = true++[target."cfg(unix)".dependencies.libc]+version = "0.2.183"+optional = true+default-features = false++[target."cfg(unix)".dependencies.wayland-sys]+version = "0.31.3"+features = [+ "client",+ "dlopen",+ "egl",+]+optional = true++[target."cfg(windows)".dependencies.bit-set]+version = "0.10"+optional = true+default-features = false++[target."cfg(windows)".dependencies.glutin_wgl_sys]+version = "0.6"+optional = true++[target."cfg(windows)".dependencies.once_cell]+version = "1.21"+optional = true+default-features = false++[target."cfg(windows)".dependencies.range-alloc]+version = "0.1"+optional = true++[target."cfg(windows)".dependencies.windows]+version = "0.62"+optional = true+default-features = false++[target."cfg(windows)".dependencies.windows-core]+version = "0.62"+optional = true+default-features = false++[target."cfg(windows)".dependencies.windows-result]+version = "0.4"+optional = true+default-features = false++[lints.rust]+missing_debug_implementations = "warn"++[lints.rust.unexpected_cfgs]+level = "warn"+priority = 0+check-cfg = [+ 'cfg(feature, values("cargo-clippy"))',+ "cfg(web_sys_unstable_apis)",+]diff --git a/vendor/wgpu-hal/Cargo.toml.orig b/vendor/wgpu-hal/Cargo.toml.orignew file mode 100644index 00000000..da99cb12--- /dev/null+++ b/vendor/wgpu-hal/Cargo.toml.orig@@ -0,0 +1,365 @@+[package]+name = "wgpu-hal"+version.workspace = true+authors.workspace = true+edition.workspace = true+description = "Hardware abstraction layer for wgpu, the cross-platform, safe, pure-rust graphics API"+homepage.workspace = true+repository.workspace = true+keywords.workspace = true+license.workspace = true++# Override the workspace's `rust-version` key. `wgpu-core` and its dependencies+# have a less strict MSRV, to allow firefox more leeway in updating their Rust toolchain.+#+# See the repo README for more information on MSRV policy.+rust-version = "1.87"++[package.metadata.docs.rs]+# Ideally we would enable all the features.+#+# However, the dx12 features fail to be documented because the docs.rs runner cross-compiling under+# x86_64-unknown-linux-gnu cannot compile in that environment at the moment.+features = ["metal", "vulkan", "gles", "renderdoc"]+rustdoc-args = ["--cfg", "docsrs"]+targets = [+ "x86_64-unknown-linux-gnu",+ "x86_64-apple-darwin",+ "x86_64-pc-windows-msvc",+ "wasm32-unknown-unknown",+]++[package.metadata.cargo-machete]+# Cargo machete can't check build.rs dependencies. See https://github.com/bnjbvr/cargo-machete/issues/100+ignored = ["cfg_aliases"]++[lints.rust]+missing_debug_implementations = "warn"+unexpected_cfgs = { level = "warn", check-cfg = [+ 'cfg(feature, values("cargo-clippy"))', # objc's `msg_send` macro injects this in our code https://github.com/SSheldon/rust-objc/issues/125+ 'cfg(web_sys_unstable_apis)', # web-sys uses this+] }++[lib]++[features]+########################+### Backend Features ###+########################++# The interaction of features between wgpu-core and wgpu-hal is a bit nuanced to get+# the desired behavior on all platforms.+#+# At the wgpu-hal level the features are defined to enable the backends on all platforms+# that can compile the backend. Vulkan for example will have an effect on Windows, Mac, Linux, and Android.+# This is done with target conditional dependencies in wgpu-hal. This allows `--all-features`+# to compile on all platforms.+#+# wgpu-core's features are defined to enable the backends on their "default" platforms. For example we+# exclude the Vulkan backend on MacOS unless a separate feature `vulkan-portability` is enabled. In response+# to these features, it enables features of platform specific crates. For example, the `vulkan` feature in wgpu-core+# enables the `vulkan` feature in `wgpu-core-deps-windows-linux-android` which in turn enables the+# `vulkan` feature in `wgpu-hal` _only_ on those platforms. If you enable the `vulkan-portability` feature, it+# will enable the `vulkan` feature in `wgpu-core-deps-apple`. The only way to do this is unfortunately to have+# a separate crate for each platform category that participates in the feature unification.+#+# This trick doesn't work at the `wgpu` level, because the `wgpu` -> `wgpu-core` dependency is conditional,+# making the Cargo.toml signifigantly more complicated in all areas.+#+# See https://github.com/gfx-rs/wgpu/issues/3514, https://github.com/gfx-rs/wgpu/pull/7076,+# and https://github.com/rust-lang/cargo/issues/1197 for more information.++## Enables the Metal backend when targeting Apple platforms.+metal = [+ # Metal is only available on Apple platforms, therefore request MSL output also only if we target an Apple platform.+ "naga/msl-out",+ "dep:arrayvec",+ "dep:block2",+ "dep:bytemuck",+ "dep:libloading",+ "dep:objc2",+ "dep:objc2-core-foundation",+ "dep:objc2-core-graphics",+ "dep:objc2-foundation",+ "dep:objc2-metal",+ "dep:objc2-quartz-core",+ "dep:parking_lot",+ "dep:profiling",+ "dep:smallvec",+ "dep:raw-window-metal",+]+vulkan = [+ "naga/spv-out",+ "dep:android_system_properties",+ "dep:arrayvec",+ "dep:ash",+ "dep:bytemuck",+ "dep:libc",+ "dep:libloading",+ "dep:ordered-float",+ "dep:parking_lot",+ "dep:profiling",+ "dep:raw-window-metal",+ "dep:smallvec",+ "dep:windows",+ "gpu-allocator/vulkan",+ "windows/Win32",+ "windows/Win32_Devices_Display",+ "windows/Win32_Foundation",+ "windows/Win32_Graphics_Dxgi",+ "windows/Win32_Graphics_Dxgi_Common",+ "windows/Win32_Graphics_Gdi",+]+gles = [+ "dep:arrayvec",+ "dep:bytemuck",+ "dep:glow",+ "dep:glutin_wgl_sys",+ "dep:js-sys",+ "dep:khronos-egl",+ "dep:libloading",+ "dep:ndk-sys",+ "dep:objc2",+ "dep:parking_lot",+ "dep:profiling",+ "dep:wasm-bindgen",+ "dep:wayland-sys",+ "dep:web-sys",+ "dep:windows-result",+ "naga/glsl-out",+ "wgpu-types/web",+ "windows-result/std", # Need to enable this for the `Error` impl on `windows_result::Error` to work.+ "windows/Win32_Graphics_Gdi",+ "windows/Win32_Graphics_OpenGL",+ "windows/Win32_System_LibraryLoader",+ "windows/Win32_UI_WindowsAndMessaging",+]+## Enables the DX12 backend when targeting Windows.+dx12 = [+ "dep:arrayvec",+ "dep:bit-set",+ "dep:bytemuck",+ "dep:gpu-allocator",+ "dep:libloading",+ "dep:once_cell",+ "dep:ordered-float",+ "dep:parking_lot",+ "dep:profiling",+ "dep:range-alloc",+ "dep:windows-core",+ "gpu-allocator/d3d12",+ "naga/hlsl-out",+ "once_cell/std",+ "windows/Win32_Devices_DeviceAndDriverInstallation",+ "windows/Win32_Devices_Display",+ "windows/Win32_Graphics_Direct3D_Dxc",+ "windows/Win32_Graphics_Direct3D_Fxc",+ "windows/Win32_Graphics_Direct3D",+ "windows/Win32_Graphics_Direct3D12",+ "windows/Win32_Graphics_DirectComposition",+ "windows/Win32_Graphics_Dxgi_Common",+ "windows/Win32_Graphics_Gdi",+ "windows/Win32_Security",+ "windows/Win32_System_Diagnostics_Debug",+ "windows/Win32_System_Kernel",+ "windows/Win32_System_Performance",+ "windows/Win32_System_Threading",+ "windows/Win32_UI_WindowsAndMessaging",+]++###########################+### Misc Other Features ###+###########################++static-dxc = ["dep:mach-dxcompiler-rs"]+renderdoc = ["dep:libloading", "dep:renderdoc-sys"]+fragile-send-sync-non-atomic-wasm = [+ "wgpu-types/fragile-send-sync-non-atomic-wasm",+]+portable-atomic = ["dep:portable-atomic", "dep:portable-atomic-util"]++###################################+### Internal Debugging Features ###+###################################++# Panic when running into a device lost error (for debugging purposes).+# Only affects the d3d12 and vulkan backends.+device_lost_panic = []+# Panic when running into an internal error other than out-of-memory and device lost+# (for debugging purposes).+#+# Only affects the d3d12 and vulkan backends.+internal_error_panic = []+# Tracks validation errors in a `VALIDATION_CANARY` static.+validation_canary = ["dep:parking_lot"]++[[example]]+name = "halmark"++[[example]]+name = "raw-gles"+required-features = ["gles"]++#####################+### Platform: All ###+#####################++[dependencies]+naga.workspace = true+wgpu-naga-bridge.workspace = true+naga-types = { workspace = true }+wgpu-types = { workspace = true, default-features = false }++# Dependencies in the lib and empty backend+bitflags.workspace = true+cfg-if.workspace = true+log = { workspace = true }+parking_lot = { workspace = true, optional = true }+raw-window-handle.workspace = true+thiserror.workspace = true++# Target agnostic dependencies used only in backends.+arrayvec = { workspace = true, optional = true }+bytemuck = { workspace = true, optional = true, features = ["derive"] }+hashbrown = { workspace = true }+ordered-float = { workspace = true, optional = true }+profiling = { workspace = true, optional = true, default-features = false }+static_assertions = { workspace = true }++# Backend: GLES+glow = { workspace = true, optional = true }++########################+### Platform: Native ###+########################++[target.'cfg(not(target_arch = "wasm32"))'.dependencies]+# Backend: Vulkan and Dx12+gpu-allocator = { workspace = true, optional = true }+# Backend: Vulkan+ash = { workspace = true, optional = true }+smallvec = { workspace = true, optional = true, features = ["union"] }+# Backend: GLES+khronos-egl = { workspace = true, features = ["dynamic"], optional = true }+libloading = { workspace = true, optional = true }+renderdoc-sys = { workspace = true, optional = true }++##########################+### Platform: All Unix ###+##########################++[target.'cfg(unix)'.dependencies]+# Backend: Vulkan+libc = { workspace = true, optional = true }+# backend: GLES+wayland-sys = { version = "0.31.3", features = [+ "client",+ "dlopen",+ "egl",+], optional = true }++## Support creating textures from DRM display/window handles.+[target.'cfg(all(unix, not(target_vendor = "apple"), not(target_family = "wasm")))'.dependencies]+drm = { version = "0.15", optional = true }++#########################+### Platform: Windows ###+#########################++[target.'cfg(windows)'.dependencies]+# Backend: Dx12 and GLES+windows = { workspace = true, optional = true }+windows-core = { workspace = true, optional = true }+windows-result = { workspace = true, optional = true }+# Backend: Dx12+bit-set = { workspace = true, optional = true }+range-alloc = { workspace = true, optional = true }+once_cell = { workspace = true, optional = true }+# backend: GLES+glutin_wgl_sys = { workspace = true, optional = true }++### Platform: x86/x86_64 Windows ###+# This doesn't support aarch64. See https://github.com/gfx-rs/wgpu/issues/6860.+# This doesn't support x86_64-pc-windows-gnu. See https://github.com/gfx-rs/wgpu/issues/8303+#+# ⚠️ Keep in sync with static_dxc cfg in build.rs and cfg_alias in `wgpu` crate ⚠️+[target.'cfg(all(windows, not(target_arch = "aarch64"), target_env = "msvc"))'.dependencies]+mach-dxcompiler-rs = { workspace = true, optional = true }++#######################+### Platform: Apple ###+#######################++[target.'cfg(target_vendor = "apple")'.dependencies]+# Backend: Metal+block2 = { workspace = true, optional = true }+bytemuck = { workspace = true, optional = true }+objc2 = { workspace = true, optional = true }+objc2-core-foundation = { workspace = true, optional = true }+objc2-core-graphics = { workspace = true, optional = true }+objc2-foundation = { workspace = true, optional = true }+objc2-metal = { workspace = true, optional = true }+objc2-quartz-core = { workspace = true, optional = true }++# backend: Metal + Vulkan+raw-window-metal = { workspace = true, optional = true }++#########################+### Platform: Android ###+#########################++[target.'cfg(target_os = "android")'.dependencies]+android_system_properties = { workspace = true, optional = true }+ndk-sys = { workspace = true, optional = true }++#############################+### Platform: Webassembly ###+#############################++[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]+# Backend: GLES+wasm-bindgen = { workspace = true, optional = true }+web-sys = { workspace = true, optional = true, features = [+ "default",+ "Window",+ "HtmlCanvasElement",+ "WebGl2RenderingContext",+ "OffscreenCanvas",+ "VideoFrame",+] }+js-sys = { workspace = true, optional = true, default-features = true }++############################+### Platform: Emscripten ###+############################++[target.'cfg(target_os = "emscripten")'.dependencies]+# Backend: GLES+khronos-egl = { workspace = true, optional = true, features = [+ "static",+ "no-pkg-config",+] }+# Note: it's unused by emscripten, but we keep it to have single code base in egl.rs+libloading = { workspace = true, optional = true }++[target.'cfg(any(not(target_has_atomic = "64"), not(target_has_atomic = "ptr")))'.dependencies]+portable-atomic = { workspace = true, optional = true }++[target.'cfg(not(target_has_atomic = "ptr"))'.dependencies]+portable-atomic-util = { workspace = true, features = [+ "alloc",+], optional = true }++[build-dependencies]+cfg_aliases.workspace = true++[dev-dependencies]+env_logger.workspace = true+glam.workspace = true # for ray-traced-triangle example+naga = { workspace = true, features = ["wgsl-in", "termcolor"] }+winit.workspace = true # for "halmark" example++### Platform: Windows + MacOS + Linux for "raw-gles" example ###+[target.'cfg(not(any(target_arch = "wasm32", target_os = "ios", target_os = "visionos", target_env = "ohos")))'.dev-dependencies]+glutin-winit = { workspace = true, features = ["egl", "wgl", "wayland", "x11"] }+glutin = { workspace = true, features = ["egl", "wgl", "wayland", "x11"] }diff --git a/vendor/wgpu-hal/LICENSE.APACHE b/vendor/wgpu-hal/LICENSE.APACHEnew file mode 100644index 00000000..d9a10c0d--- /dev/null+++ b/vendor/wgpu-hal/LICENSE.APACHE@@ -0,0 +1,176 @@+ Apache License+ Version 2.0, January 2004+ http://www.apache.org/licenses/++ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION++ 1. Definitions.++ "License" shall mean the terms and conditions for use, reproduction,+ and distribution as defined by Sections 1 through 9 of this document.++ "Licensor" shall mean the copyright owner or entity authorized by+ the copyright owner that is granting the License.++ "Legal Entity" shall mean the union of the acting entity and all+ other entities that control, are controlled by, or are under common+ control with that entity. For the purposes of this definition,+ "control" means (i) the power, direct or indirect, to cause the+ direction or management of such entity, whether by contract or+ otherwise, or (ii) ownership of fifty percent (50%) or more of the+ outstanding shares, or (iii) beneficial ownership of such entity.++ "You" (or "Your") shall mean an individual or Legal Entity+ exercising permissions granted by this License.++ "Source" form shall mean the preferred form for making modifications,+ including but not limited to software source code, documentation+ source, and configuration files.++ "Object" form shall mean any form resulting from mechanical+ transformation or translation of a Source form, including but+ not limited to compiled object code, generated documentation,+ and conversions to other media types.++ "Work" shall mean the work of authorship, whether in Source or+ Object form, made available under the License, as indicated by a+ copyright notice that is included in or attached to the work+ (an example is provided in the Appendix below).++ "Derivative Works" shall mean any work, whether in Source or Object+ form, that is based on (or derived from) the Work and for which the+ editorial revisions, annotations, elaborations, or other modifications+ represent, as a whole, an original work of authorship. For the purposes+ of this License, Derivative Works shall not include works that remain+ separable from, or merely link (or bind by name) to the interfaces of,+ the Work and Derivative Works thereof.++ "Contribution" shall mean any work of authorship, including+ the original version of the Work and any modifications or additions+ to that Work or Derivative Works thereof, that is intentionally+ submitted to Licensor for inclusion in the Work by the copyright owner+ or by an individual or Legal Entity authorized to submit on behalf of+ the copyright owner. For the purposes of this definition, "submitted"+ means any form of electronic, verbal, or written communication sent+ to the Licensor or its representatives, including but not limited to+ communication on electronic mailing lists, source code control systems,+ and issue tracking systems that are managed by, or on behalf of, the+ Licensor for the purpose of discussing and improving the Work, but+ excluding communication that is conspicuously marked or otherwise+ designated in writing by the copyright owner as "Not a Contribution."++ "Contributor" shall mean Licensor and any individual or Legal Entity+ on behalf of whom a Contribution has been received by Licensor and+ subsequently incorporated within the Work.++ 2. Grant of Copyright License. Subject to the terms and conditions of+ this License, each Contributor hereby grants to You a perpetual,+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable+ copyright license to reproduce, prepare Derivative Works of,+ publicly display, publicly perform, sublicense, and distribute the+ Work and such Derivative Works in Source or Object form.++ 3. Grant of Patent License. Subject to the terms and conditions of+ this License, each Contributor hereby grants to You a perpetual,+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable+ (except as stated in this section) patent license to make, have made,+ use, offer to sell, sell, import, and otherwise transfer the Work,+ where such license applies only to those patent claims licensable+ by such Contributor that are necessarily infringed by their+ Contribution(s) alone or by combination of their Contribution(s)+ with the Work to which such Contribution(s) was submitted. If You+ institute patent litigation against any entity (including a+ cross-claim or counterclaim in a lawsuit) alleging that the Work+ or a Contribution incorporated within the Work constitutes direct+ or contributory patent infringement, then any patent licenses+ granted to You under this License for that Work shall terminate+ as of the date such litigation is filed.++ 4. Redistribution. You may reproduce and distribute copies of the+ Work or Derivative Works thereof in any medium, with or without+ modifications, and in Source or Object form, provided that You+ meet the following conditions:++ (a) You must give any other recipients of the Work or+ Derivative Works a copy of this License; and++ (b) You must cause any modified files to carry prominent notices+ stating that You changed the files; and++ (c) You must retain, in the Source form of any Derivative Works+ that You distribute, all copyright, patent, trademark, and+ attribution notices from the Source form of the Work,+ excluding those notices that do not pertain to any part of+ the Derivative Works; and++ (d) If the Work includes a "NOTICE" text file as part of its+ distribution, then any Derivative Works that You distribute must+ include a readable copy of the attribution notices contained+ within such NOTICE file, excluding those notices that do not+ pertain to any part of the Derivative Works, in at least one+ of the following places: within a NOTICE text file distributed+ as part of the Derivative Works; within the Source form or+ documentation, if provided along with the Derivative Works; or,+ within a display generated by the Derivative Works, if and+ wherever such third-party notices normally appear. The contents+ of the NOTICE file are for informational purposes only and+ do not modify the License. You may add Your own attribution+ notices within Derivative Works that You distribute, alongside+ or as an addendum to the NOTICE text from the Work, provided+ that such additional attribution notices cannot be construed+ as modifying the License.++ You may add Your own copyright statement to Your modifications and+ may provide additional or different license terms and conditions+ for use, reproduction, or distribution of Your modifications, or+ for any such Derivative Works as a whole, provided Your use,+ reproduction, and distribution of the Work otherwise complies with+ the conditions stated in this License.++ 5. Submission of Contributions. Unless You explicitly state otherwise,+ any Contribution intentionally submitted for inclusion in the Work+ by You to the Licensor shall be under the terms and conditions of+ this License, without any additional terms or conditions.+ Notwithstanding the above, nothing herein shall supersede or modify+ the terms of any separate license agreement you may have executed+ with Licensor regarding such Contributions.++ 6. Trademarks. This License does not grant permission to use the trade+ names, trademarks, service marks, or product names of the Licensor,+ except as required for reasonable and customary use in describing the+ origin of the Work and reproducing the content of the NOTICE file.++ 7. Disclaimer of Warranty. Unless required by applicable law or+ agreed to in writing, Licensor provides the Work (and each+ Contributor provides its Contributions) on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or+ implied, including, without limitation, any warranties or conditions+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A+ PARTICULAR PURPOSE. You are solely responsible for determining the+ appropriateness of using or redistributing the Work and assume any+ risks associated with Your exercise of permissions under this License.++ 8. Limitation of Liability. In no event and under no legal theory,+ whether in tort (including negligence), contract, or otherwise,+ unless required by applicable law (such as deliberate and grossly+ negligent acts) or agreed to in writing, shall any Contributor be+ liable to You for damages, including any direct, indirect, special,+ incidental, or consequential damages of any character arising as a+ result of this License or out of the use or inability to use the+ Work (including but not limited to damages for loss of goodwill,+ work stoppage, computer failure or malfunction, or any and all+ other commercial damages or losses), even if such Contributor+ has been advised of the possibility of such damages.++ 9. Accepting Warranty or Additional Liability. While redistributing+ the Work or Derivative Works thereof, You may choose to offer,+ and charge a fee for, acceptance of support, warranty, indemnity,+ or other liability obligations and/or rights consistent with this+ License. However, in accepting such obligations, You may act only+ on Your own behalf and on Your sole responsibility, not on behalf+ of any other Contributor, and only if You agree to indemnify,+ defend, and hold each Contributor harmless for any liability+ incurred by, or claims asserted against, such Contributor by reason+ of your accepting any such warranty or additional liability.++ END OF TERMS AND CONDITIONSdiff --git a/vendor/wgpu-hal/LICENSE.MIT b/vendor/wgpu-hal/LICENSE.MITnew file mode 100644index 00000000..8d02e4db--- /dev/null+++ b/vendor/wgpu-hal/LICENSE.MIT@@ -0,0 +1,21 @@+MIT License++Copyright (c) 2025 The gfx-rs developers++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.diff --git a/vendor/wgpu-hal/README.md b/vendor/wgpu-hal/README.mdnew file mode 100644index 00000000..1ea2f44a--- /dev/null+++ b/vendor/wgpu-hal/README.md@@ -0,0 +1,119 @@+# `wgpu_hal`: a cross-platform unsafe graphics abstraction++This crate defines a set of traits abstracting over modern graphics APIs,+with implementations ("backends") for Vulkan, Metal, Direct3D, and GL.++`wgpu_hal` is a spiritual successor to+[gfx-hal](https://github.com/gfx-rs/gfx), but with reduced scope, and+oriented towards WebGPU implementation goals. It has no overhead for+validation or tracking, and the API translation overhead is kept to the bare+minimum by the design of WebGPU. This API can be used for resource-demanding+applications and engines.++The `wgpu_hal` crate's main design choices:++- Our traits are meant to be _portable_: proper use+ should get equivalent results regardless of the backend.++- Our traits' contracts are _unsafe_: implementations perform minimal+ validation, if any, and incorrect use will often cause undefined behavior.+ This allows us to minimize the overhead we impose over the underlying+ graphics system. If you need safety, the [`wgpu-core`] crate provides a+ safe API for driving `wgpu_hal`, implementing all necessary validation,+ resource state tracking, and so on. (Note that `wgpu-core` is designed for+ use via FFI; the [`wgpu`] crate provides more idiomatic Rust bindings for+ `wgpu-core`.) Or, you can do your own validation.++- In the same vein, returned errors _only cover cases the user can't+ anticipate_, like running out of memory or losing the device. Any errors+ that the user could reasonably anticipate are their responsibility to+ avoid. For example, `wgpu_hal` returns no error for mapping a buffer that's+ not mappable: as the buffer creator, the user should already know if they+ can map it.++- We use _static dispatch_. The traits are not+ generally object-safe. You must select a specific backend type+ like [`vulkan::Api`] or [`metal::Api`], and then use that+ according to the main traits, or call backend-specific methods.++- We use _idiomatic Rust parameter passing_,+ taking objects by reference, returning them by value, and so on,+ unlike `wgpu-core`, which refers to objects by ID.++- We map buffer contents _persistently_. This means that the buffer+ can remain mapped on the CPU while the GPU reads or writes to it.+ You must explicitly indicate when data might need to be+ transferred between CPU and GPU, if `wgpu_hal` indicates that the+ mapping is not coherent (that is, automatically synchronized+ between the two devices).++- You must record _explicit barriers_ between different usages of a+ resource. For example, if a buffer is written to by a compute+ shader, and then used as and index buffer to a draw call, you+ must use [`CommandEncoder::transition_buffers`] between those two+ operations.++- Pipeline layouts are _explicitly specified_ when setting bind+ group. Incompatible layouts disturb groups bound at higher indices.++- The API _accepts collections as iterators_, to avoid forcing the user to+ store data in particular containers. The implementation doesn't guarantee+ that any of the iterators are drained, unless stated otherwise by the+ function documentation. For this reason, we recommend that iterators don't+ do any mutating work.++Unfortunately, `wgpu_hal`'s safety requirements are not fully documented.+Ideally, all trait methods would have doc comments setting out the+requirements users must meet to ensure correct and portable behavior. If you+are aware of a specific requirement that a backend imposes that is not+ensured by the traits' documented rules, please file an issue. Or, if you are+a capable technical writer, please file a pull request!++[`wgpu-core`]: https://crates.io/crates/wgpu-core+[`wgpu`]: https://crates.io/crates/wgpu+[`vulkan::Api`]: vulkan/struct.Api.html+[`metal::Api`]: metal/struct.Api.html++## Primary backends++The `wgpu_hal` crate has full-featured backends implemented on the following+platform graphics APIs:++- Vulkan, available on Linux, Android, and Windows, using the [`ash`] crate's+ Vulkan bindings. It's also available on macOS, if you install [MoltenVK].++- Metal on macOS, using the [`metal`] crate's bindings.++- Direct3D 12 on Windows, using the [`windows`] crate's bindings.++[`ash`]: https://crates.io/crates/ash+[MoltenVK]: https://github.com/KhronosGroup/MoltenVK+[`metal`]: https://crates.io/crates/metal+[`windows`]: https://crates.io/crates/windows++## Secondary backends++The `wgpu_hal` crate has a partial implementation based on the following+platform graphics API:++- The GL backend is available anywhere OpenGL, OpenGL ES, or WebGL are+ available. See the [`gles`] module documentation for details.++[`gles`]: gles/index.html++You can see what capabilities an adapter is missing by checking the+[`DownlevelCapabilities`][tdc] in [`ExposedAdapter::capabilities`], available+from [`Instance::enumerate_adapters`].++The API is generally designed to fit the primary backends better than the+secondary backends, so the latter may impose more overhead.++[tdc]: wgt::DownlevelCapabilities++## Debugging++Most of the information on the wiki [Debugging wgpu Applications][wiki-debug]+page still applies to this API, with the exception of API tracing/replay+functionality, which is only available in `wgpu-core`.++[wiki-debug]: https://github.com/gfx-rs/wgpu/wiki/Debugging-wgpu-Applicationsdiff --git a/vendor/wgpu-hal/build.rs b/vendor/wgpu-hal/build.rsnew file mode 100644index 00000000..7430ccba--- /dev/null+++ b/vendor/wgpu-hal/build.rs@@ -0,0 +1,36 @@+fn main() {+ cfg_aliases::cfg_aliases! {+ native: { not(target_arch = "wasm32") },+ send_sync: { any(+ not(target_arch = "wasm32"),+ all(feature = "fragile-send-sync-non-atomic-wasm", not(target_feature = "atomics"))+ ) },+ webgl: { all(target_arch = "wasm32", not(target_os = "emscripten"), gles) },+ Emscripten: { all(target_os = "emscripten", gles) },+ dx12: { all(target_os = "windows", feature = "dx12") },+ gles: { all(feature = "gles") },+ // Within the GL ES backend, use `std` and be Send + Sync only if we are using a target+ // that, among the ones where the GL ES backend is supported, has `std`.+ gles_with_std: { all(+ feature = "gles",+ any(+ not(target_arch = "wasm32"),+ // Accept wasm32-unknown-unknown, which uniquely has a stub `std`+ all(target_vendor = "unknown", target_os = "unknown"),+ // Accept wasm32-unknown-emscripten and similar, which has a real `std`+ target_os = "emscripten"+ )+ ) },+ metal: { all(target_vendor = "apple", feature = "metal") },+ vulkan: { all(not(target_arch = "wasm32"), feature = "vulkan") },+ drm: { all(+ feature = "drm",+ any(target_os = "linux", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")+ ) },+ any_backend: { any(dx12, metal, vulkan, gles) },+ // ⚠️ Keep in sync with target.cfg() definition in Cargo.toml and cfg_alias in `wgpu` crate ⚠️+ static_dxc: { all(target_os = "windows", feature = "static-dxc", not(target_arch = "aarch64"), target_env = "msvc") },+ supports_64bit_atomics: { target_has_atomic = "64" },+ supports_ptr_atomics: { target_has_atomic = "ptr" }+ }+}diff --git a/vendor/wgpu-hal/examples/halmark/main.rs b/vendor/wgpu-hal/examples/halmark/main.rsnew file mode 100644index 00000000..d49139ef--- /dev/null+++ b/vendor/wgpu-hal/examples/halmark/main.rs@@ -0,0 +1,919 @@+//! This example shows basic usage of wgpu-hal by rendering+//! a ton of moving sprites, each with a separate texture and draw call.+extern crate wgpu_hal as hal;++use hal::{+ Adapter as _, CommandEncoder as _, Device as _, Instance as _, Queue as _, Surface as _,+};+use raw_window_handle::{HasDisplayHandle, HasWindowHandle};+use winit::{+ application::ApplicationHandler,+ event::{ElementState, KeyEvent, WindowEvent},+ event_loop::{ActiveEventLoop, ControlFlow},+ keyboard::{Key, NamedKey},+ window::Window,+};++use std::{+ borrow::{Borrow, Cow},+ iter,+ num::NonZeroU64,+ ptr,+ time::Instant,+};++const MAX_BUNNIES: usize = 1 << 20;+const BUNNY_SIZE: f32 = 0.15 * 256.0;+const GRAVITY: f32 = -9.8 * 100.0;+const MAX_VELOCITY: f32 = 750.0;+const DESIRED_MAX_LATENCY: u32 = 2;++#[repr(C)]+#[derive(Clone, Copy)]+struct Globals {+ mvp: [[f32; 4]; 4],+ size: [f32; 2],+ pad: [f32; 2],+}++#[repr(C, align(256))]+#[derive(Clone, Copy)]+struct Locals {+ position: [f32; 2],+ velocity: [f32; 2],+ color: u32,+ _pad: u32,+}++struct ExecutionContext<A: hal::Api> {+ encoder: A::CommandEncoder,+ fence: A::Fence,+ fence_value: hal::FenceValue,+ used_views: Vec<A::TextureView>,+ used_cmd_bufs: Vec<A::CommandBuffer>,+ frames_recorded: usize,+}++impl<A: hal::Api> ExecutionContext<A> {+ unsafe fn wait_and_clear(&mut self, device: &A::Device) {+ device.wait(&self.fence, self.fence_value, None).unwrap();+ self.encoder.reset_all(self.used_cmd_bufs.drain(..));+ for view in self.used_views.drain(..) {+ device.destroy_texture_view(view);+ }+ self.frames_recorded = 0;+ }+}++#[allow(dead_code)]+struct Example<A: hal::Api> {+ instance: A::Instance,+ adapter: A::Adapter,+ surface: A::Surface,+ surface_format: wgpu_types::TextureFormat,+ device: A::Device,+ queue: A::Queue,+ global_group: A::BindGroup,+ local_group: A::BindGroup,+ global_group_layout: A::BindGroupLayout,+ local_group_layout: A::BindGroupLayout,+ pipeline_layout: A::PipelineLayout,+ shader: A::ShaderModule,+ pipeline: A::RenderPipeline,+ bunnies: Vec<Locals>,+ local_buffer: A::Buffer,+ local_alignment: u32,+ global_buffer: A::Buffer,+ sampler: A::Sampler,+ texture: A::Texture,+ texture_view: A::TextureView,+ contexts: Vec<ExecutionContext<A>>,+ context_index: usize,+ extent: [u32; 2],+ start: Instant,+}++impl<A: hal::Api> Example<A> {+ fn init(window: &winit::window::Window) -> Result<Self, Box<dyn std::error::Error>> {+ // The Instance can be initialized with the DisplayHandle from the EventLoop as well+ let raw_display_handle = window.display_handle()?;++ let instance_desc = hal::InstanceDescriptor {+ name: "example",+ flags: wgpu_types::InstanceFlags::from_build_config().with_env(),+ memory_budget_thresholds: wgpu_types::MemoryBudgetThresholds::default(),+ // Can't rely on having DXC available, so use FXC instead+ backend_options: wgpu_types::BackendOptions::default(),+ telemetry: None,+ display: Some(raw_display_handle),+ };+ let instance = unsafe { A::Instance::init(&instance_desc)? };+ let surface = {+ let raw_window_handle = window.window_handle()?.as_raw();++ unsafe {+ instance+ .create_surface(raw_display_handle.as_raw(), raw_window_handle)+ .unwrap()+ }+ };++ let (adapter, capabilities) = unsafe {+ let mut adapters = instance.enumerate_adapters(Some(&surface));+ if adapters.is_empty() {+ return Err("no adapters found".into());+ }+ let exposed = adapters.swap_remove(0);+ (exposed.adapter, exposed.capabilities)+ };++ let surface_caps = unsafe { adapter.surface_capabilities(&surface) }+ .ok_or("failed to get surface capabilities")?;+ log::info!("Surface caps: {surface_caps:#?}");++ let hal::OpenDevice { device, queue } = unsafe {+ adapter+ .open(+ wgpu_types::Features::empty(),+ &wgpu_types::Limits::default(),+ &wgpu_types::MemoryHints::default(),+ )+ .unwrap()+ };++ let window_size: (u32, u32) = window.inner_size().into();+ let surface_config = hal::SurfaceConfiguration {+ maximum_frame_latency: DESIRED_MAX_LATENCY.clamp(+ *surface_caps.maximum_frame_latency.start(),+ *surface_caps.maximum_frame_latency.end(),+ ),+ present_mode: wgpu_types::PresentMode::Fifo,+ composite_alpha_mode: wgpu_types::CompositeAlphaMode::Opaque,+ format: wgpu_types::TextureFormat::Bgra8UnormSrgb,+ color_space: wgpu_types::SurfaceColorSpace::Srgb,+ extent: wgpu_types::Extent3d {+ width: window_size.0,+ height: window_size.1,+ depth_or_array_layers: 1,+ },+ usage: wgpu_types::TextureUses::COLOR_TARGET,+ view_formats: vec![],+ };+ unsafe {+ surface.configure(&device, &surface_config).unwrap();+ };++ let naga_shader = {+ let shader_file = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))+ .join("examples")+ .join("halmark")+ .join("shader.wgsl");+ let source = std::fs::read_to_string(shader_file).unwrap();+ let module = naga::front::wgsl::Frontend::new().parse(&source).unwrap();+ let info = naga::valid::Validator::new(+ naga::valid::ValidationFlags::all(),+ naga::valid::Capabilities::empty(),+ )+ .validate(&module)+ .unwrap();+ hal::NagaShader {+ module: Cow::Owned(module),+ info,+ debug_source: None,+ }+ };+ let shader_desc = hal::ShaderModuleDescriptor {+ label: None,+ runtime_checks: wgpu_types::ShaderRuntimeChecks::checked(),+ };+ let shader = unsafe {+ device+ .create_shader_module(&shader_desc, hal::ShaderInput::Naga(naga_shader))+ .unwrap()+ };++ let global_bgl_desc = hal::BindGroupLayoutDescriptor {+ label: None,+ flags: hal::BindGroupLayoutFlags::empty(),+ entries: &[+ wgpu_types::BindGroupLayoutEntry {+ binding: 0,+ visibility: wgpu_types::ShaderStages::VERTEX,+ ty: wgpu_types::BindingType::Buffer {+ ty: wgpu_types::BufferBindingType::Uniform,+ has_dynamic_offset: false,+ min_binding_size: wgpu_types::BufferSize::new(size_of::<Globals>() as _),+ },+ count: None,+ },+ wgpu_types::BindGroupLayoutEntry {+ binding: 1,+ visibility: wgpu_types::ShaderStages::FRAGMENT,+ ty: wgpu_types::BindingType::Texture {+ sample_type: wgpu_types::TextureSampleType::Float { filterable: true },+ view_dimension: wgpu_types::TextureViewDimension::D2,+ multisampled: false,+ },+ count: None,+ },+ wgpu_types::BindGroupLayoutEntry {+ binding: 2,+ visibility: wgpu_types::ShaderStages::FRAGMENT,+ ty: wgpu_types::BindingType::Sampler(wgpu_types::SamplerBindingType::Filtering),+ count: None,+ },+ ],+ };++ let global_group_layout =+ unsafe { device.create_bind_group_layout(&global_bgl_desc).unwrap() };++ let local_bgl_desc = hal::BindGroupLayoutDescriptor {+ label: None,+ flags: hal::BindGroupLayoutFlags::empty(),+ entries: &[wgpu_types::BindGroupLayoutEntry {+ binding: 0,+ visibility: wgpu_types::ShaderStages::VERTEX,+ ty: wgpu_types::BindingType::Buffer {+ ty: wgpu_types::BufferBindingType::Uniform,+ has_dynamic_offset: true,+ min_binding_size: wgpu_types::BufferSize::new(size_of::<Locals>() as _),+ },+ count: None,+ }],+ };+ let local_group_layout =+ unsafe { device.create_bind_group_layout(&local_bgl_desc).unwrap() };++ let pipeline_layout_desc = hal::PipelineLayoutDescriptor {+ label: None,+ flags: hal::PipelineLayoutFlags::empty(),+ bind_group_layouts: &[Some(&global_group_layout), Some(&local_group_layout)],+ immediate_size: 0,+ };+ let pipeline_layout = unsafe {+ device+ .create_pipeline_layout(&pipeline_layout_desc)+ .unwrap()+ };++ let constants = naga::back::PipelineConstants::default();+ let pipeline_desc = hal::RenderPipelineDescriptor {+ label: None,+ layout: &pipeline_layout,+ vertex_processor: hal::VertexProcessor::Standard {+ vertex_stage: hal::ProgrammableStage {+ module: &shader,+ entry_point: "vs_main",+ constants: &constants,+ zero_initialize_workgroup_memory: true,+ },+ vertex_buffers: &[],+ },+ fragment_stage: Some(hal::ProgrammableStage {+ module: &shader,+ entry_point: "fs_main",+ constants: &constants,+ zero_initialize_workgroup_memory: true,+ }),+ primitive: wgpu_types::PrimitiveState {+ topology: wgpu_types::PrimitiveTopology::TriangleStrip,+ ..wgpu_types::PrimitiveState::default()+ },+ depth_stencil: None,+ multisample: wgpu_types::MultisampleState::default(),+ color_targets: &[Some(wgpu_types::ColorTargetState {+ format: surface_config.format,+ blend: Some(wgpu_types::BlendState::ALPHA_BLENDING),+ write_mask: wgpu_types::ColorWrites::default(),+ })],+ multiview_mask: None,+ cache: None,+ };+ let pipeline = unsafe { device.create_render_pipeline(&pipeline_desc).unwrap() };++ let texture_data = [0xFFu8; 4];++ let staging_buffer_desc = hal::BufferDescriptor {+ label: Some("stage"),+ size: texture_data.len() as wgpu_types::BufferAddress,+ usage: wgpu_types::BufferUses::MAP_WRITE | wgpu_types::BufferUses::COPY_SRC,+ memory_flags: hal::MemoryFlags::TRANSIENT | hal::MemoryFlags::PREFER_COHERENT,+ };+ let staging_buffer = unsafe { device.create_buffer(&staging_buffer_desc).unwrap() };+ unsafe {+ let mapping = device+ .map_buffer(&staging_buffer, 0..staging_buffer_desc.size)+ .unwrap();+ ptr::copy_nonoverlapping(+ texture_data.as_ptr(),+ mapping.ptr.as_ptr(),+ texture_data.len(),+ );+ device.unmap_buffer(&staging_buffer);+ assert!(mapping.is_coherent);+ }++ let texture_desc = hal::TextureDescriptor {+ label: None,+ size: wgpu_types::Extent3d {+ width: 1,+ height: 1,+ depth_or_array_layers: 1,+ },+ mip_level_count: 1,+ sample_count: 1,+ dimension: wgpu_types::TextureDimension::D2,+ format: wgpu_types::TextureFormat::Rgba8UnormSrgb,+ usage: wgpu_types::TextureUses::COPY_DST | wgpu_types::TextureUses::RESOURCE,+ memory_flags: hal::MemoryFlags::empty(),+ view_formats: vec![],+ };+ let texture = unsafe { device.create_texture(&texture_desc).unwrap() };++ let cmd_encoder_desc = hal::CommandEncoderDescriptor {+ label: None,+ queue: &queue,+ };+ let mut cmd_encoder = unsafe { device.create_command_encoder(&cmd_encoder_desc).unwrap() };+ unsafe { cmd_encoder.begin_encoding(Some("init")).unwrap() };+ {+ let buffer_barrier = hal::BufferBarrier {+ buffer: &staging_buffer,+ usage: hal::StateTransition {+ from: wgpu_types::BufferUses::empty(),+ to: wgpu_types::BufferUses::COPY_SRC,+ },+ };+ let texture_barrier1 = hal::TextureBarrier {+ texture: &texture,+ range: wgpu_types::ImageSubresourceRange::default(),+ usage: hal::StateTransition {+ from: wgpu_types::TextureUses::UNINITIALIZED,+ to: wgpu_types::TextureUses::COPY_DST,+ },+ };+ let texture_barrier2 = hal::TextureBarrier {+ texture: &texture,+ range: wgpu_types::ImageSubresourceRange::default(),+ usage: hal::StateTransition {+ from: wgpu_types::TextureUses::COPY_DST,+ to: wgpu_types::TextureUses::RESOURCE,+ },+ };+ let copy = hal::BufferTextureCopy {+ buffer_layout: wgpu_types::TexelCopyBufferLayout {+ offset: 0,+ bytes_per_row: Some(4),+ rows_per_image: None,+ },+ texture_base: hal::TextureCopyBase {+ origin: wgpu_types::Origin3d::ZERO,+ mip_level: 0,+ array_layer: 0,+ aspect: hal::FormatAspects::COLOR,+ },+ size: hal::CopyExtent {+ width: 1,+ height: 1,+ depth: 1,+ },+ };+ unsafe {+ cmd_encoder.transition_buffers(iter::once(buffer_barrier));+ cmd_encoder.transition_textures(iter::once(texture_barrier1));+ cmd_encoder.copy_buffer_to_texture(&staging_buffer, &texture, iter::once(copy));+ cmd_encoder.transition_textures(iter::once(texture_barrier2));+ }+ }++ let sampler_desc = hal::SamplerDescriptor {+ label: None,+ address_modes: [wgpu_types::AddressMode::ClampToEdge; 3],+ mag_filter: wgpu_types::FilterMode::Linear,+ min_filter: wgpu_types::FilterMode::Nearest,+ mipmap_filter: wgpu_types::MipmapFilterMode::Nearest,+ lod_clamp: 0.0..32.0,+ compare: None,+ anisotropy_clamp: 1,+ border_color: None,+ };+ let sampler = unsafe { device.create_sampler(&sampler_desc).unwrap() };++ let globals = Globals {+ // cgmath::ortho() projection+ mvp: [+ [2.0 / window_size.0 as f32, 0.0, 0.0, 0.0],+ [0.0, 2.0 / window_size.1 as f32, 0.0, 0.0],+ [0.0, 0.0, 1.0, 0.0],+ [-1.0, -1.0, 0.0, 1.0],+ ],+ size: [BUNNY_SIZE; 2],+ pad: [0.0; 2],+ };++ let global_buffer_desc = hal::BufferDescriptor {+ label: Some("global"),+ size: size_of::<Globals>() as wgpu_types::BufferAddress,+ usage: wgpu_types::BufferUses::MAP_WRITE | wgpu_types::BufferUses::UNIFORM,+ memory_flags: hal::MemoryFlags::PREFER_COHERENT,+ };+ let global_buffer = unsafe {+ let buffer = device.create_buffer(&global_buffer_desc).unwrap();+ let mapping = device+ .map_buffer(&buffer, 0..global_buffer_desc.size)+ .unwrap();+ ptr::copy_nonoverlapping(+ &globals as *const Globals as *const u8,+ mapping.ptr.as_ptr(),+ size_of::<Globals>(),+ );+ device.unmap_buffer(&buffer);+ assert!(mapping.is_coherent);+ buffer+ };++ let local_alignment = wgpu_types::math::align_to(+ size_of::<Locals>() as u32,+ capabilities.limits.min_uniform_buffer_offset_alignment,+ );+ let local_buffer_desc = hal::BufferDescriptor {+ label: Some("local"),+ size: (MAX_BUNNIES as wgpu_types::BufferAddress)+ * (local_alignment as wgpu_types::BufferAddress),+ usage: wgpu_types::BufferUses::MAP_WRITE | wgpu_types::BufferUses::UNIFORM,+ memory_flags: hal::MemoryFlags::PREFER_COHERENT,+ };+ let local_buffer = unsafe { device.create_buffer(&local_buffer_desc).unwrap() };++ let view_desc = hal::TextureViewDescriptor {+ label: None,+ format: texture_desc.format,+ dimension: wgpu_types::TextureViewDimension::D2,+ usage: wgpu_types::TextureUses::RESOURCE,+ range: wgpu_types::ImageSubresourceRange::default(),+ };+ let texture_view = unsafe { device.create_texture_view(&texture, &view_desc).unwrap() };++ let global_group = {+ // SAFETY: This is the same size that was specified for buffer creation.+ let global_buffer_binding = hal::BufferBinding::new_unchecked(+ &global_buffer,+ 0,+ NonZeroU64::new(global_buffer_desc.size),+ );+ let texture_binding = hal::TextureBinding {+ view: &texture_view,+ usage: wgpu_types::TextureUses::RESOURCE,+ };+ let global_group_desc = hal::BindGroupDescriptor {+ label: Some("global"),+ layout: &global_group_layout,+ buffers: &[global_buffer_binding],+ samplers: &[&sampler],+ textures: &[texture_binding],+ acceleration_structures: &[],+ external_textures: &[],+ entries: &[+ hal::BindGroupEntry {+ binding: 0,+ resource_index: 0,+ count: 1,+ },+ hal::BindGroupEntry {+ binding: 1,+ resource_index: 0,+ count: 1,+ },+ hal::BindGroupEntry {+ binding: 2,+ resource_index: 0,+ count: 1,+ },+ ],+ };+ unsafe { device.create_bind_group(&global_group_desc).unwrap() }+ };++ let local_group = {+ // SAFETY: The size must fit within the buffer.+ let local_buffer_binding = hal::BufferBinding::new_unchecked(+ &local_buffer,+ 0,+ wgpu_types::BufferSize::new(size_of::<Locals>() as _),+ );+ let local_group_desc = hal::BindGroupDescriptor {+ label: Some("local"),+ layout: &local_group_layout,+ buffers: &[local_buffer_binding],+ samplers: &[],+ textures: &[],+ acceleration_structures: &[],+ external_textures: &[],+ entries: &[hal::BindGroupEntry {+ binding: 0,+ resource_index: 0,+ count: 1,+ }],+ };+ unsafe { device.create_bind_group(&local_group_desc).unwrap() }+ };++ let init_fence_value = 1;+ let fence = unsafe {+ let mut fence = device.create_fence().unwrap();+ let init_cmd = cmd_encoder.end_encoding().unwrap();+ queue+ .submit(&[&init_cmd], &[], (&mut fence, init_fence_value))+ .unwrap();+ device.wait(&fence, init_fence_value, None).unwrap();+ device.destroy_buffer(staging_buffer);+ cmd_encoder.reset_all(iter::once(init_cmd));+ fence+ };++ Ok(Example {+ instance,+ surface,+ surface_format: surface_config.format,+ adapter,+ device,+ queue,+ pipeline_layout,+ shader,+ pipeline,+ global_group,+ local_group,+ global_group_layout,+ local_group_layout,+ bunnies: Vec::new(),+ local_buffer,+ local_alignment,+ global_buffer,+ sampler,+ texture,+ texture_view,+ contexts: vec![ExecutionContext {+ encoder: cmd_encoder,+ fence,+ fence_value: init_fence_value + 1,+ used_views: Vec::new(),+ used_cmd_bufs: Vec::new(),+ frames_recorded: 0,+ }],+ context_index: 0,+ extent: [window_size.0, window_size.1],+ start: Instant::now(),+ })+ }++ fn is_empty(&self) -> bool {+ self.bunnies.is_empty()+ }++ fn exit(mut self) {+ unsafe {+ {+ let ctx = &mut self.contexts[self.context_index];+ self.queue+ .submit(&[], &[], (&mut ctx.fence, ctx.fence_value))+ .unwrap();+ }++ for mut ctx in self.contexts {+ ctx.wait_and_clear(&self.device);+ drop(ctx.encoder);+ self.device.destroy_fence(ctx.fence);+ }++ self.device.destroy_bind_group(self.local_group);+ self.device.destroy_bind_group(self.global_group);+ self.device.destroy_buffer(self.local_buffer);+ self.device.destroy_buffer(self.global_buffer);+ self.device.destroy_texture_view(self.texture_view);+ self.device.destroy_texture(self.texture);+ self.device.destroy_sampler(self.sampler);+ self.device.destroy_shader_module(self.shader);+ self.device.destroy_render_pipeline(self.pipeline);+ self.device+ .destroy_bind_group_layout(self.local_group_layout);+ self.device+ .destroy_bind_group_layout(self.global_group_layout);+ self.device.destroy_pipeline_layout(self.pipeline_layout);++ self.surface.unconfigure(&self.device);+ drop(self.queue);+ drop(self.device);+ drop(self.surface);+ drop(self.adapter);+ }+ }++ fn update(&mut self, event: winit::event::WindowEvent) {+ if let winit::event::WindowEvent::KeyboardInput {+ event:+ KeyEvent {+ logical_key: Key::Named(NamedKey::Space),+ state: ElementState::Pressed,+ ..+ },+ ..+ } = event+ {+ let spawn_count = 64 + self.bunnies.len() / 2;+ let elapsed = self.start.elapsed();+ let color = elapsed.as_nanos() as u32;+ println!(+ "Spawning {} bunnies, total at {}",+ spawn_count,+ self.bunnies.len() + spawn_count+ );+ for i in 0..spawn_count {+ let random = ((elapsed.as_nanos() * (i + 1) as u128) & 0xFF) as f32 / 255.0;+ let speed = random * MAX_VELOCITY - (MAX_VELOCITY * 0.5);+ self.bunnies.push(Locals {+ position: [0.0, 0.5 * (self.extent[1] as f32)],+ velocity: [speed, 0.0],+ color,+ _pad: 0,+ });+ }+ }+ }++ fn render(&mut self) {+ let delta = 0.01;+ for bunny in self.bunnies.iter_mut() {+ bunny.position[0] += bunny.velocity[0] * delta;+ bunny.position[1] += bunny.velocity[1] * delta;+ bunny.velocity[1] += GRAVITY * delta;+ if (bunny.velocity[0] > 0.0+ && bunny.position[0] + 0.5 * BUNNY_SIZE > self.extent[0] as f32)+ || (bunny.velocity[0] < 0.0 && bunny.position[0] - 0.5 * BUNNY_SIZE < 0.0)+ {+ bunny.velocity[0] *= -1.0;+ }+ if bunny.velocity[1] < 0.0 && bunny.position[1] < 0.5 * BUNNY_SIZE {+ bunny.velocity[1] *= -1.0;+ }+ }++ if !self.bunnies.is_empty() {+ let size = self.bunnies.len() * self.local_alignment as usize;+ unsafe {+ let mapping = self+ .device+ .map_buffer(&self.local_buffer, 0..size as wgpu_types::BufferAddress)+ .unwrap();+ ptr::copy_nonoverlapping(+ self.bunnies.as_ptr() as *const u8,+ mapping.ptr.as_ptr(),+ size,+ );+ assert!(mapping.is_coherent);+ self.device.unmap_buffer(&self.local_buffer);+ }+ }++ let ctx = &mut self.contexts[self.context_index];++ let surface_tex = unsafe {+ self.surface+ .acquire_texture(None, &ctx.fence)+ .unwrap()+ .texture+ };++ let target_barrier0 = hal::TextureBarrier {+ texture: surface_tex.borrow(),+ range: wgpu_types::ImageSubresourceRange::default(),+ usage: hal::StateTransition {+ from: wgpu_types::TextureUses::UNINITIALIZED,+ to: wgpu_types::TextureUses::COLOR_TARGET,+ },+ };+ unsafe {+ ctx.encoder.begin_encoding(Some("frame")).unwrap();+ ctx.encoder.transition_textures(iter::once(target_barrier0));+ }++ let surface_view_desc = hal::TextureViewDescriptor {+ label: None,+ format: self.surface_format,+ dimension: wgpu_types::TextureViewDimension::D2,+ usage: wgpu_types::TextureUses::COLOR_TARGET,+ range: wgpu_types::ImageSubresourceRange::default(),+ };+ let surface_tex_view = unsafe {+ self.device+ .create_texture_view(surface_tex.borrow(), &surface_view_desc)+ .unwrap()+ };+ let pass_desc = hal::RenderPassDescriptor {+ label: None,+ extent: wgpu_types::Extent3d {+ width: self.extent[0],+ height: self.extent[1],+ depth_or_array_layers: 1,+ },+ sample_count: 1,+ color_attachments: &[Some(hal::ColorAttachment {+ target: hal::Attachment {+ view: &surface_tex_view,+ usage: wgpu_types::TextureUses::COLOR_TARGET,+ },+ depth_slice: None,+ resolve_target: None,+ ops: hal::AttachmentOps::STORE | hal::AttachmentOps::LOAD_CLEAR,+ clear_value: wgpu_types::Color {+ r: 0.1,+ g: 0.2,+ b: 0.3,+ a: 1.0,+ },+ })],+ depth_stencil_attachment: None,+ multiview_mask: None,+ timestamp_writes: None,+ occlusion_query_set: None,+ };+ unsafe {+ ctx.encoder.begin_render_pass(&pass_desc).unwrap();+ ctx.encoder.set_render_pipeline(&self.pipeline);+ ctx.encoder+ .set_bind_group(&self.pipeline_layout, 0, &self.global_group, &[]);+ }++ for i in 0..self.bunnies.len() {+ let offset = (i as wgpu_types::DynamicOffset)+ * (self.local_alignment as wgpu_types::DynamicOffset);+ unsafe {+ ctx.encoder+ .set_bind_group(&self.pipeline_layout, 1, &self.local_group, &[offset]);+ ctx.encoder.draw(0, 4, 0, 1);+ }+ }++ ctx.frames_recorded += 1;++ let target_barrier1 = hal::TextureBarrier {+ texture: surface_tex.borrow(),+ range: wgpu_types::ImageSubresourceRange::default(),+ usage: hal::StateTransition {+ from: wgpu_types::TextureUses::COLOR_TARGET,+ to: wgpu_types::TextureUses::PRESENT,+ },+ };+ unsafe {+ ctx.encoder.end_render_pass();+ ctx.encoder.transition_textures(iter::once(target_barrier1));+ }++ unsafe {+ let cmd_buf = ctx.encoder.end_encoding().unwrap();+ self.queue+ .submit(+ &[&cmd_buf],+ &[&surface_tex],+ (&mut ctx.fence, ctx.fence_value),+ )+ .unwrap();+ self.queue.present(&self.surface, surface_tex).unwrap();+ ctx.used_cmd_bufs.push(cmd_buf);+ ctx.used_views.push(surface_tex_view);+ };++ log::debug!("Context switch from {}", self.context_index);+ let old_fence_value = ctx.fence_value;+ if self.contexts.len() == 1 {+ let hal_desc = hal::CommandEncoderDescriptor {+ label: None,+ queue: &self.queue,+ };+ self.contexts.push(unsafe {+ ExecutionContext {+ encoder: self.device.create_command_encoder(&hal_desc).unwrap(),+ fence: self.device.create_fence().unwrap(),+ fence_value: 0,+ used_views: Vec::new(),+ used_cmd_bufs: Vec::new(),+ frames_recorded: 0,+ }+ });+ }+ self.context_index = (self.context_index + 1) % self.contexts.len();+ let next = &mut self.contexts[self.context_index];+ unsafe {+ next.wait_and_clear(&self.device);+ }+ next.fence_value = old_fence_value + 1;+ }+}++cfg_if::cfg_if! {+ // Apple + Metal+ if #[cfg(all(target_vendor = "apple", feature = "metal"))] {+ type Api = hal::api::Metal;+ }+ // Wasm + Vulkan+ else if #[cfg(all(not(target_arch = "wasm32"), feature = "vulkan"))] {+ type Api = hal::api::Vulkan;+ }+ // Windows + DX12+ else if #[cfg(all(windows, feature = "dx12"))] {+ type Api = hal::api::Dx12;+ }+ // Anything + GLES+ else if #[cfg(feature = "gles")] {+ type Api = hal::api::Gles;+ }+ // Fallback+ else {+ type Api = hal::api::Noop;+ }+}++struct App {+ example: Option<Example<Api>>,+ window: Option<Window>,+ last_frame_inst: Instant,+ frame_count: u32,+ accum_time: f32,+}++impl ApplicationHandler for App {+ fn resumed(&mut self, event_loop: &ActiveEventLoop) {+ if self.window.is_some() {+ return;+ }+ let window = event_loop+ .create_window(Window::default_attributes().with_title("hal-bunnymark"))+ .unwrap();+ let example = Example::<Api>::init(&window).expect("Selected backend is not supported");+ self.window = Some(window);+ self.example = Some(example);+ println!("Press space to spawn bunnies.");+ self.window.as_ref().unwrap().request_redraw();+ }++ fn exiting(&mut self, _event_loop: &ActiveEventLoop) {+ self.example.take().unwrap().exit();+ }++ fn window_event(+ &mut self,+ event_loop: &ActiveEventLoop,+ _window_id: winit::window::WindowId,+ event: WindowEvent,+ ) {+ event_loop.set_control_flow(ControlFlow::Poll);++ match event {+ WindowEvent::KeyboardInput {+ event:+ KeyEvent {+ logical_key: Key::Named(NamedKey::Escape),+ state: ElementState::Pressed,+ ..+ },+ ..+ }+ | WindowEvent::CloseRequested => event_loop.exit(),+ WindowEvent::RedrawRequested => {+ let ex = self.example.as_mut().unwrap();+ {+ self.accum_time += self.last_frame_inst.elapsed().as_secs_f32();+ self.last_frame_inst = Instant::now();+ self.frame_count += 1;+ if self.frame_count == 100 && !ex.is_empty() {+ println!(+ "Avg frame time {}ms",+ self.accum_time * 1000.0 / self.frame_count as f32+ );+ self.accum_time = 0.0;+ self.frame_count = 0;+ }+ }+ ex.render();+ self.window.as_ref().unwrap().request_redraw();+ }+ _ => {+ self.example.as_mut().unwrap().update(event);+ }+ }+ }+}++fn main() {+ env_logger::init();++ let event_loop = winit::event_loop::EventLoop::new().unwrap();+ let mut app = App {+ example: None,+ window: None,+ last_frame_inst: Instant::now(),+ frame_count: 0,+ accum_time: 0.0,+ };+ event_loop.run_app(&mut app).unwrap();+}diff --git a/vendor/wgpu-hal/examples/halmark/shader.wgsl b/vendor/wgpu-hal/examples/halmark/shader.wgslnew file mode 100644index 00000000..ffa72645--- /dev/null+++ b/vendor/wgpu-hal/examples/halmark/shader.wgsl@@ -0,0 +1,50 @@+struct Globals {+ mvp: mat4x4<f32>,+ size: vec2<f32>,+ _pad0: u32,+ _pad1: u32,+};++struct Locals {+ position: vec2<f32>,+ velocity: vec2<f32>,+ color: u32,+ _pad0: u32,+ _pad1: u32,+ _pad2: u32,+};++@group(0)+@binding(0)+var<uniform> globals: Globals;++@group(1)+@binding(0)+var<uniform> locals: Locals;++struct VertexOutput {+ @builtin(position) position: vec4<f32>,+ @location(0) tex_coords: vec2<f32>,+ @location(1) color: vec4<f32>,+};++@vertex+fn vs_main(@builtin(vertex_index) vi: u32) -> VertexOutput {+ let tc = vec2<f32>(f32(vi & 1u), 0.5 * f32(vi & 2u));+ let offset = vec2<f32>(tc.x * globals.size.x, tc.y * globals.size.y);+ let pos = globals.mvp * vec4<f32>(locals.position + offset, 0.0, 1.0);+ let color = vec4<f32>((vec4<u32>(locals.color) >> vec4<u32>(0u, 8u, 16u, 24u)) & vec4<u32>(255u)) / 255.0;+ return VertexOutput(pos, tc, color);+}++@group(0)+@binding(1)+var tex: texture_2d<f32>;+@group(0)+@binding(2)+var sam: sampler;++@fragment+fn fs_main(vertex: VertexOutput) -> @location(0) vec4<f32> {+ return vertex.color * textureSampleLevel(tex, sam, vertex.tex_coords, 0.0);+}diff --git a/vendor/wgpu-hal/examples/raw-gles.em.html b/vendor/wgpu-hal/examples/raw-gles.em.htmlnew file mode 100644index 00000000..d25ca31f--- /dev/null+++ b/vendor/wgpu-hal/examples/raw-gles.em.html@@ -0,0 +1,20 @@+<html>+ <head>+ <meta charset="UTF-8" />+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />+ </head>+ <body>+ <canvas id="canvas" width="640" height="400"></canvas>+ <script>+ var Module = {+ canvas: document.getElementById("canvas"),+ preRun: [+ function () {+ ENV.RUST_LOG = "debug";+ },+ ],+ };+ </script>+ <script src="raw-gles.js"></script>+ </body>+</html>diff --git a/vendor/wgpu-hal/examples/raw-gles.rs b/vendor/wgpu-hal/examples/raw-gles.rsnew file mode 100644index 00000000..b32398c0--- /dev/null+++ b/vendor/wgpu-hal/examples/raw-gles.rs@@ -0,0 +1,396 @@+//! This example shows interop with raw GLES contexts -+//! the ability to hook up wgpu-hal to an existing context and draw into it.+//!+//! Emscripten build:+//! 1. install emsdk+//! 2. build this example with cargo:+//! EMCC_CFLAGS="-g -s ERROR_ON_UNDEFINED_SYMBOLS=0 --no-entry -s FULL_ES3=1" cargo build --example raw-gles --target wasm32-unknown-emscripten+//! 3. copy raw-gles.em.html into target directory and open it in browser:+//! cp wgpu-hal/examples/raw-gles.em.html target/wasm32-unknown-emscripten/debug/examples++extern crate wgpu_hal as hal;++#[cfg(not(any(+ target_arch = "wasm32",+ target_os = "ios",+ target_os = "visionos",+ target_env = "ohos"+)))]+fn main() {+ use std::ffi::CString;++ use glutin::{+ config::GlConfig as _,+ context::{NotCurrentGlContext as _, PossiblyCurrentGlContext as _, Version},+ display::{GetGlDisplay as _, GlDisplay as _},+ surface::GlSurface as _,+ };+ use glutin_winit::GlWindow as _;+ use raw_window_handle::HasWindowHandle as _;+ use winit::{+ application::ApplicationHandler,+ event::{KeyEvent, WindowEvent},+ event_loop::{ActiveEventLoop, ControlFlow, EventLoop},+ keyboard::{Key, NamedKey},+ window::Window,+ };++ env_logger::init();+ println!("Initializing external GL context");++ /// Find the config with the maximum number of samples, so our triangle will be+ /// smooth.+ fn gl_config_picker(+ configs: Box<dyn Iterator<Item = glutin::config::Config> + '_>,+ ) -> glutin::config::Config {+ configs+ .reduce(|accum, config| {+ if config.num_samples() > accum.num_samples() {+ config+ } else {+ accum+ }+ })+ .expect("Failed to find a matching config")+ }++ struct App {+ gl_config: Option<glutin::config::Config>,+ not_current_gl_context: Option<glutin::context::NotCurrentContext>,+ state: Option<(+ glutin::context::PossiblyCurrentContext,+ glutin::surface::Surface<glutin::surface::WindowSurface>,+ Window,+ )>,+ exposed: Option<hal::ExposedAdapter<hal::api::Gles>>,+ }++ impl App {+ fn new() -> Self {+ Self {+ gl_config: None,+ not_current_gl_context: None,+ state: None,+ exposed: None,+ }+ }+ }++ impl ApplicationHandler for App {+ fn resumed(&mut self, event_loop: &ActiveEventLoop) {+ // First call: create display, pick config, create GL context.+ if self.gl_config.is_none() {+ // Only Windows requires the window to be present before creating the display.+ // Other platforms don't really need one.+ let window_attributes = cfg!(windows).then(|| {+ Window::default_attributes()+ .with_title("wgpu raw GLES example (press Escape to exit)")+ });++ let template = glutin::config::ConfigTemplateBuilder::new();++ let display_builder =+ glutin_winit::DisplayBuilder::new().with_window_attributes(window_attributes);++ let (window, gl_config) = display_builder+ .build(event_loop, template, gl_config_picker)+ .expect("Failed to build window and config from display");++ println!("Picked a config with {} samples", gl_config.num_samples());++ let raw_window_handle = window+ .as_ref()+ .and_then(|window| window.window_handle().ok())+ .map(|handle| handle.as_raw());++ let gl_display = gl_config.display();++ // Glutin tries to create an OpenGL context by default.+ // Force it to use any version of GLES.+ let context_attributes = glutin::context::ContextAttributesBuilder::new()+ // wgpu expects GLES 3.0+.+ .with_context_api(glutin::context::ContextApi::Gles(Some(Version::new(3, 0))))+ .build(raw_window_handle);++ let gl_context = unsafe {+ gl_display+ .create_context(&gl_config, &context_attributes)+ .expect("failed to create context")+ };++ self.not_current_gl_context = Some(gl_context);+ self.gl_config = Some(gl_config);++ // On Windows, the window was already created by DisplayBuilder.+ if let Some(window) = window {+ self.create_surface(event_loop, window);+ return;+ }+ }++ // Create window + surface (non-Windows first call, or Android re-resume).+ if self.state.is_none() {+ let gl_config = self.gl_config.as_ref().unwrap();+ let window = glutin_winit::finalize_window(+ event_loop,+ Window::default_attributes()+ .with_title("wgpu raw GLES example (press Escape to exit)"),+ gl_config,+ )+ .unwrap();+ self.create_surface(event_loop, window);+ }+ }++ fn suspended(&mut self, _event_loop: &ActiveEventLoop) {+ // This event is only raised on Android, where the backing NativeWindow for a GL+ // Surface can appear and disappear at any moment.+ println!("Android window removed");++ // Destroy the GL Surface and un-current the GL Context before ndk-glue releases+ // the window back to the system.+ if let Some((gl_context, ..)) = self.state.take() {+ assert!(self+ .not_current_gl_context+ .replace(gl_context.make_not_current().unwrap())+ .is_none());+ }+ }++ fn window_event(+ &mut self,+ event_loop: &ActiveEventLoop,+ _window_id: winit::window::WindowId,+ event: WindowEvent,+ ) {+ event_loop.set_control_flow(ControlFlow::Wait);++ match event {+ WindowEvent::CloseRequested+ | WindowEvent::KeyboardInput {+ event:+ KeyEvent {+ logical_key: Key::Named(NamedKey::Escape),+ ..+ },+ ..+ } => event_loop.exit(),+ WindowEvent::Resized(size) => {+ if size.width != 0 && size.height != 0 {+ // Some platforms like EGL require resizing GL surface to update the size.+ // Notable platforms here are Wayland and macOS, other don't require it+ // and the function is no-op, but it's wise to resize it for portability+ // reasons.+ if let Some((gl_context, gl_surface, window)) = &self.state {+ window.resize_surface(gl_surface, gl_context);+ }+ }+ }+ WindowEvent::RedrawRequested => {+ if let (Some(exposed), Some((gl_context, gl_surface, window))) =+ (&self.exposed, &self.state)+ {+ let inner_size = window.inner_size();++ fill_screen(exposed, inner_size.width, inner_size.height);++ println!("Showing the window");+ gl_surface+ .swap_buffers(gl_context)+ .expect("Failed to swap buffers");+ }+ }+ _ => (),+ }+ }+ }++ impl App {+ fn create_surface(&mut self, _event_loop: &ActiveEventLoop, window: Window) {+ let gl_config = self.gl_config.as_ref().unwrap();++ let attrs = window+ .build_surface_attributes(Default::default())+ .expect("Failed to build surface attributes");+ let gl_surface = unsafe {+ gl_config+ .display()+ .create_window_surface(gl_config, &attrs)+ .expect("Cannot create GL WindowSurface")+ };++ // Make it current.+ let gl_context = self+ .not_current_gl_context+ .take()+ .unwrap()+ .make_current(&gl_surface)+ .expect("GL context cannot be made current with WindowSurface");++ // The context needs to be current for the Renderer to set up shaders and+ // buffers. It also performs function loading, which needs a current context on+ // WGL.+ println!("Hooking up to wgpu-hal");+ self.exposed.get_or_insert_with(|| {+ unsafe {+ <hal::api::Gles as hal::Api>::Adapter::new_external(+ |name| {+ // XXX: On WGL this should only be called after the context was+ // made current+ gl_config+ .display()+ .get_proc_address(&CString::new(name).expect(name))+ },+ wgpu_types::GlBackendOptions::default(),+ )+ }+ .expect("GL adapter can't be initialized")+ });++ window.request_redraw();+ self.state = Some((gl_context, gl_surface, window));+ }+ }++ let event_loop = EventLoop::new().unwrap();+ let mut app = App::new();+ event_loop+ .run_app(&mut app)+ .expect("Couldn't run event loop");+}++#[cfg(target_os = "emscripten")]+fn main() {+ env_logger::init();++ println!("Initializing external GL context");+ let egl = khronos_egl::Instance::new(khronos_egl::Static);+ let display = unsafe { egl.get_display(khronos_egl::DEFAULT_DISPLAY) }.unwrap();+ egl.initialize(display)+ .expect("unable to initialize display");++ let attributes = [+ khronos_egl::RED_SIZE,+ 8,+ khronos_egl::GREEN_SIZE,+ 8,+ khronos_egl::BLUE_SIZE,+ 8,+ khronos_egl::NONE,+ ];++ let config = egl+ .choose_first_config(display, &attributes)+ .unwrap()+ .expect("unable to choose config");+ let surface = unsafe {+ let window = std::ptr::null_mut::<std::ffi::c_void>();+ egl.create_window_surface(display, config, window, None)+ }+ .expect("unable to create surface");++ let context_attributes = [khronos_egl::CONTEXT_CLIENT_VERSION, 3, khronos_egl::NONE];++ let gl_context = egl+ .create_context(display, config, None, &context_attributes)+ .expect("unable to create context");+ egl.make_current(display, Some(surface), Some(surface), Some(gl_context))+ .expect("can't make context current");++ println!("Hooking up to wgpu-hal");+ let exposed = unsafe {+ <hal::api::Gles as hal::Api>::Adapter::new_external(|name| {+ egl.get_proc_address(name)+ .map_or(std::ptr::null(), |p| p as *const _)+ })+ }+ .expect("GL adapter can't be initialized");++ fill_screen(&exposed, 640, 400);+}++#[cfg(any(+ all(target_arch = "wasm32", not(target_os = "emscripten")),+ target_os = "ios",+ target_os = "visionos",+ target_env = "ohos"+))]+fn main() {+ eprintln!("This example is not supported on this platform")+}++#[cfg(not(any(+ all(target_arch = "wasm32", not(target_os = "emscripten")),+ target_os = "ios",+ target_os = "visionos"+)))]+fn fill_screen(exposed: &hal::ExposedAdapter<hal::api::Gles>, width: u32, height: u32) {+ use hal::{Adapter as _, CommandEncoder as _, Device as _, Queue as _};++ let od = unsafe {+ exposed.adapter.open(+ wgpu_types::Features::empty(),+ &wgpu_types::Limits::downlevel_defaults(),+ &wgpu_types::MemoryHints::default(),+ )+ }+ .unwrap();++ let format = wgpu_types::TextureFormat::Rgba8UnormSrgb;+ let texture = <hal::api::Gles as hal::Api>::Texture::default_framebuffer(format);+ let view = unsafe {+ od.device+ .create_texture_view(+ &texture,+ &hal::TextureViewDescriptor {+ label: None,+ format,+ dimension: wgpu_types::TextureViewDimension::D2,+ usage: wgpu_types::TextureUses::COLOR_TARGET,+ range: wgpu_types::ImageSubresourceRange::default(),+ },+ )+ .unwrap()+ };++ println!("Filling the screen");+ let mut encoder = unsafe {+ od.device+ .create_command_encoder(&hal::CommandEncoderDescriptor {+ label: None,+ queue: &od.queue,+ })+ .unwrap()+ };+ let mut fence = unsafe { od.device.create_fence().unwrap() };+ let rp_desc = hal::RenderPassDescriptor {+ label: None,+ extent: wgpu_types::Extent3d {+ width,+ height,+ depth_or_array_layers: 1,+ },+ sample_count: 1,+ color_attachments: &[Some(hal::ColorAttachment {+ target: hal::Attachment {+ view: &view,+ usage: wgpu_types::TextureUses::COLOR_TARGET,+ },+ depth_slice: None,+ resolve_target: None,+ ops: hal::AttachmentOps::STORE | hal::AttachmentOps::LOAD_CLEAR,+ clear_value: wgpu_types::Color::BLUE,+ })],+ depth_stencil_attachment: None,+ multiview_mask: None,+ timestamp_writes: None,+ occlusion_query_set: None,+ };+ unsafe {+ encoder.begin_encoding(None).unwrap();+ encoder.begin_render_pass(&rp_desc).unwrap();+ encoder.end_render_pass();+ let cmd_buf = encoder.end_encoding().unwrap();+ od.queue.submit(&[&cmd_buf], &[], (&mut fence, 0)).unwrap();+ }+}diff --git a/vendor/wgpu-hal/examples/ray-traced-triangle/main.rs b/vendor/wgpu-hal/examples/ray-traced-triangle/main.rsnew file mode 100644index 00000000..b0f940e9--- /dev/null+++ b/vendor/wgpu-hal/examples/ray-traced-triangle/main.rs@@ -0,0 +1,1225 @@+extern crate wgpu_hal as hal;++use hal::{+ Adapter as _, CommandEncoder as _, Device as _, Instance as _, Queue as _, Surface as _,+};+use raw_window_handle::{HasDisplayHandle, HasWindowHandle};++use glam::{Affine3A, Mat4, Vec3};+use std::{+ borrow::{Borrow, Cow},+ iter, ptr,+ time::Instant,+};+use wgpu_types::Dx12BackendOptions;+use winit::{+ application::ApplicationHandler,+ event_loop::{ActiveEventLoop, ControlFlow},+ window::{Window, WindowButtons},+};++const DESIRED_MAX_LATENCY: u32 = 2;++/// [D3D12_RAYTRACING_INSTANCE_DESC](https://microsoft.github.io/DirectX-Specs/d3d/Raytracing.html#d3d12_raytracing_instance_desc)+/// [VkAccelerationStructureInstanceKHR](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkAccelerationStructureInstanceKHR.html)+#[derive(Clone)]+#[repr(C)]+struct AccelerationStructureInstance {+ transform: [f32; 12],+ custom_index_and_mask: u32,+ shader_binding_table_record_offset_and_flags: u32,+ acceleration_structure_reference: u64,+}++impl std::fmt::Debug for AccelerationStructureInstance {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ f.debug_struct("Instance")+ .field("transform", &self.transform)+ .field("custom_data()", &self.custom_index())+ .field("mask()", &self.mask())+ .field(+ "shader_binding_table_record_offset()",+ &self.shader_binding_table_record_offset(),+ )+ .field("flags()", &self.flags())+ .field(+ "acceleration_structure_reference",+ &self.acceleration_structure_reference,+ )+ .finish()+ }+}++#[allow(dead_code)]+impl AccelerationStructureInstance {+ const LOW_24_MASK: u32 = 0x00ff_ffff;+ const MAX_U24: u32 = (1u32 << 24u32) - 1u32;++ #[inline]+ fn affine_to_rows(mat: &Affine3A) -> [f32; 12] {+ let row_0 = mat.matrix3.row(0);+ let row_1 = mat.matrix3.row(1);+ let row_2 = mat.matrix3.row(2);+ let translation = mat.translation;+ [+ row_0.x,+ row_0.y,+ row_0.z,+ translation.x,+ row_1.x,+ row_1.y,+ row_1.z,+ translation.y,+ row_2.x,+ row_2.y,+ row_2.z,+ translation.z,+ ]+ }++ #[inline]+ fn rows_to_affine(rows: &[f32; 12]) -> Affine3A {+ Affine3A::from_cols_array(&[+ rows[0], rows[3], rows[6], rows[9], rows[1], rows[4], rows[7], rows[10], rows[2],+ rows[5], rows[8], rows[11],+ ])+ }++ pub fn transform_as_affine(&self) -> Affine3A {+ Self::rows_to_affine(&self.transform)+ }+ pub fn set_transform(&mut self, transform: &Affine3A) {+ self.transform = Self::affine_to_rows(transform);+ }++ pub fn custom_index(&self) -> u32 {+ self.custom_index_and_mask & Self::LOW_24_MASK+ }++ pub fn mask(&self) -> u8 {+ (self.custom_index_and_mask >> 24) as u8+ }++ pub fn shader_binding_table_record_offset(&self) -> u32 {+ self.shader_binding_table_record_offset_and_flags & Self::LOW_24_MASK+ }++ pub fn flags(&self) -> u8 {+ (self.shader_binding_table_record_offset_and_flags >> 24) as u8+ }++ pub fn set_custom_index(&mut self, custom_index: u32) {+ debug_assert!(+ custom_index <= Self::MAX_U24,+ "custom_index uses more than 24 bits! {custom_index} > {}",+ Self::MAX_U24+ );+ self.custom_index_and_mask =+ (custom_index & Self::LOW_24_MASK) | (self.custom_index_and_mask & !Self::LOW_24_MASK)+ }++ pub fn set_mask(&mut self, mask: u8) {+ self.custom_index_and_mask =+ (self.custom_index_and_mask & Self::LOW_24_MASK) | (u32::from(mask) << 24)+ }++ pub fn set_shader_binding_table_record_offset(+ &mut self,+ shader_binding_table_record_offset: u32,+ ) {+ debug_assert!(+ shader_binding_table_record_offset <= Self::MAX_U24,+ "shader_binding_table_record_offset uses more than 24 bits! {} > {}",+ shader_binding_table_record_offset,+ Self::MAX_U24+ );+ self.shader_binding_table_record_offset_and_flags = (shader_binding_table_record_offset+ & Self::LOW_24_MASK)+ | (self.shader_binding_table_record_offset_and_flags & !Self::LOW_24_MASK)+ }++ pub fn set_flags(&mut self, flags: u8) {+ self.shader_binding_table_record_offset_and_flags =+ (self.shader_binding_table_record_offset_and_flags & Self::LOW_24_MASK)+ | (u32::from(flags) << 24)+ }++ pub fn new(+ transform: &Affine3A,+ custom_index: u32,+ mask: u8,+ shader_binding_table_record_offset: u32,+ flags: u8,+ acceleration_structure_reference: u64,+ ) -> Self {+ debug_assert!(+ custom_index <= Self::MAX_U24,+ "custom_index uses more than 24 bits! {custom_index} > {}",+ Self::MAX_U24+ );+ debug_assert!(+ shader_binding_table_record_offset <= Self::MAX_U24,+ "shader_binding_table_record_offset uses more than 24 bits! {} > {}",+ shader_binding_table_record_offset,+ Self::MAX_U24+ );+ AccelerationStructureInstance {+ transform: Self::affine_to_rows(transform),+ custom_index_and_mask: (custom_index & Self::MAX_U24) | (u32::from(mask) << 24),+ shader_binding_table_record_offset_and_flags: (shader_binding_table_record_offset+ & Self::MAX_U24)+ | (u32::from(flags) << 24),+ acceleration_structure_reference,+ }+ }+}++struct ExecutionContext<A: hal::Api> {+ encoder: A::CommandEncoder,+ fence: A::Fence,+ fence_value: hal::FenceValue,+ used_views: Vec<A::TextureView>,+ used_cmd_bufs: Vec<A::CommandBuffer>,+ frames_recorded: usize,+}++impl<A: hal::Api> ExecutionContext<A> {+ unsafe fn wait_and_clear(&mut self, device: &A::Device) {+ device.wait(&self.fence, self.fence_value, None).unwrap();+ self.encoder.reset_all(self.used_cmd_bufs.drain(..));+ for view in self.used_views.drain(..) {+ device.destroy_texture_view(view);+ }+ self.frames_recorded = 0;+ }+}++#[allow(dead_code)]+struct Example<A: hal::Api> {+ instance: A::Instance,+ adapter: A::Adapter,+ surface: A::Surface,+ surface_format: wgpu_types::TextureFormat,+ device: A::Device,+ queue: A::Queue,++ contexts: Vec<ExecutionContext<A>>,+ context_index: usize,+ extent: [u32; 2],+ start: Instant,+ pipeline: A::ComputePipeline,+ bind_group: A::BindGroup,+ bgl: A::BindGroupLayout,+ shader_module: A::ShaderModule,+ texture_view: A::TextureView,+ uniform_buffer: A::Buffer,+ pipeline_layout: A::PipelineLayout,+ vertices_buffer: A::Buffer,+ indices_buffer: Option<A::Buffer>,+ texture: A::Texture,+ instances: [AccelerationStructureInstance; 3],+ instances_buffer: A::Buffer,+ blas: A::AccelerationStructure,+ tlas: A::AccelerationStructure,+ scratch_buffer: A::Buffer,+ time: f32,+}++impl<A: hal::Api> Example<A> {+ fn init(window: &winit::window::Window) -> Result<Self, Box<dyn std::error::Error>> {+ let mut index_buffer = false;++ for arg in std::env::args() {+ if arg == "index_buffer" {+ index_buffer = true;+ }+ }++ if index_buffer {+ log::info!("using index buffer")+ }++ // The Instance can be initialized with the DisplayHandle from the EventLoop as well+ let raw_display_handle = window.display_handle()?;++ let instance_desc = hal::InstanceDescriptor {+ name: "example",+ flags: wgpu_types::InstanceFlags::default(),+ memory_budget_thresholds: wgpu_types::MemoryBudgetThresholds::default(),+ backend_options: wgpu_types::BackendOptions {+ dx12: Dx12BackendOptions {+ shader_compiler: wgpu_types::Dx12Compiler::default_dynamic_dxc(),+ ..Default::default()+ },+ ..Default::default()+ },+ telemetry: None,+ display: Some(raw_display_handle),+ };+ let instance = unsafe { A::Instance::init(&instance_desc)? };+ let surface = {+ let raw_window_handle = window.window_handle()?.as_raw();++ unsafe {+ instance+ .create_surface(raw_display_handle.as_raw(), raw_window_handle)+ .unwrap()+ }+ };++ let (adapter, features) = unsafe {+ let mut adapters = instance.enumerate_adapters(Some(&surface));+ if adapters.is_empty() {+ panic!("No adapters found");+ }+ let exposed = adapters.swap_remove(0);+ dbg!(exposed.features);+ (exposed.adapter, exposed.features)+ };+ let surface_caps = unsafe { adapter.surface_capabilities(&surface) }+ .expect("Surface doesn't support presentation");+ log::info!("Surface caps: {surface_caps:#?}");++ let hal::OpenDevice { device, queue } = unsafe {+ adapter+ .open(+ features,+ &wgpu_types::Limits::default(),+ &wgpu_types::MemoryHints::Performance,+ )+ .unwrap()+ };++ let window_size: (u32, u32) = window.inner_size().into();+ dbg!(&surface_caps.formats);+ let surface_format = if surface_caps+ .formats+ .iter()+ .any(|fc| fc.format == wgpu_types::TextureFormat::Rgba8Unorm)+ {+ wgpu_types::TextureFormat::Rgba8Unorm+ } else {+ surface_caps.formats.first().unwrap().format+ };+ let surface_config = hal::SurfaceConfiguration {+ maximum_frame_latency: DESIRED_MAX_LATENCY+ .max(*surface_caps.maximum_frame_latency.start())+ .min(*surface_caps.maximum_frame_latency.end()),+ present_mode: wgpu_types::PresentMode::Fifo,+ composite_alpha_mode: wgpu_types::CompositeAlphaMode::Opaque,+ format: surface_format,+ color_space: wgpu_types::SurfaceColorSpace::Srgb,+ extent: wgpu_types::Extent3d {+ width: window_size.0,+ height: window_size.1,+ depth_or_array_layers: 1,+ },+ usage: wgpu_types::TextureUses::COLOR_TARGET | wgpu_types::TextureUses::COPY_DST,+ view_formats: vec![surface_format],+ };+ unsafe {+ surface.configure(&device, &surface_config).unwrap();+ };++ #[allow(dead_code)]+ struct Uniforms {+ view_inverse: glam::Mat4,+ proj_inverse: glam::Mat4,+ }++ let bgl_desc = hal::BindGroupLayoutDescriptor {+ label: None,+ flags: hal::BindGroupLayoutFlags::empty(),+ entries: &[+ wgpu_types::BindGroupLayoutEntry {+ binding: 0,+ visibility: wgpu_types::ShaderStages::COMPUTE,+ ty: wgpu_types::BindingType::Buffer {+ ty: wgpu_types::BufferBindingType::Uniform,+ has_dynamic_offset: false,+ min_binding_size: wgpu_types::BufferSize::new(size_of::<Uniforms>() as _),+ },+ count: None,+ },+ wgpu_types::BindGroupLayoutEntry {+ binding: 1,+ visibility: wgpu_types::ShaderStages::COMPUTE,+ ty: wgpu_types::BindingType::StorageTexture {+ access: wgpu_types::StorageTextureAccess::WriteOnly,+ format: wgpu_types::TextureFormat::Rgba8Unorm,+ view_dimension: wgpu_types::TextureViewDimension::D2,+ },+ count: None,+ },+ wgpu_types::BindGroupLayoutEntry {+ binding: 2,+ visibility: wgpu_types::ShaderStages::COMPUTE,+ ty: wgpu_types::BindingType::AccelerationStructure {+ vertex_return: false,+ },+ count: None,+ },+ ],+ };++ let bgl = unsafe { device.create_bind_group_layout(&bgl_desc).unwrap() };++ let naga_shader = {+ let shader_file = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))+ .join("examples")+ .join("ray-traced-triangle")+ .join("shader.wgsl");+ let source = std::fs::read_to_string(shader_file).unwrap();+ let module = naga::front::wgsl::Frontend::new().parse(&source).unwrap();+ let info = naga::valid::Validator::new(+ naga::valid::ValidationFlags::all(),+ naga::valid::Capabilities::RAY_QUERY,+ )+ .validate(&module)+ .unwrap();+ hal::NagaShader {+ module: Cow::Owned(module),+ info,+ debug_source: None,+ }+ };+ let shader_desc = hal::ShaderModuleDescriptor {+ label: None,+ runtime_checks: wgpu_types::ShaderRuntimeChecks::checked(),+ };+ let shader_module = unsafe {+ device+ .create_shader_module(&shader_desc, hal::ShaderInput::Naga(naga_shader))+ .unwrap()+ };++ let pipeline_layout_desc = hal::PipelineLayoutDescriptor {+ label: None,+ flags: hal::PipelineLayoutFlags::empty(),+ bind_group_layouts: &[Some(&bgl)],+ immediate_size: 0,+ };+ let pipeline_layout = unsafe {+ device+ .create_pipeline_layout(&pipeline_layout_desc)+ .unwrap()+ };++ let pipeline = unsafe {+ device.create_compute_pipeline(&hal::ComputePipelineDescriptor {+ label: Some("pipeline"),+ layout: &pipeline_layout,+ stage: hal::ProgrammableStage {+ module: &shader_module,+ entry_point: "main",+ constants: &Default::default(),+ zero_initialize_workgroup_memory: true,+ },+ cache: None,+ })+ }+ .unwrap();++ let vertices: [f32; 9] = [1.0, 1.0, 0.0, -1.0, 1.0, 0.0, 0.0, -1.0, 0.0];++ let vertices_size_in_bytes = vertices.len() * 4;++ let indices: [u32; 3] = [0, 1, 2];++ let indices_size_in_bytes = indices.len() * 4;++ let vertices_buffer = unsafe {+ let vertices_buffer = device+ .create_buffer(&hal::BufferDescriptor {+ label: Some("vertices buffer"),+ size: vertices_size_in_bytes as u64,+ usage: wgpu_types::BufferUses::MAP_WRITE+ | wgpu_types::BufferUses::BOTTOM_LEVEL_ACCELERATION_STRUCTURE_INPUT,+ memory_flags: hal::MemoryFlags::TRANSIENT | hal::MemoryFlags::PREFER_COHERENT,+ })+ .unwrap();++ let mapping = device+ .map_buffer(&vertices_buffer, 0..vertices_size_in_bytes as u64)+ .unwrap();+ ptr::copy_nonoverlapping(+ vertices.as_ptr() as *const u8,+ mapping.ptr.as_ptr(),+ vertices_size_in_bytes,+ );+ device.unmap_buffer(&vertices_buffer);+ assert!(mapping.is_coherent);++ vertices_buffer+ };++ let indices_buffer = if index_buffer {+ unsafe {+ let indices_buffer = device+ .create_buffer(&hal::BufferDescriptor {+ label: Some("indices buffer"),+ size: indices_size_in_bytes as u64,+ usage: wgpu_types::BufferUses::MAP_WRITE+ | wgpu_types::BufferUses::BOTTOM_LEVEL_ACCELERATION_STRUCTURE_INPUT,+ memory_flags: hal::MemoryFlags::TRANSIENT+ | hal::MemoryFlags::PREFER_COHERENT,+ })+ .unwrap();++ let mapping = device+ .map_buffer(&indices_buffer, 0..indices_size_in_bytes as u64)+ .unwrap();+ ptr::copy_nonoverlapping(+ indices.as_ptr() as *const u8,+ mapping.ptr.as_ptr(),+ indices_size_in_bytes,+ );+ device.unmap_buffer(&indices_buffer);+ assert!(mapping.is_coherent);++ Some((indices_buffer, indices.len()))+ }+ } else {+ None+ };++ let blas_triangles = vec![hal::AccelerationStructureTriangles {+ vertex_buffer: Some(&vertices_buffer),+ first_vertex: 0,+ vertex_format: wgpu_types::VertexFormat::Float32x3,+ // each vertex is 3 floats, and floats are stored raw in the array+ vertex_count: vertices.len() as u32 / 3,+ vertex_stride: 3 * 4,+ indices: indices_buffer.as_ref().map(|(buf, len)| {+ hal::AccelerationStructureTriangleIndices {+ buffer: Some(buf),+ format: wgpu_types::IndexFormat::Uint32,+ offset: 0,+ count: *len as u32,+ }+ }),++ transform: None,+ flags: hal::AccelerationStructureGeometryFlags::OPAQUE,+ }];+ let blas_entries = hal::AccelerationStructureEntries::Triangles(blas_triangles);++ let blas_sizes = unsafe {+ device.get_acceleration_structure_build_sizes(+ &hal::GetAccelerationStructureBuildSizesDescriptor {+ entries: &blas_entries,+ flags: hal::AccelerationStructureBuildFlags::PREFER_FAST_TRACE,+ },+ )+ };++ let blas = unsafe {+ device.create_acceleration_structure(&hal::AccelerationStructureDescriptor {+ label: Some("blas"),+ size: blas_sizes.acceleration_structure_size,+ format: hal::AccelerationStructureFormat::BottomLevel,+ allow_compaction: false,+ })+ }+ .unwrap();++ let instances = [+ AccelerationStructureInstance::new(+ &Affine3A::from_translation(Vec3 {+ x: 0.0,+ y: 0.0,+ z: 0.0,+ }),+ 0,+ 0xff,+ 0,+ 0,+ unsafe { device.get_acceleration_structure_device_address(&blas) },+ ),+ AccelerationStructureInstance::new(+ &Affine3A::from_translation(Vec3 {+ x: -1.0,+ y: -1.0,+ z: -2.0,+ }),+ 0,+ 0xff,+ 0,+ 0,+ unsafe { device.get_acceleration_structure_device_address(&blas) },+ ),+ AccelerationStructureInstance::new(+ &Affine3A::from_translation(Vec3 {+ x: 1.0,+ y: -1.0,+ z: -2.0,+ }),+ 0,+ 0xff,+ 0,+ 0,+ unsafe { device.get_acceleration_structure_device_address(&blas) },+ ),+ ];++ let instances_buffer_size = instances.len() * size_of::<AccelerationStructureInstance>();++ let instances_buffer = unsafe {+ let instances_buffer = device+ .create_buffer(&hal::BufferDescriptor {+ label: Some("instances_buffer"),+ size: instances_buffer_size as u64,+ usage: wgpu_types::BufferUses::MAP_WRITE+ | wgpu_types::BufferUses::TOP_LEVEL_ACCELERATION_STRUCTURE_INPUT,+ memory_flags: hal::MemoryFlags::TRANSIENT | hal::MemoryFlags::PREFER_COHERENT,+ })+ .unwrap();++ let mapping = device+ .map_buffer(&instances_buffer, 0..instances_buffer_size as u64)+ .unwrap();+ ptr::copy_nonoverlapping(+ instances.as_ptr() as *const u8,+ mapping.ptr.as_ptr(),+ instances_buffer_size,+ );+ device.unmap_buffer(&instances_buffer);+ assert!(mapping.is_coherent);++ instances_buffer+ };++ let mut tlas_entries =+ hal::AccelerationStructureEntries::Instances(hal::AccelerationStructureInstances {+ buffer: Some(&instances_buffer),+ count: 3,+ offset: 0,+ });++ let tlas_flags = hal::AccelerationStructureBuildFlags::PREFER_FAST_TRACE+ | hal::AccelerationStructureBuildFlags::ALLOW_UPDATE;++ let tlas_sizes = unsafe {+ device.get_acceleration_structure_build_sizes(+ &hal::GetAccelerationStructureBuildSizesDescriptor {+ entries: &tlas_entries,+ flags: tlas_flags,+ },+ )+ };++ let tlas = unsafe {+ device.create_acceleration_structure(&hal::AccelerationStructureDescriptor {+ label: Some("tlas"),+ size: tlas_sizes.acceleration_structure_size,+ format: hal::AccelerationStructureFormat::TopLevel,+ allow_compaction: false,+ })+ }+ .unwrap();++ let uniforms = {+ let view = Mat4::look_at_rh(Vec3::new(0.0, 0.0, 2.5), Vec3::ZERO, Vec3::Y);+ let proj = Mat4::perspective_rh(59.0_f32.to_radians(), 1.0, 0.001, 1000.0);++ Uniforms {+ view_inverse: view.inverse(),+ proj_inverse: proj.inverse(),+ }+ };++ let uniforms_size = size_of::<Uniforms>();++ let uniform_buffer = unsafe {+ let uniform_buffer = device+ .create_buffer(&hal::BufferDescriptor {+ label: Some("uniform buffer"),+ size: uniforms_size as u64,+ usage: wgpu_types::BufferUses::MAP_WRITE | wgpu_types::BufferUses::UNIFORM,+ memory_flags: hal::MemoryFlags::PREFER_COHERENT,+ })+ .unwrap();++ let mapping = device+ .map_buffer(&uniform_buffer, 0..uniforms_size as u64)+ .unwrap();+ ptr::copy_nonoverlapping(+ &uniforms as *const Uniforms as *const u8,+ mapping.ptr.as_ptr(),+ uniforms_size,+ );+ device.unmap_buffer(&uniform_buffer);+ assert!(mapping.is_coherent);+ uniform_buffer+ };++ let texture_desc = hal::TextureDescriptor {+ label: None,+ size: wgpu_types::Extent3d {+ width: 512,+ height: 512,+ depth_or_array_layers: 1,+ },+ mip_level_count: 1,+ sample_count: 1,+ dimension: wgpu_types::TextureDimension::D2,+ format: wgpu_types::TextureFormat::Rgba8Unorm,+ usage: wgpu_types::TextureUses::STORAGE_READ_WRITE | wgpu_types::TextureUses::COPY_SRC,+ memory_flags: hal::MemoryFlags::empty(),+ view_formats: vec![wgpu_types::TextureFormat::Rgba8Unorm],+ };+ let texture = unsafe { device.create_texture(&texture_desc).unwrap() };++ let view_desc = hal::TextureViewDescriptor {+ label: None,+ format: texture_desc.format,+ dimension: wgpu_types::TextureViewDimension::D2,+ usage: wgpu_types::TextureUses::STORAGE_READ_WRITE | wgpu_types::TextureUses::COPY_SRC,+ range: wgpu_types::ImageSubresourceRange::default(),+ };+ let texture_view = unsafe { device.create_texture_view(&texture, &view_desc).unwrap() };++ let bind_group = {+ let buffer_binding = unsafe {+ // SAFETY: The size matches the buffer allocation.+ hal::BufferBinding::new_unchecked(+ &uniform_buffer,+ 0,+ wgpu_types::BufferSize::new_unchecked(uniforms_size as u64),+ )+ };+ let texture_binding = hal::TextureBinding {+ view: &texture_view,+ usage: wgpu_types::TextureUses::STORAGE_READ_WRITE,+ };+ let group_desc = hal::BindGroupDescriptor {+ label: Some("bind group"),+ layout: &bgl,+ buffers: &[buffer_binding],+ samplers: &[],+ textures: &[texture_binding],+ acceleration_structures: &[&tlas],+ external_textures: &[],+ entries: &[+ hal::BindGroupEntry {+ binding: 0,+ resource_index: 0,+ count: 1,+ },+ hal::BindGroupEntry {+ binding: 1,+ resource_index: 0,+ count: 1,+ },+ hal::BindGroupEntry {+ binding: 2,+ resource_index: 0,+ count: 1,+ },+ ],+ };+ unsafe { device.create_bind_group(&group_desc).unwrap() }+ };++ let scratch_buffer = unsafe {+ device+ .create_buffer(&hal::BufferDescriptor {+ label: Some("scratch buffer"),+ size: blas_sizes+ .build_scratch_size+ .max(tlas_sizes.build_scratch_size),+ usage: wgpu_types::BufferUses::ACCELERATION_STRUCTURE_SCRATCH,+ memory_flags: hal::MemoryFlags::empty(),+ })+ .unwrap()+ };++ if let hal::AccelerationStructureEntries::Instances(ref mut i) = tlas_entries {+ assert!(+ instances.len() <= i.count as usize,+ "Tlas allocation to small"+ );+ }++ let cmd_encoder_desc = hal::CommandEncoderDescriptor {+ label: None,+ queue: &queue,+ };+ let mut cmd_encoder = unsafe { device.create_command_encoder(&cmd_encoder_desc).unwrap() };++ unsafe { cmd_encoder.begin_encoding(Some("init")).unwrap() };++ unsafe {+ cmd_encoder.place_acceleration_structure_barrier(hal::AccelerationStructureBarrier {+ usage: hal::StateTransition {+ from: hal::AccelerationStructureUses::empty(),+ to: hal::AccelerationStructureUses::BUILD_OUTPUT,+ },+ });++ cmd_encoder.build_acceleration_structures(+ 1,+ [hal::BuildAccelerationStructureDescriptor {+ mode: hal::AccelerationStructureBuildMode::Build,+ flags: hal::AccelerationStructureBuildFlags::PREFER_FAST_TRACE,+ destination_acceleration_structure: &blas,+ scratch_buffer: &scratch_buffer,+ entries: &blas_entries,+ source_acceleration_structure: None,+ scratch_buffer_offset: 0,+ }],+ );++ let scratch_buffer_barrier = hal::BufferBarrier {+ buffer: &scratch_buffer,+ usage: hal::StateTransition {+ from: wgpu_types::BufferUses::BOTTOM_LEVEL_ACCELERATION_STRUCTURE_INPUT,+ to: wgpu_types::BufferUses::TOP_LEVEL_ACCELERATION_STRUCTURE_INPUT,+ },+ };+ cmd_encoder.transition_buffers(iter::once(scratch_buffer_barrier));++ cmd_encoder.place_acceleration_structure_barrier(hal::AccelerationStructureBarrier {+ usage: hal::StateTransition {+ from: hal::AccelerationStructureUses::BUILD_OUTPUT,+ to: hal::AccelerationStructureUses::BUILD_INPUT,+ },+ });++ cmd_encoder.build_acceleration_structures(+ 1,+ [hal::BuildAccelerationStructureDescriptor {+ mode: hal::AccelerationStructureBuildMode::Build,+ flags: tlas_flags,+ destination_acceleration_structure: &tlas,+ scratch_buffer: &scratch_buffer,+ entries: &tlas_entries,+ source_acceleration_structure: None,+ scratch_buffer_offset: 0,+ }],+ );++ cmd_encoder.place_acceleration_structure_barrier(hal::AccelerationStructureBarrier {+ usage: hal::StateTransition {+ from: hal::AccelerationStructureUses::BUILD_OUTPUT,+ to: hal::AccelerationStructureUses::SHADER_INPUT,+ },+ });++ let texture_barrier = hal::TextureBarrier {+ texture: &texture,+ range: wgpu_types::ImageSubresourceRange::default(),+ usage: hal::StateTransition {+ from: wgpu_types::TextureUses::UNINITIALIZED,+ to: wgpu_types::TextureUses::STORAGE_READ_WRITE,+ },+ };++ cmd_encoder.transition_textures(iter::once(texture_barrier));+ }++ let init_fence_value = 1;+ let fence = unsafe {+ let mut fence = device.create_fence().unwrap();+ let init_cmd = cmd_encoder.end_encoding().unwrap();+ queue+ .submit(&[&init_cmd], &[], (&mut fence, init_fence_value))+ .unwrap();+ device.wait(&fence, init_fence_value, None).unwrap();+ cmd_encoder.reset_all(iter::once(init_cmd));+ fence+ };++ Ok(Self {+ instance,+ adapter,+ surface,+ surface_format: surface_config.format,+ device,+ queue,+ pipeline,+ contexts: vec![ExecutionContext {+ encoder: cmd_encoder,+ fence,+ fence_value: init_fence_value + 1,+ used_views: Vec::new(),+ used_cmd_bufs: Vec::new(),+ frames_recorded: 0,+ }],+ context_index: 0,+ extent: [window_size.0, window_size.1],+ start: Instant::now(),+ pipeline_layout,+ bind_group,+ texture,+ instances,+ instances_buffer,+ blas,+ tlas,+ scratch_buffer,+ time: 0.0,+ indices_buffer: indices_buffer.map(|(buf, _)| buf),+ vertices_buffer,+ uniform_buffer,+ texture_view,+ bgl,+ shader_module,+ })+ }++ fn update(&mut self, _event: winit::event::WindowEvent) {}++ fn render(&mut self) {+ let ctx = &mut self.contexts[self.context_index];++ let surface_tex = unsafe {+ self.surface+ .acquire_texture(None, &ctx.fence)+ .unwrap()+ .texture+ };++ let target_barrier0 = hal::TextureBarrier {+ texture: surface_tex.borrow(),+ range: wgpu_types::ImageSubresourceRange::default(),+ usage: hal::StateTransition {+ from: wgpu_types::TextureUses::UNINITIALIZED,+ to: wgpu_types::TextureUses::COPY_DST,+ },+ };++ let instances_buffer_size =+ self.instances.len() * size_of::<AccelerationStructureInstance>();++ let tlas_flags = hal::AccelerationStructureBuildFlags::PREFER_FAST_TRACE+ | hal::AccelerationStructureBuildFlags::ALLOW_UPDATE;++ self.time += 1.0 / 60.0;++ self.instances[0].set_transform(&Affine3A::from_rotation_y(self.time));++ unsafe {+ let mapping = self+ .device+ .map_buffer(&self.instances_buffer, 0..instances_buffer_size as u64)+ .unwrap();+ ptr::copy_nonoverlapping(+ self.instances.as_ptr() as *const u8,+ mapping.ptr.as_ptr(),+ instances_buffer_size,+ );+ self.device.unmap_buffer(&self.instances_buffer);+ assert!(mapping.is_coherent);+ }++ unsafe {+ ctx.encoder.begin_encoding(Some("frame")).unwrap();++ let instances = hal::AccelerationStructureInstances {+ buffer: Some(&self.instances_buffer),+ count: self.instances.len() as u32,+ offset: 0,+ };++ ctx.encoder+ .place_acceleration_structure_barrier(hal::AccelerationStructureBarrier {+ usage: hal::StateTransition {+ from: hal::AccelerationStructureUses::SHADER_INPUT,+ to: hal::AccelerationStructureUses::BUILD_INPUT,+ },+ });++ ctx.encoder.build_acceleration_structures(+ 1,+ [hal::BuildAccelerationStructureDescriptor {+ mode: hal::AccelerationStructureBuildMode::Update,+ flags: tlas_flags,+ destination_acceleration_structure: &self.tlas,+ scratch_buffer: &self.scratch_buffer,+ entries: &hal::AccelerationStructureEntries::Instances(instances),+ source_acceleration_structure: Some(&self.tlas),+ scratch_buffer_offset: 0,+ }],+ );++ ctx.encoder+ .place_acceleration_structure_barrier(hal::AccelerationStructureBarrier {+ usage: hal::StateTransition {+ from: hal::AccelerationStructureUses::BUILD_OUTPUT,+ to: hal::AccelerationStructureUses::SHADER_INPUT,+ },+ });++ let scratch_buffer_barrier = hal::BufferBarrier {+ buffer: &self.scratch_buffer,+ usage: hal::StateTransition {+ from: wgpu_types::BufferUses::BOTTOM_LEVEL_ACCELERATION_STRUCTURE_INPUT,+ to: wgpu_types::BufferUses::TOP_LEVEL_ACCELERATION_STRUCTURE_INPUT,+ },+ };+ ctx.encoder+ .transition_buffers(iter::once(scratch_buffer_barrier));++ ctx.encoder.transition_textures(iter::once(target_barrier0));+ }++ let surface_view_desc = hal::TextureViewDescriptor {+ label: None,+ format: self.surface_format,+ dimension: wgpu_types::TextureViewDimension::D2,+ usage: wgpu_types::TextureUses::COPY_DST,+ range: wgpu_types::ImageSubresourceRange::default(),+ };+ let surface_tex_view = unsafe {+ self.device+ .create_texture_view(surface_tex.borrow(), &surface_view_desc)+ .unwrap()+ };+ unsafe {+ ctx.encoder.begin_compute_pass(&hal::ComputePassDescriptor {+ label: None,+ timestamp_writes: None,+ });+ ctx.encoder.set_compute_pipeline(&self.pipeline);+ ctx.encoder+ .set_bind_group(&self.pipeline_layout, 0, &self.bind_group, &[]);+ ctx.encoder.dispatch_workgroups([512 / 8, 512 / 8, 1]);+ }++ ctx.frames_recorded += 1;++ let target_barrier1 = hal::TextureBarrier {+ texture: surface_tex.borrow(),+ range: wgpu_types::ImageSubresourceRange::default(),+ usage: hal::StateTransition {+ from: wgpu_types::TextureUses::COPY_DST,+ to: wgpu_types::TextureUses::PRESENT,+ },+ };+ let target_barrier2 = hal::TextureBarrier {+ texture: &self.texture,+ range: wgpu_types::ImageSubresourceRange::default(),+ usage: hal::StateTransition {+ from: wgpu_types::TextureUses::STORAGE_READ_WRITE,+ to: wgpu_types::TextureUses::COPY_SRC,+ },+ };+ let target_barrier3 = hal::TextureBarrier {+ texture: &self.texture,+ range: wgpu_types::ImageSubresourceRange::default(),+ usage: hal::StateTransition {+ from: wgpu_types::TextureUses::COPY_SRC,+ to: wgpu_types::TextureUses::STORAGE_READ_WRITE,+ },+ };+ unsafe {+ ctx.encoder.end_compute_pass();+ ctx.encoder.transition_textures(iter::once(target_barrier2));+ ctx.encoder.copy_texture_to_texture(+ &self.texture,+ wgpu_types::TextureUses::COPY_SRC,+ surface_tex.borrow(),+ std::iter::once(hal::TextureCopy {+ src_base: hal::TextureCopyBase {+ mip_level: 0,+ array_layer: 0,+ origin: wgpu_types::Origin3d::ZERO,+ aspect: hal::FormatAspects::COLOR,+ },+ dst_base: hal::TextureCopyBase {+ mip_level: 0,+ array_layer: 0,+ origin: wgpu_types::Origin3d::ZERO,+ aspect: hal::FormatAspects::COLOR,+ },+ size: hal::CopyExtent {+ width: 512,+ height: 512,+ depth: 1,+ },+ }),+ );+ ctx.encoder.transition_textures(iter::once(target_barrier1));+ ctx.encoder.transition_textures(iter::once(target_barrier3));+ }++ unsafe {+ let cmd_buf = ctx.encoder.end_encoding().unwrap();+ self.queue+ .submit(+ &[&cmd_buf],+ &[&surface_tex],+ (&mut ctx.fence, ctx.fence_value),+ )+ .unwrap();+ self.queue.present(&self.surface, surface_tex).unwrap();+ ctx.used_cmd_bufs.push(cmd_buf);+ ctx.used_views.push(surface_tex_view);+ };++ log::info!("Context switch from {}", self.context_index);+ let old_fence_value = ctx.fence_value;+ if self.contexts.len() == 1 {+ let hal_desc = hal::CommandEncoderDescriptor {+ label: None,+ queue: &self.queue,+ };+ self.contexts.push(unsafe {+ ExecutionContext {+ encoder: self.device.create_command_encoder(&hal_desc).unwrap(),+ fence: self.device.create_fence().unwrap(),+ fence_value: 0,+ used_views: Vec::new(),+ used_cmd_bufs: Vec::new(),+ frames_recorded: 0,+ }+ });+ }+ self.context_index = (self.context_index + 1) % self.contexts.len();+ let next = &mut self.contexts[self.context_index];+ unsafe {+ next.wait_and_clear(&self.device);+ }+ next.fence_value = old_fence_value + 1;+ }++ fn exit(mut self) {+ unsafe {+ {+ let ctx = &mut self.contexts[self.context_index];+ self.queue+ .submit(&[], &[], (&mut ctx.fence, ctx.fence_value))+ .unwrap();+ }++ for mut ctx in self.contexts {+ ctx.wait_and_clear(&self.device);+ drop(ctx.encoder);+ self.device.destroy_fence(ctx.fence);+ }++ self.device.destroy_bind_group(self.bind_group);+ self.device.destroy_buffer(self.scratch_buffer);+ self.device.destroy_buffer(self.instances_buffer);+ if let Some(buffer) = self.indices_buffer {+ self.device.destroy_buffer(buffer);+ }+ self.device.destroy_buffer(self.vertices_buffer);+ self.device.destroy_buffer(self.uniform_buffer);+ self.device.destroy_acceleration_structure(self.tlas);+ self.device.destroy_acceleration_structure(self.blas);+ self.device.destroy_texture_view(self.texture_view);+ self.device.destroy_texture(self.texture);+ self.device.destroy_compute_pipeline(self.pipeline);+ self.device.destroy_pipeline_layout(self.pipeline_layout);+ self.device.destroy_bind_group_layout(self.bgl);+ self.device.destroy_shader_module(self.shader_module);++ self.surface.unconfigure(&self.device);+ drop(self.queue);+ drop(self.device);+ drop(self.surface);+ drop(self.adapter);+ }+ }+}++cfg_if::cfg_if! {+ // Apple + Metal+ if #[cfg(all(target_vendor = "apple", feature = "metal"))] {+ type Api = hal::api::Metal;+ }+ // Wasm + Vulkan+ else if #[cfg(all(not(target_arch = "wasm32"), feature = "vulkan"))] {+ type Api = hal::api::Vulkan;+ }+ // Windows + DX12+ else if #[cfg(all(windows, feature = "dx12"))] {+ type Api = hal::api::Dx12;+ }+ // Anything + GLES+ else if #[cfg(feature = "gles")] {+ type Api = hal::api::Gles;+ }+ // Fallback+ else {+ type Api = hal::api::Noop;+ }+}++struct App {+ example: Option<Example<Api>>,+ window: Option<Window>,+}++impl ApplicationHandler for App {+ fn resumed(&mut self, event_loop: &ActiveEventLoop) {+ if self.window.is_some() {+ return;+ }+ let window = event_loop+ .create_window(+ Window::default_attributes()+ .with_title("hal-ray-traced-triangle")+ .with_inner_size(winit::dpi::PhysicalSize {+ width: 512,+ height: 512,+ })+ .with_resizable(false)+ .with_enabled_buttons(WindowButtons::CLOSE),+ )+ .unwrap();+ let example = Example::<Api>::init(&window).expect("Selected backend is not supported");+ self.window = Some(window);+ self.example = Some(example);+ }++ fn exiting(&mut self, _event_loop: &ActiveEventLoop) {+ self.example.take().unwrap().exit();+ }++ fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {+ if let Some(window) = &self.window {+ window.request_redraw();+ }+ }++ fn window_event(+ &mut self,+ event_loop: &ActiveEventLoop,+ _window_id: winit::window::WindowId,+ event: winit::event::WindowEvent,+ ) {+ event_loop.set_control_flow(ControlFlow::Poll);++ match event {+ winit::event::WindowEvent::CloseRequested => {+ event_loop.exit();+ }+ winit::event::WindowEvent::KeyboardInput { event, .. }+ if event.physical_key+ == winit::keyboard::PhysicalKey::Code(winit::keyboard::KeyCode::Escape) =>+ {+ event_loop.exit();+ }+ winit::event::WindowEvent::RedrawRequested => {+ let ex = self.example.as_mut().unwrap();+ ex.render();+ }+ _ => {+ self.example.as_mut().unwrap().update(event);+ }+ }+ }+}++fn main() {+ env_logger::init();++ let event_loop = winit::event_loop::EventLoop::new().unwrap();+ let mut app = App {+ example: None,+ window: None,+ };+ event_loop.run_app(&mut app).unwrap();+}diff --git a/vendor/wgpu-hal/examples/ray-traced-triangle/shader.wgsl b/vendor/wgpu-hal/examples/ray-traced-triangle/shader.wgslnew file mode 100644index 00000000..9710ca65--- /dev/null+++ b/vendor/wgpu-hal/examples/ray-traced-triangle/shader.wgsl@@ -0,0 +1,39 @@+enable wgpu_ray_query;++struct Uniforms {+ view_inv: mat4x4<f32>,+ proj_inv: mat4x4<f32>,+};+@group(0) @binding(0)+var<uniform> uniforms: Uniforms;++@group(0) @binding(1)+var output: texture_storage_2d<rgba8unorm, write>;++@group(0) @binding(2)+var acc_struct: acceleration_structure;++@compute @workgroup_size(8, 8)+fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {+ let target_size = textureDimensions(output);++ let pixel_center = vec2<f32>(global_id.xy) + vec2<f32>(0.5);+ let in_uv = pixel_center / vec2<f32>(target_size.xy);+ let d = in_uv * 2.0 - 1.0;++ let origin = (uniforms.view_inv * vec4<f32>(0.0, 0.0, 0.0, 1.0)).xyz;+ let temp = uniforms.proj_inv * vec4<f32>(d.x, d.y, 1.0, 1.0);+ let direction = (uniforms.view_inv * vec4<f32>(normalize(temp.xyz), 0.0)).xyz;++ var rq: ray_query;+ rayQueryInitialize(&rq, acc_struct, RayDesc(0u, 0xFFu, 0.1, 200.0, origin, direction));+ rayQueryProceed(&rq);++ var color = vec4<f32>(0.0, 0.0, 0.0, 1.0);+ let intersection = rayQueryGetCommittedIntersection(&rq);+ if intersection.kind != RAY_QUERY_INTERSECTION_NONE {+ color = vec4<f32>(intersection.barycentrics, 1.0 - intersection.barycentrics.x - intersection.barycentrics.y, 1.0);+ }++ textureStore(output, global_id.xy, color);+}\ No newline at end of filediff --git a/vendor/wgpu-hal/src/auxil/dxgi/conv.rs b/vendor/wgpu-hal/src/auxil/dxgi/conv.rsnew file mode 100644index 00000000..c9e4dbc2--- /dev/null+++ b/vendor/wgpu-hal/src/auxil/dxgi/conv.rs@@ -0,0 +1,301 @@+use alloc::string::String;+use std::{ffi::OsString, os::windows::ffi::OsStringExt};++use windows::Win32::Graphics::Dxgi;++// Helper to convert DXGI adapter name to a normal string+pub fn map_adapter_name(name: [u16; 128]) -> String {+ let len = name.iter().take_while(|&&c| c != 0).count();+ let name = OsString::from_wide(&name[..len]);+ name.to_string_lossy().into_owned()+}++pub fn map_texture_format_failable(+ format: wgt::TextureFormat,+) -> Option<Dxgi::Common::DXGI_FORMAT> {+ use wgt::TextureFormat as Tf;+ use Dxgi::Common::*;++ Some(match format {+ Tf::R8Unorm => DXGI_FORMAT_R8_UNORM,+ Tf::R8Snorm => DXGI_FORMAT_R8_SNORM,+ Tf::R8Uint => DXGI_FORMAT_R8_UINT,+ Tf::R8Sint => DXGI_FORMAT_R8_SINT,+ Tf::R16Uint => DXGI_FORMAT_R16_UINT,+ Tf::R16Sint => DXGI_FORMAT_R16_SINT,+ Tf::R16Unorm => DXGI_FORMAT_R16_UNORM,+ Tf::R16Snorm => DXGI_FORMAT_R16_SNORM,+ Tf::R16Float => DXGI_FORMAT_R16_FLOAT,+ Tf::Rg8Unorm => DXGI_FORMAT_R8G8_UNORM,+ Tf::Rg8Snorm => DXGI_FORMAT_R8G8_SNORM,+ Tf::Rg8Uint => DXGI_FORMAT_R8G8_UINT,+ Tf::Rg8Sint => DXGI_FORMAT_R8G8_SINT,+ Tf::Rg16Unorm => DXGI_FORMAT_R16G16_UNORM,+ Tf::Rg16Snorm => DXGI_FORMAT_R16G16_SNORM,+ Tf::R32Uint => DXGI_FORMAT_R32_UINT,+ Tf::R32Sint => DXGI_FORMAT_R32_SINT,+ Tf::R32Float => DXGI_FORMAT_R32_FLOAT,+ Tf::Rg16Uint => DXGI_FORMAT_R16G16_UINT,+ Tf::Rg16Sint => DXGI_FORMAT_R16G16_SINT,+ Tf::Rg16Float => DXGI_FORMAT_R16G16_FLOAT,+ Tf::Rgba8Unorm => DXGI_FORMAT_R8G8B8A8_UNORM,+ Tf::Rgba8UnormSrgb => DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,+ Tf::Bgra8UnormSrgb => DXGI_FORMAT_B8G8R8A8_UNORM_SRGB,+ Tf::Rgba8Snorm => DXGI_FORMAT_R8G8B8A8_SNORM,+ Tf::Bgra8Unorm => DXGI_FORMAT_B8G8R8A8_UNORM,+ Tf::Rgba8Uint => DXGI_FORMAT_R8G8B8A8_UINT,+ Tf::Rgba8Sint => DXGI_FORMAT_R8G8B8A8_SINT,+ Tf::Rgb9e5Ufloat => DXGI_FORMAT_R9G9B9E5_SHAREDEXP,+ Tf::Rgb10a2Uint => DXGI_FORMAT_R10G10B10A2_UINT,+ Tf::Rgb10a2Unorm => DXGI_FORMAT_R10G10B10A2_UNORM,+ Tf::Rg11b10Ufloat => DXGI_FORMAT_R11G11B10_FLOAT,+ Tf::R64Uint => DXGI_FORMAT_R32G32_UINT, // R64 emulated by R32G32+ Tf::Rg32Uint => DXGI_FORMAT_R32G32_UINT,+ Tf::Rg32Sint => DXGI_FORMAT_R32G32_SINT,+ Tf::Rg32Float => DXGI_FORMAT_R32G32_FLOAT,+ Tf::Rgba16Uint => DXGI_FORMAT_R16G16B16A16_UINT,+ Tf::Rgba16Sint => DXGI_FORMAT_R16G16B16A16_SINT,+ Tf::Rgba16Unorm => DXGI_FORMAT_R16G16B16A16_UNORM,+ Tf::Rgba16Snorm => DXGI_FORMAT_R16G16B16A16_SNORM,+ Tf::Rgba16Float => DXGI_FORMAT_R16G16B16A16_FLOAT,+ Tf::Rgba32Uint => DXGI_FORMAT_R32G32B32A32_UINT,+ Tf::Rgba32Sint => DXGI_FORMAT_R32G32B32A32_SINT,+ Tf::Rgba32Float => DXGI_FORMAT_R32G32B32A32_FLOAT,+ Tf::Stencil8 => DXGI_FORMAT_D24_UNORM_S8_UINT,+ Tf::Depth16Unorm => DXGI_FORMAT_D16_UNORM,+ Tf::Depth24Plus => DXGI_FORMAT_D24_UNORM_S8_UINT,+ Tf::Depth24PlusStencil8 => DXGI_FORMAT_D24_UNORM_S8_UINT,+ Tf::Depth32Float => DXGI_FORMAT_D32_FLOAT,+ Tf::Depth32FloatStencil8 => DXGI_FORMAT_D32_FLOAT_S8X24_UINT,+ Tf::NV12 => DXGI_FORMAT_NV12,+ Tf::P010 => DXGI_FORMAT_P010,+ Tf::Bc1RgbaUnorm => DXGI_FORMAT_BC1_UNORM,+ Tf::Bc1RgbaUnormSrgb => DXGI_FORMAT_BC1_UNORM_SRGB,+ Tf::Bc2RgbaUnorm => DXGI_FORMAT_BC2_UNORM,+ Tf::Bc2RgbaUnormSrgb => DXGI_FORMAT_BC2_UNORM_SRGB,+ Tf::Bc3RgbaUnorm => DXGI_FORMAT_BC3_UNORM,+ Tf::Bc3RgbaUnormSrgb => DXGI_FORMAT_BC3_UNORM_SRGB,+ Tf::Bc4RUnorm => DXGI_FORMAT_BC4_UNORM,+ Tf::Bc4RSnorm => DXGI_FORMAT_BC4_SNORM,+ Tf::Bc5RgUnorm => DXGI_FORMAT_BC5_UNORM,+ Tf::Bc5RgSnorm => DXGI_FORMAT_BC5_SNORM,+ Tf::Bc6hRgbUfloat => DXGI_FORMAT_BC6H_UF16,+ Tf::Bc6hRgbFloat => DXGI_FORMAT_BC6H_SF16,+ Tf::Bc7RgbaUnorm => DXGI_FORMAT_BC7_UNORM,+ Tf::Bc7RgbaUnormSrgb => DXGI_FORMAT_BC7_UNORM_SRGB,+ Tf::Etc2Rgb8Unorm+ | Tf::Etc2Rgb8UnormSrgb+ | Tf::Etc2Rgb8A1Unorm+ | Tf::Etc2Rgb8A1UnormSrgb+ | Tf::Etc2Rgba8Unorm+ | Tf::Etc2Rgba8UnormSrgb+ | Tf::EacR11Unorm+ | Tf::EacR11Snorm+ | Tf::EacRg11Unorm+ | Tf::EacRg11Snorm+ | Tf::Astc {+ block: _,+ channel: _,+ } => return None,+ })+}++pub fn map_texture_format(format: wgt::TextureFormat) -> Dxgi::Common::DXGI_FORMAT {+ match map_texture_format_failable(format) {+ Some(f) => f,+ None => unreachable!(),+ }+}++// Note: DXGI doesn't allow sRGB format on the swapchain,+// but creating RTV of swapchain buffers with sRGB works.+pub fn map_texture_format_nosrgb(format: wgt::TextureFormat) -> Dxgi::Common::DXGI_FORMAT {+ match format {+ wgt::TextureFormat::Bgra8UnormSrgb => Dxgi::Common::DXGI_FORMAT_B8G8R8A8_UNORM,+ wgt::TextureFormat::Rgba8UnormSrgb => Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM,+ _ => map_texture_format(format),+ }+}++// SRV and UAV can't use the depth or typeless formats+// see https://microsoft.github.io/DirectX-Specs/d3d/PlanarDepthStencilDDISpec.html#view-creation+pub fn map_texture_format_for_srv_uav(+ format: wgt::TextureFormat,+ aspect: crate::FormatAspects,+) -> Option<Dxgi::Common::DXGI_FORMAT> {+ Some(match (format, aspect) {+ (wgt::TextureFormat::Depth16Unorm, crate::FormatAspects::DEPTH) => {+ Dxgi::Common::DXGI_FORMAT_R16_UNORM+ }+ (wgt::TextureFormat::Depth32Float, crate::FormatAspects::DEPTH) => {+ Dxgi::Common::DXGI_FORMAT_R32_FLOAT+ }+ (wgt::TextureFormat::Depth32FloatStencil8, crate::FormatAspects::DEPTH) => {+ Dxgi::Common::DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS+ }+ (+ wgt::TextureFormat::Depth24Plus | wgt::TextureFormat::Depth24PlusStencil8,+ crate::FormatAspects::DEPTH,+ ) => Dxgi::Common::DXGI_FORMAT_R24_UNORM_X8_TYPELESS,++ (wgt::TextureFormat::Depth32FloatStencil8, crate::FormatAspects::STENCIL) => {+ Dxgi::Common::DXGI_FORMAT_X32_TYPELESS_G8X24_UINT+ }+ (+ wgt::TextureFormat::Stencil8 | wgt::TextureFormat::Depth24PlusStencil8,+ crate::FormatAspects::STENCIL,+ ) => Dxgi::Common::DXGI_FORMAT_X24_TYPELESS_G8_UINT,++ (_, crate::FormatAspects::DEPTH)+ | (_, crate::FormatAspects::STENCIL)+ | (_, crate::FormatAspects::DEPTH_STENCIL) => return None,++ _ => map_texture_format(format),+ })+}++// see https://microsoft.github.io/DirectX-Specs/d3d/PlanarDepthStencilDDISpec.html#planar-layout-for-staging-from-buffer+pub fn map_texture_format_for_copy(+ format: wgt::TextureFormat,+ aspect: crate::FormatAspects,+) -> Option<Dxgi::Common::DXGI_FORMAT> {+ Some(match (format, aspect) {+ (wgt::TextureFormat::Depth16Unorm, crate::FormatAspects::DEPTH) => {+ Dxgi::Common::DXGI_FORMAT_R16_UNORM+ }+ (+ wgt::TextureFormat::Depth32Float | wgt::TextureFormat::Depth32FloatStencil8,+ crate::FormatAspects::DEPTH,+ ) => Dxgi::Common::DXGI_FORMAT_R32_FLOAT,++ (+ wgt::TextureFormat::Stencil8+ | wgt::TextureFormat::Depth24PlusStencil8+ | wgt::TextureFormat::Depth32FloatStencil8,+ crate::FormatAspects::STENCIL,+ ) => Dxgi::Common::DXGI_FORMAT_R8_UINT,++ // `CopyTextureRegion` on a plane subresource wants the+ // single-plane DXGI format, not the planar one.+ (format, aspects) if format.is_multi_planar_format() && aspects.is_one() => {+ map_texture_format(format.aspect_specific_format(aspects.map())?)+ }++ (format, crate::FormatAspects::COLOR) => map_texture_format(format),++ _ => return None,+ })+}++pub fn map_texture_format_for_resource(+ format: wgt::TextureFormat,+ usage: wgt::TextureUses,+ has_view_formats: bool,+ casting_fully_typed_format_supported: bool,+) -> Dxgi::Common::DXGI_FORMAT {+ use wgt::TextureFormat as Tf;+ use Dxgi::Common::*;++ if casting_fully_typed_format_supported {+ map_texture_format(format)++ // We might view this resource as srgb or non-srgb+ } else if has_view_formats {+ match format {+ Tf::Rgba8Unorm | Tf::Rgba8UnormSrgb => DXGI_FORMAT_R8G8B8A8_TYPELESS,+ Tf::Bgra8Unorm | Tf::Bgra8UnormSrgb => DXGI_FORMAT_B8G8R8A8_TYPELESS,+ Tf::Bc1RgbaUnorm | Tf::Bc1RgbaUnormSrgb => DXGI_FORMAT_BC1_TYPELESS,+ Tf::Bc2RgbaUnorm | Tf::Bc2RgbaUnormSrgb => DXGI_FORMAT_BC2_TYPELESS,+ Tf::Bc3RgbaUnorm | Tf::Bc3RgbaUnormSrgb => DXGI_FORMAT_BC3_TYPELESS,+ Tf::Bc7RgbaUnorm | Tf::Bc7RgbaUnormSrgb => DXGI_FORMAT_BC7_TYPELESS,+ format => map_texture_format(format),+ }++ // We might view this resource as SRV/UAV but also as DSV+ } else if format.is_depth_stencil_format()+ && usage.intersects(+ wgt::TextureUses::RESOURCE+ | wgt::TextureUses::STORAGE_READ_ONLY+ | wgt::TextureUses::STORAGE_WRITE_ONLY+ | wgt::TextureUses::STORAGE_READ_WRITE,+ )+ {+ match format {+ Tf::Depth16Unorm => DXGI_FORMAT_R16_TYPELESS,+ Tf::Depth32Float => DXGI_FORMAT_R32_TYPELESS,+ Tf::Depth32FloatStencil8 => DXGI_FORMAT_R32G8X24_TYPELESS,+ Tf::Stencil8 | Tf::Depth24Plus | Tf::Depth24PlusStencil8 => DXGI_FORMAT_R24G8_TYPELESS,+ _ => unreachable!(),+ }+ } else {+ map_texture_format(format)+ }+}++pub fn map_index_format(format: wgt::IndexFormat) -> Dxgi::Common::DXGI_FORMAT {+ match format {+ wgt::IndexFormat::Uint16 => Dxgi::Common::DXGI_FORMAT_R16_UINT,+ wgt::IndexFormat::Uint32 => Dxgi::Common::DXGI_FORMAT_R32_UINT,+ }+}++pub fn map_vertex_format(format: wgt::VertexFormat) -> Dxgi::Common::DXGI_FORMAT {+ use wgt::VertexFormat as Vf;+ use Dxgi::Common::*;++ match format {+ Vf::Unorm8 => DXGI_FORMAT_R8_UNORM,+ Vf::Snorm8 => DXGI_FORMAT_R8_SNORM,+ Vf::Uint8 => DXGI_FORMAT_R8_UINT,+ Vf::Sint8 => DXGI_FORMAT_R8_SINT,+ Vf::Unorm8x2 => DXGI_FORMAT_R8G8_UNORM,+ Vf::Snorm8x2 => DXGI_FORMAT_R8G8_SNORM,+ Vf::Uint8x2 => DXGI_FORMAT_R8G8_UINT,+ Vf::Sint8x2 => DXGI_FORMAT_R8G8_SINT,+ Vf::Unorm8x4 => DXGI_FORMAT_R8G8B8A8_UNORM,+ Vf::Snorm8x4 => DXGI_FORMAT_R8G8B8A8_SNORM,+ Vf::Uint8x4 => DXGI_FORMAT_R8G8B8A8_UINT,+ Vf::Sint8x4 => DXGI_FORMAT_R8G8B8A8_SINT,+ Vf::Unorm16 => DXGI_FORMAT_R16_UNORM,+ Vf::Snorm16 => DXGI_FORMAT_R16_SNORM,+ Vf::Uint16 => DXGI_FORMAT_R16_UINT,+ Vf::Sint16 => DXGI_FORMAT_R16_SINT,+ Vf::Float16 => DXGI_FORMAT_R16_FLOAT,+ Vf::Unorm16x2 => DXGI_FORMAT_R16G16_UNORM,+ Vf::Snorm16x2 => DXGI_FORMAT_R16G16_SNORM,+ Vf::Uint16x2 => DXGI_FORMAT_R16G16_UINT,+ Vf::Sint16x2 => DXGI_FORMAT_R16G16_SINT,+ Vf::Float16x2 => DXGI_FORMAT_R16G16_FLOAT,+ Vf::Unorm16x4 => DXGI_FORMAT_R16G16B16A16_UNORM,+ Vf::Snorm16x4 => DXGI_FORMAT_R16G16B16A16_SNORM,+ Vf::Uint16x4 => DXGI_FORMAT_R16G16B16A16_UINT,+ Vf::Sint16x4 => DXGI_FORMAT_R16G16B16A16_SINT,+ Vf::Float16x4 => DXGI_FORMAT_R16G16B16A16_FLOAT,+ Vf::Uint32 => DXGI_FORMAT_R32_UINT,+ Vf::Sint32 => DXGI_FORMAT_R32_SINT,+ Vf::Float32 => DXGI_FORMAT_R32_FLOAT,+ Vf::Uint32x2 => DXGI_FORMAT_R32G32_UINT,+ Vf::Sint32x2 => DXGI_FORMAT_R32G32_SINT,+ Vf::Float32x2 => DXGI_FORMAT_R32G32_FLOAT,+ Vf::Uint32x3 => DXGI_FORMAT_R32G32B32_UINT,+ Vf::Sint32x3 => DXGI_FORMAT_R32G32B32_SINT,+ Vf::Float32x3 => DXGI_FORMAT_R32G32B32_FLOAT,+ Vf::Uint32x4 => DXGI_FORMAT_R32G32B32A32_UINT,+ Vf::Sint32x4 => DXGI_FORMAT_R32G32B32A32_SINT,+ Vf::Float32x4 => DXGI_FORMAT_R32G32B32A32_FLOAT,+ Vf::Unorm10_10_10_2 => DXGI_FORMAT_R10G10B10A2_UNORM,+ Vf::Unorm8x4Bgra => DXGI_FORMAT_B8G8R8A8_UNORM,+ Vf::Float64 | Vf::Float64x2 | Vf::Float64x3 | Vf::Float64x4 => unimplemented!(),+ }+}++pub fn map_acomposite_alpha_mode(mode: wgt::CompositeAlphaMode) -> Dxgi::Common::DXGI_ALPHA_MODE {+ match mode {+ wgt::CompositeAlphaMode::PreMultiplied => Dxgi::Common::DXGI_ALPHA_MODE_PREMULTIPLIED,+ wgt::CompositeAlphaMode::PostMultiplied => Dxgi::Common::DXGI_ALPHA_MODE_STRAIGHT,+ wgt::CompositeAlphaMode::Opaque => Dxgi::Common::DXGI_ALPHA_MODE_IGNORE,+ wgt::CompositeAlphaMode::Auto | wgt::CompositeAlphaMode::Inherit => {+ Dxgi::Common::DXGI_ALPHA_MODE_UNSPECIFIED+ }+ }+}diff --git a/vendor/wgpu-hal/src/auxil/dxgi/exception.rs b/vendor/wgpu-hal/src/auxil/dxgi/exception.rsnew file mode 100644index 00000000..ac7d55d8--- /dev/null+++ b/vendor/wgpu-hal/src/auxil/dxgi/exception.rs@@ -0,0 +1,97 @@+use alloc::{borrow::Cow, string::String};++use parking_lot::Mutex;+use windows::Win32::{Foundation, System::Diagnostics::Debug};++// This is a mutex as opposed to an atomic as we need to completely+// lock everyone out until we have registered or unregistered the+// exception handler, otherwise really nasty races could happen.+//+// By routing all the registration through these functions we can guarantee+// there is either 1 or 0 exception handlers registered, not multiple.+static EXCEPTION_HANDLER_COUNT: Mutex<usize> = Mutex::new(0);++pub fn register_exception_handler() {+ let mut count_guard = EXCEPTION_HANDLER_COUNT.lock();+ if *count_guard == 0 {+ unsafe { Debug::AddVectoredExceptionHandler(0, Some(output_debug_string_handler)) };+ }+ *count_guard += 1;+}++pub fn unregister_exception_handler() {+ let mut count_guard = EXCEPTION_HANDLER_COUNT.lock();+ if *count_guard == 1 {+ unsafe { Debug::RemoveVectoredExceptionHandler(output_debug_string_handler as *mut _) };+ }+ *count_guard -= 1;+}++const MESSAGE_PREFIXES: &[(&str, log::Level)] = &[+ ("CORRUPTION", log::Level::Error),+ ("ERROR", log::Level::Error),+ ("WARNING", log::Level::Warn),+ // We intentionally suppress "INFO" messages down to debug+ // so that users are not innundated with info messages from the runtime.+ ("INFO", log::Level::Debug),+ ("MESSAGE", log::Level::Trace),+];++unsafe extern "system" fn output_debug_string_handler(+ exception_info: *mut Debug::EXCEPTION_POINTERS,+) -> i32 {+ // See https://stackoverflow.com/a/41480827+ let record = unsafe { &*(*exception_info).ExceptionRecord };+ if record.NumberParameters != 2 {+ return Debug::EXCEPTION_CONTINUE_SEARCH;+ }+ let message = match record.ExceptionCode {+ Foundation::DBG_PRINTEXCEPTION_C => {+ String::from_utf8_lossy(bytemuck::cast_slice(&record.ExceptionInformation))+ }+ Foundation::DBG_PRINTEXCEPTION_WIDE_C => Cow::Owned(String::from_utf16_lossy(+ bytemuck::cast_slice(&record.ExceptionInformation),+ )),+ _ => return Debug::EXCEPTION_CONTINUE_SEARCH,+ };++ let message = match message.strip_prefix("D3D12 ") {+ Some(msg) => msg+ .trim_end_matches("\n\0")+ .trim_end_matches("[ STATE_CREATION WARNING #0: UNKNOWN]"),+ None => return Debug::EXCEPTION_CONTINUE_SEARCH,+ };++ let (message, level) = match MESSAGE_PREFIXES+ .iter()+ .find(|&&(prefix, _)| message.starts_with(prefix))+ {+ Some(&(prefix, level)) => (&message[prefix.len() + 2..], level),+ None => (message, log::Level::Debug),+ };++ if level == log::Level::Warn && message.contains("#82") {+ // This is are useless spammy warnings (#820, #821):+ // "The application did not pass any clear value to resource creation"+ return Debug::EXCEPTION_CONTINUE_SEARCH;+ }++ if level == log::Level::Warn && message.contains("DRAW_EMPTY_SCISSOR_RECTANGLE") {+ // This is normal, WebGPU allows passing empty scissor rectangles.+ return Debug::EXCEPTION_CONTINUE_SEARCH;+ }++ let _ = std::panic::catch_unwind(|| {+ log::log!(level, "{message}");+ });++ #[cfg(feature = "validation_canary")]+ if cfg!(debug_assertions) && level == log::Level::Error {+ use alloc::string::ToString as _;++ // Set canary and continue+ crate::VALIDATION_CANARY.add(message.to_string());+ }++ Debug::EXCEPTION_CONTINUE_EXECUTION+}diff --git a/vendor/wgpu-hal/src/auxil/dxgi/factory.rs b/vendor/wgpu-hal/src/auxil/dxgi/factory.rsnew file mode 100644index 00000000..ac050bcd--- /dev/null+++ b/vendor/wgpu-hal/src/auxil/dxgi/factory.rs@@ -0,0 +1,181 @@+use alloc::{string::String, vec::Vec};+use core::ops::Deref;++use windows::{core::Interface as _, Win32::Graphics::Dxgi};++use crate::dx12::DxgiLib;++use super::result::HResult as _;++// We can rely on the presence of DXGI 1.4 since D3D12 requires WDDM 2.0, Windows 10 (1507), and so does DXGI 1.4.++fn should_keep_adapter(adapter: &Dxgi::IDXGIAdapter1) -> bool {+ let desc = unsafe { adapter.GetDesc1() }.unwrap();++ // The Intel Haswell family of iGPUs had support for the D3D12 API but it was later+ // removed due to a security vulnerability.+ //+ // We are explicitly filtering out all the devices in the family because we are now+ // getting reports of device loss at a later time than at device creation time (`D3D12CreateDevice`).+ //+ // See https://www.intel.com/content/www/us/en/support/articles/000057520/graphics.html+ // This list of device IDs is from https://dgpu-docs.intel.com/devices/hardware-table.html+ let haswell_device_ids = [+ 0x0422, 0x0426, 0x042A, 0x042B, 0x042E, 0x0C22, 0x0C26, 0x0C2A, 0x0C2B, 0x0C2E, 0x0A22,+ 0x0A2A, 0x0A2B, 0x0D2A, 0x0D2B, 0x0D2E, 0x0A26, 0x0A2E, 0x0D22, 0x0D26, 0x0412, 0x0416,+ 0x0D12, 0x041A, 0x041B, 0x0C12, 0x0C16, 0x0C1A, 0x0C1B, 0x0C1E, 0x0A12, 0x0A1A, 0x0A1B,+ 0x0D16, 0x0D1A, 0x0D1B, 0x0D1E, 0x041E, 0x0A16, 0x0A1E, 0x0402, 0x0406, 0x040A, 0x040B,+ 0x040E, 0x0C02, 0x0C06, 0x0C0A, 0x0C0B, 0x0C0E, 0x0A02, 0x0A06, 0x0A0A, 0x0A0B, 0x0A0E,+ 0x0D02, 0x0D06, 0x0D0A, 0x0D0B, 0x0D0E,+ ];+ if desc.VendorId == 0x8086 && haswell_device_ids.contains(&desc.DeviceId) {+ return false;+ }++ // If run completely headless, windows will show two different WARP adapters, one+ // which is lying about being an integrated card. This is so that programs+ // that ignore software adapters will actually run on headless/gpu-less machines.+ //+ // We don't want that and discourage that kind of filtering anyway, so we skip the integrated WARP.+ if desc.VendorId == 5140+ && !Dxgi::DXGI_ADAPTER_FLAG(desc.Flags as i32).contains(Dxgi::DXGI_ADAPTER_FLAG_SOFTWARE)+ {+ let adapter_name = super::conv::map_adapter_name(desc.Description);+ if adapter_name.contains("Microsoft Basic Render Driver") {+ return false;+ }+ }++ true+}++#[derive(Clone, Debug)]+pub enum DxgiAdapter {+ /// Provided by DXGI 1.4+ Adapter3(Dxgi::IDXGIAdapter3),+ /// Provided by DXGI 1.6+ Adapter4(Dxgi::IDXGIAdapter4),+}++impl DxgiAdapter {+ pub fn query_video_memory_info(+ &self,+ group: Dxgi::DXGI_MEMORY_SEGMENT_GROUP,+ ) -> Result<Dxgi::DXGI_QUERY_VIDEO_MEMORY_INFO, crate::DeviceError> {+ let mut info = Dxgi::DXGI_QUERY_VIDEO_MEMORY_INFO::default();+ unsafe { self.QueryVideoMemoryInfo(0, group, &mut info) }+ .into_device_result("QueryVideoMemoryInfo")?;+ Ok(info)+ }+}++impl Deref for DxgiAdapter {+ type Target = Dxgi::IDXGIAdapter3;++ fn deref(&self) -> &Self::Target {+ match self {+ DxgiAdapter::Adapter3(a) => a,+ DxgiAdapter::Adapter4(a) => a,+ }+ }+}++pub fn enumerate_adapters(factory: DxgiFactory) -> Vec<DxgiAdapter> {+ let mut adapters = Vec::with_capacity(8);++ for cur_index in 0.. {+ profiling::scope!("IDXGIFactory1::EnumAdapters1");+ let adapter1: Dxgi::IDXGIAdapter1 = match unsafe { factory.EnumAdapters1(cur_index) } {+ Ok(a) => a,+ Err(e) if e.code() == Dxgi::DXGI_ERROR_NOT_FOUND => break,+ Err(e) => {+ log::error!("Failed enumerating adapters: {e}");+ break;+ }+ };++ if !should_keep_adapter(&adapter1) {+ continue;+ }++ if let Ok(adapter4) = adapter1.cast::<Dxgi::IDXGIAdapter4>() {+ adapters.push(DxgiAdapter::Adapter4(adapter4));+ } else {+ let adapter3 = adapter1.cast::<Dxgi::IDXGIAdapter3>().unwrap();+ adapters.push(DxgiAdapter::Adapter3(adapter3));+ }+ }++ adapters+}++#[derive(Clone, Debug)]+pub enum DxgiFactory {+ /// Provided by DXGI 1.4+ Factory4(Dxgi::IDXGIFactory4),+ /// Provided by DXGI 1.5+ Factory5(Dxgi::IDXGIFactory5),+ /// Provided by DXGI 1.6+ Factory6(Dxgi::IDXGIFactory6),+}++impl Deref for DxgiFactory {+ type Target = Dxgi::IDXGIFactory4;++ fn deref(&self) -> &Self::Target {+ match self {+ DxgiFactory::Factory4(f) => f,+ DxgiFactory::Factory5(f) => f,+ DxgiFactory::Factory6(f) => f,+ }+ }+}++impl DxgiFactory {+ pub fn as_factory5(&self) -> Option<&Dxgi::IDXGIFactory5> {+ match self {+ Self::Factory4(_) => None,+ Self::Factory5(f) => Some(f),+ Self::Factory6(f) => Some(f),+ }+ }+}++pub fn create_factory(+ instance_flags: wgt::InstanceFlags,+) -> Result<(DxgiLib, DxgiFactory), crate::InstanceError> {+ let lib_dxgi = DxgiLib::new().map_err(|e| {+ crate::InstanceError::with_source(String::from("failed to load dxgi.dll"), e)+ })?;++ let mut factory_flags = Dxgi::DXGI_CREATE_FACTORY_FLAGS::default();++ if instance_flags.contains(wgt::InstanceFlags::VALIDATION) {+ // The `DXGI_CREATE_FACTORY_DEBUG` flag is only allowed to be passed to+ // `CreateDXGIFactory2` if the debug interface is actually available. So+ // we check for whether it exists first.+ if let Ok(Some(_)) = lib_dxgi.debug_interface1() {+ factory_flags |= Dxgi::DXGI_CREATE_FACTORY_DEBUG;+ }+ }++ let factory4 = match lib_dxgi.create_factory4(factory_flags) {+ Ok(factory) => factory,+ Err(err) => {+ return Err(crate::InstanceError::with_source(+ String::from("IDXGIFactory4 creation failed"),+ err,+ ));+ }+ };++ if let Ok(factory6) = factory4.cast::<Dxgi::IDXGIFactory6>() {+ return Ok((lib_dxgi, DxgiFactory::Factory6(factory6)));+ }++ if let Ok(factory5) = factory4.cast::<Dxgi::IDXGIFactory5>() {+ return Ok((lib_dxgi, DxgiFactory::Factory5(factory5)));+ }++ Ok((lib_dxgi, DxgiFactory::Factory4(factory4)))+}diff --git a/vendor/wgpu-hal/src/auxil/dxgi/hdr.rs b/vendor/wgpu-hal/src/auxil/dxgi/hdr.rsnew file mode 100644index 00000000..3d810ee6--- /dev/null+++ b/vendor/wgpu-hal/src/auxil/dxgi/hdr.rs@@ -0,0 +1,352 @@+//! Reading a window's [`wgt::DisplayHdrInfo`] through DXGI.+//!+//! Lives in `auxil/dxgi` so the DX12 and Vulkan-on-Windows backends share it:+//! both query through a [`DxgiHdrSource`], so the same monitor reports identical+//! numbers under either backend.++use parking_lot::Mutex;+use windows::{+ core::Interface as _,+ Win32::{+ Foundation::HWND,+ Graphics::{Dxgi, Gdi},+ },+};++/// The primary colors of a color space, in CIE 1931 xy:+/// `[[red_x, red_y], [green_x, green_y], [blue_x, blue_y]]`.+type Primaries = [[f32; 2]; 3];++/// Reference primaries (CIE 1931 xy: red, green, blue) for the three coarse+/// gamut buckets, used to classify a display's reported primaries.+const REC709: Primaries = [[0.640, 0.330], [0.300, 0.600], [0.150, 0.060]];+const DISPLAY_P3: Primaries = [[0.680, 0.320], [0.265, 0.690], [0.150, 0.060]];+const REC2020: Primaries = [[0.708, 0.292], [0.170, 0.797], [0.131, 0.046]];++/// Classifies reported primaries into the closest coarse [`wgt::DisplayGamut`]+/// bucket (least-squares over the R/G/B chromaticities). Advisory only.+///+/// Returns `None` if every primary is zeroed (the EDID reported nothing usable),+/// so a bogus all-zero descriptor is not misclassified as sRGB.+fn classify_gamut(red: [f32; 2], green: [f32; 2], blue: [f32; 2]) -> Option<wgt::DisplayGamut> {+ let measured = [red, green, blue];+ if measured.iter().all(|p| p[0] == 0.0 && p[1] == 0.0) {+ return None;+ }+ let dist = |reference: &Primaries| -> f32 {+ measured+ .iter()+ .zip(reference)+ .map(|(m, r)| {+ let dx = m[0] - r[0];+ let dy = m[1] - r[1];+ dx * dx + dy * dy+ })+ .sum()+ };+ [+ (wgt::DisplayGamut::Srgb, dist(&REC709)),+ (wgt::DisplayGamut::DisplayP3, dist(&DISPLAY_P3)),+ (wgt::DisplayGamut::Rec2020, dist(&REC2020)),+ ]+ .into_iter()+ .min_by(|a, b| a.1.total_cmp(&b.1))+ .map(|(gamut, _)| gamut)+}++/// Maps an [`IDXGIOutput6::GetDesc1`] result and the SDR white level (from+/// [`sdr_white_nits_for_monitor`]) into the backend-agnostic+/// [`wgt::DisplayHdrInfo`].+///+/// Pure: it interprets `desc1` without any system call. Numeric values are+/// advisory (EDID-sourced); see [`wgt::DisplayLuminance`].+///+/// [`IDXGIOutput6::GetDesc1`]: windows::Win32::Graphics::Dxgi::IDXGIOutput6::GetDesc1+fn display_hdr_info_from_desc1(+ desc1: &Dxgi::DXGI_OUTPUT_DESC1,+ sdr_white_nits: Option<f32>,+) -> wgt::DisplayHdrInfo {+ // The output is in an HDR color space if its `ColorSpace` is one of the two+ // Windows uses for HDR swap-chains: HDR10 / PQ (`G2084_NONE_P2020`) or+ // scRGB / linear-extended-sRGB (`G10_NONE_P709`). Checking both ensures scRGB+ // HDR is not misreported as SDR.+ let high_dynamic_range = desc1.ColorSpace+ == Dxgi::Common::DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020+ || desc1.ColorSpace == Dxgi::Common::DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709;++ let luminance = wgt::DisplayLuminance {+ max_nits: Some(desc1.MaxLuminance),+ max_full_frame_nits: Some(desc1.MaxFullFrameLuminance),+ min_nits: Some(desc1.MinLuminance),+ sdr_white_nits,+ };++ let chromaticity = wgt::DisplayChromaticity {+ red: Some(desc1.RedPrimary),+ green: Some(desc1.GreenPrimary),+ blue: Some(desc1.BluePrimary),+ white: Some(desc1.WhitePoint),+ };++ let coarse = wgt::DisplayCoarseRange {+ high_dynamic_range: Some(high_dynamic_range),+ gamut: classify_gamut(desc1.RedPrimary, desc1.GreenPrimary, desc1.BluePrimary),+ };++ // `headroom` is `None`: DXGI exposes absolute luminance (captured in+ // `luminance`) rather than a relative headroom ratio. `bits_per_color` drops+ // values that don't fit `u8`.+ wgt::DisplayHdrInfo {+ luminance: Some(luminance),+ headroom: None,+ chromaticity: Some(chromaticity),+ coarse: Some(coarse),+ bits_per_color: u8::try_from(desc1.BitsPerColor).ok(),+ }+}++/// A window's display HDR info, read through DXGI.+///+/// Bundles the window's `HWND` with the DXGI factory used to query it. The factory+/// is built once and reused, rebuilt only when DXGI reports it stale; the monitor+/// lookup and adapter/output walk still run every call, since the window can move+/// between displays and the SDR white level changes at runtime.+pub struct DxgiHdrSource {+ hwnd: HWND,+ factory: Mutex<Option<Dxgi::IDXGIFactory1>>,+}++// SAFETY: `HWND` and `IDXGIFactory1` are `!Send`/`!Sync` in windows-rs. The `HWND`+// is only ever read to run a DXGI query (fine from any thread), and the factory is+// free-threaded for the enumeration done here with the `Mutex` serializing access.+// This mirrors the manual `Send`/`Sync` on `dx12::Surface`.+unsafe impl Send for DxgiHdrSource {}+unsafe impl Sync for DxgiHdrSource {}++impl DxgiHdrSource {+ /// Creates a source for the window identified by `hwnd`. The `HWND` is+ /// borrowed; using the source after that window is destroyed is undefined+ /// behavior.+ pub fn new(hwnd: HWND) -> Self {+ Self {+ hwnd,+ factory: Mutex::new(None),+ }+ }++ /// Reads the window's [`wgt::DisplayHdrInfo`], or `None` if its monitor can't+ /// be identified or queried (a headless or composition window, a+ /// pre-Win10-1703 system without `IDXGIOutput6`, or a COM failure). Never+ /// panics.+ pub fn display_hdr_info(&self) -> Option<wgt::DisplayHdrInfo> {+ let desc1 = self.output_desc1()?;+ let sdr_white_nits = sdr_white_nits_for_monitor(desc1.Monitor);+ Some(display_hdr_info_from_desc1(&desc1, sdr_white_nits))+ }++ /// The [`DXGI_OUTPUT_DESC1`] for the window's monitor, reusing the cached+ /// factory unless DXGI reports it stale.+ ///+ /// [`DXGI_OUTPUT_DESC1`]: Dxgi::DXGI_OUTPUT_DESC1+ fn output_desc1(&self) -> Option<Dxgi::DXGI_OUTPUT_DESC1> {+ let mut factory = self.factory.lock();++ // A factory only enumerates the adapter/output topology it saw at+ // creation, so rebuild it once it reports `IsCurrent() == false` (a+ // monitor or GPU was plugged or unplugged).+ // SAFETY: a cached factory is live; `IsCurrent` takes no caller pointers.+ let need_new = factory+ .as_ref()+ .is_none_or(|f| !unsafe { f.IsCurrent() }.as_bool());+ if need_new {+ // SAFETY: `CreateDXGIFactory1` takes no caller pointers; the `windows`+ // binding fills the interface out-pointer itself.+ match unsafe { Dxgi::CreateDXGIFactory1() } {+ Ok(new) => *factory = Some(new),+ Err(e) => {+ log::warn!("CreateDXGIFactory1 failed: {e}");+ return None;+ }+ }+ }+ output_desc1_from_factory(factory.as_ref()?, self.hwnd)+ }+}++/// The [`DXGI_OUTPUT_DESC1`] for the monitor backing `wnd_handle`, found by+/// walking every adapter's outputs on `factory` — not just the rendering+/// adapter's, so the monitor is found on hybrid-GPU systems where the window sits+/// on a display wired to a different adapter. `None` if none match.+///+/// [`DXGI_OUTPUT_DESC1`]: Dxgi::DXGI_OUTPUT_DESC1+fn output_desc1_from_factory(+ factory: &Dxgi::IDXGIFactory1,+ wnd_handle: HWND,+) -> Option<Dxgi::DXGI_OUTPUT_DESC1> {+ // SAFETY: `MonitorFromWindow` is sound for any `HWND`; an invalid one yields+ // a null `HMONITOR`, checked below.+ let hmonitor = unsafe { Gdi::MonitorFromWindow(wnd_handle, Gdi::MONITOR_DEFAULTTONEAREST) };+ if hmonitor.is_invalid() {+ log::warn!("MonitorFromWindow failed; cannot identify the window's output");+ return None;+ }+ for adapter_index in 0.. {+ // SAFETY: `factory` is live; `EnumAdapters1` takes only an index.+ let adapter = match unsafe { factory.EnumAdapters1(adapter_index) } {+ Ok(adapter) => adapter,+ // End of the adapter list: the monitor matched none of them.+ Err(e) if e.code() == Dxgi::DXGI_ERROR_NOT_FOUND => break,+ Err(e) => {+ log::warn!("IDXGIFactory1::EnumAdapters1 failed: {e}");+ break;+ }+ };+ for output_index in 0.. {+ // SAFETY: `adapter` is live (from above); `EnumOutputs` takes only+ // an index.+ let output = match unsafe { adapter.EnumOutputs(output_index) } {+ Ok(output) => output,+ // End of this adapter's outputs: move on to the next adapter.+ Err(e) if e.code() == Dxgi::DXGI_ERROR_NOT_FOUND => break,+ Err(e) => {+ log::warn!("IDXGIAdapter1::EnumOutputs failed: {e}");+ break;+ }+ };+ // SAFETY: `output` is live (from above); `GetDesc` returns a+ // binding-owned `DXGI_OUTPUT_DESC` by value.+ let desc = match unsafe { output.GetDesc() } {+ Ok(desc) => desc,+ Err(e) => {+ log::warn!("IDXGIOutput::GetDesc failed: {e}");+ continue;+ }+ };+ if desc.Monitor != hmonitor {+ continue;+ }+ // The window's monitor matched, so a failure past this point is a+ // real anomaly. `IDXGIOutput6` (DXGI 1.6 / Win10 1703+) is required+ // for `GetDesc1`.+ let output6 = match output.cast::<Dxgi::IDXGIOutput6>() {+ Ok(output6) => output6,+ Err(e) => {+ log::warn!("Casting to IDXGIOutput6 failed: {e}");+ return None;+ }+ };+ // SAFETY: `output6` is live (from the cast above); `GetDesc1`+ // returns a binding-owned `DXGI_OUTPUT_DESC1` by value.+ return match unsafe { output6.GetDesc1() } {+ Ok(desc1) => Some(desc1),+ Err(e) => {+ log::warn!("IDXGIOutput6::GetDesc1 failed: {e}");+ None+ }+ };+ }+ }+ log::warn!("No DXGI output matches the window's monitor");+ None+}++/// Best-effort SDR white level in nits for `hmonitor`, via the Windows+/// DisplayConfig API. Tracks the brightness slider, so it changes at runtime.+///+/// This is the only [`wgt::DisplayLuminance`] field `GetDesc1` doesn't carry; it+/// bridges absolute nits and relative headroom. It lives in+/// `DISPLAYCONFIG_SDR_WHITE_LEVEL`, outside DXGI, so it needs its own query.+///+/// Returns `None` on any failure (no match, query error, or a `0` reading). Never+/// panics. Pass [`Dxgi::DXGI_OUTPUT_DESC1::Monitor`] so the value matches the rest+/// of the `DisplayHdrInfo`.+fn sdr_white_nits_for_monitor(hmonitor: Gdi::HMONITOR) -> Option<f32> {+ use windows::Win32::{+ Devices::Display::{+ DisplayConfigGetDeviceInfo, GetDisplayConfigBufferSizes, QueryDisplayConfig,+ DISPLAYCONFIG_DEVICE_INFO_GET_SDR_WHITE_LEVEL,+ DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME, DISPLAYCONFIG_MODE_INFO,+ DISPLAYCONFIG_PATH_INFO, DISPLAYCONFIG_SDR_WHITE_LEVEL,+ DISPLAYCONFIG_SOURCE_DEVICE_NAME, QDC_ONLY_ACTIVE_PATHS,+ },+ Foundation::ERROR_SUCCESS,+ Graphics::Gdi::{GetMonitorInfoW, MONITORINFO, MONITORINFOEXW},+ };++ // 1. HMONITOR -> GDI device name (e.g. `\\.\DISPLAY1`).+ let mut monitor_info = MONITORINFOEXW::default();+ monitor_info.monitorInfo.cbSize = size_of::<MONITORINFOEXW>() as u32;+ // `GetMonitorInfoW` takes a `*mut MONITORINFO`; the `EXW` variant is+ // layout-compatible (it starts with `MONITORINFO`) and reports `cbSize`.+ let lpmi = core::ptr::from_mut(&mut monitor_info).cast::<MONITORINFO>();+ // SAFETY: `lpmi` points at a live `MONITORINFOEXW` with `cbSize` set, which+ // bounds the write; an invalid `hmonitor` just fails (handled below).+ let got_info = unsafe { GetMonitorInfoW(hmonitor, lpmi) };+ if !got_info.as_bool() {+ return None;+ }+ let gdi_device_name = monitor_info.szDevice;++ // 2. Enumerate the active DisplayConfig paths.+ let mut path_count = 0u32;+ let mut mode_count = 0u32;+ // SAFETY: both out-params are live `u32`s; the call only writes the required+ // buffer sizes into them.+ if unsafe {+ GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &mut path_count, &mut mode_count)+ } != ERROR_SUCCESS+ {+ return None;+ }+ let mut paths = alloc::vec![DISPLAYCONFIG_PATH_INFO::default(); path_count as usize];+ let mut modes = alloc::vec![DISPLAYCONFIG_MODE_INFO::default(); mode_count as usize];+ // SAFETY: `paths`/`modes` are sized to the counts reported above and passed+ // with those counts, bounding the writes.+ if unsafe {+ QueryDisplayConfig(+ QDC_ONLY_ACTIVE_PATHS,+ &mut path_count,+ paths.as_mut_ptr(),+ &mut mode_count,+ modes.as_mut_ptr(),+ None,+ )+ } != ERROR_SUCCESS+ {+ return None;+ }++ // 3. Match the path whose source is this monitor, then read its target's+ // SDR white level. `DisplayConfigGetDeviceInfo` returns a WIN32 code as+ // `i32` (`0` == `ERROR_SUCCESS`).+ for path in paths.iter().take(path_count as usize) {+ let mut source = DISPLAYCONFIG_SOURCE_DEVICE_NAME::default();+ source.header.r#type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME;+ source.header.size = size_of::<DISPLAYCONFIG_SOURCE_DEVICE_NAME>() as u32;+ source.header.adapterId = path.sourceInfo.adapterId;+ source.header.id = path.sourceInfo.id;+ // SAFETY: `source` is live with `header.type`/`header.size` set, which+ // bounds the call's access to that struct.+ if unsafe { DisplayConfigGetDeviceInfo(&mut source.header) } != ERROR_SUCCESS.0 as i32 {+ continue;+ }+ if source.viewGdiDeviceName != gdi_device_name {+ continue;+ }++ let mut white = DISPLAYCONFIG_SDR_WHITE_LEVEL::default();+ white.header.r#type = DISPLAYCONFIG_DEVICE_INFO_GET_SDR_WHITE_LEVEL;+ white.header.size = size_of::<DISPLAYCONFIG_SDR_WHITE_LEVEL>() as u32;+ white.header.adapterId = path.targetInfo.adapterId;+ white.header.id = path.targetInfo.id;+ // SAFETY: `white` is live with `header.type`/`header.size` set, which+ // bounds the call's access to that struct.+ if unsafe { DisplayConfigGetDeviceInfo(&mut white.header) } != ERROR_SUCCESS.0 as i32 {+ return None;+ }+ // `SDRWhiteLevel` encodes nits as `(nits / 80) * 1000`; `0` means the OS+ // reported nothing usable.+ return (white.SDRWhiteLevel > 0).then(|| white.SDRWhiteLevel as f32 * 80.0 / 1000.0);+ }+ None+}diff --git a/vendor/wgpu-hal/src/auxil/dxgi/mod.rs b/vendor/wgpu-hal/src/auxil/dxgi/mod.rsnew file mode 100644index 00000000..7a4bcc75--- /dev/null+++ b/vendor/wgpu-hal/src/auxil/dxgi/mod.rs@@ -0,0 +1,16 @@+// Every module here is gated behind `dx12` except `hdr` (the+// `DXGI_OUTPUT_DESC1` -> `DisplayHdrInfo` mapping), which the+// Vulkan-on-Windows backend also uses.+#[cfg(dx12)]+pub mod conv;+#[cfg(dx12)]+pub mod exception;+#[cfg(dx12)]+pub mod factory;+pub mod hdr;+#[cfg(dx12)]+pub mod name;+#[cfg(dx12)]+pub mod result;+#[cfg(dx12)]+pub mod time;diff --git a/vendor/wgpu-hal/src/auxil/dxgi/name.rs b/vendor/wgpu-hal/src/auxil/dxgi/name.rsnew file mode 100644index 00000000..d120e4ed--- /dev/null+++ b/vendor/wgpu-hal/src/auxil/dxgi/name.rs@@ -0,0 +1,24 @@+use windows::Win32::Graphics::Direct3D12::ID3D12Object;++use crate::auxil::dxgi::result::HResult;++/// Helper trait for setting the name of a D3D12 object.+///+/// This is implemented on all types that can be converted to an [`ID3D12Object`].+pub trait ObjectExt {+ fn set_name(&self, name: &str) -> Result<(), crate::DeviceError>;+}++impl<T> ObjectExt for T+where+ // Windows impls `From` for all parent interfaces, so we can use that to convert to ID3D12Object.+ //+ // This includes implementations for references.+ for<'a> &'a ID3D12Object: From<&'a T>,+{+ fn set_name(&self, name: &str) -> Result<(), crate::DeviceError> {+ let name = windows::core::HSTRING::from(name);+ let object: &ID3D12Object = self.into();+ unsafe { object.SetName(&name).into_device_result("SetName") }+ }+}diff --git a/vendor/wgpu-hal/src/auxil/dxgi/result.rs b/vendor/wgpu-hal/src/auxil/dxgi/result.rsnew file mode 100644index 00000000..f3d77e27--- /dev/null+++ b/vendor/wgpu-hal/src/auxil/dxgi/result.rs@@ -0,0 +1,28 @@+use windows::Win32::{Foundation, Graphics::Dxgi};++pub(crate) trait HResult<O> {+ fn into_device_result(self, description: &str) -> Result<O, crate::DeviceError>;+}+impl<T> HResult<T> for windows::core::Result<T> {+ fn into_device_result(self, description: &str) -> Result<T, crate::DeviceError> {+ #![allow(unreachable_code)]++ self.map_err(|err| {+ log::error!("{description} failed: {err}");++ match err.code() {+ Foundation::E_OUTOFMEMORY => crate::DeviceError::OutOfMemory,+ Dxgi::DXGI_ERROR_DEVICE_RESET | Dxgi::DXGI_ERROR_DEVICE_REMOVED => {+ #[cfg(feature = "device_lost_panic")]+ panic!("{description} failed: Device lost ({err})");+ crate::DeviceError::Lost+ }+ _ => {+ #[cfg(feature = "internal_error_panic")]+ panic!("{description} failed: {err}");+ crate::DeviceError::Unexpected+ }+ }+ })+ }+}diff --git a/vendor/wgpu-hal/src/auxil/dxgi/time.rs b/vendor/wgpu-hal/src/auxil/dxgi/time.rsnew file mode 100644index 00000000..b7406124--- /dev/null+++ b/vendor/wgpu-hal/src/auxil/dxgi/time.rs@@ -0,0 +1,97 @@+#![allow(dead_code, reason = "IPresentationManager is unused currently")]++use windows::Win32::System::Performance::{QueryPerformanceCounter, QueryPerformanceFrequency};++pub enum PresentationTimer {+ /// DXGI uses [`QueryPerformanceCounter()`]+ Dxgi {+ /// How many ticks of QPC per second+ frequency: u64,+ },+ /// [`IPresentationManager`] uses [`QueryInterruptTimePrecise()`]+ ///+ /// [`IPresentationManager`]: https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Graphics/CompositionSwapchain/struct.IPresentationManager.html+ /// [`QueryInterruptTimePrecise()`]: https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/System/WindowsProgramming/fn.QueryInterruptTimePrecise.html+ #[allow(non_snake_case)]+ IPresentationManager {+ fnQueryInterruptTimePrecise: unsafe extern "system" fn(*mut u64),+ },+}++impl core::fmt::Debug for PresentationTimer {+ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {+ match *self {+ Self::Dxgi { frequency } => f+ .debug_struct("DXGI")+ .field("frequency", &frequency)+ .finish(),+ Self::IPresentationManager {+ fnQueryInterruptTimePrecise,+ } => f+ .debug_struct("IPresentationManager")+ .field(+ "QueryInterruptTimePrecise",+ &(fnQueryInterruptTimePrecise as usize),+ )+ .finish(),+ }+ }+}++impl PresentationTimer {+ /// Create a presentation timer using QueryPerformanceFrequency (what DXGI uses for presentation times)+ pub fn new_dxgi() -> Self {+ let mut frequency = 0;+ unsafe { QueryPerformanceFrequency(&mut frequency) }.unwrap();++ Self::Dxgi {+ frequency: frequency+ .try_into()+ .expect("Frequency should not be negative"),+ }+ }++ /// Create a presentation timer using QueryInterruptTimePrecise (what IPresentationManager uses for presentation times)+ ///+ /// Panics if QueryInterruptTimePrecise isn't found (below Win10)+ pub fn new_ipresentation_manager() -> Self {+ // We need to load this explicitly, as QueryInterruptTimePrecise is only available on Windows 10++ //+ // Docs say it's in kernel32.dll, but it's actually in kernelbase.dll.+ // api-ms-win-core-realtime-l1-1-1.dll+ let kernelbase =+ libloading::os::windows::Library::open_already_loaded("kernelbase.dll").unwrap();+ // No concerns about lifetimes here as kernelbase is always there.+ let ptr = unsafe {+ kernelbase+ .get(c"QueryInterruptTimePrecise".to_bytes())+ .unwrap()+ };+ Self::IPresentationManager {+ fnQueryInterruptTimePrecise: *ptr,+ }+ }++ /// Gets the current time in nanoseconds.+ pub fn get_timestamp_ns(&self) -> u128 {+ // Always do u128 math _after_ hitting the timing function.+ match *self {+ PresentationTimer::Dxgi { frequency } => {+ let mut counter = 0;+ unsafe { QueryPerformanceCounter(&mut counter) }.unwrap();++ // counter * (1_000_000_000 / freq) but re-ordered to make more precise+ (counter as u128 * 1_000_000_000) / frequency as u128+ }+ PresentationTimer::IPresentationManager {+ fnQueryInterruptTimePrecise,+ } => {+ let mut counter = 0;+ unsafe { fnQueryInterruptTimePrecise(&mut counter) };++ // QueryInterruptTimePrecise uses units of 100ns for its tick.+ counter as u128 * 100+ }+ }+ }+}diff --git a/vendor/wgpu-hal/src/auxil/mod.rs b/vendor/wgpu-hal/src/auxil/mod.rsnew file mode 100644index 00000000..599b7d0f--- /dev/null+++ b/vendor/wgpu-hal/src/auxil/mod.rs@@ -0,0 +1,241 @@+// Mostly DX12-only, but also compiled for Vulkan-on-Windows, which reuses+// `dxgi::hdr` to query display HDR info. The DX12-only submodules stay gated+// behind `dx12` in `dxgi/mod.rs`.+#[cfg(any(dx12, all(vulkan, windows)))]+pub(super) mod dxgi;++#[cfg(all(native, feature = "renderdoc"))]+pub(super) mod renderdoc;++pub mod db {+ pub mod amd {+ /// cbindgen:ignore+ pub const VENDOR: u32 = 0x1002;+ }+ pub mod apple {+ /// cbindgen:ignore+ pub const VENDOR: u32 = 0x106B;+ }+ pub mod arm {+ /// cbindgen:ignore+ pub const VENDOR: u32 = 0x13B5;+ }+ pub mod broadcom {+ /// cbindgen:ignore+ pub const VENDOR: u32 = 0x14E4;+ }+ pub mod imgtec {+ /// cbindgen:ignore+ pub const VENDOR: u32 = 0x1010;+ }+ pub mod intel {+ /// cbindgen:ignore+ pub const VENDOR: u32 = 0x8086;+ pub const DEVICE_KABY_LAKE_MASK: u32 = 0x5900;+ pub const DEVICE_SKY_LAKE_MASK: u32 = 0x1900;+ }+ pub mod mesa {+ // Mesa does not actually have a PCI vendor id.+ //+ // To match Vulkan, we use the VkVendorId for Mesa in the gles backend so that lavapipe (Vulkan) and+ // llvmpipe (OpenGL) have the same vendor id.+ /// cbindgen:ignore+ pub const VENDOR: u32 = 0x10005;+ }+ pub mod nvidia {+ /// cbindgen:ignore+ pub const VENDOR: u32 = 0x10DE;+ }+ pub mod qualcomm {+ /// cbindgen:ignore+ pub const VENDOR: u32 = 0x5143;+ }+}++/// Maximum binding size for the shaders that only support `i32` indexing.+/// Interestingly, the index itself can't reach that high, because the minimum+/// element size is 4 bytes, but the compiler toolchain still computes the+/// offset at some intermediate point, internally, as i32.+pub const MAX_I32_BINDING_SIZE: u32 = (1 << 31) - 1;++pub use wgpu_naga_bridge::map_naga_stage;++impl crate::CopyExtent {+ pub fn map_extent_to_copy_size(extent: &wgt::Extent3d, dim: wgt::TextureDimension) -> Self {+ Self {+ width: extent.width,+ height: extent.height,+ depth: match dim {+ wgt::TextureDimension::D1 | wgt::TextureDimension::D2 => 1,+ wgt::TextureDimension::D3 => extent.depth_or_array_layers,+ },+ }+ }++ pub fn min(&self, other: &Self) -> Self {+ Self {+ width: self.width.min(other.width),+ height: self.height.min(other.height),+ depth: self.depth.min(other.depth),+ }+ }++ // Get the copy size at a specific mipmap level. This doesn't make most sense,+ // since the copy extents are provided *for* a mipmap level to start with.+ // But backends use `CopyExtent` more sparingly, and this piece is shared.+ pub fn at_mip_level(&self, level: u32) -> Self {+ Self {+ width: (self.width >> level).max(1),+ height: (self.height >> level).max(1),+ depth: (self.depth >> level).max(1),+ }+ }+}++impl crate::TextureCopyBase {+ pub fn max_copy_size(&self, full_size: &crate::CopyExtent) -> crate::CopyExtent {+ let mip = full_size.at_mip_level(self.mip_level);+ crate::CopyExtent {+ width: mip.width - self.origin.x,+ height: mip.height - self.origin.y,+ depth: mip.depth - self.origin.z,+ }+ }+}++impl crate::BufferTextureCopy {+ pub fn clamp_size_to_virtual(&mut self, full_size: &crate::CopyExtent) {+ let max_size = self.texture_base.max_copy_size(full_size);+ self.size = self.size.min(&max_size);+ }+}++impl crate::TextureCopy {+ pub fn clamp_size_to_virtual(+ &mut self,+ full_src_size: &crate::CopyExtent,+ full_dst_size: &crate::CopyExtent,+ ) {+ let max_src_size = self.src_base.max_copy_size(full_src_size);+ let max_dst_size = self.dst_base.max_copy_size(full_dst_size);+ self.size = self.size.min(&max_src_size).min(&max_dst_size);+ }+}++/// Adjust `limits` to honor HAL-imposed maximums and comply with WebGPU's+/// adapter capability guarantees.+#[cfg_attr(not(any_backend), allow(dead_code))]+pub(crate) fn adjust_raw_limits(mut limits: wgt::Limits) -> wgt::Limits {+ // Apply hal limits.+ limits.max_bind_groups = limits.max_bind_groups.min(crate::MAX_BIND_GROUPS as u32);+ limits.max_vertex_buffers = limits+ .max_vertex_buffers+ .min(crate::MAX_VERTEX_BUFFERS as u32);+ // Once we allow the 2 limits above to be higher than 24 we should use+ // `cap_limits_to_be_under_the_sum_limit` to cap them under+ // `max_bind_groups_plus_vertex_buffers`.+ const { assert!(crate::MAX_BIND_GROUPS + crate::MAX_VERTEX_BUFFERS == 24) };+ limits.max_bind_groups_plus_vertex_buffers = limits.max_bind_groups_plus_vertex_buffers.min(24);+ limits.max_color_attachments = limits+ .max_color_attachments+ .min(crate::MAX_COLOR_ATTACHMENTS as u32);++ // Adjust limits according to WebGPU adapter capability guarantees.+ // See <https://gpuweb.github.io/gpuweb/#adapter-capability-guarantees>.++ // WebGPU requires maxBindingsPerBindGroup to be at least the sum of all+ // per-stage limits multiplied with the maximum shader stages per pipeline.+ //+ // Since backends already report their maximum maxBindingsPerBindGroup,+ // we need to lower all per-stage limits to satisfy this guarantee.+ const MAX_SHADER_STAGES_PER_PIPELINE: u32 = 2;+ let max_per_stage_resources =+ limits.max_bindings_per_bind_group / MAX_SHADER_STAGES_PER_PIPELINE;++ cap_limits_to_be_under_the_sum_limit(+ [+ &mut limits.max_sampled_textures_per_shader_stage,+ &mut limits.max_uniform_buffers_per_shader_stage,+ &mut limits.max_storage_textures_per_shader_stage,+ &mut limits.max_storage_buffers_per_shader_stage,+ &mut limits.max_samplers_per_shader_stage,+ &mut limits.max_acceleration_structures_per_shader_stage,+ ],+ max_per_stage_resources,+ );++ // Not required by the spec but dynamic buffers count+ // towards non-dynamic buffer limits as well.+ limits.max_dynamic_uniform_buffers_per_pipeline_layout = limits+ .max_dynamic_uniform_buffers_per_pipeline_layout+ .min(limits.max_uniform_buffers_per_shader_stage);+ limits.max_dynamic_storage_buffers_per_pipeline_layout = limits+ .max_dynamic_storage_buffers_per_pipeline_layout+ .min(limits.max_storage_buffers_per_shader_stage);++ limits.min_uniform_buffer_offset_alignment = limits.min_uniform_buffer_offset_alignment.max(32);+ limits.min_storage_buffer_offset_alignment = limits.min_storage_buffer_offset_alignment.max(32);++ limits.max_uniform_buffer_binding_size = limits+ .max_uniform_buffer_binding_size+ .min(limits.max_buffer_size);+ limits.max_storage_buffer_binding_size = limits+ .max_storage_buffer_binding_size+ .min(limits.max_buffer_size);++ limits.max_storage_buffer_binding_size &= !(u64::from(wgt::STORAGE_BINDING_SIZE_ALIGNMENT) - 1);+ limits.max_vertex_buffer_array_stride &= !(wgt::VERTEX_ALIGNMENT as u32 - 1);++ let x = limits.max_compute_workgroup_size_x;+ let y = limits.max_compute_workgroup_size_y;+ let z = limits.max_compute_workgroup_size_z;+ let m = limits.max_compute_invocations_per_workgroup;+ limits.max_compute_workgroup_size_x = x.min(m);+ limits.max_compute_workgroup_size_y = y.min(m);+ limits.max_compute_workgroup_size_z = z.min(m);+ limits.max_compute_invocations_per_workgroup = m.min(x.saturating_mul(y).saturating_mul(z));++ limits.max_immediate_size = limits.max_immediate_size.min(256);++ limits+}++/// Evenly allocates space to each limit,+/// capping them only if strictly necessary.+pub fn cap_limits_to_be_under_the_sum_limit<const N: usize>(+ mut limits: [&mut u32; N],+ sum_limit: u32,+) {+ limits.sort();++ let mut rem_limit = sum_limit;+ let mut divisor = limits.len() as u32;+ for limit_to_adjust in limits {+ let limit = rem_limit / divisor;+ *limit_to_adjust = (*limit_to_adjust).min(limit);+ rem_limit -= *limit_to_adjust;+ divisor -= 1;+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn test_cap_limits_to_be_under_the_sum_limit() {+ test([3, 3, 3], 3, [1, 1, 1]);+ test([3, 2, 1], 3, [1, 1, 1]);+ test([1, 2, 3], 6, [1, 2, 3]);+ test([1, 2, 3], 3, [1, 1, 1]);+ test([1, 8, 100], 6, [1, 2, 3]);+ test([2, 80, 80], 6, [2, 2, 2]);+ test([2, 80, 80], 12, [2, 5, 5]);++ #[track_caller]+ fn test<const N: usize>(mut input: [u32; N], limit: u32, output: [u32; N]) {+ cap_limits_to_be_under_the_sum_limit(input.each_mut(), limit);+ assert_eq!(input, output);+ }+ }+}diff --git a/vendor/wgpu-hal/src/auxil/renderdoc.rs b/vendor/wgpu-hal/src/auxil/renderdoc.rsnew file mode 100644index 00000000..52fa2b0b--- /dev/null+++ b/vendor/wgpu-hal/src/auxil/renderdoc.rs@@ -0,0 +1,141 @@+//! RenderDoc integration - <https://renderdoc.org/>+#![cfg_attr(not(any(feature = "gles", feature = "vulkan")), allow(dead_code))]++use alloc::format;+use alloc::string::String;+use core::{ffi, ptr};++/// The dynamically loaded RenderDoc API function table+#[repr(C)]+#[derive(Debug)]+pub struct RenderDocApi {+ api: renderdoc_sys::RENDERDOC_API_1_4_1,+ lib: libloading::Library,+}++unsafe impl Send for RenderDocApi {}+unsafe impl Sync for RenderDocApi {}++/// RenderDoc API type+#[derive(Debug)]+pub enum RenderDoc {+ /// RenderDoc functionality is available+ Available {+ /// RenderDoc API with function pointers+ api: RenderDocApi,+ },+ /// RenderDoc functionality is _not_ available+ NotAvailable {+ /// A description why renderdoc functionality is not available+ reason: String,+ },+}++// TODO: replace with libloading API once supported+#[cfg(unix)]+const RTLD_NOLOAD: i32 = 0x4;++impl RenderDoc {+ pub unsafe fn new() -> Self {+ type GetApiFn = unsafe extern "C" fn(version: u32, out: *mut *mut ffi::c_void) -> i32;++ #[cfg(windows)]+ let renderdoc_filename = "renderdoc.dll";+ #[cfg(all(unix, not(target_os = "android")))]+ let renderdoc_filename = "librenderdoc.so";+ #[cfg(target_os = "android")]+ let renderdoc_filename = "libVkLayer_GLES_RenderDoc.so";++ #[cfg(unix)]+ let renderdoc_result: Result<libloading::Library, libloading::Error> = unsafe {+ libloading::os::unix::Library::open(+ Some(renderdoc_filename),+ libloading::os::unix::RTLD_NOW | RTLD_NOLOAD,+ )+ }+ .map(|lib| lib.into());++ #[cfg(windows)]+ let renderdoc_result: Result<libloading::Library, libloading::Error> =+ libloading::os::windows::Library::open_already_loaded(renderdoc_filename)+ .map(|lib| lib.into());++ let renderdoc_lib = match renderdoc_result {+ Ok(lib) => lib,+ Err(e) => {+ return RenderDoc::NotAvailable {+ reason: format!(+ "Unable to load renderdoc library '{renderdoc_filename}': {e:?}"+ ),+ }+ }+ };++ let get_api: libloading::Symbol<GetApiFn> =+ match unsafe { renderdoc_lib.get(c"RENDERDOC_GetAPI".to_bytes()) } {+ Ok(api) => api,+ Err(e) => {+ return RenderDoc::NotAvailable {+ reason: format!(+ "Unable to get RENDERDOC_GetAPI from renderdoc library '{renderdoc_filename}': {e:?}"+ ),+ }+ }+ };+ let mut obj = ptr::null_mut();+ match unsafe { get_api(10401, &mut obj) } {+ 1 => RenderDoc::Available {+ api: RenderDocApi {+ api: unsafe { *obj.cast::<renderdoc_sys::RENDERDOC_API_1_4_1>() },+ lib: renderdoc_lib,+ },+ },+ return_value => RenderDoc::NotAvailable {+ reason: format!(+ "Unable to get API from renderdoc library '{renderdoc_filename}': {return_value}"+ ),+ },+ }+ }+}++impl Default for RenderDoc {+ fn default() -> Self {+ if !cfg!(debug_assertions) {+ return RenderDoc::NotAvailable {+ reason: "RenderDoc support is only enabled with 'debug_assertions'".into(),+ };+ }+ unsafe { Self::new() }+ }+}+/// An implementation specific handle+pub type Handle = *mut ffi::c_void;++impl RenderDoc {+ /// Start a RenderDoc frame capture+ pub unsafe fn start_frame_capture(&self, device_handle: Handle, window_handle: Handle) -> bool {+ match *self {+ Self::Available { api: ref entry } => {+ unsafe { entry.api.StartFrameCapture.unwrap()(device_handle, window_handle) };+ true+ }+ Self::NotAvailable { ref reason } => {+ log::warn!("Could not start RenderDoc frame capture: {reason}");+ false+ }+ }+ }++ /// End a RenderDoc frame capture+ pub unsafe fn end_frame_capture(&self, device_handle: Handle, window_handle: Handle) {+ match *self {+ Self::Available { api: ref entry } => {+ unsafe { entry.api.EndFrameCapture.unwrap()(device_handle, window_handle) };+ }+ Self::NotAvailable { ref reason } => {+ log::warn!("Could not end RenderDoc frame capture: {reason}")+ }+ };+ }+}diff --git a/vendor/wgpu-hal/src/dx12/adapter.rs b/vendor/wgpu-hal/src/dx12/adapter.rsnew file mode 100644index 00000000..621eea4c--- /dev/null+++ b/vendor/wgpu-hal/src/dx12/adapter.rs@@ -0,0 +1,1546 @@+use alloc::{string::String, sync::Arc, vec::Vec};+use core::{ptr, sync::atomic::AtomicU64};+use std::thread;++use parking_lot::Mutex;+use windows::{+ core::Interface as _,+ Win32::{+ Devices::DeviceAndDriverInstallation::{+ SetupDiDestroyDeviceInfoList, SetupDiEnumDeviceInfo, SetupDiGetClassDevsW,+ SetupDiGetDeviceRegistryPropertyW, DIGCF_PRESENT, GUID_DEVCLASS_DISPLAY, HDEVINFO,+ SPDRP_ADDRESS, SPDRP_BUSNUMBER, SPDRP_HARDWAREID, SP_DEVINFO_DATA,+ },+ Foundation::{GetLastError, ERROR_NO_MORE_ITEMS},+ Graphics::{Direct3D, Direct3D12, Dxgi},+ UI::WindowsAndMessaging,+ },+};++use super::D3D12Lib;+use crate::{+ auxil::{+ self,+ dxgi::{factory::DxgiAdapter, result::HResult},+ },+ dx12::{+ dcomp::DCompLib, device_creation::DeviceFactory, shader_compilation, FeatureLevel,+ ShaderModel, SurfaceTarget,+ },+};++impl Drop for super::Adapter {+ fn drop(&mut self) {+ // Debug tracking alive objects+ if !thread::panicking()+ && self+ .private_caps+ .instance_flags+ .contains(wgt::InstanceFlags::VALIDATION)+ {+ unsafe {+ self.report_live_objects();+ }+ }+ }+}++impl super::Adapter {+ pub unsafe fn report_live_objects(&self) {+ if let Ok(debug_device) = self.raw.cast::<Direct3D12::ID3D12DebugDevice>() {+ unsafe {+ debug_device.ReportLiveDeviceObjects(+ Direct3D12::D3D12_RLDO_SUMMARY | Direct3D12::D3D12_RLDO_IGNORE_INTERNAL,+ )+ }+ .unwrap()+ }+ }++ pub fn raw_adapter(&self) -> &DxgiAdapter {+ &self.raw+ }++ #[allow(clippy::too_many_arguments)]+ pub(super) fn expose(+ adapter: DxgiAdapter,+ library: &Arc<D3D12Lib>,+ device_factory: &Arc<DeviceFactory>,+ dcomp_lib: &Arc<DCompLib>,+ instance_flags: wgt::InstanceFlags,+ memory_budget_thresholds: wgt::MemoryBudgetThresholds,+ compiler_container: Arc<shader_compilation::CompilerContainer>,+ backend_options: wgt::Dx12BackendOptions,+ telemetry: Option<crate::Telemetry>,+ ) -> Option<crate::ExposedAdapter<super::Api>> {+ let desc = unsafe { adapter.GetDesc2() }.unwrap();+ let driver_version = unsafe { adapter.CheckInterfaceSupport(&Dxgi::IDXGIDevice::IID) };+ let driver_version = driver_version+ .map(|driver_version| {+ let driver_version = driver_version as u64;+ [+ (driver_version >> 48) as u16,+ (driver_version >> 32) as u16,+ (driver_version >> 16) as u16,+ driver_version as u16,+ ]+ })+ .map_err(|e| e.code());++ // Create the device so that we can get the capabilities.+ let res = {+ profiling::scope!("ID3D12Device::create_device");+ device_factory.create_device(library, &adapter, Direct3D::D3D_FEATURE_LEVEL_11_0)+ };+ if let Some(telemetry) = telemetry {+ if let Err(err) = res {+ (telemetry.d3d12_expose_adapter)(+ &desc,+ driver_version,+ crate::D3D12ExposeAdapterResult::CreateDeviceError(err),+ );+ }+ }+ let device = res.ok()?;++ profiling::scope!("feature queries");++ // Detect the highest supported feature level.+ let d3d_feature_level = [+ Direct3D::D3D_FEATURE_LEVEL_12_2,+ Direct3D::D3D_FEATURE_LEVEL_12_1,+ Direct3D::D3D_FEATURE_LEVEL_12_0,+ Direct3D::D3D_FEATURE_LEVEL_11_1,+ Direct3D::D3D_FEATURE_LEVEL_11_0,+ ];+ let mut device_levels = Direct3D12::D3D12_FEATURE_DATA_FEATURE_LEVELS {+ NumFeatureLevels: d3d_feature_level.len() as u32,+ pFeatureLevelsRequested: d3d_feature_level.as_ptr().cast(),+ MaxSupportedFeatureLevel: Default::default(),+ };+ unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_FEATURE_LEVELS,+ <*mut _>::cast(&mut device_levels),+ size_of_val(&device_levels) as u32,+ )+ }+ .unwrap();+ let max_feature_level = match device_levels.MaxSupportedFeatureLevel {+ Direct3D::D3D_FEATURE_LEVEL_11_0 => FeatureLevel::V11_0,+ Direct3D::D3D_FEATURE_LEVEL_11_1 => FeatureLevel::V11_1,+ Direct3D::D3D_FEATURE_LEVEL_12_0 => FeatureLevel::V12_0,+ Direct3D::D3D_FEATURE_LEVEL_12_1 => FeatureLevel::V12_1,+ Direct3D::D3D_FEATURE_LEVEL_12_2 => FeatureLevel::V12_2,+ fl => {+ if let Some(telemetry) = telemetry {+ (telemetry.d3d12_expose_adapter)(+ &desc,+ driver_version,+ crate::D3D12ExposeAdapterResult::UnknownFeatureLevel(fl.0),+ );+ }+ return None;+ }+ };++ let device_name = auxil::dxgi::conv::map_adapter_name(desc.Description);++ let mut features_architecture = Direct3D12::D3D12_FEATURE_DATA_ARCHITECTURE::default();++ unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_ARCHITECTURE,+ <*mut _>::cast(&mut features_architecture),+ size_of_val(&features_architecture) as u32,+ )+ }+ .unwrap();++ let mut features1 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS1::default();+ let hr = unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_D3D12_OPTIONS1,+ <*mut _>::cast(&mut features1),+ size_of_val(&features1) as u32,+ )+ };++ let mut workarounds = super::Workarounds::default();++ let is_warp = device_name.contains("Microsoft Basic Render Driver");++ // WARP uses two different versioning schemes. Versions that ship with windows+ // use a version that starts with 10.x.x.x. Versions that ship from Nuget use 1.0.x.x.+ //+ // As far as we know, this is only an issue on the Nuget versions.+ if let Ok(driver_version) = driver_version {+ if is_warp && driver_version >= [1, 0, 13, 0] && driver_version[0] < 10 {+ workarounds.avoid_shader_debug_info = true;+ }+ }++ let driver_version_string = {+ let driver_version = driver_version.unwrap_or([0, 0, 0, 0]);+ format!(+ "{}.{}.{}.{}",+ driver_version[0], driver_version[1], driver_version[2], driver_version[3]+ )+ };++ let info = wgt::AdapterInfo {+ backend: wgt::Backend::Dx12,+ name: device_name,+ vendor: desc.VendorId,+ device: desc.DeviceId,+ device_type: if Dxgi::DXGI_ADAPTER_FLAG(desc.Flags as i32)+ .contains(Dxgi::DXGI_ADAPTER_FLAG_SOFTWARE)+ {+ wgt::DeviceType::Cpu+ } else if features_architecture.UMA.as_bool() {+ wgt::DeviceType::IntegratedGpu+ } else {+ wgt::DeviceType::DiscreteGpu+ },+ device_pci_bus_id: get_adapter_pci_info(desc.VendorId, desc.DeviceId),+ driver: driver_version_string,+ driver_info: String::new(),+ subgroup_min_size: features1.WaveLaneCountMin,+ subgroup_max_size: features1.WaveLaneCountMax,+ transient_saves_memory: Some(false),+ limit_bucket: None,+ };++ let mut options = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS::default();+ unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_D3D12_OPTIONS,+ <*mut _>::cast(&mut options),+ size_of_val(&options) as u32,+ )+ }+ .unwrap();++ /// Resource Binding Tiers: https://learn.microsoft.com/en-us/windows/win32/direct3d12/hardware-support#limits-dependant-on-hardware+ #[derive(PartialEq, Eq, PartialOrd, Ord)]+ enum ResourceBindingTier {+ T1,+ T2,+ T3,+ }+ let rbt = match options.ResourceBindingTier {+ Direct3D12::D3D12_RESOURCE_BINDING_TIER_1 => ResourceBindingTier::T1,+ Direct3D12::D3D12_RESOURCE_BINDING_TIER_2 => ResourceBindingTier::T2,+ tier if tier.0 >= Direct3D12::D3D12_RESOURCE_BINDING_TIER_3.0 => {+ ResourceBindingTier::T3+ }+ other => {+ log::debug!("Got zero or negative value for resource binding tier {other:?}");+ ResourceBindingTier::T1+ }+ };++ if rbt == ResourceBindingTier::T1 {+ if let Some(telemetry) = telemetry {+ (telemetry.d3d12_expose_adapter)(+ &desc,+ driver_version,+ crate::D3D12ExposeAdapterResult::ResourceBindingTier2Requirement,+ );+ }+ // We require Tier 2 or higher for the ability to make samplers bindless in all cases.+ return None;+ }++ let _depth_bounds_test_supported = {+ let mut features2 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS2::default();+ unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_D3D12_OPTIONS2,+ <*mut _>::cast(&mut features2),+ size_of_val(&features2) as u32,+ )+ }+ .is_ok()+ && features2.DepthBoundsTestSupported.as_bool()+ };++ let (casting_fully_typed_format_supported, view_instancing) = {+ let mut features3 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS3::default();+ if unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_D3D12_OPTIONS3,+ <*mut _>::cast(&mut features3),+ size_of_val(&features3) as u32,+ )+ }+ .is_ok()+ {+ (+ features3.CastingFullyTypedFormatSupported.as_bool(),+ features3.ViewInstancingTier.0 >= Direct3D12::D3D12_VIEW_INSTANCING_TIER_1.0,+ )+ } else {+ (false, false)+ }+ };++ let heap_create_not_zeroed = {+ // For D3D12_HEAP_FLAG_CREATE_NOT_ZEROED we just need to+ // make sure that options7 can be queried. See also:+ // https://devblogs.microsoft.com/directx/coming-to-directx-12-more-control-over-memory-allocation/+ let mut features7 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS7::default();+ unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_D3D12_OPTIONS7,+ <*mut _>::cast(&mut features7),+ size_of_val(&features7) as u32,+ )+ }+ .is_ok()+ };++ let unrestricted_buffer_texture_copy_pitch_supported = {+ let mut features13 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS13::default();+ unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_D3D12_OPTIONS13,+ <*mut _>::cast(&mut features13),+ size_of_val(&features13) as u32,+ )+ }+ .is_ok()+ && features13+ .UnrestrictedBufferTextureCopyPitchSupported+ .as_bool()+ };++ let mut max_sampler_descriptor_heap_size =+ Direct3D12::D3D12_MAX_SHADER_VISIBLE_SAMPLER_HEAP_SIZE;+ {+ let mut features19 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS19::default();+ let res = unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_D3D12_OPTIONS19,+ <*mut _>::cast(&mut features19),+ size_of_val(&features19) as u32,+ )+ };++ // Sometimes on Windows 11 23H2, the function returns success, even though the runtime+ // does not know about `Options19`. This can cause this number to be 0 as the structure isn't written to.+ // This value is nonsense and creating zero-sized sampler heaps can cause drivers to explode.+ // As as we're guaranteed 2048 anyway, we make sure this value is not under 2048.+ //+ // https://github.com/gfx-rs/wgpu/issues/7053+ let is_ok = res.is_ok();+ let is_above_minimum = features19.MaxSamplerDescriptorHeapSize+ > Direct3D12::D3D12_MAX_SHADER_VISIBLE_SAMPLER_HEAP_SIZE;+ if is_ok && is_above_minimum {+ max_sampler_descriptor_heap_size = features19.MaxSamplerDescriptorHeapSize;+ }+ };++ let mut shader_models_after_5_1 = [+ Direct3D12::D3D_SHADER_MODEL_6_9,+ Direct3D12::D3D_SHADER_MODEL_6_8,+ Direct3D12::D3D_SHADER_MODEL_6_7,+ Direct3D12::D3D_SHADER_MODEL_6_6,+ Direct3D12::D3D_SHADER_MODEL_6_5,+ Direct3D12::D3D_SHADER_MODEL_6_4,+ Direct3D12::D3D_SHADER_MODEL_6_3,+ Direct3D12::D3D_SHADER_MODEL_6_2,+ Direct3D12::D3D_SHADER_MODEL_6_1,+ Direct3D12::D3D_SHADER_MODEL_6_0,+ ]+ .iter();+ let max_device_shader_model = loop {+ if let Some(&sm) = shader_models_after_5_1.next() {+ let mut sm = Direct3D12::D3D12_FEATURE_DATA_SHADER_MODEL {+ HighestShaderModel: sm,+ };+ if unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_SHADER_MODEL,+ <*mut _>::cast(&mut sm),+ size_of_val(&sm) as u32,+ )+ }+ .is_ok()+ {+ break match sm.HighestShaderModel {+ Direct3D12::D3D_SHADER_MODEL_5_1 => ShaderModel::V5_1,+ Direct3D12::D3D_SHADER_MODEL_6_0 => ShaderModel::V6_0,+ Direct3D12::D3D_SHADER_MODEL_6_1 => ShaderModel::V6_1,+ Direct3D12::D3D_SHADER_MODEL_6_2 => ShaderModel::V6_2,+ Direct3D12::D3D_SHADER_MODEL_6_3 => ShaderModel::V6_3,+ Direct3D12::D3D_SHADER_MODEL_6_4 => ShaderModel::V6_4,+ Direct3D12::D3D_SHADER_MODEL_6_5 => ShaderModel::V6_5,+ Direct3D12::D3D_SHADER_MODEL_6_6 => ShaderModel::V6_6,+ Direct3D12::D3D_SHADER_MODEL_6_7 => ShaderModel::V6_7,+ Direct3D12::D3D_SHADER_MODEL_6_8 => ShaderModel::V6_8,+ Direct3D12::D3D_SHADER_MODEL_6_9 => ShaderModel::V6_9,+ _ => unreachable!(),+ };+ }+ } else {+ break ShaderModel::V5_1;+ }+ };++ let wgt_shader_model = backend_options+ .force_shader_model+ .get()+ .or(compiler_container.max_shader_model());++ let shader_model = if let Some(max_shader_model) = wgt_shader_model {+ let max_dxc_shader_model = match max_shader_model {+ wgt::DxcShaderModel::V6_0 => ShaderModel::V6_0,+ wgt::DxcShaderModel::V6_1 => ShaderModel::V6_1,+ wgt::DxcShaderModel::V6_2 => ShaderModel::V6_2,+ wgt::DxcShaderModel::V6_3 => ShaderModel::V6_3,+ wgt::DxcShaderModel::V6_4 => ShaderModel::V6_4,+ wgt::DxcShaderModel::V6_5 => ShaderModel::V6_5,+ wgt::DxcShaderModel::V6_6 => ShaderModel::V6_6,+ wgt::DxcShaderModel::V6_7 => ShaderModel::V6_7,+ wgt::DxcShaderModel::V6_8 => ShaderModel::V6_8,+ wgt::DxcShaderModel::V6_9 => ShaderModel::V6_9,+ };++ let shader_model = max_device_shader_model.min(max_dxc_shader_model);++ match shader_model {+ ShaderModel::V5_1 => {+ if let Some(telemetry) = telemetry {+ (telemetry.d3d12_expose_adapter)(+ &desc,+ driver_version,+ crate::D3D12ExposeAdapterResult::ShaderModel6Requirement,+ );+ }+ // don't expose this adapter if it doesn't support DXIL+ return None;+ }+ ShaderModel::V6_0 => naga::back::hlsl::ShaderModel::V6_0,+ ShaderModel::V6_1 => naga::back::hlsl::ShaderModel::V6_1,+ ShaderModel::V6_2 => naga::back::hlsl::ShaderModel::V6_2,+ ShaderModel::V6_3 => naga::back::hlsl::ShaderModel::V6_3,+ ShaderModel::V6_4 => naga::back::hlsl::ShaderModel::V6_4,+ ShaderModel::V6_5 => naga::back::hlsl::ShaderModel::V6_5,+ ShaderModel::V6_6 => naga::back::hlsl::ShaderModel::V6_6,+ ShaderModel::V6_7 => naga::back::hlsl::ShaderModel::V6_7,+ ShaderModel::V6_8 => naga::back::hlsl::ShaderModel::V6_8,+ ShaderModel::V6_9 => naga::back::hlsl::ShaderModel::V6_9,+ }+ } else {+ naga::back::hlsl::ShaderModel::V5_1+ };+ let private_caps = super::PrivateCapabilities {+ instance_flags,+ workarounds,+ heterogeneous_resource_heaps: options.ResourceHeapTier+ != Direct3D12::D3D12_RESOURCE_HEAP_TIER_1,+ memory_architecture: if features_architecture.UMA.as_bool() {+ super::MemoryArchitecture::Unified {+ cache_coherent: features_architecture.CacheCoherentUMA.as_bool(),+ }+ } else {+ super::MemoryArchitecture::NonUnified+ },+ heap_create_not_zeroed,+ casting_fully_typed_format_supported,+ // See https://github.com/gfx-rs/wgpu/issues/3552+ suballocation_supported: !info.name.contains("Iris(R) Xe"),+ shader_model,+ max_sampler_descriptor_heap_size,+ unrestricted_buffer_texture_copy_pitch_supported,+ };++ // these should always be available on d3d12+ let mut features = wgt::Features::empty()+ | wgt::Features::DEPTH_CLIP_CONTROL+ | wgt::Features::DEPTH32FLOAT_STENCIL8+ | wgt::Features::INDIRECT_FIRST_INSTANCE+ | wgt::Features::MAPPABLE_PRIMARY_BUFFERS+ | wgt::Features::MULTI_DRAW_INDIRECT_COUNT+ | wgt::Features::ADDRESS_MODE_CLAMP_TO_BORDER+ | wgt::Features::ADDRESS_MODE_CLAMP_TO_ZERO+ | wgt::Features::POLYGON_MODE_LINE+ | wgt::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES+ | wgt::Features::TIMESTAMP_QUERY+ | wgt::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS+ | wgt::Features::TIMESTAMP_QUERY_INSIDE_PASSES+ | wgt::Features::TEXTURE_COMPRESSION_BC+ | wgt::Features::TEXTURE_COMPRESSION_BC_SLICED_3D+ | wgt::Features::CLEAR_TEXTURE+ | wgt::Features::TEXTURE_FORMAT_16BIT_NORM+ | wgt::Features::IMMEDIATES+ | wgt::Features::PRIMITIVE_INDEX+ | wgt::Features::RG11B10UFLOAT_RENDERABLE+ | wgt::Features::DUAL_SOURCE_BLENDING+ | wgt::Features::TEXTURE_FORMAT_NV12+ | wgt::Features::FLOAT32_FILTERABLE+ | wgt::Features::FLOAT32_BLENDABLE+ | wgt::Features::TEXTURE_ATOMIC+ | wgt::Features::PASSTHROUGH_SHADERS+ | wgt::Features::EXTERNAL_TEXTURE+ | wgt::Features::MEMORY_DECORATION_COHERENT;++ //TODO: in order to expose this, we need to run a compute shader+ // that extract the necessary statistics out of the D3D12 result.+ // Alternatively, we could allocate a buffer for the query set,+ // write the results there, and issue a bunch of copy commands.+ //| wgt::Features::PIPELINE_STATISTICS_QUERY++ if max_feature_level >= FeatureLevel::V11_1 {+ features |= wgt::Features::VERTEX_WRITABLE_STORAGE;+ }++ features.set(+ wgt::Features::CONSERVATIVE_RASTERIZATION,+ options.ConservativeRasterizationTier+ != Direct3D12::D3D12_CONSERVATIVE_RASTERIZATION_TIER_NOT_SUPPORTED,+ );++ features.set(+ wgt::Features::TEXTURE_BINDING_ARRAY+ | wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY+ | wgt::Features::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING+ | wgt::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING+ // See note below the table https://learn.microsoft.com/en-us/windows/win32/direct3d12/hardware-support+ | wgt::Features::PARTIALLY_BOUND_BINDING_ARRAY,+ shader_model >= naga::back::hlsl::ShaderModel::V5_1 && rbt >= ResourceBindingTier::T3,+ );++ let bgra8unorm_storage_supported = {+ let mut bgra8unorm_info = Direct3D12::D3D12_FEATURE_DATA_FORMAT_SUPPORT {+ Format: Dxgi::Common::DXGI_FORMAT_B8G8R8A8_UNORM,+ ..Default::default()+ };+ let hr = unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_FORMAT_SUPPORT,+ <*mut _>::cast(&mut bgra8unorm_info),+ size_of_val(&bgra8unorm_info) as u32,+ )+ };+ hr.is_ok()+ && bgra8unorm_info+ .Support2+ .contains(Direct3D12::D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE)+ };+ features.set(+ wgt::Features::BGRA8UNORM_STORAGE,+ bgra8unorm_storage_supported,+ );++ let p010_format_supported = {+ let mut p010_info = Direct3D12::D3D12_FEATURE_DATA_FORMAT_SUPPORT {+ Format: Dxgi::Common::DXGI_FORMAT_P010,+ ..Default::default()+ };+ let hr = unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_FORMAT_SUPPORT,+ <*mut _>::cast(&mut p010_info),+ size_of_val(&p010_info) as u32,+ )+ };+ if hr.is_ok() {+ let supports_texture2d = p010_info+ .Support1+ .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_TEXTURE2D);+ let supports_shader_load = p010_info+ .Support1+ .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_SHADER_LOAD);+ let supports_shader_sample = p010_info+ .Support1+ .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE);+ supports_texture2d && supports_shader_load && supports_shader_sample+ } else {+ false+ }+ };+ features.set(wgt::Features::TEXTURE_FORMAT_P010, p010_format_supported);++ features.set(+ wgt::Features::SHADER_INT64,+ shader_model >= naga::back::hlsl::ShaderModel::V6_0+ && hr.is_ok()+ && features1.Int64ShaderOps.as_bool(),+ );++ let float16_supported = {+ let mut features4 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS4::default();+ let hr = unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_D3D12_OPTIONS4, // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_feature#syntax+ ptr::from_mut(&mut features4).cast(),+ size_of::<Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS4>() as _,+ )+ };+ hr.is_ok() && features4.Native16BitShaderOpsSupported.as_bool()+ };++ features.set(+ wgt::Features::SHADER_F16,+ shader_model >= naga::back::hlsl::ShaderModel::V6_2 && float16_supported,+ );+ features.set(+ wgt::Features::SHADER_I16,+ shader_model >= naga::back::hlsl::ShaderModel::V6_2 && float16_supported,+ );++ features.set(+ wgt::Features::SUBGROUP,+ shader_model >= naga::back::hlsl::ShaderModel::V6_0+ && hr.is_ok()+ && features1.WaveOps.as_bool(),+ );+ let mut features5 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS5::default();+ let has_features5 = unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_D3D12_OPTIONS5,+ <*mut _>::cast(&mut features5),+ size_of_val(&features5) as u32,+ )+ }+ .is_ok();++ // Once ray tracing pipelines are supported they also will go here+ let supports_ray_tracing = features5.RaytracingTier.0+ >= Direct3D12::D3D12_RAYTRACING_TIER_1_1.0+ && shader_model >= naga::back::hlsl::ShaderModel::V6_5+ && has_features5;++ features.set(+ wgt::Features::EXPERIMENTAL_RAY_QUERY+ | wgt::Features::EXTENDED_ACCELERATION_STRUCTURE_VERTEX_FORMATS,+ supports_ray_tracing,+ );++ // Binding arrays of TLAS are supported on D3D12 when ray tracing is supported.+ //+ // This flag is used for shader-side `binding_array<acceleration_structure>` as well as+ // allowing `BindGroupLayoutEntry::count = Some(...)` for `BindingType::AccelerationStructure`.+ features.set(+ wgt::Features::ACCELERATION_STRUCTURE_BINDING_ARRAY,+ supports_ray_tracing,+ );++ // Check for Int64 atomic support on buffers. This is very convoluted, but is based on a conservative reading+ // of https://microsoft.github.io/DirectX-Specs/d3d/HLSL_SM_6_6_Int64_and_Float_Atomics.html#integer-64-bit-capabilities.+ let atomic_int64_buffers;+ let atomic_int64_textures;+ {+ let mut features9 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS9::default();+ let hr9 = unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_D3D12_OPTIONS9,+ <*mut _>::cast(&mut features9),+ size_of_val(&features9) as u32,+ )+ }+ .is_ok();++ let mut features11 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS11::default();+ let hr11 = unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_D3D12_OPTIONS11,+ <*mut _>::cast(&mut features11),+ size_of_val(&features11) as u32,+ )+ }+ .is_ok();++ atomic_int64_buffers = hr9 && hr11 && hr.is_ok()+ // Int64 atomics show up in SM6.6.+ && shader_model >= naga::back::hlsl::ShaderModel::V6_6+ // They require Int64 to be available in the shader at all.+ && features1.Int64ShaderOps.as_bool()+ // As our RWByteAddressBuffers can exist on both descriptor heaps and+ // as root descriptors, we need to ensure that both cases are supported.+ // base SM6.6 only guarantees Int64 atomics on resources in root descriptors.+ && features11.AtomicInt64OnDescriptorHeapResourceSupported.as_bool()+ // Our Int64 atomic caps currently require groupshared. This+ // prevents Intel or Qcomm from using Int64 currently.+ // https://github.com/gfx-rs/wgpu/issues/8666+ && features9.AtomicInt64OnGroupSharedSupported.as_bool();++ atomic_int64_textures = hr9 && hr11 && hr.is_ok()+ // Int64 atomics show up in SM6.6.+ && shader_model >= naga::back::hlsl::ShaderModel::V6_6+ // They require Int64 to be available in the shader at all.+ && features1.Int64ShaderOps.as_bool()+ // Textures are typed resources, so we need this flag.+ && features9.AtomicInt64OnTypedResourceSupported.as_bool()+ // As textures can only exist in descriptor heaps, we require this.+ // However, all architectures that support atomics on typed resources+ // support this as well, so this is somewhat redundant.+ && features11.AtomicInt64OnDescriptorHeapResourceSupported.as_bool();+ };+ features.set(+ wgt::Features::SHADER_INT64_ATOMIC_ALL_OPS | wgt::Features::SHADER_INT64_ATOMIC_MIN_MAX,+ atomic_int64_buffers,+ );+ features.set(wgt::Features::TEXTURE_INT64_ATOMIC, atomic_int64_textures);+ let mesh_shader_supported = {+ let mut features7 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS7::default();+ unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_D3D12_OPTIONS7,+ <*mut _>::cast(&mut features7),+ size_of_val(&features7) as u32,+ )+ }+ .is_ok()+ && features7.MeshShaderTier != Direct3D12::D3D12_MESH_SHADER_TIER_NOT_SUPPORTED+ && shader_model >= naga::back::hlsl::ShaderModel::V6_5+ };+ features.set(+ wgt::Features::EXPERIMENTAL_MESH_SHADER,+ mesh_shader_supported,+ );+ let shader_barycentrics_supported = {+ let mut features3 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS3::default();+ unsafe {+ device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_D3D12_OPTIONS3,+ <*mut _>::cast(&mut features3),+ size_of_val(&features3) as u32,+ )+ }+ .is_ok()+ && features3.BarycentricsSupported.as_bool()+ && shader_model >= naga::back::hlsl::ShaderModel::V6_1+ };+ features.set(+ wgt::Features::SHADER_BARYCENTRICS | wgt::Features::SHADER_PER_VERTEX,+ shader_barycentrics_supported,+ );++ features.set(+ wgt::Features::MULTIVIEW,+ view_instancing && shader_model >= naga::back::hlsl::ShaderModel::V6_1,+ );+ features.set(+ wgt::Features::SELECTIVE_MULTIVIEW,+ view_instancing && shader_model >= naga::back::hlsl::ShaderModel::V6_1,+ );++ features.set(+ wgt::Features::EXPERIMENTAL_MESH_SHADER_MULTIVIEW,+ mesh_shader_supported+ && view_instancing+ && shader_model >= naga::back::hlsl::ShaderModel::V6_1,+ );++ // TODO: Determine if IPresentationManager is supported+ let presentation_timer = auxil::dxgi::time::PresentationTimer::new_dxgi();++ let downlevel = wgt::DownlevelCapabilities::default();++ // Limits that must share D3D12's root signature size of+ // D3D12_MAX_ROOT_COST 64 DWORDS (256 bytes).+ //+ // Root constants and root tables use 1 DWORD.+ // Root descriptors use 2 DWORDs.+ // Source: https://learn.microsoft.com/en-us/windows/win32/direct3d12/root-signature-limits#memory-limits-and-costs+ //+ // Per pipeline layout:+ // - RootElement::Immediates, 32 root constants+ // (bounded by maxImmediateSize) = 32 x 4 bytes = 128 bytes+ // - RootElement::SamplerHeapDescriptorTable, a descriptor table = 4 bytes+ // - RootElement::SpecialConstantBuffer, 3 root constants = 3 x 4 bytes = 12 bytes+ // - RootElement::DynamicStorageBufferOffsets, a root constant per dynamic storage buffer+ // (bounded by maxDynamicStorageBuffersPerPipelineLayout) = 4 x 4 bytes = 16 bytes+ // - RootElement::DynamicUniformBuffer, a root descriptor per dynamic uniform buffer+ // (bounded by maxDynamicUniformBuffersPerPipelineLayout) = 8 x 8 bytes = 64 bytes+ // Per bind group:+ // - RootElement::DescriptorTable, a descriptor table+ // (bounded by maxBindGroups) = 8 x 4 bytes = 32 bytes+ //+ // Source: logic in `create_pipeline_layout`+ //+ // Total: 128 + 4 + 12 + 16 + 64 + 32 = 256 bytes+ //+ let max_immediate_size = super::MAX_IMMEDIATE_SIZE;+ let max_bind_groups = 8;+ let max_dynamic_uniform_buffers_per_pipeline_layout = 8;+ let max_dynamic_storage_buffers_per_pipeline_layout = 4;++ // "Maximum number of descriptors in a Constant Buffer View (CBV), Shader Resource View (SRV), or Unordered Access View(UAV) heap used for rendering"+ let full_heap_count = match rbt {+ ResourceBindingTier::T1 | ResourceBindingTier::T2 => 1_000_000,+ // 1_000_000++ ResourceBindingTier::T3 => {+ // Theoretically vram limited, but in practice 2^20 is the limit+ 1 << 20+ }+ };++ // "Maximum number of Constant Buffer Views in all descriptor tables per shader stage"+ let max_uniform_buffers_per_shader_stage = match rbt {+ ResourceBindingTier::T1 | ResourceBindingTier::T2 => 14,+ _ => full_heap_count,+ };++ // "Maximum number of Shader Resource Views in all descriptor tables per shader stage"+ let mut max_srv_per_shader_stage = match rbt {+ ResourceBindingTier::T1 => 128,+ _ => full_heap_count,+ };++ // We use an extra SRV for all samplers in a bind group.+ // See comment in `create_pipeline_layout`.+ max_srv_per_shader_stage -= max_bind_groups;++ // If we also support acceleration structures these are shared so we must halve it.+ // It's unlikely that this affects anything because most devices that support ray tracing+ // probably have a higher binding tier than one.+ let mut max_sampled_textures_per_shader_stage = if supports_ray_tracing {+ max_srv_per_shader_stage / 2+ } else {+ max_srv_per_shader_stage+ };+ let mut max_acceleration_structures_per_shader_stage = if supports_ray_tracing {+ max_srv_per_shader_stage / 2+ } else {+ 0+ };++ // "Maximum number of Unordered Access Views in all descriptor tables across all stages"+ let max_uav_across_all_stages = match rbt {+ ResourceBindingTier::T1 => match max_feature_level {+ FeatureLevel::V11_0 => 8,+ _ => 64,+ },+ ResourceBindingTier::T2 => 64,+ ResourceBindingTier::T3 => full_heap_count,+ };+ const MAX_SHADER_STAGES_PER_PIPELINE: u32 = 2;+ // We must share the UAV limit across both storage resource limits.+ let max_uav_per_shader_stage = max_uav_across_all_stages / MAX_SHADER_STAGES_PER_PIPELINE;+ let max_storage_textures_per_shader_stage = max_uav_per_shader_stage / 2;+ let mut max_storage_buffers_per_shader_stage = max_uav_per_shader_stage / 2;++ // WebGPU storage buffers count as 1 SRV if they are read-only+ // or as 1 UAV if they are read-write. See comment in+ // `create_pipeline_layout`. Make sure we don't exceed+ // the maximum number of SRVs for the relevant limits.+ auxil::cap_limits_to_be_under_the_sum_limit(+ [+ &mut max_sampled_textures_per_shader_stage,+ &mut max_acceleration_structures_per_shader_stage,+ &mut max_storage_buffers_per_shader_stage,+ ],+ max_srv_per_shader_stage,+ );++ // "Maximum number of Samplers in all descriptor tables per shader stage"+ let max_samplers_per_shader_stage = match rbt {+ ResourceBindingTier::T1 => 16,+ _ => 2048,+ };++ // See https://microsoft.github.io/DirectX-Specs/d3d/ViewInstancing.html#maximum-viewinstancecount+ let max_multiview_view_count = if view_instancing { 4 } else { 0 };++ if let Some(telemetry) = telemetry {+ (telemetry.d3d12_expose_adapter)(+ &desc,+ driver_version,+ crate::D3D12ExposeAdapterResult::Success(+ max_feature_level,+ max_device_shader_model,+ ),+ );+ }++ // Source: https://microsoft.github.io/DirectX-Specs/d3d/MeshShader.html#dispatchmesh-api+ let max_task_mesh_workgroup_total_count = if mesh_shader_supported {+ 2u32.pow(22)+ } else {+ 0+ };+ // Technically it says "64k" but I highly doubt they want 65536 for compute and exactly 64,000 for task workgroups+ let max_task_mesh_workgroups_per_dimension = if mesh_shader_supported {+ Direct3D12::D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION+ } else {+ 0+ };++ Some(crate::ExposedAdapter {+ adapter: super::Adapter {+ raw: adapter,+ device,+ library: Arc::clone(library),+ dcomp_lib: Arc::clone(dcomp_lib),+ private_caps,+ presentation_timer,+ memory_budget_thresholds,+ compiler_container,+ options: backend_options,+ },+ info,+ features,+ capabilities: crate::Capabilities {+ limits: auxil::adjust_raw_limits(wgt::Limits {+ //+ // WebGPU LIMITS:+ // Based on https://gpuweb.github.io/gpuweb/correspondence/#limits+ //+ // 16384+ max_texture_dimension_1d: Direct3D12::D3D12_REQ_TEXTURE1D_U_DIMENSION,+ // 16384+ max_texture_dimension_2d: Direct3D12::D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION+ .min(Direct3D12::D3D12_REQ_TEXTURECUBE_DIMENSION),+ // 2048+ max_texture_dimension_3d: Direct3D12::D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION,+ // 2048+ max_texture_array_layers: Direct3D12::D3D12_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION,+ // No limit.+ max_bind_groups_plus_vertex_buffers: u32::MAX,+ // No limit.+ max_bindings_per_bind_group: u32::MAX,+ max_sampled_textures_per_shader_stage,+ max_samplers_per_shader_stage,+ max_storage_textures_per_shader_stage,+ max_storage_buffers_per_shader_stage,+ max_uniform_buffers_per_shader_stage,+ // See `InputSlot` param docs: https://learn.microsoft.com/en-ca/windows/win32/api/d3d12/ns-d3d12-d3d12_input_element_desc+ max_vertex_buffers: 16,+ // Dx12 does not expose a maximum buffer size in the API.+ // This limit is chosen to avoid potential issues with drivers should they internally+ // store buffer sizes using 32 bit ints (a situation we have already encountered with vulkan).+ max_buffer_size: i32::MAX as u64,+ max_storage_buffer_binding_size: auxil::MAX_I32_BINDING_SIZE as u64,+ // 65536+ max_uniform_buffer_binding_size:+ Direct3D12::D3D12_REQ_CONSTANT_BUFFER_ELEMENT_COUNT as u64 * 16,+ // 256+ min_uniform_buffer_offset_alignment:+ Direct3D12::D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT,+ // 16+ min_storage_buffer_offset_alignment:+ Direct3D12::D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT,+ // 30+ max_vertex_attributes: Direct3D12::D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT+ - 2, // -2 for `SV_VertexID` and `SV_InstanceID`+ // 2048+ max_vertex_buffer_array_stride: Direct3D12::D3D12_SO_BUFFER_MAX_STRIDE_IN_BYTES,+ // 31+ max_inter_stage_shader_variables: Direct3D12::D3D12_VS_OUTPUT_REGISTER_COUNT+ .min(Direct3D12::D3D12_PS_INPUT_REGISTER_COUNT)+ - 1, // - 1 for position+ max_immediate_size,+ max_bind_groups,+ max_dynamic_uniform_buffers_per_pipeline_layout,+ max_dynamic_storage_buffers_per_pipeline_layout,+ // 8+ max_color_attachments: Direct3D12::D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT,+ // 128 (No documented limit)+ max_color_attachment_bytes_per_sample:+ Direct3D12::D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT+ * wgt::TextureFormat::MAX_TARGET_PIXEL_BYTE_COST,+ // From: https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm#18.6.6%20Inter-Thread%20Data%20Sharing+ max_compute_workgroup_storage_size: 32768,+ // 1024+ max_compute_invocations_per_workgroup:+ Direct3D12::D3D12_CS_THREAD_GROUP_MAX_THREADS_PER_GROUP,+ // 1024+ max_compute_workgroup_size_x: Direct3D12::D3D12_CS_THREAD_GROUP_MAX_X,+ // 1024+ max_compute_workgroup_size_y: Direct3D12::D3D12_CS_THREAD_GROUP_MAX_Y,+ // 64+ max_compute_workgroup_size_z: Direct3D12::D3D12_CS_THREAD_GROUP_MAX_Z,+ // 65535+ max_compute_workgroups_per_dimension:+ Direct3D12::D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION,+ //+ // NATIVE (Non-WebGPU) LIMITS:+ //+ max_non_sampler_bindings: 1_000_000,+ max_binding_array_elements_per_shader_stage: full_heap_count,+ max_binding_array_sampler_elements_per_shader_stage:+ Direct3D12::D3D12_MAX_SHADER_VISIBLE_SAMPLER_HEAP_SIZE,++ max_task_workgroup_total_count: max_task_mesh_workgroup_total_count,+ max_task_workgroups_per_dimension: max_task_mesh_workgroups_per_dimension,+ max_mesh_workgroup_total_count: max_task_mesh_workgroup_total_count,+ max_mesh_workgroups_per_dimension: max_task_mesh_workgroups_per_dimension,++ // Source: https://microsoft.github.io/DirectX-Specs/d3d/MeshShader.html#dispatchmesh-api+ // Assume this inherits from compute shaders+ max_task_invocations_per_workgroup: if mesh_shader_supported {+ Direct3D12::D3D12_CS_4_X_THREAD_GROUP_MAX_THREADS_PER_GROUP+ } else {+ 0+ },+ max_task_invocations_per_dimension: if mesh_shader_supported {+ Direct3D12::D3D12_CS_THREAD_GROUP_MAX_Z+ } else {+ 0+ },+ // Source: https://microsoft.github.io/DirectX-Specs/d3d/MeshShader.html#amplification-shader-and-mesh-shader+ max_mesh_invocations_per_workgroup: if mesh_shader_supported { 128 } else { 0 },+ max_mesh_invocations_per_dimension: if mesh_shader_supported { 128 } else { 0 },++ max_task_payload_size: if mesh_shader_supported { 16384 } else { 0 },+ max_mesh_output_vertices: if mesh_shader_supported { 256 } else { 0 },+ max_mesh_output_primitives: if mesh_shader_supported { 256 } else { 0 },+ // Source: https://microsoft.github.io/DirectX-Specs/d3d/MeshShader.html#sv_rendertargetarrayindex-limitations-based-on-queryable-capability+ max_mesh_output_layers: if mesh_shader_supported { 8 } else { 0 },+ max_mesh_multiview_view_count: if mesh_shader_supported {+ max_multiview_view_count+ } else {+ 0+ },++ max_blas_primitive_count: if supports_ray_tracing {+ 1 << 29 // 2^29+ } else {+ 0+ },+ max_blas_geometry_count: if supports_ray_tracing {+ 1 << 24 // 2^24+ } else {+ 0+ },+ max_tlas_instance_count: if supports_ray_tracing {+ 1 << 24 // 2^24+ } else {+ 0+ },+ max_acceleration_structures_per_shader_stage,+ max_buffers_and_acceleration_structures_per_shader_stage: u32::MAX,+ max_binding_array_acceleration_structure_elements_per_shader_stage:+ max_acceleration_structures_per_shader_stage,+ max_multiview_view_count,++ // not yet implemented+ max_ray_dispatch_count: 0,+ max_ray_recursion_depth: 0,+ }),+ alignments: crate::Alignments {+ buffer_copy_offset: wgt::BufferSize::new(+ Direct3D12::D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT as u64,+ )+ .unwrap(),+ buffer_copy_pitch: wgt::BufferSize::new(+ Direct3D12::D3D12_TEXTURE_DATA_PITCH_ALIGNMENT as u64,+ )+ .unwrap(),+ // Direct3D correctly bounds-checks all array accesses:+ // https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm#18.6.8.2%20Device%20Memory%20Reads+ uniform_bounds_check_alignment: wgt::BufferSize::new(1).unwrap(),+ raw_tlas_instance_size: u32::try_from(size_of::<+ Direct3D12::D3D12_RAYTRACING_INSTANCE_DESC,+ >())+ .unwrap(),+ ray_tracing_scratch_buffer_alignment:+ Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BYTE_ALIGNMENT,+ // Not yet implemented+ ray_tracing_pipeline_group_data_size: 0,+ ray_tracing_pipeline_group_data_alignment: 0,+ ray_tracing_pipeline_data_offset_alignment: 0,+ },+ downlevel,+ cooperative_matrix_properties: Vec::new(),+ },+ })+ }+}++impl crate::Adapter for super::Adapter {+ type A = super::Api;++ unsafe fn open(+ &self,+ features: wgt::Features,+ limits: &wgt::Limits,+ memory_hints: &wgt::MemoryHints,+ ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> {+ let queue: Direct3D12::ID3D12CommandQueue = {+ profiling::scope!("ID3D12Device::CreateCommandQueue");+ unsafe {+ self.device+ .CreateCommandQueue(&Direct3D12::D3D12_COMMAND_QUEUE_DESC {+ Type: Direct3D12::D3D12_COMMAND_LIST_TYPE_DIRECT,+ Priority: Direct3D12::D3D12_COMMAND_QUEUE_PRIORITY_NORMAL.0,+ Flags: Direct3D12::D3D12_COMMAND_QUEUE_FLAG_NONE,+ NodeMask: 0,+ })+ }+ .into_device_result("Queue creation")?+ };++ let device = super::Device::new(+ self.raw.clone(),+ self.device.clone(),+ queue.clone(),+ features,+ limits,+ memory_hints,+ self.private_caps,+ &self.library,+ &self.dcomp_lib,+ self.memory_budget_thresholds,+ self.compiler_container.clone(),+ self.options.clone(),+ )?;+ let idle_fence: Direct3D12::ID3D12Fence = unsafe {+ self.device+ .CreateFence(0, Direct3D12::D3D12_FENCE_FLAG_NONE)+ }+ .into_device_result("Queue idle fence creation")?;+ let idle_event = super::Event::create(false, false)?;++ Ok(crate::OpenDevice {+ device,+ queue: super::Queue {+ raw: queue,+ temp_lists: Mutex::new(Vec::new()),+ idle_fence,+ idle_event,+ idle_fence_value: AtomicU64::new(0),+ pending_waits: Mutex::new(Vec::new()),+ pending_signals: Mutex::new(Vec::new()),+ },+ })+ }++ unsafe fn texture_format_capabilities(+ &self,+ format: wgt::TextureFormat,+ ) -> crate::TextureFormatCapabilities {+ use crate::TextureFormatCapabilities as Tfc;++ let raw_format = match auxil::dxgi::conv::map_texture_format_failable(format) {+ Some(f) => f,+ None => return Tfc::empty(),+ };+ let srv_uav_format = if format.is_combined_depth_stencil_format() {+ auxil::dxgi::conv::map_texture_format_for_srv_uav(+ format,+ // use the depth aspect here as opposed to stencil since it has more capabilities+ crate::FormatAspects::DEPTH,+ )+ } else {+ auxil::dxgi::conv::map_texture_format_for_srv_uav(+ format,+ crate::FormatAspects::from(format),+ )+ }+ .unwrap();++ let mut data = Direct3D12::D3D12_FEATURE_DATA_FORMAT_SUPPORT {+ Format: raw_format,+ ..Default::default()+ };+ unsafe {+ self.device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_FORMAT_SUPPORT,+ <*mut _>::cast(&mut data),+ size_of_val(&data) as u32,+ )+ }+ .unwrap();++ // Because we use a different format for SRV and UAV views of depth textures, we need to check+ // the features that use SRV/UAVs using the no-depth format.+ let mut data_srv_uav = Direct3D12::D3D12_FEATURE_DATA_FORMAT_SUPPORT {+ Format: srv_uav_format,+ Support1: Direct3D12::D3D12_FORMAT_SUPPORT1_NONE,+ Support2: Direct3D12::D3D12_FORMAT_SUPPORT2_NONE,+ };+ if raw_format != srv_uav_format {+ // Only-recheck if we're using a different format+ unsafe {+ self.device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_FORMAT_SUPPORT,+ ptr::addr_of_mut!(data_srv_uav).cast(),+ size_of::<Direct3D12::D3D12_FEATURE_DATA_FORMAT_SUPPORT>() as u32,+ )+ }+ .unwrap();+ } else {+ // Same format, just copy over.+ data_srv_uav = data;+ }++ let mut caps = Tfc::COPY_SRC | Tfc::COPY_DST;+ // Cannot use the contains() helper, and windows-rs doesn't provide a .intersect() helper+ let is_texture = (data.Support1+ & (Direct3D12::D3D12_FORMAT_SUPPORT1_TEXTURE1D+ | Direct3D12::D3D12_FORMAT_SUPPORT1_TEXTURE2D+ | Direct3D12::D3D12_FORMAT_SUPPORT1_TEXTURE3D+ | Direct3D12::D3D12_FORMAT_SUPPORT1_TEXTURECUBE))+ .0+ != 0;+ // SRVs use srv_uav_format+ caps.set(+ Tfc::SAMPLED,+ is_texture+ && data_srv_uav+ .Support1+ .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_SHADER_LOAD),+ );+ caps.set(+ Tfc::SAMPLED_LINEAR,+ data_srv_uav+ .Support1+ .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE),+ );+ caps.set(+ Tfc::COLOR_ATTACHMENT,+ data.Support1+ .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_RENDER_TARGET),+ );+ caps.set(+ Tfc::COLOR_ATTACHMENT_BLEND,+ data.Support1+ .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_BLENDABLE),+ );+ caps.set(+ Tfc::DEPTH_STENCIL_ATTACHMENT,+ data.Support1+ .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_DEPTH_STENCIL),+ );+ // UAVs use srv_uav_format+ caps.set(+ Tfc::STORAGE_READ_ONLY,+ data_srv_uav+ .Support2+ .contains(Direct3D12::D3D12_FORMAT_SUPPORT2_UAV_TYPED_LOAD),+ );+ caps.set(+ Tfc::STORAGE_ATOMIC,+ data_srv_uav+ .Support2+ .contains(Direct3D12::D3D12_FORMAT_SUPPORT2_UAV_ATOMIC_UNSIGNED_MIN_OR_MAX),+ );+ caps.set(+ Tfc::STORAGE_WRITE_ONLY,+ data_srv_uav+ .Support2+ .contains(Direct3D12::D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE),+ );+ caps.set(+ Tfc::STORAGE_READ_WRITE,+ caps.contains(Tfc::STORAGE_READ_ONLY | Tfc::STORAGE_WRITE_ONLY),+ );++ // We load via UAV/SRV so use srv_uav_format+ let no_msaa_load = caps.contains(Tfc::SAMPLED)+ && !data_srv_uav+ .Support1+ .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_MULTISAMPLE_LOAD);++ let no_msaa_target = (data.Support1+ & (Direct3D12::D3D12_FORMAT_SUPPORT1_RENDER_TARGET+ | Direct3D12::D3D12_FORMAT_SUPPORT1_DEPTH_STENCIL))+ .0+ != 0+ && !data+ .Support1+ .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_MULTISAMPLE_RENDERTARGET);++ caps.set(+ Tfc::MULTISAMPLE_RESOLVE,+ data.Support1+ .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_MULTISAMPLE_RESOLVE),+ );++ let mut ms_levels = Direct3D12::D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS {+ Format: raw_format,+ SampleCount: 0,+ Flags: Direct3D12::D3D12_MULTISAMPLE_QUALITY_LEVELS_FLAG_NONE,+ NumQualityLevels: 0,+ };++ let mut set_sample_count = |sc: u32, tfc: Tfc| {+ ms_levels.SampleCount = sc;++ if unsafe {+ self.device.CheckFeatureSupport(+ Direct3D12::D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS,+ <*mut _>::cast(&mut ms_levels),+ size_of_val(&ms_levels) as u32,+ )+ }+ .is_ok()+ && ms_levels.NumQualityLevels != 0+ {+ caps.set(tfc, !no_msaa_load && !no_msaa_target);+ }+ };++ set_sample_count(2, Tfc::MULTISAMPLE_X2);+ set_sample_count(4, Tfc::MULTISAMPLE_X4);+ set_sample_count(8, Tfc::MULTISAMPLE_X8);+ set_sample_count(16, Tfc::MULTISAMPLE_X16);++ caps+ }++ unsafe fn surface_capabilities(+ &self,+ surface: &super::Surface,+ ) -> Option<crate::SurfaceCapabilities> {+ let current_extent = {+ match surface.target {+ SurfaceTarget::WndHandle(wnd_handle)+ | SurfaceTarget::VisualFromWndHandle {+ handle: wnd_handle, ..+ } => {+ let mut rect = Default::default();+ if unsafe { WindowsAndMessaging::GetClientRect(wnd_handle, &mut rect) }.is_ok()+ {+ Some(wgt::Extent3d {+ width: (rect.right - rect.left) as u32,+ height: (rect.bottom - rect.top) as u32,+ depth_or_array_layers: 1,+ })+ } else {+ log::warn!("Unable to get the window client rect");+ None+ }+ }+ SurfaceTarget::Visual(_)+ | SurfaceTarget::SurfaceHandle(_)+ | SurfaceTarget::SwapChainPanel(_) => None,+ }+ };++ let mut present_modes = vec![wgt::PresentMode::Mailbox, wgt::PresentMode::Fifo];+ if surface.supports_allow_tearing {+ present_modes.push(wgt::PresentMode::Immediate);+ }++ Some(crate::SurfaceCapabilities {+ // `Surface::configure` applies the requested color space with+ // `IDXGISwapChain3::SetColorSpace1`. fp16 buffers keep DXGI's+ // scRGB interpretation (`DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709`)+ // and `Rgb10a2Unorm` additionally supports BT.2100 PQ (HDR10).+ //+ // These color spaces are advertised unconditionally, not gated on+ // whether the output is currently in HDR mode: Windows always+ // composites in scRGB and tone-maps PQ down to an SDR output, so the+ // color space is configurable regardless, and `CheckColorSpaceSupport`+ // returning false does not mean it won't present. Whether HDR is+ // actually *visible* is a separate, live question (the upcoming+ // display-HDR query, #9739), not a configuration gate. Display-P3 and+ // HLG are never reported: DXGI has no RGB HLG swapchain color space,+ // and P3 isn't a DXGI swapchain color space.+ formats: [+ wgt::TextureFormat::Bgra8UnormSrgb,+ wgt::TextureFormat::Bgra8Unorm,+ wgt::TextureFormat::Rgba8UnormSrgb,+ wgt::TextureFormat::Rgba8Unorm,+ wgt::TextureFormat::Rgb10a2Unorm,+ wgt::TextureFormat::Rgba16Float,+ ]+ .map(|format| wgt::SurfaceFormatCapabilities {+ format,+ color_spaces: match format {+ wgt::TextureFormat::Rgba16Float => {+ wgt::SurfaceColorSpaces::EXTENDED_SRGB_LINEAR+ }+ wgt::TextureFormat::Rgb10a2Unorm => {+ wgt::SurfaceColorSpaces::SRGB | wgt::SurfaceColorSpaces::BT2100_PQ+ }+ _ => wgt::SurfaceColorSpaces::SRGB,+ },+ })+ .to_vec(),+ // See https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgidevice1-setmaximumframelatency+ maximum_frame_latency: 1..=16,+ current_extent,+ usage: wgt::TextureUses::COLOR_TARGET+ | wgt::TextureUses::COPY_SRC+ | wgt::TextureUses::COPY_DST,+ present_modes,+ composite_alpha_modes: match surface.target {+ SurfaceTarget::WndHandle(_) => vec![wgt::CompositeAlphaMode::Opaque],+ SurfaceTarget::Visual(_)+ | SurfaceTarget::VisualFromWndHandle { .. }+ | SurfaceTarget::SurfaceHandle(_)+ | SurfaceTarget::SwapChainPanel(_) => vec![+ wgt::CompositeAlphaMode::Auto,+ wgt::CompositeAlphaMode::Inherit,+ wgt::CompositeAlphaMode::Opaque,+ wgt::CompositeAlphaMode::PostMultiplied,+ wgt::CompositeAlphaMode::PreMultiplied,+ ],+ },+ })+ }++ unsafe fn surface_display_hdr_info(+ &self,+ surface: &super::Surface,+ ) -> Option<wgt::DisplayHdrInfo> {+ surface.hdr_source.as_ref()?.display_hdr_info()+ }++ unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {+ wgt::PresentationTimestamp(self.presentation_timer.get_timestamp_ns())+ }++ fn get_ordered_buffer_usages(&self) -> wgt::BufferUses {+ wgt::BufferUses::INCLUSIVE | wgt::BufferUses::MAP_WRITE+ }++ // Don't put barriers between inclusive uses+ // DX12 implicitly orders renderpasses on the same resources.+ fn get_ordered_texture_usages(&self) -> wgt::TextureUses {+ wgt::TextureUses::INCLUSIVE+ | wgt::TextureUses::COLOR_TARGET+ | wgt::TextureUses::DEPTH_STENCIL_WRITE+ }+}++fn get_adapter_pci_info(vendor_id: u32, device_id: u32) -> String {+ // SAFETY: SetupDiGetClassDevsW is called with valid parameters+ let device_info_set = unsafe {+ match SetupDiGetClassDevsW(Some(&GUID_DEVCLASS_DISPLAY), None, None, DIGCF_PRESENT) {+ Ok(set) => set,+ Err(_) => return String::new(),+ }+ };++ struct DeviceInfoSetGuard(HDEVINFO);+ impl Drop for DeviceInfoSetGuard {+ fn drop(&mut self) {+ // SAFETY: device_info_set is a valid HDEVINFO and is only dropped once via this guard+ unsafe {+ let _ = SetupDiDestroyDeviceInfoList(self.0);+ }+ }+ }+ let _guard = DeviceInfoSetGuard(device_info_set);++ let mut device_index = 0u32;+ loop {+ let mut device_info_data = SP_DEVINFO_DATA {+ cbSize: size_of::<SP_DEVINFO_DATA>() as u32,+ ..Default::default()+ };++ // SAFETY: device_info_set is a valid HDEVINFO, device_index starts at 0 and+ // device_info_data is properly initialized above+ unsafe {+ if SetupDiEnumDeviceInfo(device_info_set, device_index, &mut device_info_data).is_err()+ {+ if GetLastError() == ERROR_NO_MORE_ITEMS {+ break;+ }+ device_index += 1;+ continue;+ }+ }++ let mut hardware_id_size = 0u32;+ // SAFETY: device_info_set and device_info_data are valid+ unsafe {+ let _ = SetupDiGetDeviceRegistryPropertyW(+ device_info_set,+ &device_info_data,+ SPDRP_HARDWAREID,+ None,+ None,+ Some(&mut hardware_id_size),+ );+ }++ if hardware_id_size == 0 {+ device_index += 1;+ continue;+ }++ let mut hardware_id_buffer = vec![0u8; hardware_id_size as usize];+ // SAFETY: device_info_set and device_info_data are valid+ unsafe {+ if SetupDiGetDeviceRegistryPropertyW(+ device_info_set,+ &device_info_data,+ SPDRP_HARDWAREID,+ None,+ Some(&mut hardware_id_buffer),+ Some(&mut hardware_id_size),+ )+ .is_err()+ {+ device_index += 1;+ continue;+ }+ }++ let hardware_id_u16: Vec<u16> = hardware_id_buffer+ .chunks_exact(2)+ .map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]]))+ .collect();+ let hardware_ids: Vec<String> = hardware_id_u16+ .split(|&c| c == 0)+ .filter(|s| !s.is_empty())+ .map(|s| String::from_utf16_lossy(s).to_uppercase())+ .collect();++ // https://learn.microsoft.com/en-us/windows-hardware/drivers/install/identifiers-for-pci-devices+ let expected_id = format!("PCI\\VEN_{vendor_id:04X}&DEV_{device_id:04X}");+ if !hardware_ids.iter().any(|id| id.contains(&expected_id)) {+ device_index += 1;+ continue;+ }++ let mut bus_buffer = [0u8; 4];+ let mut data_size = bus_buffer.len() as u32;+ // SAFETY: device_info_set and device_info_data are valid+ let bus_number = unsafe {+ if SetupDiGetDeviceRegistryPropertyW(+ device_info_set,+ &device_info_data,+ SPDRP_BUSNUMBER,+ None,+ Some(&mut bus_buffer),+ Some(&mut data_size),+ )+ .is_err()+ {+ device_index += 1;+ continue;+ }+ u32::from_le_bytes(bus_buffer)+ };++ let mut addr_buffer = [0u8; 4];+ let mut addr_size = addr_buffer.len() as u32;+ // SAFETY: device_info_set and device_info_data are valid+ unsafe {+ if SetupDiGetDeviceRegistryPropertyW(+ device_info_set,+ &device_info_data,+ SPDRP_ADDRESS,+ None,+ Some(&mut addr_buffer),+ Some(&mut addr_size),+ )+ .is_err()+ {+ device_index += 1;+ continue;+ }+ }+ let address = u32::from_le_bytes(addr_buffer);++ // https://learn.microsoft.com/en-us/windows-hardware/drivers/kernel/obtaining-device-configuration-information-at-irql---dispatch-level+ let device = (address >> 16) & 0x0000FFFF;+ let function = address & 0x0000FFFF;++ // domain:bus:device.function+ return format!("{:04x}:{:02x}:{:02x}.{:x}", 0, bus_number, device, function);+ }++ String::new()+}diff --git a/vendor/wgpu-hal/src/dx12/command.rs b/vendor/wgpu-hal/src/dx12/command.rsnew file mode 100644index 00000000..9e90e266--- /dev/null+++ b/vendor/wgpu-hal/src/dx12/command.rs@@ -0,0 +1,1868 @@+use alloc::vec::Vec;+use core::{mem, ops::Range};++use windows::{+ core::Interface as _,+ Win32::{+ Foundation,+ Graphics::{Direct3D12, Dxgi},+ },+};++use super::conv;+use crate::{+ auxil::{+ self,+ dxgi::{name::ObjectExt as _, result::HResult as _},+ },+ dx12::borrow_interface_temporarily,+ AccelerationStructureEntries, CommandEncoder as _,+};++fn make_box(origin: &wgt::Origin3d, size: &crate::CopyExtent) -> Direct3D12::D3D12_BOX {+ Direct3D12::D3D12_BOX {+ left: origin.x,+ top: origin.y,+ right: origin.x + size.width,+ bottom: origin.y + size.height,+ front: origin.z,+ back: origin.z + size.depth,+ }+}++impl crate::BufferTextureCopy {+ fn to_subresource_footprint(+ &self,+ format: wgt::TextureFormat,+ ) -> Direct3D12::D3D12_PLACED_SUBRESOURCE_FOOTPRINT {+ let (block_width, _) = format.block_dimensions();+ Direct3D12::D3D12_PLACED_SUBRESOURCE_FOOTPRINT {+ Offset: self.buffer_layout.offset,+ Footprint: Direct3D12::D3D12_SUBRESOURCE_FOOTPRINT {+ Format: auxil::dxgi::conv::map_texture_format_for_copy(+ format,+ self.texture_base.aspect,+ )+ .unwrap(),+ Width: self.size.width,+ Height: self.size.height,+ Depth: self.size.depth,+ RowPitch: {+ let actual = self.buffer_layout.bytes_per_row.unwrap_or_else(|| {+ // this may happen for single-line updates+ let block_size = format+ .block_copy_size(Some(self.texture_base.aspect.map()))+ .unwrap();+ (self.size.width / block_width) * block_size+ });+ wgt::math::align_to(actual, Direct3D12::D3D12_TEXTURE_DATA_PITCH_ALIGNMENT)+ },+ },+ }+ }+}++impl super::Temp {+ fn prepare_marker(&mut self, marker: &str) -> (&[u16], u32) {+ self.marker.clear();+ self.marker.extend(marker.encode_utf16());+ self.marker.push(0);+ (&self.marker, self.marker.len() as u32 * 2)+ }+}++impl Drop for super::CommandEncoder {+ fn drop(&mut self) {+ use crate::CommandEncoder;+ unsafe { self.discard_encoding() }++ let mut rtv_pool = self.rtv_pool.lock();+ for handle in self.temp_rtv_handles.drain(..) {+ rtv_pool.free_handle(handle);+ }+ drop(rtv_pool);++ self.counters.command_encoders.sub(1);+ }+}++impl super::CommandEncoder {+ unsafe fn begin_pass(&mut self, kind: super::PassKind, label: crate::Label) {+ let list = self.list.as_ref().unwrap();+ self.pass.kind = kind;+ if let Some(label) = label {+ let (wide_label, size) = self.temp.prepare_marker(label);+ unsafe { list.BeginEvent(0, Some(wide_label.as_ptr().cast()), size) };+ self.pass.has_label = true;+ }+ self.pass.dirty_root_elements = 0;+ self.pass.dirty_vertex_buffers = 0;+ unsafe {+ list.SetDescriptorHeaps(&[+ Some(self.shared.heap_views.raw.clone()),+ Some(self.shared.sampler_heap.heap().clone()),+ ])+ };+ }++ unsafe fn end_pass(&mut self) {+ let list = self.list.as_ref().unwrap();+ unsafe { list.SetDescriptorHeaps(&[]) };+ if self.pass.has_label {+ unsafe { list.EndEvent() };+ }+ self.pass.clear();+ }++ unsafe fn prepare_vertex_buffers(&mut self) {+ while self.pass.dirty_vertex_buffers != 0 {+ let list = self.list.as_ref().unwrap();+ let index = self.pass.dirty_vertex_buffers.trailing_zeros();+ self.pass.dirty_vertex_buffers ^= 1 << index;+ unsafe {+ list.IASetVertexBuffers(+ index,+ Some(&self.pass.vertex_buffers[index as usize..][..1]),+ );+ }+ }+ }++ unsafe fn prepare_draw(&mut self, first_vertex: i32, first_instance: u32) {+ unsafe {+ self.prepare_vertex_buffers();+ }+ if let Some(root_index) = self+ .pass+ .layout+ .special_constants+ .as_ref()+ .map(|sc| sc.root_index)+ {+ let special_constants = super::SpecialConstants::from_indirect_draw_call_params(+ first_vertex,+ first_instance,+ );+ let needs_update = match self.pass.root_elements[root_index as usize] {+ super::RootElement::SpecialConstants(old_special_constants) => {+ old_special_constants != special_constants+ }+ _ => true,+ };+ if needs_update {+ self.pass.dirty_root_elements |= 1 << root_index;+ self.pass.root_elements[root_index as usize] =+ super::RootElement::SpecialConstants(special_constants);+ }+ }+ self.update_root_elements();+ }++ fn prepare_dispatch(&mut self, workgroup_count: [u32; 3]) {+ if let Some(root_index) = self+ .pass+ .layout+ .special_constants+ .as_ref()+ .map(|sc| sc.root_index)+ {+ let special_constants =+ super::SpecialConstants::from_compute_dispatch_params(workgroup_count);+ let needs_update = match self.pass.root_elements[root_index as usize] {+ super::RootElement::SpecialConstants(old_special_constants) => {+ old_special_constants != special_constants+ }+ _ => true,+ };+ if needs_update {+ self.pass.dirty_root_elements |= 1 << root_index;+ self.pass.root_elements[root_index as usize] =+ super::RootElement::SpecialConstants(special_constants);+ }+ }+ self.update_root_elements();+ }++ // Note: we have to call this lazily before draw calls. Otherwise, D3D complains+ // about the root parameters being incompatible with root signature.+ fn update_root_elements(&mut self) {+ use super::PassKind as Pk;++ while self.pass.dirty_root_elements != 0 {+ let list = self.list.as_ref().unwrap();+ let index = self.pass.dirty_root_elements.trailing_zeros();+ self.pass.dirty_root_elements ^= 1 << index;++ match self.pass.root_elements[index as usize] {+ super::RootElement::Empty => unreachable!(+ "Empty root element at index {index} should not have been marked as dirty"+ ),+ super::RootElement::Immediates => {+ let info = self.pass.layout.immediates_info.as_ref().unwrap();++ for offset in 0..info.size {+ let val = self.pass.immediates[offset as usize];+ match self.pass.kind {+ Pk::Render => unsafe {+ list.SetGraphicsRoot32BitConstant(index, val, offset)+ },+ Pk::Compute => unsafe {+ list.SetComputeRoot32BitConstant(index, val, offset)+ },+ Pk::Transfer => (),+ }+ }+ }+ super::RootElement::SpecialConstants(super::SpecialConstants {+ first_vertex_or_x,+ first_instance_or_y,+ unused_or_z,+ }) => match self.pass.kind {+ Pk::Render => {+ unsafe {+ list.SetGraphicsRoot32BitConstant(index, first_vertex_or_x as u32, 0)+ };+ unsafe { list.SetGraphicsRoot32BitConstant(index, first_instance_or_y, 1) };+ }+ Pk::Compute => {+ unsafe {+ list.SetComputeRoot32BitConstant(index, first_vertex_or_x as u32, 0)+ };+ unsafe { list.SetComputeRoot32BitConstant(index, first_instance_or_y, 1) };+ unsafe { list.SetComputeRoot32BitConstant(index, unused_or_z, 2) };+ }+ Pk::Transfer => (),+ },+ super::RootElement::DescriptorTable(descriptor) => match self.pass.kind {+ Pk::Render => unsafe { list.SetGraphicsRootDescriptorTable(index, descriptor) },+ Pk::Compute => unsafe { list.SetComputeRootDescriptorTable(index, descriptor) },+ Pk::Transfer => (),+ },+ super::RootElement::DynamicUniformBuffer { address } => {+ let address = address.ptr;+ match self.pass.kind {+ Pk::Render => unsafe {+ list.SetGraphicsRootConstantBufferView(index, address)+ },+ Pk::Compute => unsafe {+ list.SetComputeRootConstantBufferView(index, address)+ },+ Pk::Transfer => (),+ }+ }+ super::RootElement::DynamicStorageBufferOffsets { start, end } => {+ let values = &self.pass.dynamic_storage_buffer_offsets[start..end];++ for (offset, &value) in values.iter().enumerate() {+ match self.pass.kind {+ Pk::Render => unsafe {+ list.SetGraphicsRoot32BitConstant(index, value, offset as u32)+ },+ Pk::Compute => unsafe {+ list.SetComputeRoot32BitConstant(index, value, offset as u32)+ },+ Pk::Transfer => (),+ }+ }+ }+ super::RootElement::SamplerHeapDescriptorTable => match self.pass.kind {+ Pk::Render => unsafe {+ list.SetGraphicsRootDescriptorTable(+ index,+ self.shared.sampler_heap.gpu_descriptor_table(),+ )+ },+ Pk::Compute => unsafe {+ list.SetComputeRootDescriptorTable(+ index,+ self.shared.sampler_heap.gpu_descriptor_table(),+ )+ },+ Pk::Transfer => (),+ },+ }+ }+ }++ fn reset_signature(&mut self, layout: &super::PipelineLayoutShared) {+ if let Some(root_index) = layout.special_constants.as_ref().map(|sc| sc.root_index) {+ self.pass.root_elements[root_index as usize] =+ super::RootElement::SpecialConstants(super::SpecialConstants::default());+ }+ if let Some(root_index) = layout.sampler_heap_root_index {+ self.pass.root_elements[root_index as usize] =+ super::RootElement::SamplerHeapDescriptorTable;+ }+ self.pass.layout = layout.clone();+ self.pass.dirty_root_elements = (1 << layout.total_root_elements) - 1;+ }++ fn write_pass_end_timestamp_if_requested(&mut self) {+ if let Some((query_set_raw, index)) = self.end_of_pass_timer_query.take() {+ use crate::CommandEncoder as _;+ unsafe {+ self.write_timestamp(+ &crate::dx12::QuerySet {+ raw: query_set_raw,+ raw_ty: Direct3D12::D3D12_QUERY_TYPE_TIMESTAMP,+ },+ index,+ );+ }+ }+ }++ unsafe fn buf_tex_intermediate<T>(+ &mut self,+ region: crate::BufferTextureCopy,+ tex_fmt: wgt::TextureFormat,+ copy_op: impl FnOnce(&mut Self, &super::Buffer, wgt::BufferSize, crate::BufferTextureCopy) -> T,+ ) -> (T, super::Buffer) {+ let size = {+ let copy_info = region.buffer_layout.get_buffer_texture_copy_info(+ tex_fmt,+ region.texture_base.aspect.map(),+ ®ion.size.into(),+ );+ copy_info.unwrap().bytes_in_copy+ };++ let size = wgt::BufferSize::new(size).unwrap();++ let buffer = {+ let (resource, allocation) =+ super::suballocation::DeviceAllocationContext::from(&*self)+ .create_buffer(&crate::BufferDescriptor {+ label: None,+ size: size.get(),+ usage: wgt::BufferUses::COPY_SRC | wgt::BufferUses::COPY_DST,+ memory_flags: crate::MemoryFlags::empty(),+ })+ .expect(concat!(+ "internal error: ",+ "failed to allocate intermediate buffer ",+ "for offset alignment"+ ));+ super::Buffer {+ resource,+ size: size.get(),+ allocation,+ }+ };++ let mut region = region;+ region.buffer_layout.offset = 0;++ unsafe {+ self.transition_buffers(+ [crate::BufferBarrier {+ buffer: &buffer,+ usage: crate::StateTransition {+ from: wgt::BufferUses::empty(),+ to: wgt::BufferUses::COPY_DST,+ },+ }]+ .into_iter(),+ )+ };++ let t = copy_op(self, &buffer, size, region);++ unsafe {+ self.transition_buffers(+ [crate::BufferBarrier {+ buffer: &buffer,+ usage: crate::StateTransition {+ from: wgt::BufferUses::COPY_DST,+ to: wgt::BufferUses::COPY_SRC,+ },+ }]+ .into_iter(),+ )+ };++ (t, buffer)+ }+}++impl crate::CommandEncoder for super::CommandEncoder {+ type A = super::Api;++ unsafe fn begin_encoding(&mut self, label: crate::Label) -> Result<(), crate::DeviceError> {+ let list = loop {+ if let Some(list) = self.free_lists.pop() {+ // TODO: Is an error expected here and should we print it?+ let reset_result = unsafe { list.Reset(&self.allocator, None) };+ if reset_result.is_ok() {+ break Some(list);+ }+ } else {+ break None;+ }+ };++ let list = if let Some(list) = list {+ list+ } else {+ unsafe {+ self.device.CreateCommandList(+ 0,+ Direct3D12::D3D12_COMMAND_LIST_TYPE_DIRECT,+ &self.allocator,+ None,+ )+ }+ .into_device_result("Create command list")?+ };++ if let Some(label) = label {+ list.set_name(label)?;+ }++ // Ensure clean state even if the last encoding did not complete normally.+ self.temp.clear();+ self.pass.clear();+ self.end_of_pass_timer_query = None;++ self.list = Some(list);+ Ok(())+ }+ unsafe fn discard_encoding(&mut self) {+ if let Some(list) = self.list.take() {+ if unsafe { list.Close() }.is_ok() {+ self.free_lists.push(list);+ }+ }+ }+ unsafe fn end_encoding(&mut self) -> Result<super::CommandBuffer, crate::DeviceError> {+ let raw = self.list.take().unwrap();+ unsafe { raw.Close() }.into_device_result("GraphicsCommandList::close")?;+ Ok(super::CommandBuffer { raw })+ }+ unsafe fn reset_all<I: Iterator<Item = super::CommandBuffer>>(&mut self, command_buffers: I) {+ self.intermediate_copy_bufs.clear();+ for cmd_buf in command_buffers {+ self.free_lists.push(cmd_buf.raw);+ }+ if let Err(e) = unsafe { self.allocator.Reset() } {+ log::error!("ID3D12CommandAllocator::Reset() failed with {e}");+ }+ }++ unsafe fn transition_buffers<'a, T>(&mut self, barriers: T)+ where+ T: Iterator<Item = crate::BufferBarrier<'a, super::Buffer>>,+ {+ self.temp.barriers.clear();++ for barrier in barriers {+ let s0 = conv::map_buffer_usage_to_state(barrier.usage.from);+ let s1 = conv::map_buffer_usage_to_state(barrier.usage.to);+ if s0 != s1 {+ let raw = Direct3D12::D3D12_RESOURCE_BARRIER {+ Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,+ Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE,+ Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 {+ Transition: mem::ManuallyDrop::new(+ Direct3D12::D3D12_RESOURCE_TRANSITION_BARRIER {+ pResource: unsafe {+ borrow_interface_temporarily(&barrier.buffer.resource)+ },+ Subresource: Direct3D12::D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,+ StateBefore: s0,+ StateAfter: s1,+ },+ ),+ },+ };+ self.temp.barriers.push(raw);+ } else if barrier.usage.from == wgt::BufferUses::STORAGE_READ_WRITE+ || barrier.usage.from == wgt::BufferUses::ACCELERATION_STRUCTURE_QUERY+ {+ let raw = Direct3D12::D3D12_RESOURCE_BARRIER {+ Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_UAV,+ Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE,+ Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 {+ UAV: mem::ManuallyDrop::new(Direct3D12::D3D12_RESOURCE_UAV_BARRIER {+ pResource: unsafe {+ borrow_interface_temporarily(&barrier.buffer.resource)+ },+ }),+ },+ };+ self.temp.barriers.push(raw);+ }+ }++ if !self.temp.barriers.is_empty() {+ unsafe {+ self.list+ .as_ref()+ .unwrap()+ .ResourceBarrier(&self.temp.barriers)+ };+ }+ }++ unsafe fn transition_textures<'a, T>(&mut self, barriers: T)+ where+ T: Iterator<Item = crate::TextureBarrier<'a, super::Texture>>,+ {+ self.temp.barriers.clear();++ for barrier in barriers {+ let s0 = conv::map_texture_usage_to_state(barrier.usage.from);+ let s1 = conv::map_texture_usage_to_state(barrier.usage.to);+ if s0 != s1 {+ let mut raw = Direct3D12::D3D12_RESOURCE_BARRIER {+ Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,+ Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE,+ Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 {+ Transition: mem::ManuallyDrop::new(+ Direct3D12::D3D12_RESOURCE_TRANSITION_BARRIER {+ pResource: unsafe {+ borrow_interface_temporarily(&barrier.texture.resource)+ },+ Subresource: Direct3D12::D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,+ StateBefore: s0,+ StateAfter: s1,+ },+ ),+ },+ };++ let tex_mip_level_count = barrier.texture.mip_level_count;+ let tex_array_layer_count = barrier.texture.array_layer_count();++ if barrier.range.is_full_resource(+ barrier.texture.format,+ tex_mip_level_count,+ tex_array_layer_count,+ ) {+ // Only one barrier if it affects the whole image.+ self.temp.barriers.push(raw);+ } else {+ // Selected texture aspect is relevant if the texture format has both depth _and_ stencil aspects.+ let planes = if barrier.texture.format.is_combined_depth_stencil_format() {+ match barrier.range.aspect {+ wgt::TextureAspect::All => 0..2,+ wgt::TextureAspect::DepthOnly => 0..1,+ wgt::TextureAspect::StencilOnly => 1..2,+ _ => unreachable!(),+ }+ } else if let Some(planes) = barrier.texture.format.planes() {+ match barrier.range.aspect {+ wgt::TextureAspect::All => 0..planes,+ wgt::TextureAspect::Plane0 => 0..1,+ wgt::TextureAspect::Plane1 => 1..2,+ wgt::TextureAspect::Plane2 => 2..3,+ _ => unreachable!(),+ }+ } else {+ match barrier.texture.format {+ wgt::TextureFormat::Stencil8 => 1..2,+ wgt::TextureFormat::Depth24Plus => 0..2, // TODO: investigate why tests fail if we set this to 0..1+ _ => 0..1,+ }+ };++ for mip_level in barrier.range.mip_range(tex_mip_level_count) {+ for array_layer in barrier.range.layer_range(tex_array_layer_count) {+ for plane in planes.clone() {+ unsafe { &mut *raw.Anonymous.Transition }.Subresource = barrier+ .texture+ .calc_subresource(mip_level, array_layer, plane);+ self.temp.barriers.push(raw.clone());+ }+ }+ }+ }+ } else if barrier.usage.from == wgt::TextureUses::STORAGE_READ_WRITE {+ let raw = Direct3D12::D3D12_RESOURCE_BARRIER {+ Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_UAV,+ Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE,+ Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 {+ UAV: mem::ManuallyDrop::new(Direct3D12::D3D12_RESOURCE_UAV_BARRIER {+ pResource: unsafe {+ borrow_interface_temporarily(&barrier.texture.resource)+ },+ }),+ },+ };+ self.temp.barriers.push(raw);+ }+ }++ if !self.temp.barriers.is_empty() {+ unsafe {+ self.list+ .as_ref()+ .unwrap()+ .ResourceBarrier(&self.temp.barriers)+ };+ }+ }++ unsafe fn clear_buffer(&mut self, buffer: &super::Buffer, range: crate::MemoryRange) {+ let list = self.list.as_ref().unwrap();+ let mut offset = range.start;+ while offset < range.end {+ let size = super::ZERO_BUFFER_SIZE.min(range.end - offset);+ unsafe {+ list.CopyBufferRegion(&buffer.resource, offset, &self.shared.zero_buffer, 0, size)+ };+ offset += size;+ }+ }++ unsafe fn copy_buffer_to_buffer<T>(+ &mut self,+ src: &super::Buffer,+ dst: &super::Buffer,+ regions: T,+ ) where+ T: Iterator<Item = crate::BufferCopy>,+ {+ let list = self.list.as_ref().unwrap();+ for r in regions {+ unsafe {+ list.CopyBufferRegion(+ &dst.resource,+ r.dst_offset,+ &src.resource,+ r.src_offset,+ r.size.get(),+ )+ };+ }+ }++ unsafe fn copy_texture_to_texture<T>(+ &mut self,+ src: &super::Texture,+ _src_usage: wgt::TextureUses,+ dst: &super::Texture,+ regions: T,+ ) where+ T: Iterator<Item = crate::TextureCopy>,+ {+ let list = self.list.as_ref().unwrap();++ for r in regions {+ let src_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION {+ pResource: unsafe { borrow_interface_temporarily(&src.resource) },+ Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,+ Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 {+ SubresourceIndex: src.calc_subresource_for_copy(&r.src_base),+ },+ };+ let dst_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION {+ pResource: unsafe { borrow_interface_temporarily(&dst.resource) },+ Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,+ Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 {+ SubresourceIndex: dst.calc_subresource_for_copy(&r.dst_base),+ },+ };++ let src_box = make_box(&r.src_base.origin, &r.size);++ unsafe {+ list.CopyTextureRegion(+ &dst_location,+ r.dst_base.origin.x,+ r.dst_base.origin.y,+ r.dst_base.origin.z,+ &src_location,+ Some(&src_box),+ )+ };+ }+ }++ unsafe fn copy_buffer_to_texture<T>(+ &mut self,+ src: &super::Buffer,+ dst: &super::Texture,+ regions: T,+ ) where+ T: Iterator<Item = crate::BufferTextureCopy>,+ {+ let offset_alignment = self.shared.private_caps.texture_data_placement_alignment();++ for naive_copy_region in regions {+ let is_offset_aligned = naive_copy_region.buffer_layout.offset % offset_alignment == 0;+ let (final_copy_region, src) = if is_offset_aligned {+ (naive_copy_region, src)+ } else {+ let (intermediate_to_dst_region, intermediate_buf) = unsafe {+ let src_offset = naive_copy_region.buffer_layout.offset;+ self.buf_tex_intermediate(+ naive_copy_region,+ dst.format,+ |this, buf, size, intermediate_to_dst_region| {+ let layout = crate::BufferCopy {+ src_offset,+ dst_offset: 0,+ size,+ };+ this.copy_buffer_to_buffer(src, buf, [layout].into_iter());+ intermediate_to_dst_region+ },+ )+ };+ self.intermediate_copy_bufs.push(intermediate_buf);+ let intermediate_buf = self.intermediate_copy_bufs.last().unwrap();+ (intermediate_to_dst_region, intermediate_buf)+ };++ let list = self.list.as_ref().unwrap();++ let src_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION {+ pResource: unsafe { borrow_interface_temporarily(&src.resource) },+ Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT,+ Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 {+ PlacedFootprint: final_copy_region.to_subresource_footprint(dst.format),+ },+ };+ let dst_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION {+ pResource: unsafe { borrow_interface_temporarily(&dst.resource) },+ Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,+ Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 {+ SubresourceIndex: dst+ .calc_subresource_for_copy(&final_copy_region.texture_base),+ },+ };++ let src_box = make_box(&wgt::Origin3d::ZERO, &final_copy_region.size);+ unsafe {+ list.CopyTextureRegion(+ &dst_location,+ final_copy_region.texture_base.origin.x,+ final_copy_region.texture_base.origin.y,+ final_copy_region.texture_base.origin.z,+ &src_location,+ Some(&src_box),+ )+ };+ }+ }++ unsafe fn copy_texture_to_buffer<T>(+ &mut self,+ src: &super::Texture,+ _src_usage: wgt::TextureUses,+ dst: &super::Buffer,+ regions: T,+ ) where+ T: Iterator<Item = crate::BufferTextureCopy>,+ {+ let copy_aligned = |this: &mut Self,+ src: &super::Texture,+ dst: &super::Buffer,+ r: crate::BufferTextureCopy| {+ let list = this.list.as_ref().unwrap();++ let src_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION {+ pResource: unsafe { borrow_interface_temporarily(&src.resource) },+ Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,+ Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 {+ SubresourceIndex: src.calc_subresource_for_copy(&r.texture_base),+ },+ };+ let dst_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION {+ pResource: unsafe { borrow_interface_temporarily(&dst.resource) },+ Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT,+ Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 {+ PlacedFootprint: r.to_subresource_footprint(src.format),+ },+ };++ let src_box = make_box(&r.texture_base.origin, &r.size);+ unsafe {+ list.CopyTextureRegion(&dst_location, 0, 0, 0, &src_location, Some(&src_box))+ };+ };++ let offset_alignment = self.shared.private_caps.texture_data_placement_alignment();++ for r in regions {+ let is_offset_aligned = r.buffer_layout.offset % offset_alignment == 0;+ if is_offset_aligned {+ copy_aligned(self, src, dst, r)+ } else {+ let orig_offset = r.buffer_layout.offset;+ let (intermediate_to_dst_region, src) = unsafe {+ self.buf_tex_intermediate(+ r,+ src.format,+ |this, buf, size, intermediate_region| {+ copy_aligned(this, src, buf, intermediate_region);+ crate::BufferCopy {+ src_offset: 0,+ dst_offset: orig_offset,+ size,+ }+ },+ )+ };++ unsafe {+ self.copy_buffer_to_buffer(&src, dst, [intermediate_to_dst_region].into_iter());+ }++ self.intermediate_copy_bufs.push(src);+ };+ }+ }++ unsafe fn begin_query(&mut self, set: &super::QuerySet, index: u32) {+ unsafe {+ self.list+ .as_ref()+ .unwrap()+ .BeginQuery(&set.raw, set.raw_ty, index)+ };+ }+ unsafe fn end_query(&mut self, set: &super::QuerySet, index: u32) {+ unsafe {+ self.list+ .as_ref()+ .unwrap()+ .EndQuery(&set.raw, set.raw_ty, index)+ };+ }+ unsafe fn write_timestamp(&mut self, set: &super::QuerySet, index: u32) {+ unsafe {+ self.list.as_ref().unwrap().EndQuery(+ &set.raw,+ Direct3D12::D3D12_QUERY_TYPE_TIMESTAMP,+ index,+ )+ };+ }+ unsafe fn read_acceleration_structure_compact_size(+ &mut self,+ acceleration_structure: &super::AccelerationStructure,+ buf: &super::Buffer,+ ) {+ let list = self+ .list+ .as_ref()+ .unwrap()+ .cast::<Direct3D12::ID3D12GraphicsCommandList4>()+ .unwrap();+ unsafe {+ list.EmitRaytracingAccelerationStructurePostbuildInfo(+ &Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_DESC {+ DestBuffer: buf.resource.GetGPUVirtualAddress(),+ InfoType: Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_COMPACTED_SIZE,+ },+ &[+ acceleration_structure.resource.GetGPUVirtualAddress()+ ],+ )+ }+ }+ unsafe fn reset_queries(&mut self, _set: &super::QuerySet, _range: Range<u32>) {+ // nothing to do here+ }+ unsafe fn copy_query_results(+ &mut self,+ set: &super::QuerySet,+ range: Range<u32>,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ _stride: wgt::BufferSize,+ ) {+ unsafe {+ self.list.as_ref().unwrap().ResolveQueryData(+ &set.raw,+ set.raw_ty,+ range.start,+ range.end - range.start,+ &buffer.resource,+ offset,+ )+ };+ }++ // render++ unsafe fn begin_render_pass(+ &mut self,+ desc: &crate::RenderPassDescriptor<super::QuerySet, super::TextureView>,+ ) -> Result<(), crate::DeviceError> {+ unsafe { self.begin_pass(super::PassKind::Render, desc.label) };++ // Start timestamp if any (before all other commands but after debug marker)+ if let Some(timestamp_writes) = desc.timestamp_writes.as_ref() {+ if let Some(index) = timestamp_writes.beginning_of_pass_write_index {+ unsafe {+ self.write_timestamp(timestamp_writes.query_set, index);+ }+ }+ self.end_of_pass_timer_query = timestamp_writes+ .end_of_pass_write_index+ .map(|index| (timestamp_writes.query_set.raw.clone(), index));+ }++ let mut color_views =+ [Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE { ptr: 0 }; crate::MAX_COLOR_ATTACHMENTS];+ let mut rtv_pool = self.rtv_pool.lock();+ for (rtv, cat) in color_views.iter_mut().zip(desc.color_attachments.iter()) {+ if let Some(cat) = cat.as_ref() {+ if cat.target.view.dimension == wgt::TextureViewDimension::D3 {+ let desc = Direct3D12::D3D12_RENDER_TARGET_VIEW_DESC {+ Format: cat.target.view.raw_format,+ ViewDimension: Direct3D12::D3D12_RTV_DIMENSION_TEXTURE3D,+ Anonymous: Direct3D12::D3D12_RENDER_TARGET_VIEW_DESC_0 {+ Texture3D: Direct3D12::D3D12_TEX3D_RTV {+ MipSlice: cat.target.view.mip_slice,+ FirstWSlice: cat.depth_slice.unwrap(),+ WSize: 1,+ },+ },+ };+ let handle = rtv_pool.alloc_handle()?;+ unsafe {+ self.device.CreateRenderTargetView(+ &cat.target.view.texture,+ Some(&desc),+ handle.raw,+ )+ };+ *rtv = handle.raw;+ self.temp_rtv_handles.push(handle);+ } else {+ *rtv = cat.target.view.handle_rtv.unwrap().raw;+ }+ } else {+ *rtv = self.null_rtv_handle.raw;+ }+ }+ drop(rtv_pool);++ let ds_view = desc.depth_stencil_attachment.as_ref().map(|ds| {+ if ds.target.usage == wgt::TextureUses::DEPTH_STENCIL_WRITE {+ ds.target.view.handle_dsv_rw.as_ref().unwrap().raw+ } else {+ ds.target.view.handle_dsv_ro.as_ref().unwrap().raw+ }+ });++ let list = self.list.as_ref().unwrap();+ unsafe {+ list.OMSetRenderTargets(+ desc.color_attachments.len() as u32,+ Some(color_views.as_ptr()),+ false,+ ds_view.as_ref().map(core::ptr::from_ref),+ )+ };++ self.pass.resolves.clear();+ for (rtv, cat) in color_views.iter().zip(desc.color_attachments.iter()) {+ if let Some(cat) = cat.as_ref() {+ if cat.ops.contains(crate::AttachmentOps::LOAD_CLEAR) {+ let value = [+ cat.clear_value.r as f32,+ cat.clear_value.g as f32,+ cat.clear_value.b as f32,+ cat.clear_value.a as f32,+ ];+ unsafe { list.ClearRenderTargetView(*rtv, &value, None) };+ }+ if let Some(ref target) = cat.resolve_target {+ self.pass.resolves.push(super::PassResolve {+ src: (+ cat.target.view.texture.clone(),+ cat.target.view.subresource_index,+ ),+ dst: (target.view.texture.clone(), target.view.subresource_index),+ format: target.view.raw_format,+ });+ }+ }+ }++ if let Some(ref ds) = desc.depth_stencil_attachment {+ let mut flags = Direct3D12::D3D12_CLEAR_FLAGS::default();+ let aspects = ds.target.view.aspects;+ if ds.depth_ops.contains(crate::AttachmentOps::LOAD_CLEAR)+ && aspects.contains(crate::FormatAspects::DEPTH)+ {+ flags |= Direct3D12::D3D12_CLEAR_FLAG_DEPTH;+ }+ if ds.stencil_ops.contains(crate::AttachmentOps::LOAD_CLEAR)+ && aspects.contains(crate::FormatAspects::STENCIL)+ {+ flags |= Direct3D12::D3D12_CLEAR_FLAG_STENCIL;+ }++ if let Some(ds_view) = ds_view {+ if flags != Direct3D12::D3D12_CLEAR_FLAGS::default() {+ unsafe {+ list.ClearDepthStencilView(+ ds_view,+ flags,+ ds.clear_value.0,+ ds.clear_value.1 as u8,+ None,+ )+ }+ }+ }+ }++ if let Some(multiview_mask) = desc.multiview_mask {+ unsafe {+ list.cast::<Direct3D12::ID3D12GraphicsCommandList2>()+ .unwrap()+ .SetViewInstanceMask(multiview_mask.get());+ }+ }++ let raw_vp = Direct3D12::D3D12_VIEWPORT {+ TopLeftX: 0.0,+ TopLeftY: 0.0,+ Width: desc.extent.width as f32,+ Height: desc.extent.height as f32,+ MinDepth: 0.0,+ MaxDepth: 1.0,+ };+ let raw_rect = Foundation::RECT {+ left: 0,+ top: 0,+ right: desc.extent.width as i32,+ bottom: desc.extent.height as i32,+ };+ unsafe { list.RSSetViewports(core::slice::from_ref(&raw_vp)) };+ unsafe { list.RSSetScissorRects(core::slice::from_ref(&raw_rect)) };++ Ok(())+ }++ unsafe fn end_render_pass(&mut self) {+ if !self.pass.resolves.is_empty() {+ let list = self.list.as_ref().unwrap();+ self.temp.barriers.clear();++ // All the targets are expected to be in `COLOR_TARGET` state,+ // but D3D12 has special source/destination states for the resolves.+ for resolve in self.pass.resolves.iter() {+ let barrier = Direct3D12::D3D12_RESOURCE_BARRIER {+ Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,+ Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE,+ Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 {+ // Note: this assumes `D3D12_RESOURCE_STATE_RENDER_TARGET`.+ // If it's not the case, we can include the `TextureUses` in `PassResolve`.+ Transition: mem::ManuallyDrop::new(+ Direct3D12::D3D12_RESOURCE_TRANSITION_BARRIER {+ pResource: unsafe { borrow_interface_temporarily(&resolve.src.0) },+ Subresource: resolve.src.1,+ StateBefore: Direct3D12::D3D12_RESOURCE_STATE_RENDER_TARGET,+ StateAfter: Direct3D12::D3D12_RESOURCE_STATE_RESOLVE_SOURCE,+ },+ ),+ },+ };+ self.temp.barriers.push(barrier);+ let barrier = Direct3D12::D3D12_RESOURCE_BARRIER {+ Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,+ Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE,+ Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 {+ // Note: this assumes `D3D12_RESOURCE_STATE_RENDER_TARGET`.+ // If it's not the case, we can include the `TextureUses` in `PassResolve`.+ Transition: mem::ManuallyDrop::new(+ Direct3D12::D3D12_RESOURCE_TRANSITION_BARRIER {+ pResource: unsafe { borrow_interface_temporarily(&resolve.dst.0) },+ Subresource: resolve.dst.1,+ StateBefore: Direct3D12::D3D12_RESOURCE_STATE_RENDER_TARGET,+ StateAfter: Direct3D12::D3D12_RESOURCE_STATE_RESOLVE_DEST,+ },+ ),+ },+ };+ self.temp.barriers.push(barrier);+ }++ if !self.temp.barriers.is_empty() {+ profiling::scope!("ID3D12GraphicsCommandList::ResourceBarrier");+ unsafe { list.ResourceBarrier(&self.temp.barriers) };+ }++ for resolve in self.pass.resolves.iter() {+ profiling::scope!("ID3D12GraphicsCommandList::ResolveSubresource");+ unsafe {+ list.ResolveSubresource(+ &resolve.dst.0,+ resolve.dst.1,+ &resolve.src.0,+ resolve.src.1,+ resolve.format,+ )+ };+ }++ // Flip all the barriers to reverse, back into `COLOR_TARGET`.+ for barrier in self.temp.barriers.iter_mut() {+ let transition = unsafe { &mut *barrier.Anonymous.Transition };+ mem::swap(&mut transition.StateBefore, &mut transition.StateAfter);+ }+ if !self.temp.barriers.is_empty() {+ profiling::scope!("ID3D12GraphicsCommandList::ResourceBarrier");+ unsafe { list.ResourceBarrier(&self.temp.barriers) };+ }+ }++ self.write_pass_end_timestamp_if_requested();++ unsafe { self.end_pass() };+ }++ unsafe fn set_bind_group(+ &mut self,+ layout: &super::PipelineLayout,+ index: u32,+ group: &super::BindGroup,+ dynamic_offsets: &[wgt::DynamicOffset],+ ) {+ let info = layout.bind_group_infos[index as usize].as_ref().unwrap();+ let mut root_index = info.base_root_index as usize;++ // Bind CBV/SRC/UAV descriptor tables+ if info.tables.contains(super::TableTypes::SRV_CBV_UAV) {+ self.pass.root_elements[root_index] =+ super::RootElement::DescriptorTable(group.handle_views.unwrap().gpu);+ root_index += 1;+ }++ let mut offsets_index = 0;+ if let Some(dynamic_storage_buffer_offsets) = info.dynamic_storage_buffer_offsets.as_ref() {+ let root_index = dynamic_storage_buffer_offsets.root_index;+ let range = &dynamic_storage_buffer_offsets.range;++ if range.end > self.pass.dynamic_storage_buffer_offsets.len() {+ self.pass+ .dynamic_storage_buffer_offsets+ .resize(range.end, 0);+ }++ offsets_index += range.start;++ self.pass.root_elements[root_index as usize] =+ super::RootElement::DynamicStorageBufferOffsets {+ start: range.start,+ end: range.end,+ };++ if self.pass.layout.signature == layout.shared.signature {+ self.pass.dirty_root_elements |= 1 << root_index;+ } else {+ // D3D12 requires full reset on signature change+ // but we don't reset it here since it will be reset below+ };+ }++ // Bind root descriptors for dynamic uniform buffers+ // or set root constants for offsets of dynamic storage buffers+ for (&dynamic_buffer, &offset) in group.dynamic_buffers.iter().zip(dynamic_offsets) {+ match dynamic_buffer {+ super::DynamicBuffer::Uniform(gpu_base) => {+ self.pass.root_elements[root_index] =+ super::RootElement::DynamicUniformBuffer {+ address: Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE {+ ptr: gpu_base.ptr + offset as u64,+ },+ };+ root_index += 1;+ }+ super::DynamicBuffer::Storage => {+ self.pass.dynamic_storage_buffer_offsets[offsets_index] = offset;+ offsets_index += 1;+ }+ }+ }++ if self.pass.layout.signature == layout.shared.signature {+ self.pass.dirty_root_elements |= (1 << root_index) - (1 << info.base_root_index);+ } else {+ // D3D12 requires full reset on signature change+ self.reset_signature(&layout.shared);+ };+ }+ unsafe fn set_immediates(+ &mut self,+ layout: &super::PipelineLayout,+ offset_bytes: u32,+ data: &[u32],+ ) {+ let offset_words = offset_bytes as usize / 4;++ let info = layout.shared.immediates_info.as_ref().unwrap();++ self.pass.root_elements[info.root_index as usize] = super::RootElement::Immediates;++ self.pass.immediates[offset_words..(offset_words + data.len())].copy_from_slice(data);++ if self.pass.layout.signature == layout.shared.signature {+ self.pass.dirty_root_elements |= 1 << info.root_index;+ } else {+ // D3D12 requires full reset on signature change+ self.reset_signature(&layout.shared);+ };+ }++ unsafe fn insert_debug_marker(&mut self, label: &str) {+ let (wide_label, size) = self.temp.prepare_marker(label);+ unsafe {+ self.list+ .as_ref()+ .unwrap()+ .SetMarker(0, Some(wide_label.as_ptr().cast()), size)+ };+ }+ unsafe fn begin_debug_marker(&mut self, group_label: &str) {+ let (wide_label, size) = self.temp.prepare_marker(group_label);+ unsafe {+ self.list+ .as_ref()+ .unwrap()+ .BeginEvent(0, Some(wide_label.as_ptr().cast()), size)+ };+ }+ unsafe fn end_debug_marker(&mut self) {+ unsafe { self.list.as_ref().unwrap().EndEvent() }+ }++ unsafe fn set_render_pipeline(&mut self, pipeline: &super::RenderPipeline) {+ let list = self.list.clone().unwrap();++ if self.pass.layout.signature != pipeline.layout.signature {+ // D3D12 requires full reset on signature change+ unsafe { list.SetGraphicsRootSignature(pipeline.layout.signature.as_ref()) };+ self.reset_signature(&pipeline.layout);+ };++ unsafe { list.SetPipelineState(&pipeline.raw) };+ unsafe { list.IASetPrimitiveTopology(pipeline.topology) };++ for (index, (vb, &stride)) in self+ .pass+ .vertex_buffers+ .iter_mut()+ .zip(pipeline.vertex_strides.iter())+ .enumerate()+ {+ if let Some(stride) = stride {+ if vb.StrideInBytes != stride {+ vb.StrideInBytes = stride;+ self.pass.dirty_vertex_buffers |= 1 << index;+ }+ }+ }+ }++ unsafe fn set_index_buffer<'a>(+ &mut self,+ binding: crate::BufferBinding<'a, super::Buffer>,+ format: wgt::IndexFormat,+ ) {+ let ibv = Direct3D12::D3D12_INDEX_BUFFER_VIEW {+ BufferLocation: binding.resolve_address(),+ SizeInBytes: binding.resolve_size().try_into().unwrap(),+ Format: auxil::dxgi::conv::map_index_format(format),+ };++ unsafe { self.list.as_ref().unwrap().IASetIndexBuffer(Some(&ibv)) }+ }+ unsafe fn set_vertex_buffer<'a>(+ &mut self,+ index: u32,+ binding: crate::BufferBinding<'a, super::Buffer>,+ ) {+ let vb = &mut self.pass.vertex_buffers[index as usize];+ vb.BufferLocation = binding.resolve_address();+ vb.SizeInBytes = binding.resolve_size().try_into().unwrap();+ self.pass.dirty_vertex_buffers |= 1 << index;+ }++ unsafe fn set_viewport(&mut self, rect: &crate::Rect<f32>, depth_range: Range<f32>) {+ let raw_vp = Direct3D12::D3D12_VIEWPORT {+ TopLeftX: rect.x,+ TopLeftY: rect.y,+ Width: rect.w,+ Height: rect.h,+ MinDepth: depth_range.start,+ MaxDepth: depth_range.end,+ };+ unsafe {+ self.list+ .as_ref()+ .unwrap()+ .RSSetViewports(core::slice::from_ref(&raw_vp))+ }+ }+ unsafe fn set_scissor_rect(&mut self, rect: &crate::Rect<u32>) {+ let raw_rect = Foundation::RECT {+ left: rect.x as i32,+ top: rect.y as i32,+ right: (rect.x + rect.w) as i32,+ bottom: (rect.y + rect.h) as i32,+ };+ unsafe {+ self.list+ .as_ref()+ .unwrap()+ .RSSetScissorRects(core::slice::from_ref(&raw_rect))+ }+ }+ unsafe fn set_stencil_reference(&mut self, value: u32) {+ unsafe { self.list.as_ref().unwrap().OMSetStencilRef(value) }+ }+ unsafe fn set_blend_constants(&mut self, color: &[f32; 4]) {+ unsafe { self.list.as_ref().unwrap().OMSetBlendFactor(Some(color)) }+ }++ unsafe fn draw(+ &mut self,+ first_vertex: u32,+ vertex_count: u32,+ first_instance: u32,+ instance_count: u32,+ ) {+ unsafe { self.prepare_draw(first_vertex as i32, first_instance) };+ unsafe {+ self.list.as_ref().unwrap().DrawInstanced(+ vertex_count,+ instance_count,+ first_vertex,+ first_instance,+ )+ }+ }+ unsafe fn draw_indexed(+ &mut self,+ first_index: u32,+ index_count: u32,+ base_vertex: i32,+ first_instance: u32,+ instance_count: u32,+ ) {+ unsafe { self.prepare_draw(base_vertex, first_instance) };+ unsafe {+ self.list.as_ref().unwrap().DrawIndexedInstanced(+ index_count,+ instance_count,+ first_index,+ base_vertex,+ first_instance,+ )+ }+ }+ unsafe fn draw_mesh_tasks(+ &mut self,+ group_count_x: u32,+ group_count_y: u32,+ group_count_z: u32,+ ) {+ self.prepare_dispatch([group_count_x, group_count_y, group_count_z]);+ let cmd_list6: Direct3D12::ID3D12GraphicsCommandList6 =+ self.list.as_ref().unwrap().cast().unwrap();+ unsafe {+ cmd_list6.DispatchMesh(group_count_x, group_count_y, group_count_z);+ }+ }+ unsafe fn draw_indirect(+ &mut self,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ ) {+ if self+ .pass+ .layout+ .special_constants+ .as_ref()+ .and_then(|sc| sc.indirect_cmd_signatures.as_ref())+ .is_some()+ {+ unsafe { self.prepare_vertex_buffers() };+ self.update_root_elements();+ } else {+ unsafe { self.prepare_draw(0, 0) };+ }++ let cmd_signature = &self+ .pass+ .layout+ .special_constants+ .as_ref()+ .and_then(|sc| sc.indirect_cmd_signatures.as_ref())+ .unwrap_or_else(|| &self.shared.cmd_signatures)+ .draw;+ unsafe {+ self.list.as_ref().unwrap().ExecuteIndirect(+ cmd_signature,+ draw_count,+ &buffer.resource,+ offset,+ None,+ 0,+ )+ }+ }+ unsafe fn draw_indexed_indirect(+ &mut self,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ ) {+ if self+ .pass+ .layout+ .special_constants+ .as_ref()+ .and_then(|sc| sc.indirect_cmd_signatures.as_ref())+ .is_some()+ {+ unsafe { self.prepare_vertex_buffers() };+ self.update_root_elements();+ } else {+ unsafe { self.prepare_draw(0, 0) };+ }++ let cmd_signature = &self+ .pass+ .layout+ .special_constants+ .as_ref()+ .and_then(|sc| sc.indirect_cmd_signatures.as_ref())+ .unwrap_or_else(|| &self.shared.cmd_signatures)+ .draw_indexed;+ unsafe {+ self.list.as_ref().unwrap().ExecuteIndirect(+ cmd_signature,+ draw_count,+ &buffer.resource,+ offset,+ None,+ 0,+ )+ }+ }+ unsafe fn draw_mesh_tasks_indirect(+ &mut self,+ buffer: &<Self::A as crate::Api>::Buffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ ) {+ self.prepare_dispatch([0; 3]);+ let cmd_list6: Direct3D12::ID3D12GraphicsCommandList6 =+ self.list.as_ref().unwrap().cast().unwrap();+ let Some(cmd_signature) = &self.shared.cmd_signatures.draw_mesh else {+ panic!("Feature `MESH_SHADING` not enabled");+ };+ unsafe {+ cmd_list6.ExecuteIndirect(cmd_signature, draw_count, &buffer.resource, offset, None, 0);+ }+ }+ unsafe fn draw_indirect_count(+ &mut self,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ count_buffer: &super::Buffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ ) {+ unsafe { self.prepare_draw(0, 0) };+ unsafe {+ self.list.as_ref().unwrap().ExecuteIndirect(+ &self.shared.cmd_signatures.draw,+ max_count,+ &buffer.resource,+ offset,+ &count_buffer.resource,+ count_offset,+ )+ }+ }+ unsafe fn draw_indexed_indirect_count(+ &mut self,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ count_buffer: &super::Buffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ ) {+ unsafe { self.prepare_draw(0, 0) };+ unsafe {+ self.list.as_ref().unwrap().ExecuteIndirect(+ &self.shared.cmd_signatures.draw_indexed,+ max_count,+ &buffer.resource,+ offset,+ &count_buffer.resource,+ count_offset,+ )+ }+ }+ unsafe fn draw_mesh_tasks_indirect_count(+ &mut self,+ buffer: &<Self::A as crate::Api>::Buffer,+ offset: wgt::BufferAddress,+ count_buffer: &<Self::A as crate::Api>::Buffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ ) {+ self.prepare_dispatch([0; 3]);+ let cmd_list6: Direct3D12::ID3D12GraphicsCommandList6 =+ self.list.as_ref().unwrap().cast().unwrap();+ let Some(ref command_signature) = self.shared.cmd_signatures.draw_mesh else {+ panic!("Feature `MESH_SHADING` not enabled");+ };+ unsafe {+ cmd_list6.ExecuteIndirect(+ command_signature,+ max_count,+ &buffer.resource,+ offset,+ &count_buffer.resource,+ count_offset,+ );+ }+ }++ // compute++ unsafe fn begin_compute_pass<'a>(+ &mut self,+ desc: &crate::ComputePassDescriptor<'a, super::QuerySet>,+ ) {+ unsafe { self.begin_pass(super::PassKind::Compute, desc.label) };++ if let Some(timestamp_writes) = desc.timestamp_writes.as_ref() {+ if let Some(index) = timestamp_writes.beginning_of_pass_write_index {+ unsafe {+ self.write_timestamp(timestamp_writes.query_set, index);+ }+ }+ self.end_of_pass_timer_query = timestamp_writes+ .end_of_pass_write_index+ .map(|index| (timestamp_writes.query_set.raw.clone(), index));+ }+ }+ unsafe fn end_compute_pass(&mut self) {+ self.write_pass_end_timestamp_if_requested();+ unsafe { self.end_pass() };+ }++ unsafe fn set_compute_pipeline(&mut self, pipeline: &super::ComputePipeline) {+ let list = self.list.clone().unwrap();++ if self.pass.layout.signature != pipeline.layout.signature {+ // D3D12 requires full reset on signature change+ unsafe { list.SetComputeRootSignature(pipeline.layout.signature.as_ref()) };+ self.reset_signature(&pipeline.layout);+ };++ unsafe { list.SetPipelineState(&pipeline.raw) }+ }++ unsafe fn dispatch_workgroups(&mut self, count @ [x, y, z]: [u32; 3]) {+ self.prepare_dispatch(count);+ unsafe { self.list.as_ref().unwrap().Dispatch(x, y, z) }+ }++ unsafe fn dispatch_workgroups_indirect(+ &mut self,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ ) {+ if self+ .pass+ .layout+ .special_constants+ .as_ref()+ .and_then(|sc| sc.indirect_cmd_signatures.as_ref())+ .is_some()+ {+ self.update_root_elements();+ } else {+ self.prepare_dispatch([0; 3]);+ }++ let cmd_signature = &self+ .pass+ .layout+ .special_constants+ .as_ref()+ .and_then(|sc| sc.indirect_cmd_signatures.as_ref())+ .unwrap_or_else(|| &self.shared.cmd_signatures)+ .dispatch;+ unsafe {+ self.list.as_ref().unwrap().ExecuteIndirect(+ cmd_signature,+ 1,+ &buffer.resource,+ offset,+ None,+ 0,+ )+ }+ }++ unsafe fn build_acceleration_structures<'a, T>(+ &mut self,+ _descriptor_count: u32,+ descriptors: T,+ ) where+ super::Api: 'a,+ T: IntoIterator<+ Item = crate::BuildAccelerationStructureDescriptor<+ 'a,+ super::Buffer,+ super::AccelerationStructure,+ >,+ >,+ {+ // Implement using `BuildRaytracingAccelerationStructure`:+ // https://microsoft.github.io/DirectX-Specs/d3d/Raytracing.html#buildraytracingaccelerationstructure+ let list = self+ .list+ .as_ref()+ .unwrap()+ .cast::<Direct3D12::ID3D12GraphicsCommandList4>()+ .unwrap();+ for descriptor in descriptors {+ // TODO: This is the same as getting build sizes apart from requiring buffers, should this be de-duped?+ let mut geometry_desc;+ let ty;+ let inputs0;+ let num_desc;+ match descriptor.entries {+ AccelerationStructureEntries::Instances(instances) => {+ let desc_address = unsafe {+ instances+ .buffer+ .expect("needs buffer to build")+ .resource+ .GetGPUVirtualAddress()+ } + instances.offset as u64;+ ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL;+ inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 {+ InstanceDescs: desc_address,+ };+ num_desc = instances.count;+ }+ AccelerationStructureEntries::Triangles(triangles) => {+ geometry_desc = Vec::with_capacity(triangles.len());+ for triangle in triangles {+ let transform_address =+ triangle.transform.as_ref().map_or(0, |transform| unsafe {+ transform.buffer.resource.GetGPUVirtualAddress()+ + transform.offset as u64+ });+ let index_format = triangle+ .indices+ .as_ref()+ .map_or(Dxgi::Common::DXGI_FORMAT_UNKNOWN, |indices| {+ auxil::dxgi::conv::map_index_format(indices.format)+ });+ let vertex_format =+ auxil::dxgi::conv::map_vertex_format(triangle.vertex_format);+ let index_count =+ triangle.indices.as_ref().map_or(0, |indices| indices.count);+ let index_address = triangle.indices.as_ref().map_or(0, |indices| unsafe {+ indices+ .buffer+ .expect("needs buffer to build")+ .resource+ .GetGPUVirtualAddress()+ + indices.offset as u64+ });+ let vertex_address = unsafe {+ triangle+ .vertex_buffer+ .expect("needs buffer to build")+ .resource+ .GetGPUVirtualAddress()+ + (triangle.first_vertex as u64 * triangle.vertex_stride)+ };++ let triangle_desc = Direct3D12::D3D12_RAYTRACING_GEOMETRY_TRIANGLES_DESC {+ Transform3x4: transform_address,+ IndexFormat: index_format,+ VertexFormat: vertex_format,+ IndexCount: index_count,+ VertexCount: triangle.vertex_count,+ IndexBuffer: index_address,+ VertexBuffer: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE {+ StartAddress: vertex_address,+ StrideInBytes: triangle.vertex_stride,+ },+ };++ geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC {+ Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES,+ Flags: conv::map_acceleration_structure_geometry_flags(triangle.flags),+ Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 {+ Triangles: triangle_desc,+ },+ })+ }+ ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;+ inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 {+ pGeometryDescs: geometry_desc.as_ptr(),+ };+ num_desc = geometry_desc.len() as u32;+ }+ AccelerationStructureEntries::AABBs(aabbs) => {+ geometry_desc = Vec::with_capacity(aabbs.len());+ for aabb in aabbs {+ let aabb_address = unsafe {+ aabb.buffer+ .expect("needs buffer to build")+ .resource+ .GetGPUVirtualAddress()+ + (aabb.offset as u64 * aabb.stride)+ };++ let aabb_desc = Direct3D12::D3D12_RAYTRACING_GEOMETRY_AABBS_DESC {+ AABBCount: aabb.count as u64,+ AABBs: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE {+ StartAddress: aabb_address,+ StrideInBytes: aabb.stride,+ },+ };++ geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC {+ Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_PROCEDURAL_PRIMITIVE_AABBS,+ Flags: conv::map_acceleration_structure_geometry_flags(aabb.flags),+ Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 {+ AABBs: aabb_desc,+ },+ })+ }+ ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;+ inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 {+ pGeometryDescs: geometry_desc.as_ptr(),+ };+ num_desc = geometry_desc.len() as u32;+ }+ };+ let acceleration_structure_inputs =+ Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS {+ Type: ty,+ Flags: conv::map_acceleration_structure_build_flags(+ descriptor.flags,+ Some(descriptor.mode),+ ),+ NumDescs: num_desc,+ DescsLayout: Direct3D12::D3D12_ELEMENTS_LAYOUT_ARRAY,+ Anonymous: inputs0,+ };++ let dst_acceleration_structure_address = unsafe {+ descriptor+ .destination_acceleration_structure+ .resource+ .GetGPUVirtualAddress()+ };+ let src_acceleration_structure_address = descriptor+ .source_acceleration_structure+ .as_ref()+ .map_or(0, |source| unsafe {+ source.resource.GetGPUVirtualAddress()+ });+ let scratch_address = unsafe {+ descriptor.scratch_buffer.resource.GetGPUVirtualAddress()+ + descriptor.scratch_buffer_offset+ };++ let desc = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC {+ DestAccelerationStructureData: dst_acceleration_structure_address,+ Inputs: acceleration_structure_inputs,+ SourceAccelerationStructureData: src_acceleration_structure_address,+ ScratchAccelerationStructureData: scratch_address,+ };+ unsafe { list.BuildRaytracingAccelerationStructure(&desc, None) };+ }+ }++ unsafe fn place_acceleration_structure_barrier(+ &mut self,+ _barriers: crate::AccelerationStructureBarrier,+ ) {+ // TODO: This is not very optimal, we should be using [enhanced barriers](https://microsoft.github.io/DirectX-Specs/d3d/D3D12EnhancedBarriers.html) if possible+ let list = self+ .list+ .as_ref()+ .unwrap()+ .cast::<Direct3D12::ID3D12GraphicsCommandList4>()+ .unwrap();+ unsafe {+ list.ResourceBarrier(&[Direct3D12::D3D12_RESOURCE_BARRIER {+ Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_UAV,+ Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE,+ Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 {+ UAV: mem::ManuallyDrop::new(Direct3D12::D3D12_RESOURCE_UAV_BARRIER {+ pResource: Default::default(),+ }),+ },+ }])+ }+ }++ unsafe fn copy_acceleration_structure_to_acceleration_structure(+ &mut self,+ src: &super::AccelerationStructure,+ dst: &super::AccelerationStructure,+ copy: wgt::AccelerationStructureCopy,+ ) {+ let list = self+ .list+ .as_ref()+ .unwrap()+ .cast::<Direct3D12::ID3D12GraphicsCommandList4>()+ .unwrap();+ unsafe {+ list.CopyRaytracingAccelerationStructure(+ dst.resource.GetGPUVirtualAddress(),+ src.resource.GetGPUVirtualAddress(),+ conv::map_acceleration_structure_copy_mode(copy),+ )+ }+ }++ unsafe fn set_acceleration_structure_dependencies(+ _command_buffers: &[&super::CommandBuffer],+ _dependencies: &[&super::AccelerationStructure],+ ) {+ }++ unsafe fn begin_ray_tracing_pass(&mut self, _desc: &crate::RayTracingPassDescriptor) {+ unreachable!("Ray tracing pipelines not supported")+ }++ unsafe fn end_ray_tracing_pass(&mut self) {+ unreachable!("Ray tracing pipelines not supported")+ }++ unsafe fn set_ray_tracing_pipeline(+ &mut self,+ _pipeline: &<Self::A as crate::Api>::RayTracingPipeline,+ ) {+ unreachable!("Ray tracing pipelines not supported")+ }++ unsafe fn trace_rays(+ &mut self,+ _count: [u32; 3],+ _ray_generation_group_data: crate::PipelineGroupData<super::Buffer>,+ _miss_group_data: crate::PipelineGroupData<super::Buffer>,+ _intersection_group_data: crate::PipelineGroupData<super::Buffer>,+ ) {+ unreachable!("Ray tracing pipelines not supported")+ }+}diff --git a/vendor/wgpu-hal/src/dx12/conv.rs b/vendor/wgpu-hal/src/dx12/conv.rsnew file mode 100644index 00000000..6cc2bec2--- /dev/null+++ b/vendor/wgpu-hal/src/dx12/conv.rs@@ -0,0 +1,467 @@+use windows::Win32::Graphics::{Direct3D, Direct3D12, Dxgi};++pub fn map_buffer_usage_to_resource_flags(+ usage: wgt::BufferUses,+) -> Direct3D12::D3D12_RESOURCE_FLAGS {+ let mut flags = Direct3D12::D3D12_RESOURCE_FLAG_NONE;+ if usage.contains(wgt::BufferUses::STORAGE_READ_WRITE)+ || usage.contains(wgt::BufferUses::ACCELERATION_STRUCTURE_QUERY)+ {+ flags |= Direct3D12::D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;+ }+ flags+}++pub fn map_buffer_descriptor(+ desc: &crate::BufferDescriptor<'_>,+) -> Direct3D12::D3D12_RESOURCE_DESC {+ Direct3D12::D3D12_RESOURCE_DESC {+ Dimension: Direct3D12::D3D12_RESOURCE_DIMENSION_BUFFER,+ Alignment: 0,+ Width: desc.size,+ Height: 1,+ DepthOrArraySize: 1,+ MipLevels: 1,+ Format: Dxgi::Common::DXGI_FORMAT_UNKNOWN,+ SampleDesc: Dxgi::Common::DXGI_SAMPLE_DESC {+ Count: 1,+ Quality: 0,+ },+ Layout: Direct3D12::D3D12_TEXTURE_LAYOUT_ROW_MAJOR,+ Flags: map_buffer_usage_to_resource_flags(desc.usage),+ }+}++pub fn map_texture_dimension(dim: wgt::TextureDimension) -> Direct3D12::D3D12_RESOURCE_DIMENSION {+ match dim {+ wgt::TextureDimension::D1 => Direct3D12::D3D12_RESOURCE_DIMENSION_TEXTURE1D,+ wgt::TextureDimension::D2 => Direct3D12::D3D12_RESOURCE_DIMENSION_TEXTURE2D,+ wgt::TextureDimension::D3 => Direct3D12::D3D12_RESOURCE_DIMENSION_TEXTURE3D,+ }+}++pub fn map_texture_usage_to_resource_flags(+ usage: wgt::TextureUses,+) -> Direct3D12::D3D12_RESOURCE_FLAGS {+ let mut flags = Direct3D12::D3D12_RESOURCE_FLAG_NONE;++ if usage.contains(wgt::TextureUses::COLOR_TARGET) {+ flags |= Direct3D12::D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;+ }+ if usage+ .intersects(wgt::TextureUses::DEPTH_STENCIL_READ | wgt::TextureUses::DEPTH_STENCIL_WRITE)+ {+ flags |= Direct3D12::D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;+ if !usage.contains(wgt::TextureUses::RESOURCE) {+ flags |= Direct3D12::D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE;+ }+ }+ if usage.intersects(+ wgt::TextureUses::STORAGE_READ_ONLY+ | wgt::TextureUses::STORAGE_WRITE_ONLY+ | wgt::TextureUses::STORAGE_READ_WRITE,+ ) {+ flags |= Direct3D12::D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;+ }++ flags+}++pub fn map_address_mode(mode: wgt::AddressMode) -> Direct3D12::D3D12_TEXTURE_ADDRESS_MODE {+ use wgt::AddressMode as Am;+ match mode {+ Am::Repeat => Direct3D12::D3D12_TEXTURE_ADDRESS_MODE_WRAP,+ Am::MirrorRepeat => Direct3D12::D3D12_TEXTURE_ADDRESS_MODE_MIRROR,+ Am::ClampToEdge => Direct3D12::D3D12_TEXTURE_ADDRESS_MODE_CLAMP,+ Am::ClampToBorder => Direct3D12::D3D12_TEXTURE_ADDRESS_MODE_BORDER,+ //Am::MirrorClamp => Direct3D12::D3D12_TEXTURE_ADDRESS_MODE_MIRROR_ONCE,+ }+}++pub fn map_filter_mode(mode: wgt::FilterMode) -> Direct3D12::D3D12_FILTER_TYPE {+ match mode {+ wgt::FilterMode::Nearest => Direct3D12::D3D12_FILTER_TYPE_POINT,+ wgt::FilterMode::Linear => Direct3D12::D3D12_FILTER_TYPE_LINEAR,+ }+}++pub fn map_mipmap_filter_mode(mode: wgt::MipmapFilterMode) -> Direct3D12::D3D12_FILTER_TYPE {+ match mode {+ wgt::MipmapFilterMode::Nearest => Direct3D12::D3D12_FILTER_TYPE_POINT,+ wgt::MipmapFilterMode::Linear => Direct3D12::D3D12_FILTER_TYPE_LINEAR,+ }+}++pub fn map_comparison(func: wgt::CompareFunction) -> Direct3D12::D3D12_COMPARISON_FUNC {+ use wgt::CompareFunction as Cf;+ match func {+ Cf::Never => Direct3D12::D3D12_COMPARISON_FUNC_NEVER,+ Cf::Less => Direct3D12::D3D12_COMPARISON_FUNC_LESS,+ Cf::LessEqual => Direct3D12::D3D12_COMPARISON_FUNC_LESS_EQUAL,+ Cf::Equal => Direct3D12::D3D12_COMPARISON_FUNC_EQUAL,+ Cf::GreaterEqual => Direct3D12::D3D12_COMPARISON_FUNC_GREATER_EQUAL,+ Cf::Greater => Direct3D12::D3D12_COMPARISON_FUNC_GREATER,+ Cf::NotEqual => Direct3D12::D3D12_COMPARISON_FUNC_NOT_EQUAL,+ Cf::Always => Direct3D12::D3D12_COMPARISON_FUNC_ALWAYS,+ }+}++pub fn map_border_color(border_color: Option<wgt::SamplerBorderColor>) -> [f32; 4] {+ use wgt::SamplerBorderColor as Sbc;+ match border_color {+ Some(Sbc::TransparentBlack) | Some(Sbc::Zero) | None => [0.0; 4],+ Some(Sbc::OpaqueBlack) => [0.0, 0.0, 0.0, 1.0],+ Some(Sbc::OpaqueWhite) => [1.0; 4],+ }+}++pub fn map_visibility(visibility: wgt::ShaderStages) -> Direct3D12::D3D12_SHADER_VISIBILITY {+ match visibility {+ wgt::ShaderStages::VERTEX => Direct3D12::D3D12_SHADER_VISIBILITY_VERTEX,+ wgt::ShaderStages::FRAGMENT => Direct3D12::D3D12_SHADER_VISIBILITY_PIXEL,+ _ => Direct3D12::D3D12_SHADER_VISIBILITY_ALL,+ }+}++pub fn map_binding_type(ty: &wgt::BindingType) -> Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE {+ use wgt::BindingType as Bt;+ match *ty {+ Bt::Sampler { .. } => Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER,+ Bt::Buffer {+ ty: wgt::BufferBindingType::Uniform,+ ..+ } => Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_CBV,+ Bt::Buffer {+ ty: wgt::BufferBindingType::Storage { read_only: true },+ ..+ }+ | Bt::Texture { .. } => Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SRV,+ Bt::Buffer {+ ty: wgt::BufferBindingType::Storage { read_only: false },+ ..+ }+ | Bt::StorageTexture { .. } => Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_UAV,+ Bt::AccelerationStructure { .. } => Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SRV,+ // External textures require multiple bindings and therefore cannot+ // be mapped to a single descriptor range type. They must be handled+ // separately by the caller.+ Bt::ExternalTexture => unreachable!("External textures must be handled separately"),+ }+}++pub fn map_buffer_usage_to_state(usage: wgt::BufferUses) -> Direct3D12::D3D12_RESOURCE_STATES {+ use wgt::BufferUses as Bu;+ let mut state = Direct3D12::D3D12_RESOURCE_STATE_COMMON;++ if usage.intersects(Bu::COPY_SRC) {+ state |= Direct3D12::D3D12_RESOURCE_STATE_COPY_SOURCE;+ }+ if usage.intersects(Bu::COPY_DST) {+ state |= Direct3D12::D3D12_RESOURCE_STATE_COPY_DEST;+ }+ if usage.intersects(Bu::INDEX) {+ state |= Direct3D12::D3D12_RESOURCE_STATE_INDEX_BUFFER;+ }+ if usage.intersects(Bu::VERTEX | Bu::UNIFORM) {+ state |= Direct3D12::D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER;+ }+ if usage.intersects(Bu::STORAGE_READ_WRITE) {+ state |= Direct3D12::D3D12_RESOURCE_STATE_UNORDERED_ACCESS;+ } else if usage.intersects(Bu::STORAGE_READ_ONLY) {+ state |= Direct3D12::D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE+ | Direct3D12::D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;+ }+ if usage.intersects(Bu::INDIRECT) {+ state |= Direct3D12::D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT;+ }+ if usage.intersects(Bu::ACCELERATION_STRUCTURE_QUERY) {+ state |= Direct3D12::D3D12_RESOURCE_STATE_UNORDERED_ACCESS;+ }+ state+}++pub fn map_texture_usage_to_state(usage: wgt::TextureUses) -> Direct3D12::D3D12_RESOURCE_STATES {+ use wgt::TextureUses as Tu;+ let mut state = Direct3D12::D3D12_RESOURCE_STATE_COMMON;+ //Note: `RESOLVE_SOURCE` and `RESOLVE_DEST` are not used here+ //Note: `PRESENT` is the same as `COMMON`+ if usage == wgt::TextureUses::UNINITIALIZED {+ return state;+ }++ if usage.intersects(Tu::COPY_SRC) {+ state |= Direct3D12::D3D12_RESOURCE_STATE_COPY_SOURCE;+ }+ if usage.intersects(Tu::COPY_DST) {+ state |= Direct3D12::D3D12_RESOURCE_STATE_COPY_DEST;+ }+ if usage.intersects(Tu::RESOURCE) {+ state |= Direct3D12::D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE+ | Direct3D12::D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;+ }+ if usage.intersects(Tu::COLOR_TARGET) {+ state |= Direct3D12::D3D12_RESOURCE_STATE_RENDER_TARGET;+ }+ if usage.intersects(Tu::DEPTH_STENCIL_READ) {+ state |= Direct3D12::D3D12_RESOURCE_STATE_DEPTH_READ;+ }+ if usage.intersects(Tu::DEPTH_STENCIL_WRITE) {+ state |= Direct3D12::D3D12_RESOURCE_STATE_DEPTH_WRITE;+ }+ if usage.intersects(Tu::STORAGE_READ_ONLY | Tu::STORAGE_WRITE_ONLY | Tu::STORAGE_READ_WRITE) {+ state |= Direct3D12::D3D12_RESOURCE_STATE_UNORDERED_ACCESS;+ }+ state+}++pub fn map_topology(+ topology: wgt::PrimitiveTopology,+) -> (+ Direct3D12::D3D12_PRIMITIVE_TOPOLOGY_TYPE,+ Direct3D::D3D_PRIMITIVE_TOPOLOGY,+) {+ match topology {+ wgt::PrimitiveTopology::PointList => (+ Direct3D12::D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT,+ Direct3D::D3D_PRIMITIVE_TOPOLOGY_POINTLIST,+ ),+ wgt::PrimitiveTopology::LineList => (+ Direct3D12::D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE,+ Direct3D::D3D_PRIMITIVE_TOPOLOGY_LINELIST,+ ),+ wgt::PrimitiveTopology::LineStrip => (+ Direct3D12::D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE,+ Direct3D::D3D_PRIMITIVE_TOPOLOGY_LINESTRIP,+ ),+ wgt::PrimitiveTopology::TriangleList => (+ Direct3D12::D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,+ Direct3D::D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST,+ ),+ wgt::PrimitiveTopology::TriangleStrip => (+ Direct3D12::D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,+ Direct3D::D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP,+ ),+ }+}++pub fn map_polygon_mode(mode: wgt::PolygonMode) -> Direct3D12::D3D12_FILL_MODE {+ match mode {+ wgt::PolygonMode::Fill => Direct3D12::D3D12_FILL_MODE_SOLID,+ wgt::PolygonMode::Line => Direct3D12::D3D12_FILL_MODE_WIREFRAME,+ wgt::PolygonMode::Point => panic!(+ "{:?} is not enabled for this backend",+ wgt::Features::POLYGON_MODE_POINT+ ),+ }+}++/// D3D12 doesn't support passing factors ending in `_COLOR` for alpha blending+/// (see <https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_render_target_blend_desc>).+/// Therefore this function takes an additional `is_alpha` argument+/// which if set will return an equivalent `_ALPHA` factor.+fn map_blend_factor(factor: wgt::BlendFactor, is_alpha: bool) -> Direct3D12::D3D12_BLEND {+ use wgt::BlendFactor as Bf;+ match factor {+ Bf::Zero => Direct3D12::D3D12_BLEND_ZERO,+ Bf::One => Direct3D12::D3D12_BLEND_ONE,+ Bf::Src if is_alpha => Direct3D12::D3D12_BLEND_SRC_ALPHA,+ Bf::Src => Direct3D12::D3D12_BLEND_SRC_COLOR,+ Bf::OneMinusSrc if is_alpha => Direct3D12::D3D12_BLEND_INV_SRC_ALPHA,+ Bf::OneMinusSrc => Direct3D12::D3D12_BLEND_INV_SRC_COLOR,+ Bf::Dst if is_alpha => Direct3D12::D3D12_BLEND_DEST_ALPHA,+ Bf::Dst => Direct3D12::D3D12_BLEND_DEST_COLOR,+ Bf::OneMinusDst if is_alpha => Direct3D12::D3D12_BLEND_INV_DEST_ALPHA,+ Bf::OneMinusDst => Direct3D12::D3D12_BLEND_INV_DEST_COLOR,+ Bf::SrcAlpha => Direct3D12::D3D12_BLEND_SRC_ALPHA,+ Bf::OneMinusSrcAlpha => Direct3D12::D3D12_BLEND_INV_SRC_ALPHA,+ Bf::DstAlpha => Direct3D12::D3D12_BLEND_DEST_ALPHA,+ Bf::OneMinusDstAlpha => Direct3D12::D3D12_BLEND_INV_DEST_ALPHA,+ Bf::Constant => Direct3D12::D3D12_BLEND_BLEND_FACTOR,+ Bf::OneMinusConstant => Direct3D12::D3D12_BLEND_INV_BLEND_FACTOR,+ Bf::SrcAlphaSaturated => Direct3D12::D3D12_BLEND_SRC_ALPHA_SAT,+ Bf::Src1 if is_alpha => Direct3D12::D3D12_BLEND_SRC1_ALPHA,+ Bf::Src1 => Direct3D12::D3D12_BLEND_SRC1_COLOR,+ Bf::OneMinusSrc1 if is_alpha => Direct3D12::D3D12_BLEND_INV_SRC1_ALPHA,+ Bf::OneMinusSrc1 => Direct3D12::D3D12_BLEND_INV_SRC1_COLOR,+ Bf::Src1Alpha => Direct3D12::D3D12_BLEND_SRC1_ALPHA,+ Bf::OneMinusSrc1Alpha => Direct3D12::D3D12_BLEND_INV_SRC1_ALPHA,+ }+}++fn map_blend_component(+ component: &wgt::BlendComponent,+ is_alpha: bool,+) -> (+ Direct3D12::D3D12_BLEND_OP,+ Direct3D12::D3D12_BLEND,+ Direct3D12::D3D12_BLEND,+) {+ let raw_op = match component.operation {+ wgt::BlendOperation::Add => Direct3D12::D3D12_BLEND_OP_ADD,+ wgt::BlendOperation::Subtract => Direct3D12::D3D12_BLEND_OP_SUBTRACT,+ wgt::BlendOperation::ReverseSubtract => Direct3D12::D3D12_BLEND_OP_REV_SUBTRACT,+ wgt::BlendOperation::Min => Direct3D12::D3D12_BLEND_OP_MIN,+ wgt::BlendOperation::Max => Direct3D12::D3D12_BLEND_OP_MAX,+ };+ let raw_src = map_blend_factor(component.src_factor, is_alpha);+ let raw_dst = map_blend_factor(component.dst_factor, is_alpha);+ (raw_op, raw_src, raw_dst)+}++pub fn map_render_targets(+ color_targets: &[Option<wgt::ColorTargetState>],+) -> [Direct3D12::D3D12_RENDER_TARGET_BLEND_DESC;+ Direct3D12::D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT as usize] {+ let dummy_target = Direct3D12::D3D12_RENDER_TARGET_BLEND_DESC {+ BlendEnable: false.into(),+ LogicOpEnable: false.into(),+ SrcBlend: Direct3D12::D3D12_BLEND_ZERO,+ DestBlend: Direct3D12::D3D12_BLEND_ZERO,+ BlendOp: Direct3D12::D3D12_BLEND_OP_ADD,+ SrcBlendAlpha: Direct3D12::D3D12_BLEND_ZERO,+ DestBlendAlpha: Direct3D12::D3D12_BLEND_ZERO,+ BlendOpAlpha: Direct3D12::D3D12_BLEND_OP_ADD,+ LogicOp: Direct3D12::D3D12_LOGIC_OP_CLEAR,+ RenderTargetWriteMask: 0,+ };+ let mut raw_targets =+ [dummy_target; Direct3D12::D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT as usize];++ for (raw, ct) in raw_targets.iter_mut().zip(color_targets.iter()) {+ if let Some(ct) = ct.as_ref() {+ raw.RenderTargetWriteMask = ct.write_mask.bits() as u8;+ if let Some(ref blend) = ct.blend {+ let (color_op, color_src, color_dst) = map_blend_component(&blend.color, false);+ let (alpha_op, alpha_src, alpha_dst) = map_blend_component(&blend.alpha, true);+ raw.BlendEnable = true.into();+ raw.BlendOp = color_op;+ raw.SrcBlend = color_src;+ raw.DestBlend = color_dst;+ raw.BlendOpAlpha = alpha_op;+ raw.SrcBlendAlpha = alpha_src;+ raw.DestBlendAlpha = alpha_dst;+ }+ }+ }++ raw_targets+}++fn map_stencil_op(op: wgt::StencilOperation) -> Direct3D12::D3D12_STENCIL_OP {+ use wgt::StencilOperation as So;+ match op {+ So::Keep => Direct3D12::D3D12_STENCIL_OP_KEEP,+ So::Zero => Direct3D12::D3D12_STENCIL_OP_ZERO,+ So::Replace => Direct3D12::D3D12_STENCIL_OP_REPLACE,+ So::IncrementClamp => Direct3D12::D3D12_STENCIL_OP_INCR_SAT,+ So::IncrementWrap => Direct3D12::D3D12_STENCIL_OP_INCR,+ So::DecrementClamp => Direct3D12::D3D12_STENCIL_OP_DECR_SAT,+ So::DecrementWrap => Direct3D12::D3D12_STENCIL_OP_DECR,+ So::Invert => Direct3D12::D3D12_STENCIL_OP_INVERT,+ }+}++fn map_stencil_face(face: &wgt::StencilFaceState) -> Direct3D12::D3D12_DEPTH_STENCILOP_DESC {+ Direct3D12::D3D12_DEPTH_STENCILOP_DESC {+ StencilFailOp: map_stencil_op(face.fail_op),+ StencilDepthFailOp: map_stencil_op(face.depth_fail_op),+ StencilPassOp: map_stencil_op(face.pass_op),+ StencilFunc: map_comparison(face.compare),+ }+}++pub fn map_depth_stencil(ds: &wgt::DepthStencilState) -> Direct3D12::D3D12_DEPTH_STENCIL_DESC {+ Direct3D12::D3D12_DEPTH_STENCIL_DESC {+ DepthEnable: ds.is_depth_enabled().into(),+ DepthWriteMask: if ds.depth_write_enabled.unwrap_or_default() {+ Direct3D12::D3D12_DEPTH_WRITE_MASK_ALL+ } else {+ Direct3D12::D3D12_DEPTH_WRITE_MASK_ZERO+ },+ DepthFunc: map_comparison(ds.depth_compare.unwrap_or_default()),+ StencilEnable: ds.stencil.is_enabled().into(),+ StencilReadMask: ds.stencil.read_mask as u8,+ StencilWriteMask: ds.stencil.write_mask as u8,+ FrontFace: map_stencil_face(&ds.stencil.front),+ BackFace: map_stencil_face(&ds.stencil.back),+ }+}++pub(crate) fn map_acceleration_structure_build_flags(+ flags: wgt::AccelerationStructureFlags,+ mode: Option<crate::AccelerationStructureBuildMode>,+) -> Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS {+ let mut d3d_flags = Default::default();+ if flags.contains(wgt::AccelerationStructureFlags::ALLOW_COMPACTION) {+ d3d_flags |=+ Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_COMPACTION;+ }++ if flags.contains(wgt::AccelerationStructureFlags::ALLOW_UPDATE) {+ d3d_flags |= Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_UPDATE;+ }++ if flags.contains(wgt::AccelerationStructureFlags::LOW_MEMORY) {+ d3d_flags |= Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_MINIMIZE_MEMORY;+ }++ if flags.contains(wgt::AccelerationStructureFlags::PREFER_FAST_BUILD) {+ d3d_flags |=+ Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_BUILD;+ }++ if flags.contains(wgt::AccelerationStructureFlags::PREFER_FAST_TRACE) {+ d3d_flags |=+ Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE;+ }++ if let Some(crate::AccelerationStructureBuildMode::Update) = mode {+ d3d_flags |= Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE+ }++ d3d_flags+}++pub(crate) fn map_acceleration_structure_geometry_flags(+ flags: wgt::AccelerationStructureGeometryFlags,+) -> Direct3D12::D3D12_RAYTRACING_GEOMETRY_FLAGS {+ let mut d3d_flags = Default::default();+ if flags.contains(wgt::AccelerationStructureGeometryFlags::OPAQUE) {+ d3d_flags |= Direct3D12::D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE;+ }+ if flags.contains(wgt::AccelerationStructureGeometryFlags::NO_DUPLICATE_ANY_HIT_INVOCATION) {+ d3d_flags |= Direct3D12::D3D12_RAYTRACING_GEOMETRY_FLAG_NO_DUPLICATE_ANYHIT_INVOCATION;+ }+ d3d_flags+}++pub(crate) fn map_acceleration_structure_copy_mode(+ mode: wgt::AccelerationStructureCopy,+) -> Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE {+ match mode {+ wgt::AccelerationStructureCopy::Clone => {+ Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_CLONE+ }+ wgt::AccelerationStructureCopy::Compact => {+ Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_COMPACT+ }+ }+}++pub(crate) const fn make_shader_component_mapping(+ src0: Direct3D12::D3D12_SHADER_COMPONENT_MAPPING,+ src1: Direct3D12::D3D12_SHADER_COMPONENT_MAPPING,+ src2: Direct3D12::D3D12_SHADER_COMPONENT_MAPPING,+ src3: Direct3D12::D3D12_SHADER_COMPONENT_MAPPING,+) -> Direct3D12::D3D12_SHADER_COMPONENT_MAPPING {+ const M: i32 = Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_MASK as i32;+ const S: i32 = Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_SHIFT as i32;+ Direct3D12::D3D12_SHADER_COMPONENT_MAPPING(+ (src0.0 & M)+ | (src1.0 & M) << S+ | (src2.0 & M) << (S * 2)+ | (src3.0 & M) << (S * 3)+ | Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_ALWAYS_SET_BIT_AVOIDING_ZEROMEM_MISTAKES+ as i32,+ )+}diff --git a/vendor/wgpu-hal/src/dx12/dcomp.rs b/vendor/wgpu-hal/src/dx12/dcomp.rsnew file mode 100644index 00000000..46ef3da4--- /dev/null+++ b/vendor/wgpu-hal/src/dx12/dcomp.rs@@ -0,0 +1,120 @@+use alloc::sync::Arc;+use core::{ffi, ptr};++use once_cell::sync::Lazy;+use windows::{+ core::Interface as _,+ Win32::{Foundation::HWND, Graphics::DirectComposition},+};++use super::DynLib;++// Lazy-loaded DirectComposition library+#[derive(Debug)]+pub(crate) struct DCompLib {+ lib: Lazy<Result<DynLib, crate::SurfaceError>>,+}++impl DCompLib {+ pub(crate) fn new() -> Self {+ Self {+ lib: Lazy::new(|| unsafe {+ DynLib::new("dcomp.dll").map_err(|err| {+ log::error!("Error loading dcomp.dll: {err}");+ crate::SurfaceError::Other("Error loading dcomp.dll")+ })+ }),+ }+ }++ fn get_lib(&self) -> Result<&DynLib, crate::SurfaceError> {+ match self.lib.as_ref() {+ Ok(lib) => Ok(lib),+ Err(err) => Err(err.clone()),+ }+ }++ pub(crate) fn create_device(+ &self,+ ) -> Result<DirectComposition::IDCompositionDevice, crate::SurfaceError> {+ let lib = self.get_lib()?;++ // Calls windows::Win32::Graphics::DirectComposition::DCompositionCreateDevice2 on dcomp.dll+ type Fun = extern "system" fn(+ pdxdevice: *mut ffi::c_void,+ riid: *const windows_core::GUID,+ ppdcompdevice: *mut *mut ffi::c_void,+ ) -> windows_core::HRESULT;+ let func: libloading::Symbol<Fun> =+ unsafe { lib.get(c"DCompositionCreateDevice2".to_bytes()) }?;++ let mut res: Option<DirectComposition::IDCompositionDevice> = None;++ (func)(+ ptr::null_mut(),+ &DirectComposition::IDCompositionDevice::IID,+ <*mut _>::cast(&mut res),+ )+ .map(|| res.unwrap())+ .map_err(|err| {+ log::error!("DirectComposition::DCompositionCreateDevice2 failed: {err}");+ crate::SurfaceError::Other("DirectComposition::DCompositionCreateDevice2")+ })+ }+}++#[derive(Default)]+pub struct DCompState {+ inner: Option<InnerState>,+}++impl DCompState {+ /// This will create a DirectComposition device and a target for the window handle if not already initialized.+ /// If the device is already initialized, it will return the existing state.+ pub unsafe fn get_or_init(+ &mut self,+ lib: &Arc<DCompLib>,+ hwnd: &HWND,+ ) -> Result<&mut InnerState, crate::SurfaceError> {+ if self.inner.is_none() {+ self.inner = Some(unsafe { InnerState::init(lib, hwnd) }?);+ }+ Ok(self.inner.as_mut().unwrap())+ }+}++pub struct InnerState {+ pub visual: DirectComposition::IDCompositionVisual,+ pub device: DirectComposition::IDCompositionDevice,+ // Must be kept alive but is otherwise unused after initialization.+ pub _target: DirectComposition::IDCompositionTarget,+}++impl InnerState {+ /// Creates a DirectComposition device and a target for the given window handle.+ pub unsafe fn init(lib: &Arc<DCompLib>, hwnd: &HWND) -> Result<Self, crate::SurfaceError> {+ profiling::scope!("DCompState::init");+ let dcomp_device = lib.create_device()?;++ let target = unsafe { dcomp_device.CreateTargetForHwnd(*hwnd, false) }.map_err(|err| {+ log::error!("IDCompositionDevice::CreateTargetForHwnd failed: {err}");+ crate::SurfaceError::Other("IDCompositionDevice::CreateTargetForHwnd")+ })?;++ let visual = unsafe { dcomp_device.CreateVisual() }.map_err(|err| {+ log::error!("IDCompositionDevice::CreateVisual failed: {err}");+ crate::SurfaceError::Other("IDCompositionDevice::CreateVisual")+ })?;++ unsafe { target.SetRoot(&visual) }.map_err(|err| {+ log::error!("IDCompositionTarget::SetRoot failed: {err}");+ crate::SurfaceError::Other("IDCompositionTarget::SetRoot")+ })?;++ Ok(InnerState {+ visual,+ device: dcomp_device,+ _target: target,+ })+ }+}diff --git a/vendor/wgpu-hal/src/dx12/descriptor.rs b/vendor/wgpu-hal/src/dx12/descriptor.rsnew file mode 100644index 00000000..f57ba46e--- /dev/null+++ b/vendor/wgpu-hal/src/dx12/descriptor.rs@@ -0,0 +1,329 @@+use alloc::vec::Vec;+use core::fmt;++use bit_set::BitSet;+use parking_lot::Mutex;+use range_alloc::RangeAllocator;+use windows::Win32::Graphics::Direct3D12;++use crate::auxil::dxgi::result::HResult as _;++const HEAP_SIZE_FIXED: usize = 64;++#[derive(Copy, Clone)]+pub(super) struct DualHandle {+ cpu: Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE,+ pub gpu: Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE,+ /// How large the block allocated to this handle is.+ count: u64,+}++impl fmt::Debug for DualHandle {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ f.debug_struct("DualHandle")+ .field("cpu", &self.cpu.ptr)+ .field("gpu", &self.gpu.ptr)+ .field("count", &self.count)+ .finish()+ }+}++type DescriptorIndex = u64;++pub(super) struct GeneralHeap {+ pub raw: Direct3D12::ID3D12DescriptorHeap,+ ty: Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE,+ handle_size: u64,+ total_handles: u64,+ start: DualHandle,+ ranges: Mutex<RangeAllocator<DescriptorIndex>>,+}++impl GeneralHeap {+ pub(super) fn new(+ device: &Direct3D12::ID3D12Device,+ ty: Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE,+ total_handles: u64,+ ) -> Result<Self, crate::DeviceError> {+ let raw = {+ profiling::scope!("ID3D12Device::CreateDescriptorHeap");+ let desc = Direct3D12::D3D12_DESCRIPTOR_HEAP_DESC {+ Type: ty,+ NumDescriptors: total_handles as u32,+ Flags: Direct3D12::D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,+ NodeMask: 0,+ };+ unsafe { device.CreateDescriptorHeap::<Direct3D12::ID3D12DescriptorHeap>(&desc) }+ .into_device_result("Descriptor heap creation")?+ };++ let start = DualHandle {+ cpu: unsafe { raw.GetCPUDescriptorHandleForHeapStart() },+ gpu: unsafe { raw.GetGPUDescriptorHandleForHeapStart() },+ count: 0,+ };++ Ok(Self {+ raw,+ ty,+ handle_size: unsafe { device.GetDescriptorHandleIncrementSize(ty) } as u64,+ total_handles,+ start,+ ranges: Mutex::new(RangeAllocator::new(0..total_handles)),+ })+ }++ pub(super) fn at(&self, index: DescriptorIndex, count: u64) -> DualHandle {+ assert!(index < self.total_handles);+ DualHandle {+ cpu: self.cpu_descriptor_at(index),+ gpu: self.gpu_descriptor_at(index),+ count,+ }+ }++ fn cpu_descriptor_at(&self, index: u64) -> Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE {+ Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE {+ ptr: self.start.cpu.ptr + (self.handle_size * index) as usize,+ }+ }++ fn gpu_descriptor_at(&self, index: u64) -> Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE {+ Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE {+ ptr: self.start.gpu.ptr + self.handle_size * index,+ }+ }++ pub(super) fn allocate_slice(&self, count: u64) -> Result<DescriptorIndex, crate::DeviceError> {+ let range = self.ranges.lock().allocate_range(count).map_err(|err| {+ log::error!("Unable to allocate descriptors: {err:?}");+ crate::DeviceError::OutOfMemory+ })?;+ Ok(range.start)+ }++ /// Free handles previously given out by this `DescriptorHeapSlice`.+ /// Do not use this with handles not given out by this `DescriptorHeapSlice`.+ pub(crate) fn free_slice(&self, handle: DualHandle) {+ let start = (handle.gpu.ptr - self.start.gpu.ptr) / self.handle_size;+ self.ranges.lock().free_range(start..start + handle.count);+ }+}++/// Fixed-size free-list allocator for CPU descriptors.+struct FixedSizeHeap {+ _raw: Direct3D12::ID3D12DescriptorHeap,+ /// Bit flag representation of available handles in the heap.+ ///+ /// 0 - Occupied+ /// 1 - free+ availability: u64,+ handle_size: usize,+ start: Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE,+}++impl FixedSizeHeap {+ fn new(+ device: &Direct3D12::ID3D12Device,+ ty: Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE,+ ) -> Result<Self, crate::DeviceError> {+ let desc = Direct3D12::D3D12_DESCRIPTOR_HEAP_DESC {+ Type: ty,+ NumDescriptors: HEAP_SIZE_FIXED as u32,+ Flags: Direct3D12::D3D12_DESCRIPTOR_HEAP_FLAG_NONE,+ NodeMask: 0,+ };+ let heap =+ unsafe { device.CreateDescriptorHeap::<Direct3D12::ID3D12DescriptorHeap>(&desc) }+ .into_device_result("Descriptor heap creation")?;++ Ok(Self {+ handle_size: unsafe { device.GetDescriptorHandleIncrementSize(ty) } as usize,+ availability: !0, // all free!+ start: unsafe { heap.GetCPUDescriptorHandleForHeapStart() },+ _raw: heap,+ })+ }++ fn alloc_handle(+ &mut self,+ ) -> Result<Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE, crate::DeviceError> {+ // Find first free slot.+ let slot = self.availability.trailing_zeros() as usize;+ if slot >= HEAP_SIZE_FIXED {+ log::error!("Failed to allocate a handle form a fixed size heap");+ return Err(crate::DeviceError::OutOfMemory);+ }+ // Set the slot as occupied.+ self.availability ^= 1 << slot;++ Ok(Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE {+ ptr: self.start.ptr + self.handle_size * slot,+ })+ }++ fn free_handle(&mut self, handle: Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE) {+ let slot = (handle.ptr - self.start.ptr) / self.handle_size;+ assert!(slot < HEAP_SIZE_FIXED);+ assert_eq!(self.availability & (1 << slot), 0);+ self.availability ^= 1 << slot;+ }++ fn is_full(&self) -> bool {+ self.availability == 0+ }+}++#[derive(Clone, Copy)]+pub(super) struct Handle {+ pub raw: Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE,+ heap_index: usize,+}++impl fmt::Debug for Handle {+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {+ fmt.debug_struct("Handle")+ .field("ptr", &self.raw.ptr)+ .field("heap_index", &self.heap_index)+ .finish()+ }+}++pub(super) struct CpuPool {+ device: Direct3D12::ID3D12Device,+ ty: Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE,+ heaps: Vec<FixedSizeHeap>,+ available_heap_indices: BitSet,+}++impl CpuPool {+ pub(super) fn new(+ device: Direct3D12::ID3D12Device,+ ty: Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE,+ ) -> Self {+ Self {+ device,+ ty,+ heaps: Vec::new(),+ available_heap_indices: BitSet::new(),+ }+ }++ pub(super) fn alloc_handle(&mut self) -> Result<Handle, crate::DeviceError> {+ let heap_index = self+ .available_heap_indices+ .iter()+ .next()+ .unwrap_or(self.heaps.len());++ // Allocate a new heap+ if heap_index == self.heaps.len() {+ self.heaps.push(FixedSizeHeap::new(&self.device, self.ty)?);+ self.available_heap_indices.insert(heap_index);+ }++ let heap = &mut self.heaps[heap_index];+ let handle = Handle {+ raw: heap.alloc_handle()?,+ heap_index,+ };+ if heap.is_full() {+ self.available_heap_indices.remove(heap_index);+ }++ Ok(handle)+ }++ pub(super) fn free_handle(&mut self, handle: Handle) {+ self.heaps[handle.heap_index].free_handle(handle.raw);+ self.available_heap_indices.insert(handle.heap_index);+ }+}++pub(super) struct CpuHeapInner {+ pub _raw: Direct3D12::ID3D12DescriptorHeap,+ pub stage: Vec<Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE>,+}++pub(super) struct CpuHeap {+ pub inner: Mutex<CpuHeapInner>,+ start: Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE,+ handle_size: u32,+ total: u32,+}++unsafe impl Send for CpuHeap {}+unsafe impl Sync for CpuHeap {}++impl CpuHeap {+ pub(super) fn new(+ device: &Direct3D12::ID3D12Device,+ ty: Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE,+ total: u32,+ ) -> Result<Self, crate::DeviceError> {+ let handle_size = unsafe { device.GetDescriptorHandleIncrementSize(ty) };+ let desc = Direct3D12::D3D12_DESCRIPTOR_HEAP_DESC {+ Type: ty,+ NumDescriptors: total,+ Flags: Direct3D12::D3D12_DESCRIPTOR_HEAP_FLAG_NONE,+ NodeMask: 0,+ };+ let raw = unsafe { device.CreateDescriptorHeap::<Direct3D12::ID3D12DescriptorHeap>(&desc) }+ .into_device_result("CPU descriptor heap creation")?;++ let start = unsafe { raw.GetCPUDescriptorHandleForHeapStart() };++ Ok(Self {+ inner: Mutex::new(CpuHeapInner {+ _raw: raw,+ stage: Vec::new(),+ }),+ start,+ handle_size,+ total,+ })+ }++ pub(super) fn at(&self, index: u32) -> Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE {+ debug_assert!(+ index < self.total,+ "Index ({index}) out of bounds {total}",+ total = self.total+ );+ Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE {+ ptr: self.start.ptr + (self.handle_size * index) as usize,+ }+ }+}++impl fmt::Debug for CpuHeap {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ f.debug_struct("CpuHeap")+ .field("start", &self.start.ptr)+ .field("handle_size", &self.handle_size)+ .field("total", &self.total)+ .finish()+ }+}++pub(super) unsafe fn upload(+ device: &Direct3D12::ID3D12Device,+ src: &CpuHeapInner,+ dst: &GeneralHeap,+ dummy_copy_counts: &[u32],+) -> Result<DualHandle, crate::DeviceError> {+ let count = src.stage.len() as u32;+ let index = dst.allocate_slice(count as u64)?;+ unsafe {+ device.CopyDescriptors(+ 1,+ &dst.cpu_descriptor_at(index),+ Some(&count),+ count,+ src.stage.as_ptr(),+ Some(dummy_copy_counts.as_ptr()),+ dst.ty,+ )+ };+ Ok(dst.at(index, count as u64))+}diff --git a/vendor/wgpu-hal/src/dx12/device.rs b/vendor/wgpu-hal/src/dx12/device.rsnew file mode 100644index 00000000..63fb3139--- /dev/null+++ b/vendor/wgpu-hal/src/dx12/device.rs@@ -0,0 +1,2691 @@+use alloc::borrow::ToOwned;+use alloc::{+ borrow::Cow,+ string::{String, ToString as _},+ sync::Arc,+ vec::Vec,+};+use arrayvec::ArrayVec;+use core::{ffi, num::NonZeroU32, ptr, time::Duration};+use std::time::Instant;++use bytemuck::TransparentWrapper;+use parking_lot::Mutex;+use windows::{+ core::Interface as _,+ Win32::{+ Foundation,+ Graphics::{Direct3D12, Dxgi},+ System::Threading,+ },+};++use super::{conv, descriptor, D3D12Lib};+use crate::{+ auxil::{+ self,+ dxgi::{name::ObjectExt as _, result::HResult as _},+ },+ dx12::{+ borrow_optional_interface_temporarily, pipeline_desc::RenderPipelineStateStreamDesc,+ shader_compilation, suballocation, DCompLib, DynamicStorageBufferOffsets, Event,+ ShaderCacheKey, ShaderCacheValue,+ },+ AccelerationStructureEntries, TlasInstance,+};++// this has to match Naga's HLSL backend, and also needs to be null-terminated+const NAGA_LOCATION_SEMANTIC: &[u8] = c"LOC".to_bytes();++impl super::Device {+ #[allow(clippy::too_many_arguments)]+ pub(super) fn new(+ adapter: auxil::dxgi::factory::DxgiAdapter,+ raw: Direct3D12::ID3D12Device,+ present_queue: Direct3D12::ID3D12CommandQueue,+ features: wgt::Features,+ limits: &wgt::Limits,+ memory_hints: &wgt::MemoryHints,+ private_caps: super::PrivateCapabilities,+ library: &Arc<D3D12Lib>,+ dcomp_lib: &Arc<DCompLib>,+ memory_budget_thresholds: wgt::MemoryBudgetThresholds,+ compiler_container: Arc<shader_compilation::CompilerContainer>,+ backend_options: wgt::Dx12BackendOptions,+ ) -> Result<Self, crate::DeviceError> {+ if private_caps+ .instance_flags+ .contains(wgt::InstanceFlags::VALIDATION)+ {+ auxil::dxgi::exception::register_exception_handler();+ }++ let mem_allocator =+ suballocation::Allocator::new(&raw, memory_hints, memory_budget_thresholds)?;++ let idle_fence: Direct3D12::ID3D12Fence = unsafe {+ profiling::scope!("ID3D12Device::CreateFence");+ raw.CreateFence(0, Direct3D12::D3D12_FENCE_FLAG_NONE)+ }+ .into_device_result("Idle fence creation")?;++ let raw_desc = Direct3D12::D3D12_RESOURCE_DESC {+ Dimension: Direct3D12::D3D12_RESOURCE_DIMENSION_BUFFER,+ Alignment: 0,+ Width: super::ZERO_BUFFER_SIZE,+ Height: 1,+ DepthOrArraySize: 1,+ MipLevels: 1,+ Format: Dxgi::Common::DXGI_FORMAT_UNKNOWN,+ SampleDesc: Dxgi::Common::DXGI_SAMPLE_DESC {+ Count: 1,+ Quality: 0,+ },+ Layout: Direct3D12::D3D12_TEXTURE_LAYOUT_ROW_MAJOR,+ Flags: Direct3D12::D3D12_RESOURCE_FLAG_NONE,+ };++ let heap_properties = Direct3D12::D3D12_HEAP_PROPERTIES {+ Type: Direct3D12::D3D12_HEAP_TYPE_CUSTOM,+ CPUPageProperty: Direct3D12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE,+ MemoryPoolPreference: match private_caps.memory_architecture {+ super::MemoryArchitecture::Unified { .. } => Direct3D12::D3D12_MEMORY_POOL_L0,+ super::MemoryArchitecture::NonUnified => Direct3D12::D3D12_MEMORY_POOL_L1,+ },+ CreationNodeMask: 0,+ VisibleNodeMask: 0,+ };++ profiling::scope!("Zero Buffer Allocation");+ let mut zero_buffer = None::<Direct3D12::ID3D12Resource>;+ unsafe {+ raw.CreateCommittedResource(+ &heap_properties,+ Direct3D12::D3D12_HEAP_FLAG_NONE,+ &raw_desc,+ Direct3D12::D3D12_RESOURCE_STATE_COMMON,+ None,+ &mut zero_buffer,+ )+ }+ .into_device_result("Zero buffer creation")?;++ let zero_buffer = zero_buffer.ok_or(crate::DeviceError::Unexpected)?;++ // Note: without `D3D12_HEAP_FLAG_CREATE_NOT_ZEROED`+ // this resource is zeroed by default.++ // maximum number of CBV/SRV/UAV descriptors in heap for Tier 1+ let capacity_views = limits.max_non_sampler_bindings as u64;++ let draw_mesh = if features+ .features_wgpu+ .contains(wgt::FeaturesWGPU::EXPERIMENTAL_MESH_SHADER)+ {+ Some(Self::create_command_signature(+ &raw,+ None,+ size_of::<wgt::DispatchIndirectArgs>(),+ &[Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC {+ Type: Direct3D12::D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH_MESH,+ ..Default::default()+ }],+ 0,+ )?)+ } else {+ None+ };++ let shared = super::DeviceShared {+ adapter,+ zero_buffer,+ cmd_signatures: super::CommandSignatures {+ draw: Self::create_command_signature(+ &raw,+ None,+ size_of::<wgt::DrawIndirectArgs>(),+ &[Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC {+ Type: Direct3D12::D3D12_INDIRECT_ARGUMENT_TYPE_DRAW,+ ..Default::default()+ }],+ 0,+ )?,+ draw_indexed: Self::create_command_signature(+ &raw,+ None,+ size_of::<wgt::DrawIndexedIndirectArgs>(),+ &[Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC {+ Type: Direct3D12::D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED,+ ..Default::default()+ }],+ 0,+ )?,+ draw_mesh,+ dispatch: Self::create_command_signature(+ &raw,+ None,+ size_of::<wgt::DispatchIndirectArgs>(),+ &[Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC {+ Type: Direct3D12::D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH,+ ..Default::default()+ }],+ 0,+ )?,+ },+ heap_views: descriptor::GeneralHeap::new(+ &raw,+ Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,+ capacity_views,+ )?,+ sampler_heap: super::sampler::SamplerHeap::new(&raw, &private_caps)?,+ private_caps,+ };++ let mut rtv_pool =+ descriptor::CpuPool::new(raw.clone(), Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE_RTV);+ let null_rtv_handle = rtv_pool.alloc_handle()?;+ // A null pResource is used to initialize a null descriptor,+ // which guarantees D3D11-like null binding behavior (reading 0s, writes are discarded)+ unsafe {+ raw.CreateRenderTargetView(+ None,+ Some(&Direct3D12::D3D12_RENDER_TARGET_VIEW_DESC {+ Format: Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM,+ ViewDimension: Direct3D12::D3D12_RTV_DIMENSION_TEXTURE2D,+ Anonymous: Direct3D12::D3D12_RENDER_TARGET_VIEW_DESC_0 {+ Texture2D: Direct3D12::D3D12_TEX2D_RTV {+ MipSlice: 0,+ PlaneSlice: 0,+ },+ },+ }),+ null_rtv_handle.raw,+ )+ };++ Ok(super::Device {+ raw: raw.clone(),+ present_queue,+ idler: super::Idler { fence: idle_fence },+ features,+ shared: Arc::new(shared),+ rtv_pool: Arc::new(Mutex::new(rtv_pool)),+ dsv_pool: Mutex::new(descriptor::CpuPool::new(+ raw.clone(),+ Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE_DSV,+ )),+ srv_uav_pool: Mutex::new(descriptor::CpuPool::new(+ raw.clone(),+ Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,+ )),+ options: backend_options,+ library: Arc::clone(library),+ dcomp_lib: Arc::clone(dcomp_lib),+ #[cfg(feature = "renderdoc")]+ render_doc: Default::default(),+ null_rtv_handle,+ mem_allocator,+ compiler_container,+ shader_cache: Default::default(),+ counters: Default::default(),+ limits: limits.clone(),+ })+ }++ fn create_command_signature(+ raw: &Direct3D12::ID3D12Device,+ root_signature: Option<&Direct3D12::ID3D12RootSignature>,+ byte_stride: usize,+ arguments: &[Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC],+ node_mask: u32,+ ) -> Result<Direct3D12::ID3D12CommandSignature, crate::DeviceError> {+ let mut signature = None;+ unsafe {+ raw.CreateCommandSignature(+ &Direct3D12::D3D12_COMMAND_SIGNATURE_DESC {+ ByteStride: byte_stride as u32,+ NumArgumentDescs: arguments.len() as u32,+ pArgumentDescs: arguments.as_ptr(),+ NodeMask: node_mask,+ },+ root_signature,+ &mut signature,+ )+ }+ .into_device_result("Command signature creation")?;+ signature.ok_or(crate::DeviceError::Unexpected)+ }++ // Blocks until the dedicated present queue is finished with all of its work.+ //+ // Once this method completes, the surface is able to be resized or deleted.+ pub(super) unsafe fn wait_for_present_queue_idle(&self) -> Result<(), crate::DeviceError> {+ let cur_value = unsafe { self.idler.fence.GetCompletedValue() };+ if cur_value == !0 {+ return Err(crate::DeviceError::Lost);+ }++ let event = Event::create(false, false)?;++ let value = cur_value + 1;+ unsafe { self.present_queue.Signal(&self.idler.fence, value) }+ .into_device_result("Signal")?;+ let hr = unsafe { self.idler.fence.SetEventOnCompletion(value, event.0) };+ hr.into_device_result("Set event")?;+ unsafe { Threading::WaitForSingleObject(event.0, Threading::INFINITE) };+ Ok(())+ }++ /// When generating the vertex shader, the fragment stage must be passed if it exists!+ /// Otherwise, the generated HLSL may be incorrect since the fragment shader inputs are+ /// allowed to be a subset of the vertex outputs.+ fn load_shader(+ &self,+ stage: &crate::ProgrammableStage<super::ShaderModule>,+ layout: &super::PipelineLayout,+ naga_stage: naga::ShaderStage,+ fragment_stage: Option<&crate::ProgrammableStage<super::ShaderModule>>,+ ) -> Result<super::CompiledShader, crate::PipelineError> {+ let stage_bit = auxil::map_naga_stage(naga_stage);++ let needs_temp_options = stage.zero_initialize_workgroup_memory+ != layout.naga_options.zero_initialize_workgroup_memory+ || stage.module.runtime_checks.bounds_checks != layout.naga_options.restrict_indexing+ || !stage.module.runtime_checks.task_shader_dispatch_tracking+ || !stage+ .module+ .runtime_checks+ .mesh_shader_primitive_indices_clamp+ || stage.module.runtime_checks.force_loop_bounding+ != layout.naga_options.force_loop_bounding+ || stage+ .module+ .runtime_checks+ .ray_query_initialization_tracking+ != layout.naga_options.ray_query_initialization_tracking;+ let mut temp_options;+ let naga_options = if needs_temp_options {+ temp_options = layout.naga_options.clone();+ temp_options.zero_initialize_workgroup_memory = stage.zero_initialize_workgroup_memory;+ temp_options.restrict_indexing = stage.module.runtime_checks.bounds_checks;+ temp_options.force_loop_bounding = stage.module.runtime_checks.force_loop_bounding;+ if !stage.module.runtime_checks.task_shader_dispatch_tracking {+ temp_options.task_dispatch_limits = None;+ }+ temp_options.mesh_shader_primitive_indices_clamp = stage+ .module+ .runtime_checks+ .mesh_shader_primitive_indices_clamp;+ temp_options.ray_query_initialization_tracking = stage+ .module+ .runtime_checks+ .ray_query_initialization_tracking;+ &temp_options+ } else {+ &layout.naga_options+ };++ let key = match &stage.module.source {+ super::ShaderModuleSource::Naga(naga_shader) => {+ use naga::back::hlsl;++ let frag_ep = match fragment_stage {+ Some(crate::ProgrammableStage {+ module:+ super::ShaderModule {+ source: super::ShaderModuleSource::Naga(naga_shader),+ ..+ },+ entry_point,+ ..+ }) => Some(+ hlsl::FragmentEntryPoint::new(&naga_shader.module, entry_point).ok_or(+ crate::PipelineError::EntryPoint(naga::ShaderStage::Fragment),+ ),+ ),+ _ => None,+ }+ .transpose()?;+ let (module, info) = naga::back::pipeline_constants::process_overrides(+ &naga_shader.module,+ &naga_shader.info,+ Some((naga_stage, stage.entry_point)),+ stage.constants,+ )+ .map_err(|e| {+ crate::PipelineError::PipelineConstants(stage_bit, format!("HLSL: {e:?}"))+ })?;++ let pipeline_options = hlsl::PipelineOptions {+ entry_point: Some((naga_stage, stage.entry_point.to_string())),+ };++ //TODO: reuse the writer+ let (source, entry_point) = {+ let mut source = String::new();+ let mut writer =+ hlsl::Writer::new(&mut source, naga_options, &pipeline_options);++ profiling::scope!("naga::back::hlsl::write");+ let mut reflection_info = writer+ .write(&module, &info, frag_ep.as_ref())+ .map_err(|e| {+ crate::PipelineError::Linkage(stage_bit, format!("HLSL: {e:?}"))+ })?;++ assert_eq!(reflection_info.entry_point_names.len(), 1);++ let entry_point = reflection_info+ .entry_point_names+ .pop()+ .unwrap()+ .map_err(|e| crate::PipelineError::Linkage(stage_bit, format!("{e}")))?;++ (source, entry_point)+ };+ log::debug!(+ "Naga generated shader for {entry_point:?} at {naga_stage:?}:\n{source}"+ );++ ShaderCacheKey {+ source,+ entry_point,+ stage: naga_stage,+ shader_model: naga_options.shader_model,+ }+ }+ super::ShaderModuleSource::HlslPassthrough(passthrough) => ShaderCacheKey {+ source: passthrough.shader.clone(),+ entry_point: stage.entry_point.to_string(),+ stage: naga_stage,+ shader_model: naga_options.shader_model,+ },+ super::ShaderModuleSource::DxilPassthrough(passthrough) => {+ return Ok(super::CompiledShader::Precompiled(+ passthrough.shader.clone(),+ ))+ }+ };++ {+ let mut shader_cache = self.shader_cache.lock();+ let nr_of_shaders_compiled = shader_cache.nr_of_shaders_compiled;+ if let Some(value) = shader_cache.entries.get_mut(&key) {+ value.last_used = nr_of_shaders_compiled;+ return Ok(value.shader.clone());+ }+ }++ let source_name = stage.module.raw_name.as_deref();++ let full_stage = format!(+ "{}_{}",+ naga::back::hlsl::shader_stage_to_hlsl_str(naga_stage),+ key.shader_model.to_str()+ );++ let compiled_shader = self.compiler_container.compile(+ self,+ &key.source,+ source_name,+ &key.entry_point,+ stage_bit,+ &full_stage,+ )?;++ {+ let mut shader_cache = self.shader_cache.lock();+ shader_cache.nr_of_shaders_compiled += 1;+ let nr_of_shaders_compiled = shader_cache.nr_of_shaders_compiled;+ let value = ShaderCacheValue {+ last_used: nr_of_shaders_compiled,+ shader: compiled_shader.clone(),+ };+ shader_cache.entries.insert(key, value);++ // Retain all entries that have been used since we compiled the last 100 shaders.+ if shader_cache.entries.len() > 200 {+ shader_cache+ .entries+ .retain(|_, v| v.last_used >= nr_of_shaders_compiled - 100);+ }+ }++ Ok(compiled_shader)+ }++ pub fn raw_device(&self) -> &Direct3D12::ID3D12Device {+ &self.raw+ }++ pub fn raw_queue(&self) -> &Direct3D12::ID3D12CommandQueue {+ &self.present_queue+ }++ pub unsafe fn texture_from_raw(+ resource: Direct3D12::ID3D12Resource,+ format: wgt::TextureFormat,+ dimension: wgt::TextureDimension,+ size: wgt::Extent3d,+ mip_level_count: u32,+ sample_count: u32,+ ) -> super::Texture {+ super::Texture {+ resource,+ format,+ dimension,+ size,+ mip_level_count,+ sample_count,+ allocation: suballocation::Allocation::none(+ suballocation::AllocationType::Texture,+ format.theoretical_memory_footprint(size),+ ),+ plane_slice_override: None,+ }+ }++ pub unsafe fn buffer_from_raw(+ resource: Direct3D12::ID3D12Resource,+ size: wgt::BufferAddress,+ ) -> super::Buffer {+ super::Buffer {+ resource,+ size,+ allocation: suballocation::Allocation::none(+ suballocation::AllocationType::Buffer,+ size,+ ),+ }+ }+}++impl crate::Device for super::Device {+ type A = super::Api;++ unsafe fn create_buffer(+ &self,+ desc: &crate::BufferDescriptor,+ ) -> Result<super::Buffer, crate::DeviceError> {+ let mut desc = desc.clone();++ if desc.usage.contains(wgt::BufferUses::UNIFORM) {+ desc.size = desc+ .size+ .next_multiple_of(Direct3D12::D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT.into())+ }++ let (resource, allocation) =+ suballocation::DeviceAllocationContext::from(self).create_buffer(&desc)?;++ self.counters.buffers.add(1);++ Ok(super::Buffer {+ resource,+ size: desc.size,+ allocation,+ })+ }++ unsafe fn destroy_buffer(&self, buffer: super::Buffer) {+ suballocation::DeviceAllocationContext::from(self)+ .free_resource(buffer.resource, buffer.allocation);++ self.counters.buffers.sub(1);+ }++ unsafe fn add_raw_buffer(&self, _buffer: &super::Buffer) {+ self.counters.buffers.add(1);+ }++ unsafe fn map_buffer(+ &self,+ buffer: &super::Buffer,+ range: crate::MemoryRange,+ ) -> Result<crate::BufferMapping, crate::DeviceError> {+ let mut ptr = ptr::null_mut();+ // TODO: 0 for subresource should be fine here until map and unmap buffer is subresource aware?+ unsafe { buffer.resource.Map(0, None, Some(&mut ptr)) }.into_device_result("Map buffer")?;++ Ok(crate::BufferMapping {+ ptr: ptr::NonNull::new(unsafe { ptr.offset(range.start as isize).cast::<u8>() })+ .unwrap(),+ //TODO: double-check this. Documentation is a bit misleading -+ // it implies that Map/Unmap is needed to invalidate/flush memory.+ is_coherent: true,+ })+ }++ unsafe fn unmap_buffer(&self, buffer: &super::Buffer) {+ unsafe { buffer.resource.Unmap(0, None) };+ }++ unsafe fn flush_mapped_ranges<I>(&self, _buffer: &super::Buffer, _ranges: I) {}+ unsafe fn invalidate_mapped_ranges<I>(&self, _buffer: &super::Buffer, _ranges: I) {}++ unsafe fn create_texture(+ &self,+ desc: &crate::TextureDescriptor,+ ) -> Result<super::Texture, crate::DeviceError> {+ let raw_desc = Direct3D12::D3D12_RESOURCE_DESC {+ Dimension: conv::map_texture_dimension(desc.dimension),+ Alignment: 0,+ Width: desc.size.width as u64,+ Height: desc.size.height,+ DepthOrArraySize: desc.size.depth_or_array_layers as u16,+ MipLevels: desc.mip_level_count as u16,+ Format: auxil::dxgi::conv::map_texture_format_for_resource(+ desc.format,+ desc.usage,+ !desc.view_formats.is_empty(),+ self.shared+ .private_caps+ .casting_fully_typed_format_supported,+ ),+ SampleDesc: Dxgi::Common::DXGI_SAMPLE_DESC {+ Count: desc.sample_count,+ Quality: 0,+ },+ Layout: Direct3D12::D3D12_TEXTURE_LAYOUT_UNKNOWN,+ Flags: conv::map_texture_usage_to_resource_flags(desc.usage),+ };++ let (resource, allocation) =+ suballocation::DeviceAllocationContext::from(self).create_texture(desc, raw_desc)?;++ self.counters.textures.add(1);++ Ok(super::Texture {+ resource,+ format: desc.format,+ dimension: desc.dimension,+ size: desc.size,+ mip_level_count: desc.mip_level_count,+ sample_count: desc.sample_count,+ allocation,+ plane_slice_override: None,+ })+ }++ unsafe fn destroy_texture(&self, texture: super::Texture) {+ suballocation::DeviceAllocationContext::from(self)+ .free_resource(texture.resource, texture.allocation);++ self.counters.textures.sub(1);+ }++ unsafe fn add_raw_texture(&self, _texture: &super::Texture) {+ self.counters.textures.add(1);+ }++ unsafe fn create_texture_view(+ &self,+ texture: &super::Texture,+ desc: &crate::TextureViewDescriptor,+ ) -> Result<super::TextureView, crate::DeviceError> {+ let view_desc = desc.to_internal(texture);++ self.counters.texture_views.add(1);++ Ok(super::TextureView {+ raw_format: view_desc.rtv_dsv_format,+ aspects: view_desc.aspects,+ dimension: desc.dimension,+ texture: texture.resource.clone(),+ subresource_index: texture.calc_subresource(+ desc.range.base_mip_level,+ desc.range.base_array_layer,+ view_desc.plane_slice(),+ ),+ mip_slice: desc.range.base_mip_level,+ handle_srv: if desc.usage.intersects(wgt::TextureUses::RESOURCE) {+ match unsafe { view_desc.to_srv() } {+ Some(raw_desc) => {+ let handle = self.srv_uav_pool.lock().alloc_handle()?;+ unsafe {+ self.raw.CreateShaderResourceView(+ &texture.resource,+ Some(&raw_desc),+ handle.raw,+ )+ };+ Some(handle)+ }+ None => None,+ }+ } else {+ None+ },+ handle_uav: if desc.usage.intersects(+ wgt::TextureUses::STORAGE_READ_ONLY+ | wgt::TextureUses::STORAGE_WRITE_ONLY+ | wgt::TextureUses::STORAGE_READ_WRITE,+ ) {+ match unsafe { view_desc.to_uav() } {+ Some(raw_desc) => {+ let handle = self.srv_uav_pool.lock().alloc_handle()?;+ unsafe {+ self.raw.CreateUnorderedAccessView(+ &texture.resource,+ None,+ Some(&raw_desc),+ handle.raw,+ );+ }+ Some(handle)+ }+ None => None,+ }+ } else {+ None+ },+ handle_rtv: if desc.usage.intersects(wgt::TextureUses::COLOR_TARGET)+ && desc.dimension != wgt::TextureViewDimension::D3+ // 3D RTVs must be created in the render pass+ {+ let raw_desc = unsafe { view_desc.to_rtv() };+ let handle = self.rtv_pool.lock().alloc_handle()?;+ unsafe {+ self.raw+ .CreateRenderTargetView(&texture.resource, Some(&raw_desc), handle.raw)+ };+ Some(handle)+ } else {+ None+ },+ handle_dsv_ro: if desc.usage.intersects(wgt::TextureUses::DEPTH_STENCIL_READ) {+ let raw_desc = unsafe { view_desc.to_dsv(true) };+ let handle = self.dsv_pool.lock().alloc_handle()?;+ unsafe {+ self.raw+ .CreateDepthStencilView(&texture.resource, Some(&raw_desc), handle.raw)+ };+ Some(handle)+ } else {+ None+ },+ handle_dsv_rw: if desc.usage.intersects(wgt::TextureUses::DEPTH_STENCIL_WRITE) {+ let raw_desc = unsafe { view_desc.to_dsv(false) };+ let handle = self.dsv_pool.lock().alloc_handle()?;+ unsafe {+ self.raw+ .CreateDepthStencilView(&texture.resource, Some(&raw_desc), handle.raw)+ };+ Some(handle)+ } else {+ None+ },+ })+ }++ unsafe fn destroy_texture_view(&self, view: super::TextureView) {+ if view.handle_srv.is_some() || view.handle_uav.is_some() {+ let mut pool = self.srv_uav_pool.lock();+ if let Some(handle) = view.handle_srv {+ pool.free_handle(handle);+ }+ if let Some(handle) = view.handle_uav {+ pool.free_handle(handle);+ }+ }+ if let Some(handle) = view.handle_rtv {+ self.rtv_pool.lock().free_handle(handle);+ }+ if view.handle_dsv_ro.is_some() || view.handle_dsv_rw.is_some() {+ let mut pool = self.dsv_pool.lock();+ if let Some(handle) = view.handle_dsv_ro {+ pool.free_handle(handle);+ }+ if let Some(handle) = view.handle_dsv_rw {+ pool.free_handle(handle);+ }+ }++ self.counters.texture_views.sub(1);+ }++ unsafe fn create_sampler(+ &self,+ desc: &crate::SamplerDescriptor,+ ) -> Result<super::Sampler, crate::DeviceError> {+ let reduction = match desc.compare {+ Some(_) => Direct3D12::D3D12_FILTER_REDUCTION_TYPE_COMPARISON,+ None => Direct3D12::D3D12_FILTER_REDUCTION_TYPE_STANDARD,+ };+ let mut filter = Direct3D12::D3D12_FILTER(+ (conv::map_filter_mode(desc.min_filter).0 << Direct3D12::D3D12_MIN_FILTER_SHIFT)+ | (conv::map_filter_mode(desc.mag_filter).0 << Direct3D12::D3D12_MAG_FILTER_SHIFT)+ | (conv::map_mipmap_filter_mode(desc.mipmap_filter).0+ << Direct3D12::D3D12_MIP_FILTER_SHIFT)+ | (reduction.0 << Direct3D12::D3D12_FILTER_REDUCTION_TYPE_SHIFT),+ );++ if desc.anisotropy_clamp != 1 {+ filter.0 |= Direct3D12::D3D12_FILTER_ANISOTROPIC.0;+ };++ let border_color = conv::map_border_color(desc.border_color);++ let raw_desc = Direct3D12::D3D12_SAMPLER_DESC {+ Filter: filter,+ AddressU: conv::map_address_mode(desc.address_modes[0]),+ AddressV: conv::map_address_mode(desc.address_modes[1]),+ AddressW: conv::map_address_mode(desc.address_modes[2]),+ MipLODBias: 0f32,+ MaxAnisotropy: desc.anisotropy_clamp as u32,++ ComparisonFunc: conv::map_comparison(desc.compare.unwrap_or_default()),+ BorderColor: border_color,+ MinLOD: desc.lod_clamp.start,+ MaxLOD: desc.lod_clamp.end,+ };++ let index = self+ .shared+ .sampler_heap+ .create_sampler(&self.raw, raw_desc)?;++ self.counters.samplers.add(1);++ Ok(super::Sampler {+ index,+ desc: raw_desc,+ })+ }++ unsafe fn destroy_sampler(&self, sampler: super::Sampler) {+ self.shared+ .sampler_heap+ .destroy_sampler(sampler.desc, sampler.index);+ self.counters.samplers.sub(1);+ }++ unsafe fn create_command_encoder(+ &self,+ desc: &crate::CommandEncoderDescriptor<super::Queue>,+ ) -> Result<super::CommandEncoder, crate::DeviceError> {+ let allocator: Direct3D12::ID3D12CommandAllocator = unsafe {+ self.raw+ .CreateCommandAllocator(Direct3D12::D3D12_COMMAND_LIST_TYPE_DIRECT)+ }+ .into_device_result("Command allocator creation")?;++ if let Some(label) = desc.label {+ allocator.set_name(label)?;+ }++ self.counters.command_encoders.add(1);++ Ok(super::CommandEncoder {+ allocator,+ device: self.raw.clone(),+ shared: Arc::clone(&self.shared),+ mem_allocator: self.mem_allocator.clone(),+ rtv_pool: Arc::clone(&self.rtv_pool),+ temp_rtv_handles: Vec::new(),+ intermediate_copy_bufs: Vec::new(),+ null_rtv_handle: self.null_rtv_handle,+ list: None,+ free_lists: Vec::new(),+ pass: super::PassState::new(),+ temp: super::Temp::default(),+ end_of_pass_timer_query: None,+ counters: Arc::clone(&self.counters),+ })+ }++ unsafe fn create_bind_group_layout(+ &self,+ desc: &crate::BindGroupLayoutDescriptor,+ ) -> Result<super::BindGroupLayout, crate::DeviceError> {+ let mut num_views = 0;+ let mut has_sampler_in_group = false;+ for entry in desc.entries.iter() {+ let count = entry.count.map_or(1, NonZeroU32::get);+ match entry.ty {+ wgt::BindingType::Buffer {+ ty: wgt::BufferBindingType::Uniform,+ has_dynamic_offset: true,+ ..+ } => {}+ wgt::BindingType::Buffer { .. }+ | wgt::BindingType::Texture { .. }+ | wgt::BindingType::StorageTexture { .. }+ | wgt::BindingType::AccelerationStructure { .. } => num_views += count,+ wgt::BindingType::Sampler { .. } => has_sampler_in_group = true,+ // Three texture planes and one params buffer+ wgt::BindingType::ExternalTexture => num_views += 4 * count,+ }+ }++ if has_sampler_in_group {+ num_views += 1;+ }++ self.counters.bind_group_layouts.add(1);++ Ok(super::BindGroupLayout {+ entries: desc.entries.to_vec(),+ cpu_heap_views: if num_views != 0 {+ let heap = descriptor::CpuHeap::new(+ &self.raw,+ Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,+ num_views,+ )?;+ Some(heap)+ } else {+ None+ },+ copy_counts: vec![1; num_views as usize],+ })+ }++ unsafe fn destroy_bind_group_layout(&self, _bg_layout: super::BindGroupLayout) {+ self.counters.bind_group_layouts.sub(1);+ }++ unsafe fn create_pipeline_layout(+ &self,+ desc: &crate::PipelineLayoutDescriptor<super::BindGroupLayout>,+ ) -> Result<super::PipelineLayout, crate::DeviceError> {+ use naga::back::hlsl;+ // Pipeline layouts are implemented as RootSignature for D3D12.+ //+ // Immediates are implemented as root constants.+ //+ // Each bind group layout might use one SRV/CBV/UAV descriptor table.+ // With resources in the bind group layout using:+ // - 1 CBV per non-dynamic uniform buffer+ // - 1 SRV per acceleration structure+ // - 1 SRV for all samplers in a bind group+ // - 1 SRV per texture+ // - 1 SRV per read-only storage buffer+ // - 1 UAV per storage texture+ // - 1 UAV per read-write storage buffer+ // - 3 SRVs & 1 CBV per external texture+ //+ // Each dynamic uniform buffer takes up a CBV root descriptor.+ // This is easier than trying to patch up the offset on the shader side.+ //+ // Each dynamic storage buffer is an SRV or UAV in the descriptor table+ // and its dynamic offsets are passed via root constants.+ //+ // All samplers go into a single sampler descriptor table.+ //+ // 3 additional root constants are used to populate built-in (shader) inputs.+ //+ // Root signature layout:+ // Root Constants: Parameter=0, Space=0+ // ...+ // (bind group [0]) - Space=0+ // View descriptor table, if any+ // Sampler buffer descriptor table, if any+ // Root descriptors (for dynamic offset buffers)+ // (bind group [1]) - Space=0+ // ...+ // (bind group [2]) - Space=0+ // Special constant buffer: Space=0+ // Sampler descriptor tables: Space=0+ // SamplerState Array: Space=0, Register=0-2047+ // SamplerComparisonState Array: Space=0, Register=2048-4095++ //TODO: put lower bind group indices further down the root signature. See:+ // https://microsoft.github.io/DirectX-Specs/d3d/ResourceBinding.html#binding-model+ // Currently impossible because wgpu-core only re-binds the descriptor sets based+ // on Vulkan-like layout compatibility rules.++ let mut binding_map = hlsl::BindingMap::default();+ let mut sampler_buffer_binding_map = hlsl::SamplerIndexBufferBindingMap::default();+ let mut external_texture_binding_map = hlsl::ExternalTextureBindingMap::default();+ let mut bind_cbv = hlsl::BindTarget::default();+ let mut bind_srv = hlsl::BindTarget::default();+ let mut bind_uav = hlsl::BindTarget::default();+ let mut parameters = Vec::new();+ let mut immediates_target = None;+ let mut immediates_info = None;++ if desc.immediate_size != 0 {+ let parameter_index = parameters.len();+ let size = desc.immediate_size / 4;+ parameters.push(Direct3D12::D3D12_ROOT_PARAMETER {+ ParameterType: Direct3D12::D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS,+ Anonymous: Direct3D12::D3D12_ROOT_PARAMETER_0 {+ Constants: Direct3D12::D3D12_ROOT_CONSTANTS {+ ShaderRegister: bind_cbv.register,+ RegisterSpace: bind_cbv.space as u32,+ Num32BitValues: size,+ },+ },+ ShaderVisibility: Direct3D12::D3D12_SHADER_VISIBILITY_ALL,+ });+ let binding = bind_cbv;+ bind_cbv.register += 1;+ immediates_info = Some(super::ImmediatesInfo {+ root_index: parameter_index as u32,+ size,+ });+ immediates_target = Some(binding);++ bind_cbv.space += 1;+ }++ let mut dynamic_storage_buffer_offsets_targets = alloc::collections::BTreeMap::new();+ let mut total_dynamic_storage_buffers = 0;++ // Collect the whole number of bindings we will create upfront.+ // It allows us to preallocate enough storage to avoid reallocation,+ // which could cause invalid pointers.+ let mut total_non_dynamic_entries = 0_usize;+ let mut sampler_in_any_bind_group = false;+ for bgl in desc.bind_group_layouts {+ let Some(bgl) = bgl else {+ continue;+ };++ let mut sampler_in_bind_group = false;++ for entry in &bgl.entries {+ match entry.ty {+ wgt::BindingType::Buffer {+ ty: wgt::BufferBindingType::Uniform,+ has_dynamic_offset: true,+ ..+ } => {}+ wgt::BindingType::Sampler(_) => sampler_in_bind_group = true,+ // Three texture planes and one params buffer+ wgt::BindingType::ExternalTexture => total_non_dynamic_entries += 4,+ _ => total_non_dynamic_entries += 1,+ }+ }++ if sampler_in_bind_group {+ // One for the sampler buffer+ total_non_dynamic_entries += 1;+ sampler_in_any_bind_group = true;+ }+ }++ if sampler_in_any_bind_group {+ // Two for the sampler arrays themselves+ total_non_dynamic_entries += 2;+ }++ let mut ranges = Vec::with_capacity(total_non_dynamic_entries);++ let mut bind_group_infos = [const { None }; crate::MAX_BIND_GROUPS];+ for (index, bgl) in desc.bind_group_layouts.iter().enumerate() {+ let Some(bgl) = bgl else {+ continue;+ };++ let mut info = super::BindGroupInfo {+ tables: super::TableTypes::empty(),+ base_root_index: parameters.len() as u32,+ dynamic_storage_buffer_offsets: None,+ };++ let mut visibility_view_static = wgt::ShaderStages::empty();+ let mut visibility_view_dynamic_uniform = wgt::ShaderStages::empty();+ let mut visibility_view_dynamic_storage = wgt::ShaderStages::empty();+ for entry in bgl.entries.iter() {+ match entry.ty {+ wgt::BindingType::Sampler { .. } => {+ visibility_view_static |= wgt::ShaderStages::all()+ }+ wgt::BindingType::Buffer {+ ty: wgt::BufferBindingType::Uniform,+ has_dynamic_offset: true,+ ..+ } => visibility_view_dynamic_uniform |= entry.visibility,+ wgt::BindingType::Buffer {+ ty: wgt::BufferBindingType::Storage { .. },+ has_dynamic_offset: true,+ ..+ } => visibility_view_dynamic_storage |= entry.visibility,+ _ => visibility_view_static |= entry.visibility,+ }+ }++ let mut dynamic_storage_buffers = 0;++ // SRV/CBV/UAV descriptor tables+ let range_base = ranges.len();+ for entry in bgl.entries.iter() {+ let count = entry.count.map_or(1, NonZeroU32::get);+ if let wgt::BindingType::ExternalTexture = entry.ty {+ // External textures need 3 SRVs (a texture for each plane)+ // and 1 CBV for the parameters buffer.+ let bind_target = hlsl::ExternalTextureBindTarget {+ planes: core::array::from_fn(|_| hlsl::BindTarget {+ register: {+ let register = bind_srv.register;+ bind_srv.register += count;+ register+ },+ ..bind_srv+ }),+ params: hlsl::BindTarget {+ register: {+ let register = bind_cbv.register;+ bind_cbv.register += count;+ register+ },+ ..bind_cbv+ },+ };+ external_texture_binding_map.insert(+ naga::ResourceBinding {+ group: index as u32,+ binding: entry.binding,+ },+ bind_target,+ );+ for bt in bind_target.planes {+ ranges.push(Direct3D12::D3D12_DESCRIPTOR_RANGE {+ RangeType: Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SRV,+ NumDescriptors: count,+ BaseShaderRegister: bt.register,+ RegisterSpace: bt.space as u32,+ OffsetInDescriptorsFromTableStart:+ Direct3D12::D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND,+ });+ }+ ranges.push(Direct3D12::D3D12_DESCRIPTOR_RANGE {+ RangeType: Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_CBV,+ NumDescriptors: count,+ BaseShaderRegister: bind_target.params.register,+ RegisterSpace: bind_target.params.space as u32,+ OffsetInDescriptorsFromTableStart:+ Direct3D12::D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND,+ });+ } else {+ let (range_ty, has_dynamic_offset) = match entry.ty {+ wgt::BindingType::Buffer {+ ty,+ has_dynamic_offset: true,+ ..+ } => match ty {+ wgt::BufferBindingType::Uniform => continue,+ wgt::BufferBindingType::Storage { .. } => {+ (conv::map_binding_type(&entry.ty), true)+ }+ },+ ref other => (conv::map_binding_type(other), false),+ };+ let bt = match range_ty {+ Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_CBV => &mut bind_cbv,+ Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SRV => &mut bind_srv,+ Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_UAV => &mut bind_uav,+ Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER => continue,+ _ => todo!(),+ };++ let binding_array_size = entry.count.map(NonZeroU32::get);++ let dynamic_storage_buffer_offsets_index = if has_dynamic_offset {+ debug_assert!(+ binding_array_size.is_none(),+ "binding arrays and dynamic buffers are mutually exclusive"+ );+ let ret = Some(dynamic_storage_buffers);+ dynamic_storage_buffers += 1;+ ret+ } else {+ None+ };++ binding_map.insert(+ naga::ResourceBinding {+ group: index as u32,+ binding: entry.binding,+ },+ hlsl::BindTarget {+ binding_array_size,+ dynamic_storage_buffer_offsets_index,+ ..*bt+ },+ );+ ranges.push(Direct3D12::D3D12_DESCRIPTOR_RANGE {+ RangeType: range_ty,+ NumDescriptors: count,+ BaseShaderRegister: bt.register,+ RegisterSpace: bt.space as u32,+ OffsetInDescriptorsFromTableStart:+ Direct3D12::D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND,+ });+ bt.register += count;+ }+ }++ let mut sampler_index_within_bind_group = 0;+ for entry in bgl.entries.iter() {+ if let wgt::BindingType::Sampler(_) = entry.ty {+ binding_map.insert(+ naga::ResourceBinding {+ group: index as u32,+ binding: entry.binding,+ },+ hlsl::BindTarget {+ // Naga does not use the space field for samplers+ space: 255,+ register: sampler_index_within_bind_group,+ binding_array_size: None,+ dynamic_storage_buffer_offsets_index: None,+ restrict_indexing: false,+ },+ );+ sampler_index_within_bind_group += 1;+ }+ }++ if sampler_index_within_bind_group != 0 {+ sampler_buffer_binding_map.insert(+ hlsl::SamplerIndexBufferKey {+ group: index as u32,+ },+ bind_srv,+ );+ ranges.push(Direct3D12::D3D12_DESCRIPTOR_RANGE {+ RangeType: Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SRV,+ NumDescriptors: 1,+ BaseShaderRegister: bind_srv.register,+ RegisterSpace: bind_srv.space as u32,+ OffsetInDescriptorsFromTableStart:+ Direct3D12::D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND,+ });+ bind_srv.register += 1;+ }++ if ranges.len() > range_base {+ let range = &ranges[range_base..];+ parameters.push(Direct3D12::D3D12_ROOT_PARAMETER {+ ParameterType: Direct3D12::D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE,+ Anonymous: Direct3D12::D3D12_ROOT_PARAMETER_0 {+ DescriptorTable: Direct3D12::D3D12_ROOT_DESCRIPTOR_TABLE {+ NumDescriptorRanges: range.len() as u32,+ pDescriptorRanges: range.as_ptr(),+ },+ },+ ShaderVisibility: conv::map_visibility(visibility_view_static),+ });+ info.tables |= super::TableTypes::SRV_CBV_UAV;+ }++ // Root descriptors for dynamic uniform buffers+ let dynamic_buffers_visibility = conv::map_visibility(visibility_view_dynamic_uniform);+ for entry in bgl.entries.iter() {+ match entry.ty {+ wgt::BindingType::Buffer {+ ty: wgt::BufferBindingType::Uniform,+ has_dynamic_offset: true,+ ..+ } => {}+ _ => continue,+ };++ binding_map.insert(+ naga::ResourceBinding {+ group: index as u32,+ binding: entry.binding,+ },+ hlsl::BindTarget {+ binding_array_size: entry.count.map(NonZeroU32::get),+ restrict_indexing: true,+ ..bind_cbv+ },+ );++ parameters.push(Direct3D12::D3D12_ROOT_PARAMETER {+ ParameterType: Direct3D12::D3D12_ROOT_PARAMETER_TYPE_CBV,+ Anonymous: Direct3D12::D3D12_ROOT_PARAMETER_0 {+ Descriptor: Direct3D12::D3D12_ROOT_DESCRIPTOR {+ ShaderRegister: bind_cbv.register,+ RegisterSpace: bind_cbv.space as u32,+ },+ },+ ShaderVisibility: dynamic_buffers_visibility,+ });++ bind_cbv.register += entry.count.map_or(1, NonZeroU32::get);+ }++ // Root constants for (offsets of) dynamic storage buffers+ if dynamic_storage_buffers > 0 {+ let parameter_index = parameters.len();++ parameters.push(Direct3D12::D3D12_ROOT_PARAMETER {+ ParameterType: Direct3D12::D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS,+ Anonymous: Direct3D12::D3D12_ROOT_PARAMETER_0 {+ Constants: Direct3D12::D3D12_ROOT_CONSTANTS {+ ShaderRegister: bind_cbv.register,+ RegisterSpace: bind_cbv.space as u32,+ Num32BitValues: dynamic_storage_buffers,+ },+ },+ ShaderVisibility: conv::map_visibility(visibility_view_dynamic_storage),+ });++ let binding = hlsl::OffsetsBindTarget {+ space: bind_cbv.space,+ register: bind_cbv.register,+ size: dynamic_storage_buffers,+ };++ bind_cbv.register += 1;++ dynamic_storage_buffer_offsets_targets.insert(index as u32, binding);+ info.dynamic_storage_buffer_offsets = Some(DynamicStorageBufferOffsets {+ root_index: parameter_index as u32,+ range: total_dynamic_storage_buffers as usize+ ..total_dynamic_storage_buffers as usize + dynamic_storage_buffers as usize,+ });+ total_dynamic_storage_buffers += dynamic_storage_buffers;+ }++ bind_group_infos[index] = Some(info);+ }++ let sampler_heap_target = hlsl::SamplerHeapBindTargets {+ standard_samplers: hlsl::BindTarget {+ space: 0,+ register: 0,+ binding_array_size: None,+ dynamic_storage_buffer_offsets_index: None,+ restrict_indexing: false,+ },+ comparison_samplers: hlsl::BindTarget {+ space: 0,+ register: 2048,+ binding_array_size: None,+ dynamic_storage_buffer_offsets_index: None,+ restrict_indexing: false,+ },+ };++ let mut sampler_heap_root_index = None;+ if sampler_in_any_bind_group {+ // Sampler descriptor tables+ //+ // We bind two sampler ranges pointing to the same descriptor heap, using two different register ranges.+ //+ // We bind them as normal samplers in registers 0-2047 and comparison samplers in registers 2048-4095.+ // Tier 2 hardware guarantees that the type of sampler only needs to match if the sampler is actually+ // accessed in the shader. As such, we can bind the same array of samplers to both registers.+ //+ // We do this because HLSL does not allow you to alias registers at all.+ let range_base = ranges.len();+ // Standard samplers, registers 0-2047+ ranges.push(Direct3D12::D3D12_DESCRIPTOR_RANGE {+ RangeType: Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER,+ NumDescriptors: 2048,+ BaseShaderRegister: 0,+ RegisterSpace: 0,+ OffsetInDescriptorsFromTableStart: 0,+ });+ // Comparison samplers, registers 2048-4095+ ranges.push(Direct3D12::D3D12_DESCRIPTOR_RANGE {+ RangeType: Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER,+ NumDescriptors: 2048,+ BaseShaderRegister: 2048,+ RegisterSpace: 0,+ OffsetInDescriptorsFromTableStart: 0,+ });++ let range = &ranges[range_base..];+ sampler_heap_root_index = Some(parameters.len() as super::RootIndex);+ parameters.push(Direct3D12::D3D12_ROOT_PARAMETER {+ ParameterType: Direct3D12::D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE,+ Anonymous: Direct3D12::D3D12_ROOT_PARAMETER_0 {+ DescriptorTable: Direct3D12::D3D12_ROOT_DESCRIPTOR_TABLE {+ NumDescriptorRanges: range.len() as u32,+ pDescriptorRanges: range.as_ptr(),+ },+ },+ ShaderVisibility: Direct3D12::D3D12_SHADER_VISIBILITY_ALL,+ });+ }++ // Ensure that we didn't reallocate!+ debug_assert_eq!(ranges.len(), total_non_dynamic_entries);++ let (special_constants_root_index, special_constants_binding) = if desc.flags.intersects(+ crate::PipelineLayoutFlags::FIRST_VERTEX_INSTANCE+ | crate::PipelineLayoutFlags::NUM_WORK_GROUPS,+ ) {+ let parameter_index = parameters.len();+ parameters.push(Direct3D12::D3D12_ROOT_PARAMETER {+ ParameterType: Direct3D12::D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS,+ Anonymous: Direct3D12::D3D12_ROOT_PARAMETER_0 {+ Constants: Direct3D12::D3D12_ROOT_CONSTANTS {+ ShaderRegister: bind_cbv.register,+ RegisterSpace: bind_cbv.space as u32,+ Num32BitValues: 3, // 0 = first_vertex, 1 = first_instance, 2 = other+ },+ },+ ShaderVisibility: Direct3D12::D3D12_SHADER_VISIBILITY_ALL, // really needed for VS and CS only,+ });+ let binding = bind_cbv;+ // This is the last time we use this, but lets increment+ // it so if we add more later, the value behaves correctly.++ // This is an allow as it doesn't trigger on 1.90, hal's MSRV.+ #[allow(unused_assignments)]+ {+ bind_cbv.register += 1;+ }+ (Some(parameter_index as u32), Some(binding))+ } else {+ (None, None)+ };++ let blob = self.library.serialize_root_signature(+ Direct3D12::D3D_ROOT_SIGNATURE_VERSION_1_0,+ ¶meters,+ &[],+ Direct3D12::D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT,+ )?;++ let raw = unsafe {+ self.raw+ .CreateRootSignature::<Direct3D12::ID3D12RootSignature>(0, blob.as_slice())+ }+ .into_device_result("Root signature creation")?;++ let special_constants = if let Some(root_index) = special_constants_root_index {+ let cmd_signatures = if desc+ .flags+ .contains(crate::PipelineLayoutFlags::INDIRECT_BUILTIN_UPDATE)+ {+ let constant_indirect_argument_desc = Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC {+ Type: Direct3D12::D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT,+ Anonymous: Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC_0 {+ Constant: Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC_0_1 {+ RootParameterIndex: root_index,+ DestOffsetIn32BitValues: 0,+ Num32BitValuesToSet: 3,+ },+ },+ };+ let special_constant_buffer_args_len = size_of::<super::SpecialConstants>();++ Some(super::CommandSignatures {+ draw: Self::create_command_signature(+ &self.raw,+ Some(&raw),+ special_constant_buffer_args_len + size_of::<wgt::DrawIndirectArgs>(),+ &[+ constant_indirect_argument_desc,+ Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC {+ Type: Direct3D12::D3D12_INDIRECT_ARGUMENT_TYPE_DRAW,+ ..Default::default()+ },+ ],+ 0,+ )?,+ draw_indexed: Self::create_command_signature(+ &self.raw,+ Some(&raw),+ special_constant_buffer_args_len+ + size_of::<wgt::DrawIndexedIndirectArgs>(),+ &[+ constant_indirect_argument_desc,+ Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC {+ Type: Direct3D12::D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED,+ ..Default::default()+ },+ ],+ 0,+ )?,+ // Mesh pipelines don't support updating special constants.+ draw_mesh: None,+ dispatch: Self::create_command_signature(+ &self.raw,+ Some(&raw),+ special_constant_buffer_args_len + size_of::<wgt::DispatchIndirectArgs>(),+ &[+ constant_indirect_argument_desc,+ Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC {+ Type: Direct3D12::D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH,+ ..Default::default()+ },+ ],+ 0,+ )?,+ })+ } else {+ None+ };+ Some(super::PipelineLayoutSpecialConstants {+ root_index,+ indirect_cmd_signatures: cmd_signatures,+ })+ } else {+ None+ };++ if let Some(label) = desc.label {+ raw.set_name(label)?;+ }++ self.counters.pipeline_layouts.add(1);++ Ok(super::PipelineLayout {+ shared: super::PipelineLayoutShared {+ signature: Some(raw),+ total_root_elements: parameters.len() as super::RootIndex,+ special_constants,+ immediates_info,+ sampler_heap_root_index,+ },+ bind_group_infos,+ naga_options: hlsl::Options {+ shader_model: self.shared.private_caps.shader_model,+ binding_map,+ fake_missing_bindings: false,+ special_constants_binding,+ immediates_target,+ dynamic_storage_buffer_offsets_targets,+ zero_initialize_workgroup_memory: true,+ restrict_indexing: true,+ sampler_heap_target,+ sampler_buffer_binding_map,+ external_texture_binding_map,+ force_loop_bounding: true,+ task_dispatch_limits: Some(naga::back::TaskDispatchLimits {+ max_mesh_workgroups_per_dim: self.limits.max_mesh_workgroups_per_dimension,+ max_mesh_workgroups_total: self.limits.max_mesh_workgroup_total_count,+ }),+ mesh_shader_primitive_indices_clamp: true,+ ray_query_initialization_tracking: true,+ },+ })+ }++ unsafe fn destroy_pipeline_layout(&self, _pipeline_layout: super::PipelineLayout) {+ self.counters.pipeline_layouts.sub(1);+ }++ unsafe fn create_bind_group(+ &self,+ desc: &crate::BindGroupDescriptor<+ super::BindGroupLayout,+ super::Buffer,+ super::Sampler,+ super::TextureView,+ super::AccelerationStructure,+ >,+ ) -> Result<super::BindGroup, crate::DeviceError> {+ let mut cpu_views = desc+ .layout+ .cpu_heap_views+ .as_ref()+ .map(|cpu_heap| cpu_heap.inner.lock());+ if let Some(ref mut inner) = cpu_views {+ inner.stage.clear();+ }+ let mut dynamic_buffers = Vec::new();++ let layout_and_entry_iter = desc.entries.iter().map(|entry| {+ let layout = desc+ .layout+ .entries+ .iter()+ .find(|layout_entry| layout_entry.binding == entry.binding)+ .expect("internal error: no layout entry found with binding slot");+ (layout, entry)+ });+ let mut sampler_indexes: Vec<super::sampler::SamplerIndex> = Vec::new();++ for (layout, entry) in layout_and_entry_iter {+ match layout.ty {+ wgt::BindingType::Buffer {+ ty,+ has_dynamic_offset,+ ..+ } => {+ let start = entry.resource_index as usize;+ let end = start + entry.count as usize;+ for data in &desc.buffers[start..end] {+ let gpu_address = data.resolve_address();+ let mut size = data.resolve_size().try_into().unwrap();++ if has_dynamic_offset {+ match ty {+ wgt::BufferBindingType::Uniform => {+ dynamic_buffers.push(super::DynamicBuffer::Uniform(+ Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE {+ ptr: data.resolve_address(),+ },+ ));+ continue;+ }+ wgt::BufferBindingType::Storage { .. } => {+ size = (data.buffer.size - data.offset) as u32;+ dynamic_buffers.push(super::DynamicBuffer::Storage);+ }+ }+ }++ let inner = cpu_views.as_mut().unwrap();+ let cpu_index = inner.stage.len() as u32;+ let handle = desc.layout.cpu_heap_views.as_ref().unwrap().at(cpu_index);+ match ty {+ wgt::BufferBindingType::Uniform => {+ let size_mask =+ Direct3D12::D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT - 1;+ let raw_desc = Direct3D12::D3D12_CONSTANT_BUFFER_VIEW_DESC {+ BufferLocation: gpu_address,+ SizeInBytes: ((size - 1) | size_mask) + 1,+ };+ unsafe {+ self.raw.CreateConstantBufferView(Some(&raw_desc), handle)+ };+ }+ wgt::BufferBindingType::Storage { read_only: true } => {+ let raw_desc = Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC {+ Format: Dxgi::Common::DXGI_FORMAT_R32_TYPELESS,+ Shader4ComponentMapping:+ Direct3D12::D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,+ ViewDimension: Direct3D12::D3D12_SRV_DIMENSION_BUFFER,+ Anonymous: Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC_0 {+ Buffer: Direct3D12::D3D12_BUFFER_SRV {+ FirstElement: data.offset / 4,+ NumElements: size / 4,+ StructureByteStride: 0,+ Flags: Direct3D12::D3D12_BUFFER_SRV_FLAG_RAW,+ },+ },+ };+ unsafe {+ self.raw.CreateShaderResourceView(+ &data.buffer.resource,+ Some(&raw_desc),+ handle,+ )+ };+ }+ wgt::BufferBindingType::Storage { read_only: false } => {+ let raw_desc = Direct3D12::D3D12_UNORDERED_ACCESS_VIEW_DESC {+ Format: Dxgi::Common::DXGI_FORMAT_R32_TYPELESS,+ ViewDimension: Direct3D12::D3D12_UAV_DIMENSION_BUFFER,+ Anonymous: Direct3D12::D3D12_UNORDERED_ACCESS_VIEW_DESC_0 {+ Buffer: Direct3D12::D3D12_BUFFER_UAV {+ FirstElement: data.offset / 4,+ NumElements: size / 4,+ StructureByteStride: 0,+ CounterOffsetInBytes: 0,+ Flags: Direct3D12::D3D12_BUFFER_UAV_FLAG_RAW,+ },+ },+ };+ unsafe {+ self.raw.CreateUnorderedAccessView(+ &data.buffer.resource,+ None,+ Some(&raw_desc),+ handle,+ )+ };+ }+ }+ inner.stage.push(handle);+ }+ }+ wgt::BindingType::Texture { .. } => {+ let start = entry.resource_index as usize;+ let end = start + entry.count as usize;+ for data in &desc.textures[start..end] {+ let handle = data.view.handle_srv.unwrap();+ cpu_views.as_mut().unwrap().stage.push(handle.raw);+ }+ // The table range reserves the full `layout.count`, so pad a+ // partially-bound array with null SRVs to keep later bindings aligned.+ let array_size = layout.count.map_or(1, NonZeroU32::get);+ for _ in entry.count..array_size {+ let inner = cpu_views.as_mut().unwrap();+ let cpu_index = inner.stage.len() as u32;+ let handle = desc.layout.cpu_heap_views.as_ref().unwrap().at(cpu_index);+ let raw_desc = Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC {+ Format: Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM,+ ViewDimension: Direct3D12::D3D12_SRV_DIMENSION_TEXTURE2D,+ Shader4ComponentMapping:+ Direct3D12::D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,+ Anonymous: Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC_0 {+ Texture2D: Direct3D12::D3D12_TEX2D_SRV {+ MostDetailedMip: 0,+ MipLevels: 1,+ PlaneSlice: 0,+ ResourceMinLODClamp: 0.0,+ },+ },+ };+ unsafe {+ self.raw+ .CreateShaderResourceView(None, Some(&raw_desc), handle)+ };+ inner.stage.push(handle);+ }+ }+ wgt::BindingType::StorageTexture { .. } => {+ let start = entry.resource_index as usize;+ let end = start + entry.count as usize;+ for data in &desc.textures[start..end] {+ let handle = data.view.handle_uav.unwrap();+ cpu_views.as_mut().unwrap().stage.push(handle.raw);+ }+ // Same partial-binding-array padding as the `Texture` branch above, but+ // with null UAVs so that bindings following the array keep their offsets.+ let array_size = layout.count.map_or(1, NonZeroU32::get);+ for _ in entry.count..array_size {+ let inner = cpu_views.as_mut().unwrap();+ let cpu_index = inner.stage.len() as u32;+ let handle = desc.layout.cpu_heap_views.as_ref().unwrap().at(cpu_index);+ let raw_desc = Direct3D12::D3D12_UNORDERED_ACCESS_VIEW_DESC {+ Format: Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM,+ ViewDimension: Direct3D12::D3D12_UAV_DIMENSION_TEXTURE2D,+ Anonymous: Direct3D12::D3D12_UNORDERED_ACCESS_VIEW_DESC_0 {+ Texture2D: Direct3D12::D3D12_TEX2D_UAV {+ MipSlice: 0,+ PlaneSlice: 0,+ },+ },+ };+ unsafe {+ self.raw+ .CreateUnorderedAccessView(None, None, Some(&raw_desc), handle)+ };+ inner.stage.push(handle);+ }+ }+ wgt::BindingType::Sampler { .. } => {+ let start = entry.resource_index as usize;+ let end = start + entry.count as usize;+ for &data in &desc.samplers[start..end] {+ sampler_indexes.push(data.index);+ }+ }+ wgt::BindingType::AccelerationStructure { .. } => {+ let start = entry.resource_index as usize;+ let end = start + entry.count as usize;+ for data in &desc.acceleration_structures[start..end] {+ let inner = cpu_views.as_mut().unwrap();+ let cpu_index = inner.stage.len() as u32;+ let handle = desc.layout.cpu_heap_views.as_ref().unwrap().at(cpu_index);+ let raw_desc = Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC {+ Format: Dxgi::Common::DXGI_FORMAT_UNKNOWN,+ Shader4ComponentMapping:+ Direct3D12::D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,+ ViewDimension:+ Direct3D12::D3D12_SRV_DIMENSION_RAYTRACING_ACCELERATION_STRUCTURE,+ Anonymous: Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC_0 {+ RaytracingAccelerationStructure:+ Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_SRV {+ Location: unsafe { data.resource.GetGPUVirtualAddress() },+ },+ },+ };+ unsafe {+ self.raw+ .CreateShaderResourceView(None, Some(&raw_desc), handle)+ };+ inner.stage.push(handle);+ }+ }+ wgt::BindingType::ExternalTexture => {+ // We don't yet support binding arrays of external textures.+ // https://github.com/gfx-rs/wgpu/issues/8027+ assert_eq!(entry.count, 1);+ let external_texture = &desc.external_textures[entry.resource_index as usize];+ for plane in &external_texture.planes {+ let plane_handle = plane.view.handle_srv.unwrap();+ cpu_views.as_mut().unwrap().stage.push(plane_handle.raw);+ }+ let gpu_address = external_texture.params.resolve_address();+ let size = external_texture.params.resolve_size() as u32;+ let inner = cpu_views.as_mut().unwrap();+ let cpu_index = inner.stage.len() as u32;+ let params_handle = desc.layout.cpu_heap_views.as_ref().unwrap().at(cpu_index);+ let size_mask = Direct3D12::D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT - 1;+ let raw_desc = Direct3D12::D3D12_CONSTANT_BUFFER_VIEW_DESC {+ BufferLocation: gpu_address,+ SizeInBytes: ((size - 1) | size_mask) + 1,+ };+ unsafe {+ self.raw+ .CreateConstantBufferView(Some(&raw_desc), params_handle)+ };+ inner.stage.push(params_handle);+ }+ }+ }++ let sampler_index_buffer = if !sampler_indexes.is_empty() {+ let buffer_size = (sampler_indexes.len() * size_of::<u32>()) as u64;++ let label = if let Some(label) = desc.label {+ Cow::Owned(format!("{label} (Internal Sampler Index Buffer)"))+ } else {+ Cow::Borrowed("Internal Sampler Index Buffer")+ };++ let buffer_desc = crate::BufferDescriptor {+ label: Some(&label),+ size: buffer_size,+ usage: wgt::BufferUses::STORAGE_READ_ONLY | wgt::BufferUses::MAP_WRITE,+ // D3D12 backend doesn't care about the memory flags+ memory_flags: crate::MemoryFlags::empty(),+ };++ let (buffer, allocation) =+ suballocation::DeviceAllocationContext::from(self).create_buffer(&buffer_desc)?;++ let mut mapping = ptr::null_mut::<ffi::c_void>();+ unsafe { buffer.Map(0, None, Some(&mut mapping)) }.into_device_result("Map")?;++ assert!(!mapping.is_null());+ assert_eq!(mapping as usize % 4, 0);++ unsafe {+ ptr::copy_nonoverlapping(+ sampler_indexes.as_ptr(),+ mapping.cast(),+ sampler_indexes.len(),+ )+ };++ // The unmapping is not needed, as all memory is coherent in d3d12, but lets be nice to our address space.+ unsafe { buffer.Unmap(0, None) };++ let srv_desc = Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC {+ Format: Dxgi::Common::DXGI_FORMAT_UNKNOWN,+ ViewDimension: Direct3D12::D3D12_SRV_DIMENSION_BUFFER,+ Anonymous: Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC_0 {+ Buffer: Direct3D12::D3D12_BUFFER_SRV {+ FirstElement: 0,+ NumElements: sampler_indexes.len() as u32,+ StructureByteStride: 4,+ Flags: Direct3D12::D3D12_BUFFER_SRV_FLAG_NONE,+ },+ },+ Shader4ComponentMapping: Direct3D12::D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,+ };++ let inner = cpu_views.as_mut().unwrap();+ let cpu_index = inner.stage.len() as u32;+ let srv = desc.layout.cpu_heap_views.as_ref().unwrap().at(cpu_index);++ unsafe {+ self.raw+ .CreateShaderResourceView(&buffer, Some(&srv_desc), srv)+ };++ cpu_views.as_mut().unwrap().stage.push(srv);++ Some(super::SamplerIndexBuffer { buffer, allocation })+ } else {+ None+ };++ let handle_views = match cpu_views {+ Some(inner) => {+ let dual = unsafe {+ descriptor::upload(+ &self.raw,+ &inner,+ &self.shared.heap_views,+ &desc.layout.copy_counts,+ )+ }?;+ Some(dual)+ }+ None => None,+ };++ self.counters.bind_groups.add(1);++ Ok(super::BindGroup {+ handle_views,+ sampler_index_buffer,+ dynamic_buffers,+ })+ }++ unsafe fn destroy_bind_group(&self, group: super::BindGroup) {+ if let Some(dual) = group.handle_views {+ self.shared.heap_views.free_slice(dual);+ }++ if let Some(sampler_buffer) = group.sampler_index_buffer {+ suballocation::DeviceAllocationContext::from(self)+ .free_resource(sampler_buffer.buffer, sampler_buffer.allocation);+ }++ self.counters.bind_groups.sub(1);+ }++ unsafe fn create_shader_module(+ &self,+ desc: &crate::ShaderModuleDescriptor,+ shader: crate::ShaderInput,+ ) -> Result<super::ShaderModule, crate::ShaderError> {+ self.counters.shader_modules.add(1);++ let raw_name = desc+ .label+ .and_then(|label| alloc::ffi::CString::new(label).ok());+ match shader {+ crate::ShaderInput::Naga(naga) => Ok(super::ShaderModule {+ source: super::ShaderModuleSource::Naga(naga),+ raw_name,+ runtime_checks: desc.runtime_checks,+ }),+ crate::ShaderInput::Dxil { shader } => Ok(super::ShaderModule {+ source: super::ShaderModuleSource::DxilPassthrough(super::DxilPassthroughShader {+ shader: shader.to_vec(),+ }),+ raw_name,+ runtime_checks: desc.runtime_checks,+ }),+ crate::ShaderInput::Hlsl { shader } => Ok(super::ShaderModule {+ source: super::ShaderModuleSource::HlslPassthrough(super::HlslPassthroughShader {+ shader: shader.to_owned(),+ }),+ raw_name,+ runtime_checks: desc.runtime_checks,+ }),+ crate::ShaderInput::SpirV(_)+ | crate::ShaderInput::MetalLib { .. }+ | crate::ShaderInput::Msl { .. }+ | crate::ShaderInput::Glsl { .. } => {+ unreachable!()+ }+ }+ }+ unsafe fn destroy_shader_module(&self, _module: super::ShaderModule) {+ self.counters.shader_modules.sub(1);+ // just drop+ }++ unsafe fn create_render_pipeline(+ &self,+ desc: &crate::RenderPipelineDescriptor<+ super::PipelineLayout,+ super::ShaderModule,+ super::PipelineCache,+ >,+ ) -> Result<super::RenderPipeline, crate::PipelineError> {+ let mut shader_stages = wgt::ShaderStages::empty();+ let (topology_class, topology) = conv::map_topology(desc.primitive.topology);+ let mut rtv_formats = [Dxgi::Common::DXGI_FORMAT_UNKNOWN;+ Direct3D12::D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT as usize];+ for (rtv_format, ct) in rtv_formats.iter_mut().zip(desc.color_targets) {+ if let Some(ct) = ct.as_ref() {+ *rtv_format = auxil::dxgi::conv::map_texture_format(ct.format);+ }+ }++ let bias = desc+ .depth_stencil+ .as_ref()+ .map(|ds| ds.bias)+ .unwrap_or_default();++ let rasterizer_state = Direct3D12::D3D12_RASTERIZER_DESC {+ FillMode: conv::map_polygon_mode(desc.primitive.polygon_mode),+ CullMode: match desc.primitive.cull_mode {+ None => Direct3D12::D3D12_CULL_MODE_NONE,+ Some(wgt::Face::Front) => Direct3D12::D3D12_CULL_MODE_FRONT,+ Some(wgt::Face::Back) => Direct3D12::D3D12_CULL_MODE_BACK,+ },+ FrontCounterClockwise: match desc.primitive.front_face {+ wgt::FrontFace::Cw => Foundation::FALSE,+ wgt::FrontFace::Ccw => Foundation::TRUE,+ },+ DepthBias: bias.constant,+ DepthBiasClamp: bias.clamp,+ SlopeScaledDepthBias: bias.slope_scale,+ DepthClipEnable: windows_core::BOOL::from(!desc.primitive.unclipped_depth),+ MultisampleEnable: windows_core::BOOL::from(desc.multisample.count > 1),+ ForcedSampleCount: 0,+ AntialiasedLineEnable: false.into(),+ ConservativeRaster: if desc.primitive.conservative {+ Direct3D12::D3D12_CONSERVATIVE_RASTERIZATION_MODE_ON+ } else {+ Direct3D12::D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF+ },+ };++ let blob_fs = match desc.fragment_stage {+ Some(ref stage) => {+ shader_stages |= wgt::ShaderStages::FRAGMENT;+ Some(self.load_shader(stage, desc.layout, naga::ShaderStage::Fragment, None)?)+ }+ None => None,+ };+ let pixel_shader = match &blob_fs {+ Some(shader) => shader.create_native_shader(),+ None => Direct3D12::D3D12_SHADER_BYTECODE::default(),+ };+ let stream_output = Direct3D12::D3D12_STREAM_OUTPUT_DESC {+ pSODeclaration: ptr::null(),+ NumEntries: 0,+ pBufferStrides: ptr::null(),+ NumStrides: 0,+ RasterizedStream: 0,+ };+ let blend_state = Direct3D12::D3D12_BLEND_DESC {+ AlphaToCoverageEnable: windows_core::BOOL::from(+ desc.multisample.alpha_to_coverage_enabled,+ ),+ IndependentBlendEnable: true.into(),+ RenderTarget: conv::map_render_targets(desc.color_targets),+ };+ let depth_stencil_state = match desc.depth_stencil {+ Some(ref ds) => conv::map_depth_stencil(ds),+ None => Default::default(),+ };+ let dsv_format = desc+ .depth_stencil+ .as_ref()+ .map_or(Dxgi::Common::DXGI_FORMAT_UNKNOWN, |ds| {+ auxil::dxgi::conv::map_texture_format(ds.format)+ });+ let sample_desc = Dxgi::Common::DXGI_SAMPLE_DESC {+ Count: desc.multisample.count,+ Quality: 0,+ };+ let cached_pso = Direct3D12::D3D12_CACHED_PIPELINE_STATE {+ pCachedBlob: ptr::null(),+ CachedBlobSizeInBytes: 0,+ };+ let flags = Direct3D12::D3D12_PIPELINE_STATE_FLAG_NONE;++ let mut view_instancing = ArrayVec::<Direct3D12::D3D12_VIEW_INSTANCE_LOCATION, 32>::new();+ if let Some(mask) = desc.multiview_mask {+ let mask = mask.get();+ // This array is just what _could_ be rendered to. We actually apply the mask at+ // renderpass creation time. The `view_index` passed to the shader depends on the+ // view's index in this array, so if we include every view in this array, `view_index`+ // actually the texture array layer, like in vulkan.+ for i in 0..32 - mask.leading_zeros() {+ view_instancing.push(Direct3D12::D3D12_VIEW_INSTANCE_LOCATION {+ ViewportArrayIndex: 0,+ RenderTargetArrayIndex: i,+ });+ }+ }++ // Borrow view instancing slice, so we can be sure that it won't be moved while we have pointers into this buffer.+ let view_instancing_slice = view_instancing.as_slice();++ let mut stream_desc = RenderPipelineStateStreamDesc {+ // Shared by vertex and mesh pipelines+ root_signature: desc.layout.shared.signature.as_ref(),+ pixel_shader,+ blend_state,+ sample_mask: desc.multisample.mask as u32,+ rasterizer_state,+ depth_stencil_state,+ primitive_topology_type: topology_class,+ rtv_formats: Direct3D12::D3D12_RT_FORMAT_ARRAY {+ RTFormats: rtv_formats,+ NumRenderTargets: desc.color_targets.len() as u32,+ },+ dsv_format,+ sample_desc,+ node_mask: 0,+ cached_pso,+ flags,+ view_instancing: if !view_instancing_slice.is_empty() {+ Some(Direct3D12::D3D12_VIEW_INSTANCING_DESC {+ ViewInstanceCount: view_instancing_slice.len() as u32,+ pViewInstanceLocations: view_instancing_slice.as_ptr(),+ // This lets us hide/mask certain values later, at renderpass creation time.+ Flags: Direct3D12::D3D12_VIEW_INSTANCING_FLAG_ENABLE_VIEW_INSTANCE_MASKING,+ })+ } else {+ None+ },++ // Optional data that depends on the pipeline type (vertex vs mesh).+ vertex_shader: Default::default(),+ input_layout: Default::default(),+ index_buffer_strip_cut_value: Default::default(),+ stream_output,+ task_shader: Default::default(),+ mesh_shader: Default::default(),+ };+ let mut input_element_descs = Vec::new();+ let blob_vs;+ let blob_ts;+ let blob_ms;+ let mut vertex_strides = [None; crate::MAX_VERTEX_BUFFERS];+ match &desc.vertex_processor {+ &crate::VertexProcessor::Standard {+ vertex_buffers,+ ref vertex_stage,+ } => {+ shader_stages |= wgt::ShaderStages::VERTEX;+ blob_vs = Some(self.load_shader(+ vertex_stage,+ desc.layout,+ naga::ShaderStage::Vertex,+ desc.fragment_stage.as_ref(),+ )?);++ for (i, (stride, vbuf)) in vertex_strides.iter_mut().zip(vertex_buffers).enumerate()+ {+ let Some(vbuf) = vbuf else {+ continue;+ };+ *stride = Some(vbuf.array_stride as u32);+ let (slot_class, step_rate) = match vbuf.step_mode {+ wgt::VertexStepMode::Vertex => {+ (Direct3D12::D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0)+ }+ wgt::VertexStepMode::Instance => {+ (Direct3D12::D3D12_INPUT_CLASSIFICATION_PER_INSTANCE_DATA, 1)+ }+ };+ for attribute in vbuf.attributes {+ input_element_descs.push(Direct3D12::D3D12_INPUT_ELEMENT_DESC {+ SemanticName: windows::core::PCSTR(NAGA_LOCATION_SEMANTIC.as_ptr()),+ SemanticIndex: attribute.shader_location,+ Format: auxil::dxgi::conv::map_vertex_format(attribute.format),+ InputSlot: i as u32,+ AlignedByteOffset: attribute.offset as u32,+ InputSlotClass: slot_class,+ InstanceDataStepRate: step_rate,+ });+ }+ }+ stream_desc.vertex_shader = blob_vs.as_ref().unwrap().create_native_shader();+ stream_desc.input_layout = Direct3D12::D3D12_INPUT_LAYOUT_DESC {+ pInputElementDescs: if input_element_descs.is_empty() {+ ptr::null()+ } else {+ input_element_descs.as_ptr()+ },+ NumElements: input_element_descs.len() as u32,+ };+ stream_desc.index_buffer_strip_cut_value = match desc.primitive.strip_index_format {+ Some(wgt::IndexFormat::Uint16) => {+ Direct3D12::D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFF+ }+ Some(wgt::IndexFormat::Uint32) => {+ Direct3D12::D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFFFFFF+ }+ None => Direct3D12::D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_DISABLED,+ };+ stream_desc.stream_output = Direct3D12::D3D12_STREAM_OUTPUT_DESC {+ pSODeclaration: ptr::null(),+ NumEntries: 0,+ pBufferStrides: ptr::null(),+ NumStrides: 0,+ RasterizedStream: 0,+ };+ }+ crate::VertexProcessor::Mesh {+ task_stage,+ mesh_stage,+ } => {+ blob_ts = if let Some(ts) = task_stage {+ shader_stages |= wgt::ShaderStages::TASK;+ Some(self.load_shader(+ ts,+ desc.layout,+ naga::ShaderStage::Task,+ desc.fragment_stage.as_ref(),+ )?)+ } else {+ None+ };+ let task_shader = if let Some(ts) = &blob_ts {+ ts.create_native_shader()+ } else {+ Default::default()+ };+ shader_stages |= wgt::ShaderStages::MESH;+ blob_ms = Some(self.load_shader(+ mesh_stage,+ desc.layout,+ naga::ShaderStage::Mesh,+ desc.fragment_stage.as_ref(),+ )?);+ stream_desc.task_shader = task_shader;+ stream_desc.mesh_shader = blob_ms.as_ref().unwrap().create_native_shader();+ }+ };+ let raw: Direct3D12::ID3D12PipelineState =+ // If stream descriptors are available, use them as they are more flexible.+ if let Ok(device) = self.raw.cast::<Direct3D12::ID3D12Device2>() {+ // Prefer stream descs where possible+ let mut stream = stream_desc.to_stream();+ unsafe {+ profiling::scope!("ID3D12Device2::CreatePipelineState");+ stream.create_pipeline_state(&device).map_err(|err| {+ crate::PipelineError::Linkage(shader_stages, err.to_string())+ })?+ }+ } else {+ unsafe {+ // Safety: `stream_desc` entirely outlives the `desc`.+ let desc = stream_desc.to_graphics_pipeline_descriptor();+ self.raw.CreateGraphicsPipelineState(&desc).map_err(|err| {+ crate::PipelineError::Linkage(shader_stages, err.to_string())+ })?+ }+ };++ if let Some(label) = desc.label {+ raw.set_name(label)?;+ }++ self.counters.render_pipelines.add(1);++ Ok(super::RenderPipeline {+ raw,+ layout: desc.layout.shared.clone(),+ topology,+ vertex_strides,+ })+ }++ unsafe fn destroy_render_pipeline(&self, _pipeline: super::RenderPipeline) {+ self.counters.render_pipelines.sub(1);+ }++ unsafe fn create_compute_pipeline(+ &self,+ desc: &crate::ComputePipelineDescriptor<+ super::PipelineLayout,+ super::ShaderModule,+ super::PipelineCache,+ >,+ ) -> Result<super::ComputePipeline, crate::PipelineError> {+ let blob_cs =+ self.load_shader(&desc.stage, desc.layout, naga::ShaderStage::Compute, None)?;++ let pair = {+ profiling::scope!("ID3D12Device::CreateComputePipelineState");+ unsafe {+ self.raw.CreateComputePipelineState(+ &Direct3D12::D3D12_COMPUTE_PIPELINE_STATE_DESC {+ pRootSignature: borrow_optional_interface_temporarily(+ &desc.layout.shared.signature,+ ),+ CS: blob_cs.create_native_shader(),+ NodeMask: 0,+ CachedPSO: Direct3D12::D3D12_CACHED_PIPELINE_STATE::default(),+ Flags: Direct3D12::D3D12_PIPELINE_STATE_FLAG_NONE,+ },+ )+ }+ };++ let raw: Direct3D12::ID3D12PipelineState = pair.map_err(|err| {+ crate::PipelineError::Linkage(wgt::ShaderStages::COMPUTE, err.to_string())+ })?;++ if let Some(label) = desc.label {+ raw.set_name(label)?;+ }++ self.counters.compute_pipelines.add(1);++ Ok(super::ComputePipeline {+ raw,+ layout: desc.layout.shared.clone(),+ })+ }++ unsafe fn destroy_compute_pipeline(&self, _pipeline: super::ComputePipeline) {+ self.counters.compute_pipelines.sub(1);+ }++ unsafe fn create_ray_tracing_pipeline(+ &self,+ _desc: &crate::RayTracingPipelineDescriptor<+ super::PipelineLayout,+ super::ShaderModule,+ super::PipelineCache,+ >,+ ) -> Result<<Self::A as crate::Api>::RayTracingPipeline, crate::PipelineError> {+ unreachable!("ray tracing pipelines not yet implemented")+ }++ unsafe fn destroy_ray_tracing_pipeline(&self, _pipeline: super::RayTracingPipeline) {+ unreachable!("ray tracing pipelines not yet implemented")+ }++ unsafe fn get_raytracing_pipeline_group_data(+ &self,+ _pipeline: &super::RayTracingPipeline,+ _groups: core::ops::Range<u32>,+ ) -> Result<Vec<u8>, crate::DeviceError> {+ unimplemented!("ray tracing pipelines not yet implemented")+ }++ unsafe fn create_pipeline_cache(+ &self,+ _desc: &crate::PipelineCacheDescriptor<'_>,+ ) -> Result<super::PipelineCache, crate::PipelineCacheError> {+ Ok(super::PipelineCache)+ }+ unsafe fn destroy_pipeline_cache(&self, _: super::PipelineCache) {}++ unsafe fn create_query_set(+ &self,+ desc: &wgt::QuerySetDescriptor<crate::Label>,+ ) -> Result<super::QuerySet, crate::DeviceError> {+ let (heap_ty, raw_ty) = match desc.ty {+ wgt::QueryType::Occlusion => (+ Direct3D12::D3D12_QUERY_HEAP_TYPE_OCCLUSION,+ Direct3D12::D3D12_QUERY_TYPE_BINARY_OCCLUSION,+ ),+ wgt::QueryType::PipelineStatistics(_) => (+ Direct3D12::D3D12_QUERY_HEAP_TYPE_PIPELINE_STATISTICS,+ Direct3D12::D3D12_QUERY_TYPE_PIPELINE_STATISTICS,+ ),+ wgt::QueryType::Timestamp => (+ Direct3D12::D3D12_QUERY_HEAP_TYPE_TIMESTAMP,+ Direct3D12::D3D12_QUERY_TYPE_TIMESTAMP,+ ),+ };++ if let Some(threshold) = self+ .mem_allocator+ .memory_budget_thresholds+ .for_resource_creation+ {+ let info = self+ .shared+ .adapter+ .query_video_memory_info(Dxgi::DXGI_MEMORY_SEGMENT_GROUP_LOCAL)?;++ // Assume each query is 256 bytes.+ // On an AMD W6800 with driver version 32.0.12030.9, occlusion and pipeline statistics are 256, timestamp is 8.++ if info.CurrentUsage + desc.count as u64 * 256 >= info.Budget / 100 * threshold as u64 {+ return Err(crate::DeviceError::OutOfMemory);+ }+ }++ let mut raw = None::<Direct3D12::ID3D12QueryHeap>;+ unsafe {+ self.raw.CreateQueryHeap(+ &Direct3D12::D3D12_QUERY_HEAP_DESC {+ Type: heap_ty,+ Count: desc.count,+ NodeMask: 0,+ },+ &mut raw,+ )+ }+ .into_device_result("Query heap creation")?;++ let raw = raw.ok_or(crate::DeviceError::Unexpected)?;++ if let Some(label) = desc.label {+ raw.set_name(label)?;+ }++ self.counters.query_sets.add(1);++ Ok(super::QuerySet { raw, raw_ty })+ }++ unsafe fn destroy_query_set(&self, _set: super::QuerySet) {+ self.counters.query_sets.sub(1);+ }++ unsafe fn create_fence(&self) -> Result<super::Fence, crate::DeviceError> {+ let raw: Direct3D12::ID3D12Fence =+ unsafe { self.raw.CreateFence(0, Direct3D12::D3D12_FENCE_FLAG_SHARED) }+ .into_device_result("Fence creation")?;++ self.counters.fences.add(1);++ Ok(super::Fence { raw })+ }+ unsafe fn destroy_fence(&self, _fence: super::Fence) {+ self.counters.fences.sub(1);+ }++ unsafe fn get_fence_value(+ &self,+ fence: &super::Fence,+ ) -> Result<crate::FenceValue, crate::DeviceError> {+ Ok(unsafe { fence.raw.GetCompletedValue() })+ }+ unsafe fn wait(+ &self,+ fence: &super::Fence,+ value: crate::FenceValue,+ timeout: Option<Duration>,+ ) -> Result<bool, crate::DeviceError> {+ let timeout = timeout.unwrap_or(Duration::MAX);++ // We first check if the fence has already reached the value we're waiting for.+ let mut fence_value = unsafe { fence.raw.GetCompletedValue() };+ if fence_value >= value {+ return Ok(true);+ }++ let event = Event::create(false, false)?;++ unsafe { fence.raw.SetEventOnCompletion(value, event.0) }+ .into_device_result("Set event")?;++ let start_time = Instant::now();++ // We need to loop to get correct behavior when timeouts are involved.+ //+ // wait(0):+ // - We set the event from the fence value 0.+ // - WaitForSingleObject times out, we return false.+ //+ // wait(1):+ // - We set the event from the fence value 1.+ // - WaitForSingleObject returns. However we do not know if the fence value is 0 or 1,+ // just that _something_ triggered the event. We check the fence value, and if it is+ // 1, we return true. Otherwise, we loop and wait again.+ loop {+ let elapsed = start_time.elapsed();++ // We need to explicitly use checked_sub. Overflow with duration panics, and if the+ // timing works out just right, we can get a negative remaining wait duration.+ //+ // This happens when a previous iteration WaitForSingleObject succeeded with a previous fence value,+ // right before the timeout would have been hit.+ let remaining_wait_duration = match timeout.checked_sub(elapsed) {+ Some(remaining) => remaining,+ None => {+ log::trace!("Timeout elapsed in between waits!");+ break Ok(false);+ }+ };++ log::trace!("Waiting for fence value {value} for {remaining_wait_duration:?}");++ match unsafe {+ Threading::WaitForSingleObject(+ event.0,+ remaining_wait_duration.as_millis().min(u32::MAX as u128) as u32,+ )+ } {+ Foundation::WAIT_OBJECT_0 => {}+ Foundation::WAIT_ABANDONED | Foundation::WAIT_FAILED => {+ log::error!("Wait failed!");+ break Err(crate::DeviceError::Lost);+ }+ Foundation::WAIT_TIMEOUT => {+ log::trace!("Wait timed out!");+ break Ok(false);+ }+ other => {+ log::error!("Unexpected wait status: 0x{other:?}");+ break Err(crate::DeviceError::Lost);+ }+ };++ fence_value = unsafe { fence.raw.GetCompletedValue() };+ log::trace!("Wait complete! Fence actual value: {fence_value}");++ if fence_value >= value {+ break Ok(true);+ }+ }+ }++ unsafe fn start_graphics_debugger_capture(&self) -> bool {+ #[cfg(feature = "renderdoc")]+ {+ unsafe {+ self.render_doc+ .start_frame_capture(self.raw.as_raw(), ptr::null_mut())+ }+ }+ #[cfg(not(feature = "renderdoc"))]+ false+ }++ unsafe fn stop_graphics_debugger_capture(&self) {+ #[cfg(feature = "renderdoc")]+ unsafe {+ self.render_doc+ .end_frame_capture(self.raw.as_raw(), ptr::null_mut())+ }+ }++ unsafe fn get_acceleration_structure_build_sizes<'a>(+ &self,+ desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, super::Buffer>,+ ) -> crate::AccelerationStructureBuildSizes {+ let mut geometry_desc;+ let device5 = self.raw.cast::<Direct3D12::ID3D12Device5>().unwrap();+ let ty;+ let inputs0;+ let num_desc;+ match desc.entries {+ AccelerationStructureEntries::Instances(instances) => {+ ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL;+ inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 {+ InstanceDescs: 0,+ };+ num_desc = instances.count;+ }+ AccelerationStructureEntries::Triangles(triangles) => {+ geometry_desc = Vec::with_capacity(triangles.len());+ for triangle in triangles {+ let index_format = triangle+ .indices+ .as_ref()+ .map_or(Dxgi::Common::DXGI_FORMAT_UNKNOWN, |indices| {+ auxil::dxgi::conv::map_index_format(indices.format)+ });+ let index_count = triangle.indices.as_ref().map_or(0, |indices| indices.count);++ let triangle_desc = Direct3D12::D3D12_RAYTRACING_GEOMETRY_TRIANGLES_DESC {+ // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12device5-getraytracingaccelerationstructureprebuildinfo+ // It may not inspect/dereference any GPU virtual addresses, other than+ // to check to see if a pointer is NULL or not, such as the optional+ // transform in D3D12_RAYTRACING_GEOMETRY_TRIANGLES_DESC, without+ // dereferencing it.+ //+ // This suggests we could pass a non-zero invalid address here if fetching the+ // real address has significant overhead, but we pass the real one to be on the+ // safe side for now.+ Transform3x4: if desc+ .flags+ .contains(wgt::AccelerationStructureFlags::USE_TRANSFORM)+ {+ unsafe {+ triangle+ .transform+ .as_ref()+ .unwrap()+ .buffer+ .resource+ .GetGPUVirtualAddress()+ }+ } else {+ 0+ },+ IndexFormat: index_format,+ VertexFormat: auxil::dxgi::conv::map_vertex_format(triangle.vertex_format),+ IndexCount: index_count,+ VertexCount: triangle.vertex_count,+ IndexBuffer: 0,+ VertexBuffer: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE {+ StartAddress: 0,+ StrideInBytes: triangle.vertex_stride,+ },+ };++ geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC {+ Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES,+ Flags: conv::map_acceleration_structure_geometry_flags(triangle.flags),+ Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 {+ Triangles: triangle_desc,+ },+ })+ }+ ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;+ inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 {+ pGeometryDescs: geometry_desc.as_ptr(),+ };+ num_desc = geometry_desc.len() as u32;+ }+ AccelerationStructureEntries::AABBs(aabbs) => {+ geometry_desc = Vec::with_capacity(aabbs.len());+ for aabb in aabbs {+ let aabb_desc = Direct3D12::D3D12_RAYTRACING_GEOMETRY_AABBS_DESC {+ AABBCount: aabb.count as u64,+ AABBs: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE {+ StartAddress: 0,+ StrideInBytes: aabb.stride,+ },+ };+ geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC {+ Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_PROCEDURAL_PRIMITIVE_AABBS,+ Flags: conv::map_acceleration_structure_geometry_flags(aabb.flags),+ Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 {+ AABBs: aabb_desc,+ },+ })+ }+ ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;+ inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 {+ pGeometryDescs: geometry_desc.as_ptr(),+ };+ num_desc = geometry_desc.len() as u32;+ }+ };+ let acceleration_structure_inputs =+ Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS {+ Type: ty,+ Flags: conv::map_acceleration_structure_build_flags(desc.flags, None),+ NumDescs: num_desc,+ DescsLayout: Direct3D12::D3D12_ELEMENTS_LAYOUT_ARRAY,+ Anonymous: inputs0,+ };+ let mut info = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO::default();+ unsafe {+ device5.GetRaytracingAccelerationStructurePrebuildInfo(+ &acceleration_structure_inputs,+ &mut info,+ )+ };+ crate::AccelerationStructureBuildSizes {+ acceleration_structure_size: info.ResultDataMaxSizeInBytes,+ update_scratch_size: info.UpdateScratchDataSizeInBytes,+ build_scratch_size: info.ScratchDataSizeInBytes,+ }+ }++ unsafe fn get_acceleration_structure_device_address(+ &self,+ acceleration_structure: &super::AccelerationStructure,+ ) -> wgt::BufferAddress {+ unsafe { acceleration_structure.resource.GetGPUVirtualAddress() }+ }++ unsafe fn create_acceleration_structure(+ &self,+ desc: &crate::AccelerationStructureDescriptor,+ ) -> Result<super::AccelerationStructure, crate::DeviceError> {+ // Create a D3D12 resource as per-usual.+ let size = desc.size;++ let raw_desc = Direct3D12::D3D12_RESOURCE_DESC {+ Dimension: Direct3D12::D3D12_RESOURCE_DIMENSION_BUFFER,+ Alignment: 0,+ Width: size,+ Height: 1,+ DepthOrArraySize: 1,+ MipLevels: 1,+ Format: Dxgi::Common::DXGI_FORMAT_UNKNOWN,+ SampleDesc: Dxgi::Common::DXGI_SAMPLE_DESC {+ Count: 1,+ Quality: 0,+ },+ Layout: Direct3D12::D3D12_TEXTURE_LAYOUT_ROW_MAJOR,+ // TODO: when moving to enhanced barriers use Direct3D12::D3D12_RESOURCE_FLAG_RAYTRACING_ACCELERATION_STRUCTURE+ Flags: Direct3D12::D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS,+ };++ let (resource, allocation) = suballocation::DeviceAllocationContext::from(self)+ .create_acceleration_structure(desc, raw_desc)?;++ // for some reason there is no counter for acceleration structures++ Ok(super::AccelerationStructure {+ resource,+ allocation,+ })+ }++ unsafe fn destroy_acceleration_structure(+ &self,+ acceleration_structure: super::AccelerationStructure,+ ) {+ suballocation::DeviceAllocationContext::from(self).free_resource(+ acceleration_structure.resource,+ acceleration_structure.allocation,+ );+ }++ fn get_internal_counters(&self) -> wgt::HalCounters {+ self.counters.as_ref().clone()+ }++ fn generate_allocator_report(&self) -> Option<wgt::AllocatorReport> {+ Some(self.mem_allocator.generate_report())+ }++ fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8> {+ const MAX_U24: u32 = (1u32 << 24u32) - 1u32;+ let temp = Direct3D12::D3D12_RAYTRACING_INSTANCE_DESC {+ Transform: instance.transform,+ _bitfield1: (instance.custom_data & MAX_U24) | (u32::from(instance.mask) << 24),+ _bitfield2: (instance.pipeline_intersection_data_offset & MAX_U24),+ AccelerationStructure: instance.blas_address,+ };++ wgt::bytemuck_wrapper!(unsafe struct Desc(Direct3D12::D3D12_RAYTRACING_INSTANCE_DESC));++ bytemuck::bytes_of(&Desc::wrap(temp)).to_vec()+ }++ fn check_if_oom(&self) -> Result<(), crate::DeviceError> {+ let Some(threshold) = self.mem_allocator.memory_budget_thresholds.for_device_loss else {+ return Ok(());+ };++ let info = self+ .shared+ .adapter+ .query_video_memory_info(Dxgi::DXGI_MEMORY_SEGMENT_GROUP_LOCAL)?;++ if info.CurrentUsage >= info.Budget / 100 * threshold as u64 {+ return Err(crate::DeviceError::OutOfMemory);+ }++ if matches!(+ self.shared.private_caps.memory_architecture,+ super::MemoryArchitecture::NonUnified+ ) {+ let info = self+ .shared+ .adapter+ .query_video_memory_info(Dxgi::DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL)?;++ if info.CurrentUsage >= info.Budget / 100 * threshold as u64 {+ return Err(crate::DeviceError::OutOfMemory);+ }+ }++ Ok(())+ }+}diff --git a/vendor/wgpu-hal/src/dx12/device_creation.rs b/vendor/wgpu-hal/src/dx12/device_creation.rsnew file mode 100644index 00000000..dc5cdf9e--- /dev/null+++ b/vendor/wgpu-hal/src/dx12/device_creation.rs@@ -0,0 +1,176 @@+use alloc::sync::Arc;+use core::ops::Deref;++use windows::core::Interface as _;+use windows::Win32::Graphics::{Direct3D, Direct3D12};++use super::D3D12Lib;+use crate::auxil::dxgi::factory::DxgiAdapter;++/// Abstraction over D3D12 device creation.+///+/// Supports two paths:+/// - **Independent**: Uses `ID3D12DeviceFactory` from the Agility SDK's Independent Devices API.+/// - **Legacy**: Uses the traditional `D3D12CreateDevice` export.+pub(super) enum DeviceFactory {+ /// Uses `ID3D12DeviceFactory` from the Independent Devices API.+ Independent(Direct3D12::ID3D12DeviceFactory),+ /// Uses the traditional `D3D12CreateDevice` export.+ Legacy,+}++impl DeviceFactory {+ /// Create a new `DeviceFactory`.+ ///+ /// If `agility_sdk` is `Some`, attempts to set up the Independent Devices API path.+ /// On failure, the behavior depends on+ /// [`on_load_failure`](wgt::Dx12AgilitySDKLoadFailure):+ /// - [`Fallback`](wgt::Dx12AgilitySDKLoadFailure::Fallback): logs a warning and+ /// returns `Ok(Legacy)`.+ /// - [`Error`](wgt::Dx12AgilitySDKLoadFailure::Error): returns an `Err`.+ pub(super) fn new(+ lib: &D3D12Lib,+ agility_sdk: Option<&wgt::Dx12AgilitySDK>,+ ) -> Result<Self, crate::InstanceError> {+ let Some(agility_sdk) = agility_sdk else {+ log::debug!("No D3D12 Agility SDK configuration provided; using system D3D12 runtime");+ return Ok(Self::Legacy);+ };++ match Self::try_create_independent(lib, agility_sdk) {+ Ok(factory) => {+ log::debug!(+ "Using D3D12 Agility SDK v{} from '{}'",+ agility_sdk.sdk_version,+ agility_sdk.sdk_path+ );+ Ok(Self::Independent(factory))+ }+ Err(err) => {+ let message = format!(+ "Failed to initialize D3D12 Agility SDK (v{} at '{}'): {err}",+ agility_sdk.sdk_version, agility_sdk.sdk_path+ );++ match agility_sdk.on_load_failure {+ wgt::Dx12AgilitySDKLoadFailure::Fallback => {+ log::warn!("{message}; falling back to system D3D12 runtime");+ Ok(Self::Legacy)+ }+ wgt::Dx12AgilitySDKLoadFailure::Error => {+ Err(crate::InstanceError::new(message))+ }+ }+ }+ }+ }++ fn try_create_independent(+ lib: &D3D12Lib,+ agility_sdk: &wgt::Dx12AgilitySDK,+ ) -> Result<Direct3D12::ID3D12DeviceFactory, DeviceFactoryError> {+ // Step 1: Get ID3D12SDKConfiguration1 via D3D12GetInterface+ let sdk_config: Direct3D12::ID3D12SDKConfiguration1 = lib+ .get_interface(&Direct3D12::CLSID_D3D12SDKConfiguration)+ .map_err(DeviceFactoryError::GetInterface)?;++ // Step 2: Create device factory with the specified SDK version and path+ let sdk_path = std::ffi::CString::new(agility_sdk.sdk_path.as_bytes())+ .map_err(|_| DeviceFactoryError::InvalidPath)?;+ let factory: Direct3D12::ID3D12DeviceFactory = unsafe {+ sdk_config.CreateDeviceFactory(+ agility_sdk.sdk_version,+ windows::core::PCSTR(sdk_path.as_ptr().cast::<u8>()),+ )+ }+ .map_err(DeviceFactoryError::CreateDeviceFactory)?;++ Ok(factory)+ }++ /// Enable the D3D12 debug layer and optionally GPU-based validation.+ ///+ /// - **Legacy**: configures debug globally via `D3D12GetDebugInterface`.+ /// - **Independent**: uses `GetConfigurationInterface` to get an+ /// `ID3D12Debug` scoped to the factory.+ pub(super) fn enable_debug_layer(&self, lib: &D3D12Lib, flags: wgt::InstanceFlags) {+ if !flags+ .intersects(wgt::InstanceFlags::VALIDATION | wgt::InstanceFlags::GPU_BASED_VALIDATION)+ {+ return;+ }++ let debug_controller = match self {+ Self::Independent(factory) => {+ match unsafe {+ factory.GetConfigurationInterface::<Direct3D12::ID3D12Debug>(+ &Direct3D12::CLSID_D3D12Debug,+ )+ } {+ Ok(debug) => debug,+ Err(err) => {+ log::warn!("Failed to get debug interface from device factory: {err}");+ return;+ }+ }+ }+ Self::Legacy => match lib.debug_interface() {+ Ok(Some(debug)) => debug,+ Ok(None) => return,+ Err(err) => {+ log::warn!("Failed to get debug interface: {err}");+ return;+ }+ },+ };++ if flags.intersects(wgt::InstanceFlags::VALIDATION) {+ unsafe { debug_controller.EnableDebugLayer() }+ }+ if flags.intersects(wgt::InstanceFlags::GPU_BASED_VALIDATION) {+ if let Ok(debug1) = debug_controller.cast::<Direct3D12::ID3D12Debug1>() {+ unsafe { debug1.SetEnableGPUBasedValidation(true) }+ } else {+ log::warn!("Failed to enable GPU-based validation");+ }+ }+ }++ /// Create a D3D12 device using the appropriate method.+ pub(super) fn create_device(+ &self,+ lib: &Arc<D3D12Lib>,+ adapter: &DxgiAdapter,+ feature_level: Direct3D::D3D_FEATURE_LEVEL,+ ) -> Result<Direct3D12::ID3D12Device, super::CreateDeviceError> {+ match self {+ Self::Independent(factory) => {+ let mut result__: Option<Direct3D12::ID3D12Device> = None;+ unsafe { factory.CreateDevice(adapter.deref(), feature_level, &mut result__) }+ .map_err(|e| super::CreateDeviceError::D3D12CreateDevice(e.into()))?;++ result__.ok_or(super::CreateDeviceError::RetDeviceIsNull)+ }+ Self::Legacy => lib.create_device(adapter, feature_level),+ }+ }+}++impl core::fmt::Debug for DeviceFactory {+ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {+ match self {+ Self::Independent(_) => write!(f, "DeviceFactory::Independent"),+ Self::Legacy => write!(f, "DeviceFactory::Legacy"),+ }+ }+}++#[derive(Debug, thiserror::Error)]+enum DeviceFactoryError {+ #[error("failed to get ID3D12SDKConfiguration1: {0}")]+ GetInterface(super::GetInterfaceError),+ #[error("SDK path contains null bytes")]+ InvalidPath,+ #[error("CreateDeviceFactory failed: {0}")]+ CreateDeviceFactory(windows::core::Error),+}diff --git a/vendor/wgpu-hal/src/dx12/instance.rs b/vendor/wgpu-hal/src/dx12/instance.rsnew file mode 100644index 00000000..fdb63bcb--- /dev/null+++ b/vendor/wgpu-hal/src/dx12/instance.rs@@ -0,0 +1,189 @@+use alloc::{string::String, sync::Arc, vec::Vec};++use parking_lot::RwLock;+use windows::Win32::{Foundation, Graphics::Dxgi};++use super::SurfaceTarget;+use crate::{+ auxil,+ dx12::{+ device_creation::DeviceFactory, shader_compilation::CompilerContainer, D3D12Lib, DCompLib,+ },+};++impl crate::Instance for super::Instance {+ type A = super::Api;++ unsafe fn init(desc: &crate::InstanceDescriptor<'_>) -> Result<Self, crate::InstanceError> {+ profiling::scope!("Init DX12 Backend");+ let lib_main = D3D12Lib::new().map_err(|e| {+ crate::InstanceError::with_source(String::from("failed to load d3d12.dll"), e)+ })?;++ // Create DeviceFactory first so we know which debug path to use+ let device_factory =+ DeviceFactory::new(&lib_main, desc.backend_options.dx12.agility_sdk.as_ref())?;++ device_factory.enable_debug_layer(&lib_main, desc.flags);++ let (lib_dxgi, factory) = auxil::dxgi::factory::create_factory(desc.flags)?;++ // Create IDXGIFactoryMedia+ let factory_media = lib_dxgi.create_factory_media().ok();++ let mut supports_allow_tearing = false;+ if let Some(factory5) = factory.as_factory5() {+ let mut allow_tearing = Foundation::FALSE;+ let hr = unsafe {+ factory5.CheckFeatureSupport(+ Dxgi::DXGI_FEATURE_PRESENT_ALLOW_TEARING,+ <*mut _>::cast(&mut allow_tearing),+ size_of_val(&allow_tearing) as u32,+ )+ };++ match hr {+ Err(err) => log::warn!("Unable to check for tearing support: {err}"),+ Ok(()) => supports_allow_tearing = true,+ }+ }++ // Initialize the shader compiler+ let compiler_container = match desc.backend_options.dx12.shader_compiler.clone() {+ wgt::Dx12Compiler::DynamicDxc { dxc_path } => {+ CompilerContainer::new_dynamic_dxc(dxc_path.into()).map_err(|e| {+ crate::InstanceError::with_source(String::from("Failed to load dynamic DXC"), e)+ })?+ }+ wgt::Dx12Compiler::StaticDxc => CompilerContainer::new_static_dxc().map_err(|e| {+ crate::InstanceError::with_source(String::from("Failed to load static DXC"), e)+ })?,+ wgt::Dx12Compiler::Fxc => CompilerContainer::new_fxc().map_err(|e| {+ crate::InstanceError::with_source(String::from("Failed to load FXC"), e)+ })?,+ wgt::Dx12Compiler::Auto => {+ if cfg!(static_dxc) {+ // Prefer static DXC if its compiled in+ CompilerContainer::new_static_dxc().map_err(|e| {+ crate::InstanceError::with_source(+ String::from("Failed to load static DXC"),+ e,+ )+ })?+ } else {+ // Try to load dynamic DXC+ let dynamic = CompilerContainer::new_dynamic_dxc("dxcompiler.dll".into());+ match dynamic {+ Ok(v) => v,+ Err(super::shader_compilation::GetContainerError::FailedToLoad(..)) => {+ // If it can't be found load FXC+ CompilerContainer::new_fxc().map_err(|e| {+ crate::InstanceError::with_source(+ String::from("Failed to load FXC"),+ e,+ )+ })?+ }+ Err(e) => {+ // If another error occurs when loading static DXC return that error+ return Err(crate::InstanceError::with_source(+ String::from("Failed to load dynamic DXC"),+ e,+ ));+ }+ }+ }+ }+ };++ match compiler_container {+ CompilerContainer::DynamicDxc(..) => {+ log::debug!("Using dynamic DXC for shader compilation")+ }+ CompilerContainer::StaticDxc(..) => {+ log::debug!("Using static DXC for shader compilation")+ }+ CompilerContainer::Fxc(..) => {+ log::debug!("Using FXC for shader compilation")+ }+ }++ Ok(Self {+ // The call to create_factory will only succeed if we get a factory4, so this is safe.+ factory,+ factory_media,+ library: Arc::new(lib_main),+ device_factory: Arc::new(device_factory),+ dcomp_lib: Arc::new(DCompLib::new()),+ presentation_system: desc.backend_options.dx12.presentation_system,+ _lib_dxgi: lib_dxgi,+ supports_allow_tearing,+ flags: desc.flags,+ memory_budget_thresholds: desc.memory_budget_thresholds,+ compiler_container: Arc::new(compiler_container),+ options: desc.backend_options.dx12.clone(),+ telemetry: desc.telemetry,+ })+ }++ unsafe fn create_surface(+ &self,+ display_handle: raw_window_handle::RawDisplayHandle,+ window_handle: raw_window_handle::RawWindowHandle,+ ) -> Result<super::Surface, crate::InstanceError> {+ assert!(matches!(+ display_handle,+ raw_window_handle::RawDisplayHandle::Windows(_)+ ));+ match window_handle {+ raw_window_handle::RawWindowHandle::Win32(handle) => {+ // https://github.com/rust-windowing/raw-window-handle/issues/171+ let handle = Foundation::HWND(handle.hwnd.get() as *mut _);+ let target = match self.presentation_system {+ wgt::Dx12SwapchainKind::DxgiFromHwnd => SurfaceTarget::WndHandle(handle),+ wgt::Dx12SwapchainKind::DxgiFromVisual => SurfaceTarget::VisualFromWndHandle {+ handle,+ dcomp_state: Default::default(),+ },+ };++ Ok(super::Surface {+ factory: self.factory.clone(),+ factory_media: self.factory_media.clone(),+ target,+ supports_allow_tearing: self.supports_allow_tearing,+ swap_chain: RwLock::new(None),+ options: self.options.clone(),+ hdr_source: Some(auxil::dxgi::hdr::DxgiHdrSource::new(handle)),+ })+ }+ _ => Err(crate::InstanceError::new(format!(+ "window handle {window_handle:?} is not a Win32 handle"+ ))),+ }+ }++ unsafe fn enumerate_adapters(+ &self,+ _surface_hint: Option<&super::Surface>,+ ) -> Vec<crate::ExposedAdapter<super::Api>> {+ let adapters = auxil::dxgi::factory::enumerate_adapters(self.factory.clone());++ adapters+ .into_iter()+ .filter_map(|raw| {+ super::Adapter::expose(+ raw,+ &self.library,+ &self.device_factory,+ &self.dcomp_lib,+ self.flags,+ self.memory_budget_thresholds,+ self.compiler_container.clone(),+ self.options.clone(),+ self.telemetry,+ )+ })+ .collect()+ }+}diff --git a/vendor/wgpu-hal/src/dx12/mod.rs b/vendor/wgpu-hal/src/dx12/mod.rsnew file mode 100644index 00000000..285d6356--- /dev/null+++ b/vendor/wgpu-hal/src/dx12/mod.rs@@ -0,0 +1,1908 @@+/*!+# DirectX12 API internals.++Generally the mapping is straightforward.++## Resource transitions++D3D12 API matches WebGPU internal states very well. The only+caveat here is issuing a special UAV barrier whenever both source+and destination states match, and they are for storage sync.++## Memory++For now, all resources are created with "committed" memory.++## Sampler Descriptor Management++At most one descriptor heap of each type can be bound at once. This+means that the descriptors from all bind groups need to be present+in the same heap, and they need to be contiguous within that heap.+This is not a problem for the SRV/CBV/UAV heap as it can be sized into+the millions of entries. However the sampler heap is limited to 2048 entries.++In order to work around this limitation, we refer to samplers indirectly by index.+The entire sampler heap is bound at once and a buffer containing all sampler indexes+for that bind group is bound. The shader then uses the index to look up the sampler+in the heap. To help visualize this, the generated HLSL looks like this:++```wgsl+@group(0) @binding(2) var myLinearSampler: sampler;+@group(1) @binding(1) var myAnisoSampler: sampler;+@group(1) @binding(4) var myCompSampler: sampler;+```++```cpp+// These bindings alias the same descriptors. Depending on the type, the shader will use the correct one.+SamplerState nagaSamplerHeap[2048]: register(s0, space0);+SamplerComparisonState nagaComparisonSamplerHeap[2048]: register(s2048, space1);++StructuredBuffer<uint> nagaGroup0SamplerIndexArray : register(t0, space0);+StructuredBuffer<uint> nagaGroup1SamplerIndexArray : register(t1, space0);++// Indexes into group 0 index array+static const SamplerState myLinearSampler = nagaSamplerHeap[nagaGroup0SamplerIndexArray[0]];++// Indexes into group 1 index array+static const SamplerState myAnisoSampler = nagaSamplerHeap[nagaGroup1SamplerIndexArray[0]];+static const SamplerComparisonState myCompSampler = nagaComparisonSamplerHeap[nagaGroup1SamplerIndexArray[1]];+```++Without this transform we would need separate set of sampler descriptors for each unique combination of samplers+in a bind group. This results in a lot of duplication and makes it easy to hit the 2048 limit. With the transform+the limit is merely 2048 unique samplers in existence, which is much more reasonable.++## Resource binding++See [`crate::Device::create_pipeline_layout`] documentation for the structure+of the root signature corresponding to WebGPU pipeline layout.++Binding groups is mostly straightforward, with one big caveat:+all bindings have to be reset whenever the root signature changes.+This is the rule of D3D12, and we can do nothing to help it.++We detect this change at both [`crate::CommandEncoder::set_bind_group`]+and [`crate::CommandEncoder::set_render_pipeline`] with+[`crate::CommandEncoder::set_compute_pipeline`].++For this reason, in order avoid repeating the binding code,+we are binding everything in `CommandEncoder::update_root_elements`.+When the pipeline layout is changed, we reset all bindings.+Otherwise, we pass a range corresponding only to the current bind group.++!*/++#![expect(+ missing_debug_implementations,+ reason = "TODO: someone developing on Windows add Debug impls where possible"+)]++mod adapter;+mod command;+mod conv;+mod dcomp;+mod descriptor;+mod device;+mod device_creation;+mod instance;+mod pipeline_desc;+mod sampler;+mod shader_compilation;+mod suballocation;+mod types;+mod view;++use alloc::{borrow::ToOwned as _, string::String, sync::Arc, vec::Vec};+use core::{ffi, fmt, mem, ops::Deref, sync::atomic::AtomicU64};++use arrayvec::ArrayVec;+use hashbrown::HashMap;+use parking_lot::{Mutex, RwLock};+use suballocation::Allocator;+use windows::{+ core::{Free as _, Interface},+ Win32::{+ Foundation,+ Graphics::{+ Direct3D,+ Direct3D12::{self, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT},+ DirectComposition, Dxgi,+ },+ System::Threading,+ },+};++use self::dcomp::DCompLib;+use crate::auxil::{+ self,+ dxgi::{+ factory::{DxgiAdapter, DxgiFactory},+ result::HResult,+ },+};++#[derive(Debug)]+struct DynLib {+ inner: libloading::Library,+}++impl DynLib {+ unsafe fn new<P>(filename: P) -> Result<Self, libloading::Error>+ where+ P: AsRef<std::ffi::OsStr>,+ {+ unsafe { libloading::Library::new(filename) }.map(|inner| Self { inner })+ }++ unsafe fn get<T>(+ &self,+ symbol: &[u8],+ ) -> Result<libloading::Symbol<'_, T>, crate::DeviceError> {+ unsafe { self.inner.get(symbol) }.map_err(|e| match e {+ libloading::Error::GetProcAddress { .. } | libloading::Error::GetProcAddressUnknown => {+ crate::DeviceError::Unexpected+ }+ libloading::Error::IncompatibleSize+ | libloading::Error::CreateCString { .. }+ | libloading::Error::CreateCStringWithTrailing { .. } => crate::hal_internal_error(e),+ _ => crate::DeviceError::Unexpected, // could be unreachable!() but we prefer to be more robust+ })+ }+}++#[derive(Debug)]+struct D3D12Lib {+ lib: DynLib,+}++#[derive(Clone, Copy, Debug)]+pub enum CreateDeviceError {+ GetProcAddress,+ D3D12CreateDevice(windows_core::HRESULT),+ RetDeviceIsNull,+}++impl D3D12Lib {+ fn new() -> Result<Self, libloading::Error> {+ unsafe { DynLib::new("d3d12.dll").map(|lib| Self { lib }) }+ }++ fn create_device(+ &self,+ adapter: &DxgiAdapter,+ feature_level: Direct3D::D3D_FEATURE_LEVEL,+ ) -> Result<Direct3D12::ID3D12Device, CreateDeviceError> {+ // Calls windows::Win32::Graphics::Direct3D12::D3D12CreateDevice on d3d12.dll+ type Fun = extern "system" fn(+ padapter: *mut ffi::c_void,+ minimumfeaturelevel: Direct3D::D3D_FEATURE_LEVEL,+ riid: *const windows_core::GUID,+ ppdevice: *mut *mut ffi::c_void,+ ) -> windows_core::HRESULT;+ let func: libloading::Symbol<Fun> =+ unsafe { self.lib.get(c"D3D12CreateDevice".to_bytes()) }+ .map_err(|_| CreateDeviceError::GetProcAddress)?;++ let mut result__: Option<Direct3D12::ID3D12Device> = None;++ let res = (func)(+ adapter.as_raw(),+ feature_level,+ // TODO: Generic?+ &Direct3D12::ID3D12Device::IID,+ <*mut _>::cast(&mut result__),+ );++ if res.is_err() {+ return Err(CreateDeviceError::D3D12CreateDevice(res));+ }++ result__.ok_or(CreateDeviceError::RetDeviceIsNull)+ }++ fn serialize_root_signature(+ &self,+ version: Direct3D12::D3D_ROOT_SIGNATURE_VERSION,+ parameters: &[Direct3D12::D3D12_ROOT_PARAMETER],+ static_samplers: &[Direct3D12::D3D12_STATIC_SAMPLER_DESC],+ flags: Direct3D12::D3D12_ROOT_SIGNATURE_FLAGS,+ ) -> Result<D3DBlob, crate::DeviceError> {+ // Calls windows::Win32::Graphics::Direct3D12::D3D12SerializeRootSignature on d3d12.dll+ type Fun = extern "system" fn(+ prootsignature: *const Direct3D12::D3D12_ROOT_SIGNATURE_DESC,+ version: Direct3D12::D3D_ROOT_SIGNATURE_VERSION,+ ppblob: *mut *mut ffi::c_void,+ pperrorblob: *mut *mut ffi::c_void,+ ) -> windows_core::HRESULT;+ let func: libloading::Symbol<Fun> =+ unsafe { self.lib.get(c"D3D12SerializeRootSignature".to_bytes()) }?;++ let desc = Direct3D12::D3D12_ROOT_SIGNATURE_DESC {+ NumParameters: parameters.len() as _,+ pParameters: parameters.as_ptr(),+ NumStaticSamplers: static_samplers.len() as _,+ pStaticSamplers: static_samplers.as_ptr(),+ Flags: flags,+ };++ let mut blob = None;+ let mut error = None::<Direct3D::ID3DBlob>;+ (func)(+ &desc,+ version,+ <*mut _>::cast(&mut blob),+ <*mut _>::cast(&mut error),+ )+ .ok()+ .into_device_result("Root signature serialization")?;++ if let Some(error) = error {+ let error = D3DBlob(error);+ log::error!(+ "Root signature serialization error: {:?}",+ unsafe { error.as_c_str() }.unwrap().to_str().unwrap()+ );+ return Err(crate::DeviceError::Unexpected); // could be hal_usage_error or hal_internal_error+ }++ blob.ok_or(crate::DeviceError::Unexpected)+ }++ fn debug_interface(&self) -> Result<Option<Direct3D12::ID3D12Debug>, crate::DeviceError> {+ // Calls windows::Win32::Graphics::Direct3D12::D3D12GetDebugInterface on d3d12.dll+ type Fun = extern "system" fn(+ riid: *const windows_core::GUID,+ ppvdebug: *mut *mut ffi::c_void,+ ) -> windows_core::HRESULT;+ let func: libloading::Symbol<Fun> =+ unsafe { self.lib.get(c"D3D12GetDebugInterface".to_bytes()) }?;++ let mut result__ = None;++ let res = (func)(&Direct3D12::ID3D12Debug::IID, <*mut _>::cast(&mut result__)).ok();++ if let Err(ref err) = res {+ match err.code() {+ Dxgi::DXGI_ERROR_SDK_COMPONENT_MISSING => return Ok(None),+ _ => {}+ }+ }++ res.into_device_result("GetDebugInterface")?;++ result__.ok_or(crate::DeviceError::Unexpected).map(Some)+ }++ /// Calls D3D12GetInterface to obtain a COM interface by CLSID and IID.+ ///+ /// This is used by the Independent Devices API to obtain `ID3D12SDKConfiguration1`.+ fn get_interface<T: Interface>(+ &self,+ clsid: &windows_core::GUID,+ ) -> Result<T, GetInterfaceError> {+ // Calls windows::Win32::Graphics::Direct3D12::D3D12GetInterface on d3d12.dll+ type Fun = extern "system" fn(+ rclsid: *const windows_core::GUID,+ riid: *const windows_core::GUID,+ ppvdebug: *mut *mut ffi::c_void,+ ) -> windows_core::HRESULT;+ let func: libloading::Symbol<Fun> =+ unsafe { self.lib.get(c"D3D12GetInterface".to_bytes()) }+ .map_err(|_| GetInterfaceError::GetProcAddress)?;++ let mut result__: Option<T> = None;++ let res = (func)(clsid, &T::IID, <*mut _>::cast(&mut result__));++ if res.is_err() {+ return Err(GetInterfaceError::D3D12GetInterface(res));+ }++ result__.ok_or(GetInterfaceError::RetIsNull)+ }+}++#[derive(Clone, Copy, Debug)]+pub(super) enum GetInterfaceError {+ GetProcAddress,+ D3D12GetInterface(windows_core::HRESULT),+ RetIsNull,+}++impl fmt::Display for GetInterfaceError {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ match self {+ Self::GetProcAddress => write!(f, "D3D12GetInterface not found in d3d12.dll"),+ Self::D3D12GetInterface(hr) => write!(f, "D3D12GetInterface failed: {hr}"),+ Self::RetIsNull => write!(f, "D3D12GetInterface returned null"),+ }+ }+}++impl core::error::Error for GetInterfaceError {}++#[derive(Debug)]+pub(super) struct DxgiLib {+ lib: DynLib,+}++impl DxgiLib {+ pub fn new() -> Result<Self, libloading::Error> {+ unsafe { DynLib::new("dxgi.dll").map(|lib| Self { lib }) }+ }++ /// Will error with crate::DeviceError::Unexpected if DXGI 1.3 is not available.+ pub fn debug_interface1(&self) -> Result<Option<Dxgi::IDXGIInfoQueue>, crate::DeviceError> {+ // Calls windows::Win32::Graphics::Dxgi::DXGIGetDebugInterface1 on dxgi.dll+ type Fun = extern "system" fn(+ flags: u32,+ riid: *const windows_core::GUID,+ pdebug: *mut *mut ffi::c_void,+ ) -> windows_core::HRESULT;+ let func: libloading::Symbol<Fun> =+ unsafe { self.lib.get(c"DXGIGetDebugInterface1".to_bytes()) }?;++ let mut result__ = None;++ let res = (func)(0, &Dxgi::IDXGIInfoQueue::IID, <*mut _>::cast(&mut result__)).ok();++ if let Err(ref err) = res {+ match err.code() {+ Dxgi::DXGI_ERROR_SDK_COMPONENT_MISSING => return Ok(None),+ _ => {}+ }+ }++ res.into_device_result("debug_interface1")?;++ result__.ok_or(crate::DeviceError::Unexpected).map(Some)+ }++ /// Will error with crate::DeviceError::Unexpected if DXGI 1.4 is not available.+ pub fn create_factory4(+ &self,+ factory_flags: Dxgi::DXGI_CREATE_FACTORY_FLAGS,+ ) -> Result<Dxgi::IDXGIFactory4, crate::DeviceError> {+ // Calls windows::Win32::Graphics::Dxgi::CreateDXGIFactory2 on dxgi.dll+ type Fun = extern "system" fn(+ flags: Dxgi::DXGI_CREATE_FACTORY_FLAGS,+ riid: *const windows_core::GUID,+ ppfactory: *mut *mut ffi::c_void,+ ) -> windows_core::HRESULT;+ let func: libloading::Symbol<Fun> =+ unsafe { self.lib.get(c"CreateDXGIFactory2".to_bytes()) }?;++ let mut result__ = None;++ (func)(+ factory_flags,+ &Dxgi::IDXGIFactory4::IID,+ <*mut _>::cast(&mut result__),+ )+ .ok()+ .into_device_result("create_factory4")?;++ result__.ok_or(crate::DeviceError::Unexpected)+ }++ /// Will error with crate::DeviceError::Unexpected if DXGI 1.3 is not available.+ pub fn create_factory_media(&self) -> Result<Dxgi::IDXGIFactoryMedia, crate::DeviceError> {+ // Calls windows::Win32::Graphics::Dxgi::CreateDXGIFactory1 on dxgi.dll+ type Fun = extern "system" fn(+ riid: *const windows_core::GUID,+ ppfactory: *mut *mut ffi::c_void,+ ) -> windows_core::HRESULT;+ let func: libloading::Symbol<Fun> =+ unsafe { self.lib.get(c"CreateDXGIFactory1".to_bytes()) }?;++ let mut result__ = None;++ // https://learn.microsoft.com/en-us/windows/win32/api/dxgi1_3/nn-dxgi1_3-idxgifactorymedia+ (func)(&Dxgi::IDXGIFactoryMedia::IID, <*mut _>::cast(&mut result__))+ .ok()+ .into_device_result("create_factory_media")?;++ result__.ok_or(crate::DeviceError::Unexpected)+ }+}++/// Create a temporary "owned" copy inside a [`mem::ManuallyDrop`] without increasing the refcount or+/// moving away the source variable.+///+/// This is a common pattern when needing to pass interface pointers ("borrows") into Windows+/// structs. Moving/cloning ownership is impossible/inconvenient because:+///+/// - The caller does _not_ assume ownership (and decrement the refcount at a later time);+/// - Unnecessarily increasing and decrementing the refcount;+/// - [`Drop`] destructors cannot run inside `union` structures (when the created structure is+/// implicitly dropped after a call).+///+/// See also <https://github.com/microsoft/windows-rs/pull/2361#discussion_r1150799401> and+/// <https://github.com/microsoft/windows-rs/issues/2386>.+///+/// # Safety+/// Performs a [`mem::transmute_copy()`] on a refcounted [`Interface`] type. The returned+/// [`mem::ManuallyDrop`] should _not_ be dropped.+pub unsafe fn borrow_interface_temporarily<I: Interface>(src: &I) -> mem::ManuallyDrop<Option<I>> {+ unsafe { mem::transmute_copy(src) }+}++/// See [`borrow_interface_temporarily()`]+pub unsafe fn borrow_optional_interface_temporarily<I: Interface>(+ src: &Option<I>,+) -> mem::ManuallyDrop<Option<I>> {+ unsafe { mem::transmute_copy(src) }+}++struct D3DBlob(Direct3D::ID3DBlob);++impl Deref for D3DBlob {+ type Target = Direct3D::ID3DBlob;++ fn deref(&self) -> &Self::Target {+ &self.0+ }+}++impl D3DBlob {+ unsafe fn as_slice(&self) -> &[u8] {+ unsafe { core::slice::from_raw_parts(self.GetBufferPointer().cast(), self.GetBufferSize()) }+ }++ unsafe fn as_c_str(&self) -> Result<&ffi::CStr, ffi::FromBytesUntilNulError> {+ ffi::CStr::from_bytes_until_nul(unsafe { self.as_slice() })+ }+}++#[derive(Clone, Debug)]+pub struct Api;++impl crate::Api for Api {+ const VARIANT: wgt::Backend = wgt::Backend::Dx12;++ type Instance = Instance;+ type Surface = Surface;+ type Adapter = Adapter;+ type Device = Device;++ type Queue = Queue;+ type CommandEncoder = CommandEncoder;+ type CommandBuffer = CommandBuffer;++ type Buffer = Buffer;+ type Texture = Texture;+ type SurfaceTexture = Texture;+ type TextureView = TextureView;+ type Sampler = Sampler;+ type QuerySet = QuerySet;+ type Fence = Fence;++ type BindGroupLayout = BindGroupLayout;+ type BindGroup = BindGroup;+ type PipelineLayout = PipelineLayout;+ type ShaderModule = ShaderModule;+ type RenderPipeline = RenderPipeline;+ type ComputePipeline = ComputePipeline;+ type RayTracingPipeline = RayTracingPipeline;+ type PipelineCache = PipelineCache;++ type AccelerationStructure = AccelerationStructure;+}++crate::impl_dyn_resource!(+ Adapter,+ AccelerationStructure,+ BindGroup,+ BindGroupLayout,+ Buffer,+ CommandBuffer,+ CommandEncoder,+ ComputePipeline,+ Device,+ Fence,+ Instance,+ PipelineCache,+ PipelineLayout,+ QuerySet,+ Queue,+ RayTracingPipeline,+ RenderPipeline,+ Sampler,+ ShaderModule,+ Surface,+ Texture,+ TextureView+);++// Limited by D3D12's root signature size of 64. Each element takes 1 or 2 entries.+const MAX_ROOT_ELEMENTS: usize = 64;+/// See comment in [`Adapter::expose`].+/// You must change the math in the comment before you update this value.+const MAX_IMMEDIATE_SIZE: u32 = 128;+const MAX_IMMEDIATES: usize = MAX_IMMEDIATE_SIZE as usize / 4;+const ZERO_BUFFER_SIZE: wgt::BufferAddress = 256 << 10;++pub struct Instance {+ factory: DxgiFactory,+ factory_media: Option<Dxgi::IDXGIFactoryMedia>,+ // `device_factory` must be dropped before `library` because the COM+ // object's Release call goes through the d3d12.dll vtable. If+ // `library` (which unloads d3d12.dll) is dropped first the Release+ // segfaults.+ device_factory: Arc<device_creation::DeviceFactory>,+ library: Arc<D3D12Lib>,+ dcomp_lib: Arc<DCompLib>,+ supports_allow_tearing: bool,+ presentation_system: wgt::Dx12SwapchainKind,+ _lib_dxgi: DxgiLib,+ flags: wgt::InstanceFlags,+ memory_budget_thresholds: wgt::MemoryBudgetThresholds,+ compiler_container: Arc<shader_compilation::CompilerContainer>,+ options: wgt::Dx12BackendOptions,+ telemetry: Option<crate::Telemetry>,+}++impl Instance {+ /// Get the raw DXGI factory associated with this instance.+ pub unsafe fn raw_factory4(&self) -> &Dxgi::IDXGIFactory4 {+ self.factory.deref()+ }++ pub unsafe fn create_surface_from_visual(&self, visual: *mut ffi::c_void) -> Surface {+ let visual = unsafe { DirectComposition::IDCompositionVisual::from_raw_borrowed(&visual) }+ .expect("COM pointer should not be NULL");+ Surface {+ factory: self.factory.clone(),+ factory_media: self.factory_media.clone(),+ target: SurfaceTarget::Visual(visual.to_owned()),+ supports_allow_tearing: self.supports_allow_tearing,+ swap_chain: RwLock::new(None),+ options: self.options.clone(),+ hdr_source: None,+ }+ }++ pub unsafe fn create_surface_from_surface_handle(+ &self,+ surface_handle: *mut ffi::c_void,+ ) -> Surface {+ // TODO: We're not given ownership, so we shouldn't call HANDLE::free(). This puts an extra burden on the caller to keep it alive.+ // https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-duplicatehandle could help us, even though DirectComposition is not in the list?+ // Or we make all these types owned, require an ownership transition, and replace SurfaceTargetUnsafe with SurfaceTarget.+ let surface_handle = Foundation::HANDLE(surface_handle);+ Surface {+ factory: self.factory.clone(),+ factory_media: self.factory_media.clone(),+ target: SurfaceTarget::SurfaceHandle(surface_handle),+ supports_allow_tearing: self.supports_allow_tearing,+ swap_chain: RwLock::new(None),+ options: self.options.clone(),+ hdr_source: None,+ }+ }++ pub unsafe fn create_surface_from_swap_chain_panel(+ &self,+ swap_chain_panel: *mut ffi::c_void,+ ) -> Surface {+ let swap_chain_panel =+ unsafe { types::ISwapChainPanelNative::from_raw_borrowed(&swap_chain_panel) }+ .expect("COM pointer should not be NULL");+ Surface {+ factory: self.factory.clone(),+ factory_media: self.factory_media.clone(),+ target: SurfaceTarget::SwapChainPanel(swap_chain_panel.to_owned()),+ supports_allow_tearing: self.supports_allow_tearing,+ swap_chain: RwLock::new(None),+ options: self.options.clone(),+ hdr_source: None,+ }+ }+}++unsafe impl Send for Instance {}+unsafe impl Sync for Instance {}++struct SwapChain {+ // TODO: Drop order frees the SWC before the raw image pointers...?+ raw: Dxgi::IDXGISwapChain3,+ // need to associate raw image pointers with the swapchain so they can be properly released+ // when the swapchain is destroyed+ resources: Vec<Direct3D12::ID3D12Resource>,+ /// Handle is freed in [`Self::release_resources()`]+ waitable: Option<Foundation::HANDLE>,+ acquired_count: usize,+ present_mode: wgt::PresentMode,+ format: wgt::TextureFormat,+ size: wgt::Extent3d,+}++enum SurfaceTarget {+ /// Borrowed, lifetime externally managed+ WndHandle(Foundation::HWND),+ /// `handle` is borrowed, lifetime externally managed+ VisualFromWndHandle {+ handle: Foundation::HWND,+ dcomp_state: Mutex<dcomp::DCompState>,+ },+ Visual(DirectComposition::IDCompositionVisual),+ /// Borrowed, lifetime externally managed+ SurfaceHandle(Foundation::HANDLE),+ SwapChainPanel(types::ISwapChainPanelNative),+}++pub struct Surface {+ factory: DxgiFactory,+ factory_media: Option<Dxgi::IDXGIFactoryMedia>,+ target: SurfaceTarget,+ supports_allow_tearing: bool,+ swap_chain: RwLock<Option<SwapChain>>,+ options: wgt::Dx12BackendOptions,+ /// HDR-info source for HWND-backed targets; `None` for composition /+ /// SwapChainPanel / surface-handle targets, which have no monitor identity.+ hdr_source: Option<auxil::dxgi::hdr::DxgiHdrSource>,+}++unsafe impl Send for Surface {}+unsafe impl Sync for Surface {}++impl Surface {+ pub fn swap_chain(&self) -> Option<Dxgi::IDXGISwapChain3> {+ Some(self.swap_chain.read().as_ref()?.raw.clone())+ }++ /// Returns the waitable handle associated with this swap chain, if any.+ /// Handle is only valid while the swap chain is alive.+ pub unsafe fn waitable_handle(&self) -> Option<Foundation::HANDLE> {+ self.swap_chain.read().as_ref()?.waitable+ }+}++#[derive(Debug, Clone, Copy)]+enum MemoryArchitecture {+ Unified {+ #[allow(unused)]+ cache_coherent: bool,+ },+ NonUnified,+}++#[derive(Debug, Clone, Copy)]+struct PrivateCapabilities {+ instance_flags: wgt::InstanceFlags,+ workarounds: Workarounds,+ #[allow(unused)]+ heterogeneous_resource_heaps: bool,+ memory_architecture: MemoryArchitecture,+ heap_create_not_zeroed: bool,+ casting_fully_typed_format_supported: bool,+ suballocation_supported: bool,+ shader_model: naga::back::hlsl::ShaderModel,+ max_sampler_descriptor_heap_size: u32,+ unrestricted_buffer_texture_copy_pitch_supported: bool,+}++impl PrivateCapabilities {+ fn texture_data_placement_alignment(&self) -> u64 {+ if self.unrestricted_buffer_texture_copy_pitch_supported {+ 4+ } else {+ D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT.into()+ }+ }+}++#[derive(Default, Debug, Copy, Clone)]+struct Workarounds {+ // On WARP 1.0.13+, debug information in shaders in certain situations causes the device+ // to hang. https://github.com/gfx-rs/wgpu/issues/8368+ avoid_shader_debug_info: bool,+}++pub struct Adapter {+ raw: DxgiAdapter,+ device: Direct3D12::ID3D12Device,+ library: Arc<D3D12Lib>,+ dcomp_lib: Arc<DCompLib>,+ private_caps: PrivateCapabilities,+ presentation_timer: auxil::dxgi::time::PresentationTimer,+ memory_budget_thresholds: wgt::MemoryBudgetThresholds,+ compiler_container: Arc<shader_compilation::CompilerContainer>,+ options: wgt::Dx12BackendOptions,+}++unsafe impl Send for Adapter {}+unsafe impl Sync for Adapter {}++impl Adapter {+ pub fn as_raw(&self) -> &Dxgi::IDXGIAdapter3 {+ &self.raw+ }+}++struct Event(pub Foundation::HANDLE);+impl Event {+ pub fn create(manual_reset: bool, initial_state: bool) -> Result<Self, crate::DeviceError> {+ Ok(Self(+ unsafe { Threading::CreateEventA(None, manual_reset, initial_state, None) }+ .into_device_result("CreateEventA")?,+ ))+ }+}++impl Drop for Event {+ fn drop(&mut self) {+ unsafe { Foundation::HANDLE::free(&mut self.0) }+ }+}++/// Helper structure for waiting for GPU.+struct Idler {+ fence: Direct3D12::ID3D12Fence,+}++#[derive(Debug, Clone)]+struct CommandSignatures {+ draw: Direct3D12::ID3D12CommandSignature,+ draw_indexed: Direct3D12::ID3D12CommandSignature,+ draw_mesh: Option<Direct3D12::ID3D12CommandSignature>,+ dispatch: Direct3D12::ID3D12CommandSignature,+}++struct DeviceShared {+ adapter: DxgiAdapter,+ zero_buffer: Direct3D12::ID3D12Resource,+ cmd_signatures: CommandSignatures,+ heap_views: descriptor::GeneralHeap,+ sampler_heap: sampler::SamplerHeap,+ private_caps: PrivateCapabilities,+}++unsafe impl Send for DeviceShared {}+unsafe impl Sync for DeviceShared {}++pub struct Device {+ raw: Direct3D12::ID3D12Device,+ present_queue: Direct3D12::ID3D12CommandQueue,+ idler: Idler,+ features: wgt::Features,+ shared: Arc<DeviceShared>,+ options: wgt::Dx12BackendOptions,+ // CPU only pools+ rtv_pool: Arc<Mutex<descriptor::CpuPool>>,+ dsv_pool: Mutex<descriptor::CpuPool>,+ srv_uav_pool: Mutex<descriptor::CpuPool>,+ // library+ library: Arc<D3D12Lib>,+ dcomp_lib: Arc<DCompLib>,+ #[cfg(feature = "renderdoc")]+ render_doc: auxil::renderdoc::RenderDoc,+ null_rtv_handle: descriptor::Handle,+ mem_allocator: Allocator,+ compiler_container: Arc<shader_compilation::CompilerContainer>,+ shader_cache: Mutex<ShaderCache>,+ counters: Arc<wgt::HalCounters>,+ limits: wgt::Limits,+}++impl Drop for Device {+ fn drop(&mut self) {+ self.rtv_pool.lock().free_handle(self.null_rtv_handle);+ if self+ .shared+ .private_caps+ .instance_flags+ .contains(wgt::InstanceFlags::VALIDATION)+ {+ auxil::dxgi::exception::unregister_exception_handler();+ }+ }+}++unsafe impl Send for Device {}+unsafe impl Sync for Device {}++pub struct Queue {+ raw: Direct3D12::ID3D12CommandQueue,+ temp_lists: Mutex<Vec<Option<Direct3D12::ID3D12CommandList>>>,+ idle_fence: Direct3D12::ID3D12Fence,+ idle_event: Event,+ idle_fence_value: AtomicU64,+ pending_waits: Mutex<Vec<(Direct3D12::ID3D12Fence, u64)>>,+ pending_signals: Mutex<Vec<(Direct3D12::ID3D12Fence, u64)>>,+}++impl Queue {+ pub fn as_raw(&self) -> &Direct3D12::ID3D12CommandQueue {+ &self.raw+ }++ /// Stage a `ID3D12CommandQueue::Wait(fence, value)` for the next+ /// [`crate::Queue::submit`]. The wait is enqueued before the+ /// submit's command lists, so subsequent GPU work observes the+ /// foreign signal at `value`.+ pub fn add_wait_fence(&self, fence: Direct3D12::ID3D12Fence, value: u64) {+ self.pending_waits.lock().push((fence, value));+ }++ /// Remove `fence` from the pending wait list if it is still present.+ /// Returns `true` if it was found and removed.+ pub fn remove_wait_fence(&self, fence: &Direct3D12::ID3D12Fence) -> bool {+ let target = fence.as_raw();+ let mut waits = self.pending_waits.lock();+ let before = waits.len();+ waits.retain(|(f, _)| f.as_raw() != target);+ waits.len() != before+ }++ /// Stage a `ID3D12CommandQueue::Signal(fence, value)` for the next+ /// [`crate::Queue::submit`]. The signal is enqueued after the+ /// submit's command lists complete, so a foreign API waiting on+ /// `(fence, value)` observes the wgpu work as done.+ pub fn add_signal_fence(&self, fence: Direct3D12::ID3D12Fence, value: u64) {+ self.pending_signals.lock().push((fence, value));+ }++ /// Remove `fence` from the pending signal list if it is still present.+ /// Returns `true` if it was found and removed.+ pub fn remove_signal_fence(&self, fence: &Direct3D12::ID3D12Fence) -> bool {+ let target = fence.as_raw();+ let mut signals = self.pending_signals.lock();+ let before = signals.len();+ signals.retain(|(f, _)| f.as_raw() != target);+ signals.len() != before+ }+}++unsafe impl Send for Queue {}+unsafe impl Sync for Queue {}++#[derive(Default)]+struct Temp {+ marker: Vec<u16>,+ barriers: Vec<Direct3D12::D3D12_RESOURCE_BARRIER>,+}++impl Temp {+ fn clear(&mut self) {+ self.marker.clear();+ self.barriers.clear();+ }+}++struct PassResolve {+ src: (Direct3D12::ID3D12Resource, u32),+ dst: (Direct3D12::ID3D12Resource, u32),+ format: Dxgi::Common::DXGI_FORMAT,+}++#[derive(Clone, Copy, Debug, Default, PartialEq)]+struct SpecialConstants {+ /// The first vertex in an indirect draw call, _or_ the `x` of a compute dispatch.+ first_vertex_or_x: i32,+ /// The first instance in an indirect draw call, _or_ the `y` of a compute dispatch.+ first_instance_or_y: u32,+ /// Unused in an indirect draw call, _or_ the `z` of a compute dispatch.+ unused_or_z: u32,+}++impl SpecialConstants {+ fn from_indirect_draw_call_params(first_vertex: i32, first_instance: u32) -> Self {+ Self {+ first_vertex_or_x: first_vertex,+ first_instance_or_y: first_instance,+ unused_or_z: 0,+ }+ }++ fn from_compute_dispatch_params(workgroup_count: [u32; 3]) -> Self {+ Self {+ first_vertex_or_x: workgroup_count[0] as i32,+ first_instance_or_y: workgroup_count[1],+ unused_or_z: workgroup_count[2],+ }+ }+}++#[derive(Clone, Copy, Debug)]+enum RootElement {+ Empty,+ Immediates,+ SpecialConstants(SpecialConstants),+ DescriptorTable(Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE),+ /// Descriptor table referring to the entire sampler heap.+ SamplerHeapDescriptorTable,+ /// Root descriptor for a uniform buffer binding that has a dynamic offset.+ DynamicUniformBuffer {+ address: Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE,+ },+ /// Root constants for storage buffer bindings with dynamic offsets.+ ///+ /// start..end is the range of values in [`PassState::dynamic_storage_buffer_offsets`]+ /// that will be used to update the root constants.+ DynamicStorageBufferOffsets {+ start: usize,+ end: usize,+ },+}++#[derive(Clone, Copy)]+enum PassKind {+ Render,+ Compute,+ Transfer,+}++struct PassState {+ has_label: bool,+ resolves: ArrayVec<PassResolve, { crate::MAX_COLOR_ATTACHMENTS }>,+ layout: PipelineLayoutShared,+ root_elements: [RootElement; MAX_ROOT_ELEMENTS],+ immediates: [u32; MAX_IMMEDIATES],+ dynamic_storage_buffer_offsets: Vec<u32>,+ dirty_root_elements: u64,+ vertex_buffers: [Direct3D12::D3D12_VERTEX_BUFFER_VIEW; crate::MAX_VERTEX_BUFFERS],+ dirty_vertex_buffers: usize,+ kind: PassKind,+}++// `root_elements` size must match `dirty_root_elements` bit size+const _: () = assert!(MAX_ROOT_ELEMENTS == u64::BITS as usize);++impl PassState {+ fn new() -> Self {+ PassState {+ has_label: false,+ resolves: ArrayVec::new(),+ layout: PipelineLayoutShared {+ signature: None,+ total_root_elements: 0,+ special_constants: None,+ immediates_info: None,+ sampler_heap_root_index: None,+ },+ root_elements: [RootElement::Empty; MAX_ROOT_ELEMENTS],+ immediates: [0; MAX_IMMEDIATES],+ dynamic_storage_buffer_offsets: Vec::new(),+ dirty_root_elements: 0,+ vertex_buffers: [Default::default(); crate::MAX_VERTEX_BUFFERS],+ dirty_vertex_buffers: 0,+ kind: PassKind::Transfer,+ }+ }++ fn clear(&mut self) {+ // careful about heap allocations!+ *self = Self::new();+ }+}++// Any state in this struct that may be dirty after an abandoned encoding must+// be reset for reused encoders in `begin_encoding`.+pub struct CommandEncoder {+ allocator: Direct3D12::ID3D12CommandAllocator,+ device: Direct3D12::ID3D12Device,+ shared: Arc<DeviceShared>,+ mem_allocator: Allocator,++ rtv_pool: Arc<Mutex<descriptor::CpuPool>>,+ temp_rtv_handles: Vec<descriptor::Handle>,++ intermediate_copy_bufs: Vec<Buffer>,++ null_rtv_handle: descriptor::Handle,+ list: Option<Direct3D12::ID3D12GraphicsCommandList>,+ free_lists: Vec<Direct3D12::ID3D12GraphicsCommandList>,+ pass: PassState,+ temp: Temp,++ /// If set, the end of the next render/compute pass will write a timestamp at+ /// the given pool & location.+ end_of_pass_timer_query: Option<(Direct3D12::ID3D12QueryHeap, u32)>,++ counters: Arc<wgt::HalCounters>,+}++unsafe impl Send for CommandEncoder {}+unsafe impl Sync for CommandEncoder {}++impl fmt::Debug for CommandEncoder {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ f.debug_struct("CommandEncoder")+ .field("allocator", &self.allocator)+ .field("device", &self.allocator)+ .finish()+ }+}++#[derive(Debug)]+pub struct CommandBuffer {+ raw: Direct3D12::ID3D12GraphicsCommandList,+}++impl crate::DynCommandBuffer for CommandBuffer {}++unsafe impl Send for CommandBuffer {}+unsafe impl Sync for CommandBuffer {}++#[derive(Debug)]+pub struct Buffer {+ resource: Direct3D12::ID3D12Resource,+ // While the allocation also has _a_ size, it may not+ // be the same as the original size of the buffer,+ // as the allocation size varies for assorted reasons.+ size: wgt::BufferAddress,+ allocation: suballocation::Allocation,+}++impl Buffer {+ pub unsafe fn raw_resource(&self) -> &Direct3D12::ID3D12Resource {+ &self.resource+ }+}++unsafe impl Send for Buffer {}+unsafe impl Sync for Buffer {}++impl crate::DynBuffer for Buffer {}++impl crate::BufferBinding<'_, Buffer> {+ fn resolve_size(&self) -> wgt::BufferAddress {+ match self.size {+ Some(size) => size.get(),+ None => self.buffer.size - self.offset,+ }+ }++ // TODO: Return GPU handle directly?+ fn resolve_address(&self) -> wgt::BufferAddress {+ (unsafe { self.buffer.resource.GetGPUVirtualAddress() }) + self.offset+ }+}++#[derive(Debug)]+pub struct Texture {+ resource: Direct3D12::ID3D12Resource,+ format: wgt::TextureFormat,+ dimension: wgt::TextureDimension,+ size: wgt::Extent3d,+ mip_level_count: u32,+ sample_count: u32,+ allocation: suballocation::Allocation,+ /// Pins `PlaneSlice` for every view and copy derived from this texture,+ /// overriding the default aspect-based derivation. Used by cross-API+ /// importers wrapping one plane of a multi-plane DXGI resource as a+ /// single-plane wgpu texture.+ plane_slice_override: Option<u32>,+}++impl Texture {+ pub unsafe fn raw_resource(&self) -> &Direct3D12::ID3D12Resource {+ &self.resource+ }++ /// Pin the D3D12 plane slice for every view and copy derived from+ /// this texture. Pass `0` for the luma plane, `1` for chroma. The+ /// declared `TextureFormat` must be single-plane (e.g. `R8Unorm`,+ /// `Rg8Unorm`) and match the plane's texel layout.+ ///+ /// # Safety / aliasing+ ///+ /// The intended use is to wrap one plane of a multi-plane DXGI+ /// resource (e.g. NV12) as a single-plane wgpu texture, typically+ /// by calling [`Device::texture_from_raw`](Device::texture_from_raw)+ /// twice with two clones of the same `ID3D12Resource` and a+ /// different `plane_slice` on each. wgpu's state tracker treats+ /// these wrappers as independent textures: per-subresource state,+ /// hazard tracking, and resource-state barriers all assume+ /// non-aliased ownership and have no way to know the two wrappers+ /// alias the same underlying resource.+ ///+ /// The caller is responsible for ensuring that the underlying+ /// resource is not concurrently used by another wgpu texture+ /// wrapping a different plane in a way that would race or+ /// invalidate state tracking — in particular, do not submit work+ /// touching both wrappers in the same submission unless every+ /// access goes through `COPY_SRC` / `COPY_DST` on the wrapper that+ /// owns that plane's subresource, and do not destroy the wrappers+ /// concurrently with in-flight work that uses either of them.+ pub fn with_plane_slice(mut self, plane_slice: u32) -> Self {+ debug_assert!(+ !self.format.is_multi_planar_format(),+ "`with_plane_slice` expects a single-plane format wrapping a \+ multi-plane DXGI resource; got planar format `{:?}`",+ self.format,+ );+ self.plane_slice_override = Some(plane_slice);+ self+ }++ pub(super) fn plane_slice_override(&self) -> Option<u32> {+ self.plane_slice_override+ }+}++impl crate::DynTexture for Texture {}+impl crate::DynSurfaceTexture for Texture {}++impl core::borrow::Borrow<dyn crate::DynTexture> for Texture {+ fn borrow(&self) -> &dyn crate::DynTexture {+ self+ }+}++unsafe impl Send for Texture {}+unsafe impl Sync for Texture {}++impl Texture {+ fn array_layer_count(&self) -> u32 {+ match self.dimension {+ wgt::TextureDimension::D1 | wgt::TextureDimension::D3 => 1,+ wgt::TextureDimension::D2 => self.size.depth_or_array_layers,+ }+ }++ /// see <https://learn.microsoft.com/en-us/windows/win32/direct3d12/subresources#plane-slice>+ fn calc_subresource(&self, mip_level: u32, array_layer: u32, plane: u32) -> u32 {+ mip_level + (array_layer + plane * self.array_layer_count()) * self.mip_level_count+ }++ fn calc_subresource_for_copy(&self, base: &crate::TextureCopyBase) -> u32 {+ // Single-plane wrappers around a multi-plane resource report+ // `COLOR` aspect, which would otherwise resolve to plane 0.+ let plane = self+ .plane_slice_override+ .unwrap_or_else(|| match base.aspect {+ crate::FormatAspects::COLOR+ | crate::FormatAspects::DEPTH+ | crate::FormatAspects::PLANE_0 => 0,+ crate::FormatAspects::STENCIL | crate::FormatAspects::PLANE_1 => 1,+ crate::FormatAspects::PLANE_2 => 2,+ _ => unreachable!(),+ });+ self.calc_subresource(base.mip_level, base.array_layer, plane)+ }+}++#[derive(Debug)]+pub struct TextureView {+ raw_format: Dxgi::Common::DXGI_FORMAT,+ aspects: crate::FormatAspects,+ dimension: wgt::TextureViewDimension,+ texture: Direct3D12::ID3D12Resource,+ subresource_index: u32,+ mip_slice: u32,+ handle_srv: Option<descriptor::Handle>,+ handle_uav: Option<descriptor::Handle>,+ handle_rtv: Option<descriptor::Handle>,+ handle_dsv_ro: Option<descriptor::Handle>,+ handle_dsv_rw: Option<descriptor::Handle>,+}++impl crate::DynTextureView for TextureView {}++unsafe impl Send for TextureView {}+unsafe impl Sync for TextureView {}++#[derive(Debug)]+pub struct Sampler {+ index: sampler::SamplerIndex,+ desc: Direct3D12::D3D12_SAMPLER_DESC,+}++impl crate::DynSampler for Sampler {}++#[cfg(send_sync)]+static_assertions::assert_impl_all!(Sampler: Send, Sync);++#[derive(Debug)]+pub struct QuerySet {+ raw: Direct3D12::ID3D12QueryHeap,+ raw_ty: Direct3D12::D3D12_QUERY_TYPE,+}++impl crate::DynQuerySet for QuerySet {}++unsafe impl Send for QuerySet {}+unsafe impl Sync for QuerySet {}++#[derive(Debug)]+pub struct Fence {+ raw: Direct3D12::ID3D12Fence,+}++impl crate::DynFence for Fence {}++unsafe impl Send for Fence {}+unsafe impl Sync for Fence {}++impl Fence {+ pub fn raw_fence(&self) -> &Direct3D12::ID3D12Fence {+ &self.raw+ }+}++#[derive(Debug)]+pub struct BindGroupLayout {+ /// Sorted list of entries.+ entries: Vec<wgt::BindGroupLayoutEntry>,+ cpu_heap_views: Option<descriptor::CpuHeap>,+ copy_counts: Vec<u32>, // all 1's+}++impl crate::DynBindGroupLayout for BindGroupLayout {}++#[derive(Debug, Clone, Copy)]+enum DynamicBuffer {+ Uniform(Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE),+ Storage,+}++#[derive(Debug)]+struct SamplerIndexBuffer {+ buffer: Direct3D12::ID3D12Resource,+ allocation: suballocation::Allocation,+}++#[derive(Debug)]+pub struct BindGroup {+ handle_views: Option<descriptor::DualHandle>,+ sampler_index_buffer: Option<SamplerIndexBuffer>,+ dynamic_buffers: Vec<DynamicBuffer>,+}++impl crate::DynBindGroup for BindGroup {}++bitflags::bitflags! {+ #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]+ struct TableTypes: u8 {+ const SRV_CBV_UAV = 1 << 0;+ const SAMPLERS = 1 << 1;+ }+}++// Element (also known as parameter) index into the root signature.+type RootIndex = u32;++#[derive(Debug)]+struct BindGroupInfo {+ base_root_index: RootIndex,+ tables: TableTypes,+ dynamic_storage_buffer_offsets: Option<DynamicStorageBufferOffsets>,+}++#[derive(Debug, Clone)]+struct ImmediatesInfo {+ root_index: RootIndex,+ size: u32,+}++#[derive(Debug, Clone)]+struct DynamicStorageBufferOffsets {+ root_index: RootIndex,+ range: core::ops::Range<usize>,+}++#[derive(Debug, Clone)]+struct PipelineLayoutShared {+ signature: Option<Direct3D12::ID3D12RootSignature>,+ total_root_elements: RootIndex,+ special_constants: Option<PipelineLayoutSpecialConstants>,+ immediates_info: Option<ImmediatesInfo>,+ sampler_heap_root_index: Option<RootIndex>,+}++unsafe impl Send for PipelineLayoutShared {}+unsafe impl Sync for PipelineLayoutShared {}++#[derive(Debug, Clone)]+struct PipelineLayoutSpecialConstants {+ root_index: RootIndex,+ indirect_cmd_signatures: Option<CommandSignatures>,+}++unsafe impl Send for PipelineLayoutSpecialConstants {}+unsafe impl Sync for PipelineLayoutSpecialConstants {}++#[derive(Debug)]+pub struct PipelineLayout {+ shared: PipelineLayoutShared,+ // Storing for each associated bind group, which tables we created+ // in the root signature. This is required for binding descriptor sets.+ bind_group_infos: [Option<BindGroupInfo>; crate::MAX_BIND_GROUPS],+ naga_options: naga::back::hlsl::Options,+}++impl crate::DynPipelineLayout for PipelineLayout {}++#[derive(Debug)]+pub struct ShaderModule {+ source: ShaderModuleSource,+ raw_name: Option<alloc::ffi::CString>,+ runtime_checks: wgt::ShaderRuntimeChecks,+}++impl crate::DynShaderModule for ShaderModule {}++#[derive(Default)]+pub struct ShaderCache {+ nr_of_shaders_compiled: u32,+ entries: HashMap<ShaderCacheKey, ShaderCacheValue>,+}++#[derive(PartialEq, Eq, Hash)]+pub(super) struct ShaderCacheKey {+ source: String,+ entry_point: String,+ stage: naga::ShaderStage,+ shader_model: naga::back::hlsl::ShaderModel,+}++pub(super) struct ShaderCacheValue {+ /// This is the value of [`ShaderCache::nr_of_shaders_compiled`]+ /// at the time the cache entry was last used.+ last_used: u32,+ shader: CompiledShader,+}++#[derive(Clone)]+pub(super) enum CompiledShader {+ Dxc(Direct3D::Dxc::IDxcBlob),+ Fxc(Direct3D::ID3DBlob),+ Precompiled(Vec<u8>),+}++impl CompiledShader {+ fn create_native_shader(&self) -> Direct3D12::D3D12_SHADER_BYTECODE {+ match self {+ CompiledShader::Dxc(shader) => Direct3D12::D3D12_SHADER_BYTECODE {+ pShaderBytecode: unsafe { shader.GetBufferPointer() },+ BytecodeLength: unsafe { shader.GetBufferSize() },+ },+ CompiledShader::Fxc(shader) => Direct3D12::D3D12_SHADER_BYTECODE {+ pShaderBytecode: unsafe { shader.GetBufferPointer() },+ BytecodeLength: unsafe { shader.GetBufferSize() },+ },+ CompiledShader::Precompiled(shader) => Direct3D12::D3D12_SHADER_BYTECODE {+ pShaderBytecode: shader.as_ptr().cast(),+ BytecodeLength: shader.len(),+ },+ }+ }+}++#[derive(Debug)]+pub struct RenderPipeline {+ raw: Direct3D12::ID3D12PipelineState,+ layout: PipelineLayoutShared,+ topology: Direct3D::D3D_PRIMITIVE_TOPOLOGY,+ vertex_strides: [Option<u32>; crate::MAX_VERTEX_BUFFERS],+}++impl crate::DynRenderPipeline for RenderPipeline {}++unsafe impl Send for RenderPipeline {}+unsafe impl Sync for RenderPipeline {}++#[derive(Debug)]+pub struct ComputePipeline {+ raw: Direct3D12::ID3D12PipelineState,+ layout: PipelineLayoutShared,+}++impl crate::DynComputePipeline for ComputePipeline {}++unsafe impl Send for ComputePipeline {}+unsafe impl Sync for ComputePipeline {}++#[derive(Debug)]+pub struct RayTracingPipeline {}++impl crate::DynRayTracingPipeline for RayTracingPipeline {}++#[derive(Debug)]+pub struct PipelineCache;++impl crate::DynPipelineCache for PipelineCache {}++#[derive(Debug)]+pub struct AccelerationStructure {+ resource: Direct3D12::ID3D12Resource,+ allocation: suballocation::Allocation,+}++impl crate::DynAccelerationStructure for AccelerationStructure {}++impl SwapChain {+ unsafe fn release_resources(mut self) -> Dxgi::IDXGISwapChain3 {+ if let Some(mut waitable) = self.waitable.take() {+ unsafe { Foundation::HANDLE::free(&mut waitable) };+ }+ self.raw+ }++ unsafe fn wait(+ &mut self,+ timeout: Option<core::time::Duration>,+ ) -> Result<bool, crate::SurfaceError> {+ let timeout_ms = match timeout {+ Some(duration) => duration.as_millis() as u32,+ None => Threading::INFINITE,+ };++ if let Some(waitable) = self.waitable {+ match unsafe { Threading::WaitForSingleObject(waitable, timeout_ms) } {+ Foundation::WAIT_ABANDONED | Foundation::WAIT_FAILED => {+ Err(crate::SurfaceError::Lost)+ }+ Foundation::WAIT_OBJECT_0 => Ok(true),+ Foundation::WAIT_TIMEOUT => Ok(false),+ other => {+ log::error!("Unexpected wait status: 0x{other:x?}");+ Err(crate::SurfaceError::Lost)+ }+ }+ } else {+ Ok(true)+ }+ }+}++fn map_surface_color_space(+ color_space: wgt::SurfaceColorSpace,+) -> Dxgi::Common::DXGI_COLOR_SPACE_TYPE {+ use wgt::SurfaceColorSpace as Scs;+ match color_space {+ Scs::Srgb => Dxgi::Common::DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709,+ Scs::ExtendedSrgbLinear => Dxgi::Common::DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709,+ Scs::Bt2100Pq => Dxgi::Common::DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020,+ Scs::Auto+ | Scs::DisplayP3+ | Scs::Bt2100Hlg+ | Scs::ExtendedSrgb+ | Scs::ExtendedDisplayP3 => {+ unreachable!("`{color_space:?}` is never reported in the DX12 surface capabilities")+ }+ }+}++impl crate::Surface for Surface {+ type A = Api;++ unsafe fn configure(+ &self,+ device: &Device,+ config: &crate::SurfaceConfiguration,+ ) -> Result<(), crate::SurfaceError> {+ let mut flags = Dxgi::DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT;+ // We always set ALLOW_TEARING on the swapchain no matter+ // what kind of swapchain we want because ResizeBuffers+ // cannot change the swapchain's ALLOW_TEARING flag.+ //+ // This does not change the behavior of the swapchain, just+ // allow present calls to use tearing.+ if self.supports_allow_tearing {+ flags |= Dxgi::DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING;+ }++ // While `configure`s contract ensures that no work on the GPU's main queues+ // are in flight, we still need to wait for the present queue to be idle.+ unsafe { device.wait_for_present_queue_idle() }?;++ let non_srgb_format = auxil::dxgi::conv::map_texture_format_nosrgb(config.format);++ // The range for `SetMaximumFrameLatency` is 1-16 so the maximum latency requested should be 15 because we add 1.+ // https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgidevice1-setmaximumframelatency+ debug_assert!(config.maximum_frame_latency <= 15);++ // Nvidia recommends to use 1-2 more buffers than the maximum latency+ // https://developer.nvidia.com/blog/advanced-api-performance-swap-chains/+ // For high latency extra buffers seems excessive, so go with a minimum of 3 and beyond that add 1.+ let swap_chain_buffer = (config.maximum_frame_latency + 1).min(16);++ let swap_chain = match self.swap_chain.write().take() {+ //Note: this path doesn't properly re-initialize all of the things+ Some(sc) => {+ let raw = unsafe { sc.release_resources() };+ let result = unsafe {+ raw.ResizeBuffers(+ swap_chain_buffer,+ config.extent.width,+ config.extent.height,+ non_srgb_format,+ flags,+ )+ };+ if let Err(err) = result {+ log::error!("ResizeBuffers failed: {err}");+ return Err(crate::SurfaceError::Other("window is in use"));+ }+ raw+ }+ None => {+ let desc = Dxgi::DXGI_SWAP_CHAIN_DESC1 {+ AlphaMode: auxil::dxgi::conv::map_acomposite_alpha_mode(+ config.composite_alpha_mode,+ ),+ Width: config.extent.width,+ Height: config.extent.height,+ Format: non_srgb_format,+ Stereo: false.into(),+ SampleDesc: Dxgi::Common::DXGI_SAMPLE_DESC {+ Count: 1,+ Quality: 0,+ },+ BufferUsage: Dxgi::DXGI_USAGE_RENDER_TARGET_OUTPUT,+ BufferCount: swap_chain_buffer,+ Scaling: Dxgi::DXGI_SCALING_STRETCH,+ SwapEffect: Dxgi::DXGI_SWAP_EFFECT_FLIP_DISCARD,+ Flags: flags.0 as u32,+ };+ let swap_chain1 = match self.target {+ SurfaceTarget::Visual(_)+ | SurfaceTarget::VisualFromWndHandle { .. }+ | SurfaceTarget::SwapChainPanel(_) => {+ profiling::scope!("IDXGIFactory2::CreateSwapChainForComposition");+ unsafe {+ self.factory.CreateSwapChainForComposition(+ &device.present_queue,+ &desc,+ None,+ )+ }+ }+ SurfaceTarget::SurfaceHandle(handle) => {+ profiling::scope!(+ "IDXGIFactoryMedia::CreateSwapChainForCompositionSurfaceHandle"+ );+ unsafe {+ self.factory_media+ .as_ref()+ .ok_or(crate::SurfaceError::Other("IDXGIFactoryMedia not found"))?+ .CreateSwapChainForCompositionSurfaceHandle(+ &device.present_queue,+ Some(handle),+ &desc,+ None,+ )+ }+ }+ SurfaceTarget::WndHandle(hwnd) => {+ profiling::scope!("IDXGIFactory2::CreateSwapChainForHwnd");+ unsafe {+ self.factory.CreateSwapChainForHwnd(+ &device.present_queue,+ hwnd,+ &desc,+ None,+ None,+ )+ }+ }+ };++ let swap_chain1 = swap_chain1.map_err(|err| {+ log::error!("SwapChain creation error: {err}");+ crate::SurfaceError::Other("swapchain creation")+ })?;++ match &self.target {+ SurfaceTarget::WndHandle(_) | SurfaceTarget::SurfaceHandle(_) => {}+ SurfaceTarget::VisualFromWndHandle {+ handle,+ dcomp_state,+ } => {+ let mut dcomp_state = dcomp_state.lock();+ let dcomp_state =+ unsafe { dcomp_state.get_or_init(&device.dcomp_lib, handle) }?;+ // Set the new swap chain as the content for the backing visual+ // and commit the changes to the composition visual tree.+ {+ profiling::scope!("IDCompositionVisual::SetContent");+ unsafe { dcomp_state.visual.SetContent(&swap_chain1) }.map_err(+ |err| {+ log::error!("IDCompositionVisual::SetContent failed: {err}");+ crate::SurfaceError::Other("IDCompositionVisual::SetContent")+ },+ )?;+ }++ // Commit the changes to the composition device.+ {+ profiling::scope!("IDCompositionDevice::Commit");+ unsafe { dcomp_state.device.Commit() }.map_err(|err| {+ log::error!("IDCompositionDevice::Commit failed: {err}");+ crate::SurfaceError::Other("IDCompositionDevice::Commit")+ })?;+ }+ }+ SurfaceTarget::Visual(visual) => {+ if let Err(err) = unsafe { visual.SetContent(&swap_chain1) } {+ log::error!("Unable to SetContent: {err}");+ return Err(crate::SurfaceError::Other(+ "IDCompositionVisual::SetContent",+ ));+ }+ }+ SurfaceTarget::SwapChainPanel(swap_chain_panel) => {+ if let Err(err) = unsafe { swap_chain_panel.SetSwapChain(&swap_chain1) } {+ log::error!("Unable to SetSwapChain: {err}");+ return Err(crate::SurfaceError::Other(+ "ISwapChainPanelNative::SetSwapChain",+ ));+ }+ }+ }++ swap_chain1.cast::<Dxgi::IDXGISwapChain3>().map_err(|err| {+ log::error!("Unable to cast swapchain: {err}");+ crate::SurfaceError::Other("swapchain cast to version 3")+ })?+ }+ };++ // Apply the color space unconditionally (even for sRGB) so that+ // reconfiguring away from HDR10 resets the swapchain's state.+ //+ // We deliberately skip `CheckColorSpaceSupport`: a color space reporting as+ // unsupported is not a failure. Windows composites in scRGB and tone-maps+ // the color space down to the output, so `SetColorSpace1` still presents+ // correctly even when the check returns false (as the MS docs note). This+ // lets an app configure e.g. BT.2100 PQ on an SDR output and let the+ // compositor map it.+ let color_space = map_surface_color_space(config.color_space);+ // SAFETY: `swap_chain` is a live `IDXGISwapChain3`; `color_space` is a+ // valid `DXGI_COLOR_SPACE_TYPE`.+ unsafe { swap_chain.SetColorSpace1(color_space) }.map_err(|err| {+ log::error!("SetColorSpace1 failed: {err}");+ crate::SurfaceError::Other("IDXGISwapChain3::SetColorSpace1")+ })?;++ match self.target {+ SurfaceTarget::WndHandle(wnd_handle) => {+ // Disable automatic Alt+Enter handling by DXGI.+ unsafe {+ self.factory.MakeWindowAssociation(+ wnd_handle,+ Dxgi::DXGI_MWA_NO_WINDOW_CHANGES | Dxgi::DXGI_MWA_NO_ALT_ENTER,+ )+ }+ .into_device_result("MakeWindowAssociation")?;+ }+ SurfaceTarget::Visual(_)+ | SurfaceTarget::VisualFromWndHandle { .. }+ | SurfaceTarget::SurfaceHandle(_)+ | SurfaceTarget::SwapChainPanel(_) => {}+ }++ unsafe { swap_chain.SetMaximumFrameLatency(config.maximum_frame_latency) }+ .into_device_result("SetMaximumFrameLatency")?;++ let waitable = match device.options.latency_waitable_object {+ wgt::Dx12UseFrameLatencyWaitableObject::None => None,+ wgt::Dx12UseFrameLatencyWaitableObject::Wait+ | wgt::Dx12UseFrameLatencyWaitableObject::DontWait => {+ Some(unsafe { swap_chain.GetFrameLatencyWaitableObject() })+ }+ };++ let mut resources = Vec::with_capacity(swap_chain_buffer as usize);+ for i in 0..swap_chain_buffer {+ let resource = unsafe { swap_chain.GetBuffer(i) }+ .into_device_result("Failed to get swapchain buffer")?;+ resources.push(resource);+ }++ let mut swapchain = self.swap_chain.write();+ *swapchain = Some(SwapChain {+ raw: swap_chain,+ resources,+ waitable,+ acquired_count: 0,+ present_mode: config.present_mode,+ format: config.format,+ size: config.extent,+ });++ Ok(())+ }++ unsafe fn unconfigure(&self, device: &Device) {+ if let Some(sc) = self.swap_chain.write().take() {+ unsafe {+ // While `unconfigure`s contract ensures that no work on the GPU's main queues+ // are in flight, we still need to wait for the present queue to be idle.++ // The major failure mode of this function is device loss,+ // which if we have lost the device, we should just continue+ // cleaning up, without error.+ let _ = device.wait_for_present_queue_idle();++ let _raw = sc.release_resources();+ }+ }+ }++ unsafe fn acquire_texture(+ &self,+ timeout: Option<core::time::Duration>,+ _fence: &Fence,+ ) -> Result<crate::AcquiredSurfaceTexture<Api>, crate::SurfaceError> {+ let mut swapchain = self.swap_chain.write();+ let sc = swapchain.as_mut().unwrap();++ match self.options.latency_waitable_object {+ wgt::Dx12UseFrameLatencyWaitableObject::None+ | wgt::Dx12UseFrameLatencyWaitableObject::DontWait => {}+ wgt::Dx12UseFrameLatencyWaitableObject::Wait => {+ unsafe { sc.wait(timeout) }?;+ }+ }++ let base_index = unsafe { sc.raw.GetCurrentBackBufferIndex() } as usize;+ let index = (base_index + sc.acquired_count) % sc.resources.len();+ sc.acquired_count += 1;++ let texture = Texture {+ resource: sc.resources[index].clone(),+ format: sc.format,+ dimension: wgt::TextureDimension::D2,+ size: sc.size,+ mip_level_count: 1,+ sample_count: 1,+ allocation: suballocation::Allocation::none(+ suballocation::AllocationType::Texture,+ sc.format.theoretical_memory_footprint(sc.size),+ ),+ plane_slice_override: None,+ };+ Ok(crate::AcquiredSurfaceTexture {+ texture,+ suboptimal: false,+ })+ }+ unsafe fn discard_texture(&self, _texture: Texture) {+ let mut swapchain = self.swap_chain.write();+ let sc = swapchain.as_mut().unwrap();+ sc.acquired_count -= 1;+ }+}++impl crate::Queue for Queue {+ type A = Api;++ unsafe fn submit(+ &self,+ command_buffers: &[&CommandBuffer],+ _surface_textures: &[&Texture],+ (signal_fence, signal_value): (&Fence, crate::FenceValue),+ ) -> Result<(), crate::DeviceError> {+ let mut temp_lists = self.temp_lists.lock();+ temp_lists.clear();+ for cmd_buf in command_buffers {+ temp_lists.push(Some(cmd_buf.raw.clone().into()));+ }++ // Drain caller-staged waits before ExecuteCommandLists so the+ // GPU queue blocks on each foreign signal before running our+ // command lists. D3D12 queue commands are FIFO - Wait calls+ // here gate everything submitted after them.+ {+ let mut waits = self.pending_waits.lock();+ for (fence, value) in waits.drain(..) {+ unsafe { self.raw.Wait(&fence, value) }.into_device_result("Wait pending fence")?;+ }+ }++ {+ profiling::scope!("ID3D12CommandQueue::ExecuteCommandLists");+ unsafe { self.raw.ExecuteCommandLists(&temp_lists) }+ }++ unsafe { self.raw.Signal(&signal_fence.raw, signal_value) }+ .into_device_result("Signal fence")?;++ // Drain caller-staged signals after our own Signal so each+ // additional fence value publishes once the submit completes.+ {+ let mut signals = self.pending_signals.lock();+ for (fence, value) in signals.drain(..) {+ unsafe { self.raw.Signal(&fence, value) }+ .into_device_result("Signal pending fence")?;+ }+ }++ // Note the lack of synchronization here between the main Direct queue+ // and the dedicated presentation queue. This is automatically handled+ // by the D3D runtime by detecting uses of resources derived from the+ // swapchain. This automatic detection is why you cannot use a swapchain+ // as an UAV in D3D12.++ Ok(())+ }+ unsafe fn present(+ &self,+ surface: &Surface,+ _texture: Texture,+ ) -> Result<(), crate::SurfaceError> {+ let mut swapchain = surface.swap_chain.write();+ let sc = swapchain.as_mut().unwrap();+ sc.acquired_count -= 1;++ let (interval, flags) = match sc.present_mode {+ // We only allow immediate if ALLOW_TEARING is valid.+ wgt::PresentMode::Immediate => (0, Dxgi::DXGI_PRESENT_ALLOW_TEARING),+ wgt::PresentMode::Mailbox => (0, Dxgi::DXGI_PRESENT::default()),+ wgt::PresentMode::Fifo => (1, Dxgi::DXGI_PRESENT::default()),+ m => unreachable!("Cannot make surface with present mode {m:?}"),+ };++ profiling::scope!("IDXGISwapchain3::Present");+ unsafe { sc.raw.Present(interval, flags) }+ .ok()+ .into_device_result("Present")?;++ Ok(())+ }++ unsafe fn get_timestamp_period(&self) -> f32 {+ let frequency = unsafe { self.raw.GetTimestampFrequency() }.expect("GetTimestampFrequency");+ (1_000_000_000.0 / frequency as f64) as f32+ }++ unsafe fn wait_for_idle(&self) -> Result<(), crate::DeviceError> {+ let value = self+ .idle_fence_value+ .fetch_add(1, core::sync::atomic::Ordering::Relaxed)+ + 1;+ unsafe { self.raw.Signal(&self.idle_fence, value) }+ .into_device_result("Signal idle fence")?;+ unsafe {+ self.idle_fence+ .SetEventOnCompletion(value, self.idle_event.0)+ }+ .into_device_result("SetEventOnCompletion")?;+ unsafe { Threading::WaitForSingleObject(self.idle_event.0, Threading::INFINITE) };+ Ok(())+ }+}+#[derive(Debug)]+pub struct DxilPassthroughShader {+ pub shader: Vec<u8>,+}++#[derive(Debug)]+pub struct HlslPassthroughShader {+ pub shader: String,+}++#[derive(Debug)]+pub enum ShaderModuleSource {+ Naga(crate::NagaShader),+ DxilPassthrough(DxilPassthroughShader),+ HlslPassthrough(HlslPassthroughShader),+}++#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]+pub enum FeatureLevel {+ V11_0,+ V11_1,+ V12_0,+ V12_1,+ V12_2,+}++#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]+pub enum ShaderModel {+ V5_1,+ V6_0,+ V6_1,+ V6_2,+ V6_3,+ V6_4,+ V6_5,+ V6_6,+ V6_7,+ V6_8,+ V6_9,+}diff --git a/vendor/wgpu-hal/src/dx12/pipeline_desc.rs b/vendor/wgpu-hal/src/dx12/pipeline_desc.rsnew file mode 100644index 00000000..9fcf5661--- /dev/null+++ b/vendor/wgpu-hal/src/dx12/pipeline_desc.rs@@ -0,0 +1,344 @@+//! We try to use pipeline stream descriptors where possible, but this isn't allowed+//! on some older windows 10 versions. Therefore, we also must have some logic to+//! convert such descriptors to the "traditional" equivalent,+//! `D3D12_GRAPHICS_PIPELINE_STATE_DESC`.+//!+//! Stream descriptors allow extending the pipeline, enabling more advanced features,+//! including mesh shaders and multiview/view instancing. Using a stream descriptor+//! is like using a vulkan descriptor with a `pNext` chain. It doesn't have direct+//! benefits to all use cases, but allows new use cases.+//!+//! The code for pipeline stream descriptors is very complicated, and can have bad+//! consequences if it is written incorrectly. It has been isolated to this file for+//! that reason.++use core::{ffi::c_void, mem::ManuallyDrop, ptr::NonNull};++use alloc::vec::Vec;+use windows::Win32::Graphics::Direct3D12::*;+use windows::Win32::Graphics::Dxgi::Common::*;+use windows_core::Interface;++use crate::dx12::borrow_interface_temporarily;++// Wrapper newtypes for various pipeline subobjects which+// use complicated or non-unique representations.++#[repr(transparent)]+#[derive(Copy, Clone)]+// Option<NonNull<c_void>> is guaranteed to have the same representation as a raw pointer.+struct RootSignature(Option<NonNull<c_void>>);++#[repr(transparent)]+#[derive(Copy, Clone)]+struct VertexShader(D3D12_SHADER_BYTECODE);+#[repr(transparent)]+#[derive(Copy, Clone)]+struct PixelShader(D3D12_SHADER_BYTECODE);++#[repr(transparent)]+#[derive(Copy, Clone)]+struct MeshShader(D3D12_SHADER_BYTECODE);++#[repr(transparent)]+#[derive(Copy, Clone)]+struct TaskShader(D3D12_SHADER_BYTECODE);++#[repr(transparent)]+#[derive(Copy, Clone)]+struct SampleMask(u32);++#[repr(transparent)]+#[derive(Copy, Clone)]+struct NodeMask(u32);++/// Trait for types that can be used as subobjects in a pipeline state stream.+///+/// Safety:+/// - The type must be the correct alignment and size for the subobject it represents.+/// - The type must map to exactly one `D3D12_PIPELINE_STATE_SUBOBJECT_TYPE` variant.+/// - The variant must correctly represent the type's role in the pipeline state stream.+/// - The type must be `Copy` to ensure safe duplication in the stream.+/// - The type must be valid to memcpy into the pipeline state stream.+unsafe trait RenderPipelineStreamObject: Copy {+ const SUBOBJECT_TYPE: D3D12_PIPELINE_STATE_SUBOBJECT_TYPE;+}++macro_rules! implement_stream_object {+ (unsafe $ty:ty => $variant:expr) => {+ unsafe impl RenderPipelineStreamObject for $ty {+ const SUBOBJECT_TYPE: D3D12_PIPELINE_STATE_SUBOBJECT_TYPE = $variant;+ }+ };+}++implement_stream_object! { unsafe RootSignature => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE }+implement_stream_object! { unsafe VertexShader => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VS }+implement_stream_object! { unsafe PixelShader => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PS }+implement_stream_object! { unsafe MeshShader => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MS }+implement_stream_object! { unsafe TaskShader => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_AS }+implement_stream_object! { unsafe D3D12_BLEND_DESC => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_BLEND }+implement_stream_object! { unsafe SampleMask => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_MASK }+implement_stream_object! { unsafe D3D12_RASTERIZER_DESC => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RASTERIZER }+implement_stream_object! { unsafe D3D12_DEPTH_STENCIL_DESC => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL }+implement_stream_object! { unsafe D3D12_PRIMITIVE_TOPOLOGY_TYPE => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PRIMITIVE_TOPOLOGY }+implement_stream_object! { unsafe D3D12_RT_FORMAT_ARRAY => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RENDER_TARGET_FORMATS }+implement_stream_object! { unsafe DXGI_FORMAT => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL_FORMAT }+implement_stream_object! { unsafe DXGI_SAMPLE_DESC => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_DESC }+implement_stream_object! { unsafe NodeMask => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_NODE_MASK }+implement_stream_object! { unsafe D3D12_CACHED_PIPELINE_STATE => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_CACHED_PSO }+implement_stream_object! { unsafe D3D12_PIPELINE_STATE_FLAGS => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_FLAGS }+implement_stream_object! { unsafe D3D12_INPUT_LAYOUT_DESC => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_INPUT_LAYOUT }+implement_stream_object! { unsafe D3D12_INDEX_BUFFER_STRIP_CUT_VALUE => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_IB_STRIP_CUT_VALUE }+implement_stream_object! { unsafe D3D12_STREAM_OUTPUT_DESC => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_STREAM_OUTPUT }+implement_stream_object! { unsafe D3D12_VIEW_INSTANCING_DESC => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VIEW_INSTANCING }++/// Implementaation of a pipeline state stream, which is a sequence of subobjects put into+/// a byte array according to some basic alignment rules.+///+/// Each subobject must start on an 8 byte boundary. Each subobject contains a 32 bit+/// type identifier, followed by the actual subobject data, aligned as required by the+/// subobject's structure.+///+/// See <https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_pipeline_state_stream_desc>+/// for more information.+pub(super) struct RenderPipelineStateStream<'a> {+ bytes: Vec<u8>,+ _marker: core::marker::PhantomData<&'a ()>,+}++impl<'a> RenderPipelineStateStream<'a> {+ fn new() -> Self {+ // Dynamic allocation is used here because the resulting stream can become very large.+ // We pre-allocate the size based on an estimate of the size of the struct plus some extra space+ // per member for tags and alignment padding. In practice this will always be too big, as not+ // all members will be used.+ let size_of_stream_desc = size_of::<RenderPipelineStateStreamDesc>();+ let members = 20; // Approximate number of members we might push+ let capacity = size_of_stream_desc + members * 8; // Extra space for tags and alignment+ Self {+ bytes: Vec::with_capacity(capacity),+ _marker: core::marker::PhantomData,+ }+ }++ /// Align the internal byte buffer to the given alignment,+ /// padding with zeros as necessary.+ fn align_to(&mut self, alignment: usize) {+ let aligned_length = self.bytes.len().next_multiple_of(alignment);+ self.bytes.resize(aligned_length, 0);+ }++ /// Adds a subobject to the pipeline state stream.+ fn add_object<T: RenderPipelineStreamObject>(&mut self, object: T) {+ // Ensure 8-byte alignment for the subobject start.+ self.align_to(8);++ // Append the type tag (u32)+ let tag: u32 = T::SUBOBJECT_TYPE.0 as u32;+ self.bytes.extend_from_slice(&tag.to_ne_bytes());++ // Align the data to its natural alignment.+ self.align_to(align_of_val::<T>(&object));++ // Append the data itself, as raw bytes+ let data_ptr: *const T = &object;+ let data_u8_ptr: *const u8 = data_ptr.cast::<u8>();+ let data_size = size_of_val::<T>(&object);+ let slice = unsafe { core::slice::from_raw_parts::<u8>(data_u8_ptr, data_size) };+ self.bytes.extend_from_slice(slice);+ }++ /// Creates a pipeline state object from the stream.+ ///+ /// Safety:+ /// - All unsafety invariants required by [`ID3D12Device2::CreatePipelineState`] must be upheld by the caller.+ pub unsafe fn create_pipeline_state(+ &mut self,+ device: &ID3D12Device2,+ ) -> windows::core::Result<ID3D12PipelineState> {+ let stream_desc = D3D12_PIPELINE_STATE_STREAM_DESC {+ SizeInBytes: self.bytes.len(),+ pPipelineStateSubobjectStream: self.bytes.as_mut_ptr().cast(),+ };++ // Safety: lifetime on Self preserved the contents+ // of the stream. Other unsafety invariants are upheld by the caller.+ unsafe { device.CreatePipelineState(&stream_desc) }+ }+}++#[repr(C)]+#[derive(Debug)]+pub struct RenderPipelineStateStreamDesc<'a> {+ pub root_signature: Option<&'a ID3D12RootSignature>,+ pub pixel_shader: D3D12_SHADER_BYTECODE,+ pub blend_state: D3D12_BLEND_DESC,+ pub sample_mask: u32,+ pub rasterizer_state: D3D12_RASTERIZER_DESC,+ pub depth_stencil_state: D3D12_DEPTH_STENCIL_DESC,+ pub primitive_topology_type: D3D12_PRIMITIVE_TOPOLOGY_TYPE,+ pub rtv_formats: D3D12_RT_FORMAT_ARRAY,+ pub dsv_format: DXGI_FORMAT,+ pub sample_desc: DXGI_SAMPLE_DESC,+ pub node_mask: u32,+ pub cached_pso: D3D12_CACHED_PIPELINE_STATE,+ pub flags: D3D12_PIPELINE_STATE_FLAGS,+ pub view_instancing: Option<D3D12_VIEW_INSTANCING_DESC>,++ // Vertex pipeline specific+ pub vertex_shader: D3D12_SHADER_BYTECODE,+ pub input_layout: D3D12_INPUT_LAYOUT_DESC,+ pub index_buffer_strip_cut_value: D3D12_INDEX_BUFFER_STRIP_CUT_VALUE,+ pub stream_output: D3D12_STREAM_OUTPUT_DESC,++ // Mesh pipeline specific+ pub task_shader: D3D12_SHADER_BYTECODE,+ pub mesh_shader: D3D12_SHADER_BYTECODE,+}++impl RenderPipelineStateStreamDesc<'_> {+ pub fn to_stream(&self) -> RenderPipelineStateStream<'_> {+ let mut stream = RenderPipelineStateStream::new();++ // Importantly here, the ID3D12RootSignature _itself_ is the pointer we're+ // trying to serialize into the stream, not a pointer to the pointer.+ //+ // This is correct because as_raw() returns turns that smart object into the raw+ // pointer that _is_ the com object handle.+ let root_sig_pointer = self+ .root_signature+ .map(|a| NonNull::new(a.as_raw()).unwrap());+ // Because the stream object borrows from self for its entire lifetime,+ // it is safe to store the pointer into it.+ stream.add_object(RootSignature(root_sig_pointer));++ stream.add_object(self.blend_state);+ stream.add_object(SampleMask(self.sample_mask));+ stream.add_object(self.rasterizer_state);+ stream.add_object(self.depth_stencil_state);+ stream.add_object(self.primitive_topology_type);+ if self.rtv_formats.NumRenderTargets != 0 {+ stream.add_object(self.rtv_formats);+ }+ if self.dsv_format != DXGI_FORMAT_UNKNOWN {+ stream.add_object(self.dsv_format);+ }+ stream.add_object(self.sample_desc);+ if self.node_mask != 0 {+ stream.add_object(NodeMask(self.node_mask));+ }+ if !self.cached_pso.pCachedBlob.is_null() {+ stream.add_object(self.cached_pso);+ }+ stream.add_object(self.flags);+ if let Some(view_instancing) = self.view_instancing {+ stream.add_object(view_instancing);+ }+ if !self.pixel_shader.pShaderBytecode.is_null() {+ stream.add_object(PixelShader(self.pixel_shader));+ }+ if !self.vertex_shader.pShaderBytecode.is_null() {+ stream.add_object(VertexShader(self.vertex_shader));+ stream.add_object(self.input_layout);+ stream.add_object(self.index_buffer_strip_cut_value);+ stream.add_object(self.stream_output);+ }+ if !self.task_shader.pShaderBytecode.is_null() {+ stream.add_object(TaskShader(self.task_shader));+ }+ if !self.mesh_shader.pShaderBytecode.is_null() {+ stream.add_object(MeshShader(self.mesh_shader));+ }++ stream+ }++ /// Returns a traditional D3D12_GRAPHICS_PIPELINE_STATE_DESC.+ ///+ /// Safety:+ /// - This returned struct must not outlive self.+ pub unsafe fn to_graphics_pipeline_descriptor(&self) -> D3D12_GRAPHICS_PIPELINE_STATE_DESC {+ D3D12_GRAPHICS_PIPELINE_STATE_DESC {+ pRootSignature: if let Some(rsig) = self.root_signature {+ unsafe { borrow_interface_temporarily(rsig) }+ } else {+ ManuallyDrop::new(None)+ },+ VS: self.vertex_shader,+ PS: self.pixel_shader,+ DS: D3D12_SHADER_BYTECODE::default(),+ HS: D3D12_SHADER_BYTECODE::default(),+ GS: D3D12_SHADER_BYTECODE::default(),+ StreamOutput: self.stream_output,+ BlendState: self.blend_state,+ SampleMask: self.sample_mask,+ RasterizerState: self.rasterizer_state,+ DepthStencilState: self.depth_stencil_state,+ InputLayout: self.input_layout,+ IBStripCutValue: self.index_buffer_strip_cut_value,+ PrimitiveTopologyType: self.primitive_topology_type,+ NumRenderTargets: self.rtv_formats.NumRenderTargets,+ RTVFormats: self.rtv_formats.RTFormats,+ DSVFormat: self.dsv_format,+ SampleDesc: self.sample_desc,+ NodeMask: self.node_mask,+ CachedPSO: self.cached_pso,+ Flags: self.flags,+ }+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn wrappers() {+ assert_eq!(size_of::<RootSignature>(), size_of::<ID3D12RootSignature>());+ assert_eq!(+ align_of::<RootSignature>(),+ align_of::<ID3D12RootSignature>()+ )+ }++ implement_stream_object!(unsafe u16 => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE(1));+ implement_stream_object!(unsafe u32 => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE(2));+ implement_stream_object!(unsafe u64 => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE(3));++ #[test]+ fn stream() {+ let mut stream = RenderPipelineStateStream::new();++ stream.add_object(42u16);+ stream.add_object(84u32);+ stream.add_object(168u64);++ assert_eq!(stream.bytes.len(), 32);++ // Object 1: u16++ // Tag at the beginning+ assert_eq!(&stream.bytes[0..4], &1u32.to_ne_bytes());+ // Data tucked in, aligned to the natural alignment of u16+ assert_eq!(&stream.bytes[4..6], &42u16.to_ne_bytes());+ // Padding to align the next subobject to an 8 byte boundary.+ assert_eq!(&stream.bytes[6..8], &[0, 0]);++ // Object 2: u32++ // Tag at the beginning+ assert_eq!(&stream.bytes[8..12], &2u32.to_ne_bytes());+ // Data tucked in, aligned to the natural alignment of u32+ assert_eq!(&stream.bytes[12..16], &84u32.to_ne_bytes());++ // Object 3: u64++ // Tag at the beginning+ assert_eq!(&stream.bytes[16..20], &3u32.to_ne_bytes());+ // Padding to align the u64 to an 8 byte boundary.+ assert_eq!(&stream.bytes[20..24], &[0, 0, 0, 0]);+ // Data tucked in, aligned to the natural alignment of u64+ assert_eq!(&stream.bytes[24..32], &168u64.to_ne_bytes());+ }+}diff --git a/vendor/wgpu-hal/src/dx12/sampler.rs b/vendor/wgpu-hal/src/dx12/sampler.rsnew file mode 100644index 00000000..4b58a0b4--- /dev/null+++ b/vendor/wgpu-hal/src/dx12/sampler.rs@@ -0,0 +1,252 @@+//! Sampler management for DX12.+//!+//! Nearly identical to the Vulkan sampler cache, with added descriptor heap management.++use alloc::vec::Vec;++use hashbrown::{hash_map::Entry, HashMap};++use ordered_float::OrderedFloat;+use parking_lot::Mutex;+use windows::Win32::Graphics::Direct3D12::*;++use crate::dx12::HResult;++/// The index of a sampler in the global sampler heap.+///+/// This is a type-safe, transparent wrapper around a u32.+#[repr(transparent)]+#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]+pub(crate) struct SamplerIndex(u32);++/// [`D3D12_SAMPLER_DESC`] is not hashable, so we wrap it in a newtype that is.+///+/// We use [`OrderedFloat`] to allow for floating point values to be compared and+/// hashed in a defined way.+#[derive(Debug, Copy, Clone)]+struct HashableSamplerDesc(D3D12_SAMPLER_DESC);++impl PartialEq for HashableSamplerDesc {+ fn eq(&self, other: &Self) -> bool {+ self.0.Filter == other.0.Filter+ && self.0.AddressU == other.0.AddressU+ && self.0.AddressV == other.0.AddressV+ && self.0.AddressW == other.0.AddressW+ && OrderedFloat(self.0.MipLODBias) == OrderedFloat(other.0.MipLODBias)+ && self.0.MaxAnisotropy == other.0.MaxAnisotropy+ && self.0.ComparisonFunc == other.0.ComparisonFunc+ && self.0.BorderColor.map(OrderedFloat) == other.0.BorderColor.map(OrderedFloat)+ && OrderedFloat(self.0.MinLOD) == OrderedFloat(other.0.MinLOD)+ && OrderedFloat(self.0.MaxLOD) == OrderedFloat(other.0.MaxLOD)+ }+}++impl Eq for HashableSamplerDesc {}++impl core::hash::Hash for HashableSamplerDesc {+ fn hash<H: core::hash::Hasher>(&self, state: &mut H) {+ self.0.Filter.0.hash(state);+ self.0.AddressU.0.hash(state);+ self.0.AddressV.0.hash(state);+ self.0.AddressW.0.hash(state);+ OrderedFloat(self.0.MipLODBias).hash(state);+ self.0.MaxAnisotropy.hash(state);+ self.0.ComparisonFunc.0.hash(state);+ self.0.BorderColor.map(OrderedFloat).hash(state);+ OrderedFloat(self.0.MinLOD).hash(state);+ OrderedFloat(self.0.MaxLOD).hash(state);+ }+}++/// Entry in the sampler cache.+struct CacheEntry {+ index: SamplerIndex,+ ref_count: u32,+}++/// Container for the mutable management state of the sampler heap.+///+/// We have this separated, using interior mutability, to allow for the outside world+/// to access the heap directly without needing to take the lock.+pub(crate) struct SamplerHeapState {+ /// Mapping from the sampler description to the index within the heap and the refcount.+ mapping: HashMap<HashableSamplerDesc, CacheEntry>,+ /// List of free sampler indices.+ freelist: Vec<SamplerIndex>,+}++/// Global sampler heap for the device.+///+/// As D3D12 only allows 2048 samplers to be in a single heap, we need to cache+/// samplers aggressively and refer to them in shaders by index.+pub(crate) struct SamplerHeap {+ /// Mutable management state of the sampler heap.+ state: Mutex<SamplerHeapState>,++ /// The heap itself.+ heap: ID3D12DescriptorHeap,+ /// The CPU-side handle to the first descriptor in the heap.+ ///+ /// Both the CPU and GPU handles point to the same descriptor, just in+ /// different contexts.+ heap_cpu_start_handle: D3D12_CPU_DESCRIPTOR_HANDLE,+ /// The GPU-side handle to the first descriptor in the heap.+ ///+ /// Both the CPU and GPU handles point to the same descriptor, just in+ /// different contexts.+ heap_gpu_start_handle: D3D12_GPU_DESCRIPTOR_HANDLE,++ /// This is the device-specific size of sampler descriptors.+ descriptor_stride: u32,+}++impl SamplerHeap {+ pub fn new(+ device: &ID3D12Device,+ private_caps: &super::PrivateCapabilities,+ ) -> Result<Self, crate::DeviceError> {+ profiling::scope!("SamplerHeap::new");++ // WARP can report this as 2M or more. We clamp it to 64k to be safe.+ const SAMPLER_HEAP_SIZE_CLAMP: u32 = 64 * 1024;++ let max_unique_samplers = private_caps+ .max_sampler_descriptor_heap_size+ .min(SAMPLER_HEAP_SIZE_CLAMP);++ let desc = D3D12_DESCRIPTOR_HEAP_DESC {+ Type: D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,+ NumDescriptors: max_unique_samplers,+ Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,+ NodeMask: 0,+ };+ let heap = unsafe { device.CreateDescriptorHeap::<ID3D12DescriptorHeap>(&desc) }+ .into_device_result("Failed to create global GPU-Visible Sampler Descriptor Heap")?;++ let heap_cpu_start_handle = unsafe { heap.GetCPUDescriptorHandleForHeapStart() };+ let heap_gpu_start_handle = unsafe { heap.GetGPUDescriptorHandleForHeapStart() };++ let descriptor_stride =+ unsafe { device.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER) };++ Ok(Self {+ state: Mutex::new(SamplerHeapState {+ mapping: HashMap::new(),+ // Reverse so that samplers get allocated starting from zero.+ freelist: (0..max_unique_samplers).map(SamplerIndex).rev().collect(),+ }),+ heap,+ heap_cpu_start_handle,+ heap_gpu_start_handle,+ descriptor_stride,+ })+ }++ /// Returns a reference to the raw descriptor heap.+ pub fn heap(&self) -> &ID3D12DescriptorHeap {+ &self.heap+ }++ /// Returns a reference the handle to be bound to the descriptor table.+ pub fn gpu_descriptor_table(&self) -> D3D12_GPU_DESCRIPTOR_HANDLE {+ self.heap_gpu_start_handle+ }++ /// Add a sampler with the given description to the heap.+ ///+ /// If the sampler already exists, the refcount is incremented and the existing index is returned.+ ///+ /// If the sampler does not exist, a new sampler is created and the index is returned.+ ///+ /// If the heap is full, an error is returned.+ pub fn create_sampler(+ &self,+ device: &ID3D12Device,+ desc: D3D12_SAMPLER_DESC,+ ) -> Result<SamplerIndex, crate::DeviceError> {+ profiling::scope!("SamplerHeap::create_sampler");++ let hashable_desc = HashableSamplerDesc(desc);++ // Eagarly dereference the lock to allow split borrows.+ let state = &mut *self.state.lock();++ // Lookup the sampler in the mapping.+ match state.mapping.entry(hashable_desc) {+ Entry::Occupied(occupied_entry) => {+ // We have found a match, so increment the refcount and return the index.+ let entry = occupied_entry.into_mut();+ entry.ref_count += 1;+ Ok(entry.index)+ }+ Entry::Vacant(vacant_entry) => {+ // We need to create a new sampler.++ // Try to get a new index from the freelist.+ let Some(index) = state.freelist.pop() else {+ // If the freelist is empty, we have hit the maximum number of samplers.+ log::error!("There is no more room in the global sampler heap for more unique samplers. Your device supports a maximum of {} unique samplers.", state.mapping.len());+ return Err(crate::DeviceError::OutOfMemory);+ };++ // Compute the CPU side handle for the new sampler.+ let handle = D3D12_CPU_DESCRIPTOR_HANDLE {+ ptr: self.heap_cpu_start_handle.ptr+ + self.descriptor_stride as usize * index.0 as usize,+ };++ unsafe {+ device.CreateSampler(&desc, handle);+ }++ // Insert the new sampler into the mapping.+ vacant_entry.insert(CacheEntry {+ index,+ ref_count: 1,+ });++ Ok(index)+ }+ }+ }++ /// Decrement the refcount of the sampler with the given description.+ ///+ /// If the refcount reaches zero, the sampler is destroyed and the index is returned to the freelist.+ ///+ /// The provided index is checked against the index of the sampler with the given description, ensuring+ /// that there isn't a clerical error from the caller.+ pub fn destroy_sampler(&self, desc: D3D12_SAMPLER_DESC, provided_index: SamplerIndex) {+ profiling::scope!("SamplerHeap::destroy_sampler");++ // Eagarly dereference the lock to allow split borrows.+ let state = &mut *self.state.lock();++ // Get the index of the sampler to destroy.+ let Entry::Occupied(mut hash_map_entry) = state.mapping.entry(HashableSamplerDesc(desc))+ else {+ log::error!(+ "Tried to destroy a sampler that doesn't exist. Sampler description: {desc:#?}"+ );+ return;+ };+ let cache_entry = hash_map_entry.get_mut();++ // Ensure that the provided index matches the index of the sampler to destroy.+ assert_eq!(+ cache_entry.index, provided_index,+ "Mismatched sampler index, this is an implementation bug"+ );++ // Decrement the refcount of the sampler.+ cache_entry.ref_count -= 1;++ // If we are the last reference, remove the sampler from the mapping and return the index to the freelist.+ //+ // As samplers only exist as descriptors in the heap, there is nothing needed to be done to destroy the sampler.+ if cache_entry.ref_count == 0 {+ state.freelist.push(cache_entry.index);+ hash_map_entry.remove();+ }+ }+}diff --git a/vendor/wgpu-hal/src/dx12/shader_compilation.rs b/vendor/wgpu-hal/src/dx12/shader_compilation.rsnew file mode 100644index 00000000..ff41e503--- /dev/null+++ b/vendor/wgpu-hal/src/dx12/shader_compilation.rs@@ -0,0 +1,450 @@+use alloc::{string::String, vec::Vec};+use core::ffi::CStr;+use std::path::PathBuf;++use crate::auxil::dxgi::result::HResult;+use thiserror::Error;+use windows::{+ core::{Interface, PCSTR, PCWSTR},+ Win32::Graphics::Direct3D::{Dxc, Fxc, ID3DBlob, D3D_SHADER_MACRO},+};++pub(super) enum CompilerContainer {+ Fxc(CompilerFxc),+ DynamicDxc(CompilerDynamicDxc),+ #[cfg_attr(not(static_dxc), allow(unused))]+ StaticDxc(CompilerStaticDxc),+}++pub(super) struct CompilerFxc {+ fxc: FxcLib,+}++pub(super) struct CompilerDynamicDxc {+ max_shader_model: wgt::DxcShaderModel,+ compiler: Dxc::IDxcCompiler3,+ // Has to be held onto for the lifetime of the device otherwise shaders will fail to compile.+ // Only needed when using dynamic linking.+ _dxc: DxcLib,+}++pub(super) struct CompilerStaticDxc {+ max_shader_model: wgt::DxcShaderModel,+ compiler: Dxc::IDxcCompiler3,+}++#[derive(Debug, Error)]+pub(super) enum GetContainerError {+ #[error(transparent)]+ Device(#[from] crate::DeviceError),+ #[error("Failed to load {0}: {1}")]+ FailedToLoad(&'static str, libloading::Error),+}++impl CompilerContainer {+ pub(super) fn new_fxc() -> Result<Self, GetContainerError> {+ FxcLib::new_dynamic().map(|fxc| Self::Fxc(CompilerFxc { fxc }))+ }++ pub(super) fn new_dynamic_dxc(dxc_path: PathBuf) -> Result<Self, GetContainerError> {+ let dxc = DxcLib::new_dynamic(dxc_path)+ .map_err(|e| GetContainerError::FailedToLoad("dxcompiler.dll", e))?;++ let compiler = dxc.create_instance::<Dxc::IDxcCompiler3>()?;++ let (mut major, mut minor) = (1, 0);+ // DXC 1.0 didn't support this. If the cast fails assume it is DXC 1.0.+ if let Ok(version_info) = compiler.cast::<Dxc::IDxcVersionInfo>() {+ unsafe {+ version_info.GetVersion(&mut major, &mut minor).unwrap();+ }+ }++ Ok(Self::DynamicDxc(CompilerDynamicDxc {+ max_shader_model: wgt::DxcShaderModel::from_dxc_version(major, minor),+ compiler,+ _dxc: dxc,+ }))+ }++ /// Creates a [`CompilerContainer`] that delegates to the statically-linked version of DXC.+ pub(super) fn new_static_dxc() -> Result<CompilerContainer, crate::DeviceError> {+ #[cfg(static_dxc)]+ {+ unsafe {+ let compiler = dxc_create_instance::<Dxc::IDxcCompiler3>(|clsid, iid, ppv| {+ windows_core::HRESULT(mach_dxcompiler_rs::DxcCreateInstance(+ clsid.cast(),+ iid.cast(),+ ppv,+ ))+ })?;++ Ok(CompilerContainer::StaticDxc(CompilerStaticDxc {+ max_shader_model: wgt::DxcShaderModel::V6_7,+ compiler,+ }))+ }+ }+ #[cfg(not(static_dxc))]+ {+ panic!("Attempted to create a static DXC shader compiler, but the static-dxc feature was not enabled")+ }+ }++ pub(super) fn max_shader_model(&self) -> Option<wgt::DxcShaderModel> {+ match self {+ CompilerContainer::Fxc(..) => None,+ CompilerContainer::DynamicDxc(CompilerDynamicDxc {+ max_shader_model, ..+ })+ | CompilerContainer::StaticDxc(CompilerStaticDxc {+ max_shader_model, ..+ }) => Some(max_shader_model.clone()),+ }+ }++ pub(super) fn compile(+ &self,+ device: &super::Device,+ source: &str,+ source_name: Option<&CStr>,+ raw_ep: &str,+ stage_bit: wgt::ShaderStages,+ full_stage: &str,+ ) -> Result<super::CompiledShader, crate::PipelineError> {+ match self {+ CompilerContainer::Fxc(CompilerFxc { fxc }) => compile_fxc(+ device,+ source,+ source_name,+ raw_ep,+ stage_bit,+ full_stage,+ fxc,+ ),+ CompilerContainer::DynamicDxc(CompilerDynamicDxc { compiler, .. })+ | CompilerContainer::StaticDxc(CompilerStaticDxc { compiler, .. }) => compile_dxc(+ device,+ source,+ source_name,+ raw_ep,+ stage_bit,+ full_stage,+ compiler,+ ),+ }+ }+}++type D3DCompileFn = unsafe extern "system" fn(+ psrcdata: *const core::ffi::c_void,+ srcdatasize: usize,+ psourcename: PCSTR,+ pdefines: *const D3D_SHADER_MACRO,+ pinclude: *mut core::ffi::c_void,+ pentrypoint: PCSTR,+ ptarget: PCSTR,+ flags1: u32,+ flags2: u32,+ ppcode: *mut *mut core::ffi::c_void,+ pperrormsgs: *mut *mut core::ffi::c_void,+) -> windows_core::HRESULT;++#[derive(Debug)]+struct FxcLib {+ // `d3dcompile_fn` points into `_lib`, so `_lib` must be held for as long+ // as we want to keep compiling shaders with FXC.+ _lib: crate::dx12::DynLib,+ d3dcompile_fn: D3DCompileFn,+}++impl FxcLib {+ const PATH: &str = "d3dcompiler_47.dll";++ fn new_dynamic() -> Result<Self, GetContainerError> {+ unsafe {+ let lib = crate::dx12::DynLib::new(Self::PATH)+ .map_err(|e| GetContainerError::FailedToLoad(FxcLib::PATH, e))?;+ let d3dcompile_fn: D3DCompileFn = *lib.get::<D3DCompileFn>(c"D3DCompile".to_bytes())?;++ Ok(Self {+ _lib: lib,+ d3dcompile_fn,+ })+ }+ }++ #[allow(clippy::too_many_arguments)]+ fn compile(+ &self,+ source: &str,+ source_name: Option<&CStr>,+ raw_ep: &str,+ full_stage: &str,+ compile_flags: u32,+ shader_data: &mut Option<ID3DBlob>,+ error: &mut Option<ID3DBlob>,+ ) -> Result<windows_core::Result<()>, crate::DeviceError> {+ unsafe {+ let raw_ep = alloc::ffi::CString::new(raw_ep).unwrap();+ let full_stage = alloc::ffi::CString::new(full_stage).unwrap();++ // If no name has been set, D3DCompile wants the null pointer.+ let source_name = source_name+ .map(|cstr| cstr.as_ptr().cast())+ .unwrap_or(core::ptr::null());++ let shader_data: *mut Option<ID3DBlob> = shader_data;+ let error: *mut Option<ID3DBlob> = error;++ {+ profiling::scope!("Fxc::D3DCompile");+ Ok((self.d3dcompile_fn)(+ source.as_ptr().cast(),+ source.len(),+ PCSTR(source_name),+ core::ptr::null(),+ core::ptr::null_mut(),+ PCSTR(raw_ep.as_ptr().cast()),+ PCSTR(full_stage.as_ptr().cast()),+ compile_flags,+ 0,+ shader_data.cast(),+ error.cast(),+ )+ .ok())+ }+ }+ }+}++fn compile_fxc(+ device: &super::Device,+ source: &str,+ source_name: Option<&CStr>,+ raw_ep: &str,+ stage_bit: wgt::ShaderStages,+ full_stage: &str,+ fxc: &FxcLib,+) -> Result<super::CompiledShader, crate::PipelineError> {+ profiling::scope!("compile_fxc");+ let mut compile_flags = Fxc::D3DCOMPILE_ENABLE_STRICTNESS;+ if device+ .shared+ .private_caps+ .instance_flags+ .contains(wgt::InstanceFlags::DEBUG)+ {+ compile_flags |= Fxc::D3DCOMPILE_DEBUG | Fxc::D3DCOMPILE_SKIP_OPTIMIZATION;+ }++ let mut shader_data = None;+ let mut error = None;+ let hr = fxc.compile(+ source,+ source_name,+ raw_ep,+ full_stage,+ compile_flags,+ &mut shader_data,+ &mut error,+ )?;++ match hr {+ Ok(()) => {+ let shader_data = shader_data.unwrap();+ Ok(super::CompiledShader::Fxc(shader_data))+ }+ Err(e) => {+ let mut full_msg = format!("FXC D3DCompile error ({e})");+ if let Some(error) = error {+ use core::fmt::Write as _;+ let message = unsafe {+ core::slice::from_raw_parts(+ error.GetBufferPointer().cast(),+ error.GetBufferSize(),+ )+ };+ let _ = write!(full_msg, ": {}", String::from_utf8_lossy(message));+ }+ Err(crate::PipelineError::Linkage(stage_bit, full_msg))+ }+ }+}++trait DxcObj: Interface {+ const CLSID: windows::core::GUID;+}+impl DxcObj for Dxc::IDxcCompiler3 {+ const CLSID: windows::core::GUID = Dxc::CLSID_DxcCompiler;+}+impl DxcObj for Dxc::IDxcUtils {+ const CLSID: windows::core::GUID = Dxc::CLSID_DxcUtils;+}+impl DxcObj for Dxc::IDxcValidator {+ const CLSID: windows::core::GUID = Dxc::CLSID_DxcValidator;+}++#[derive(Debug)]+struct DxcLib {+ lib: crate::dx12::DynLib,+}++impl DxcLib {+ fn new_dynamic(lib_path: PathBuf) -> Result<Self, libloading::Error> {+ unsafe { crate::dx12::DynLib::new(lib_path).map(|lib| Self { lib }) }+ }++ pub fn create_instance<T: DxcObj>(&self) -> Result<T, crate::DeviceError> {+ unsafe {+ type DxcCreateInstanceFn = unsafe extern "system" fn(+ rclsid: *const windows_core::GUID,+ riid: *const windows_core::GUID,+ ppv: *mut *mut core::ffi::c_void,+ )+ -> windows_core::HRESULT;++ let func: libloading::Symbol<DxcCreateInstanceFn> =+ self.lib.get(c"DxcCreateInstance".to_bytes())?;+ dxc_create_instance::<T>(|clsid, iid, ppv| func(clsid, iid, ppv))+ }+ }+}++/// Invokes the provided library function to create a DXC object.+unsafe fn dxc_create_instance<T: DxcObj>(+ f: impl Fn(+ *const windows_core::GUID,+ *const windows_core::GUID,+ *mut *mut core::ffi::c_void,+ ) -> windows_core::HRESULT,+) -> Result<T, crate::DeviceError> {+ let mut result__ = None;+ f(&T::CLSID, &T::IID, <*mut _>::cast(&mut result__))+ .ok()+ .into_device_result("DxcCreateInstance")?;+ result__.ok_or(crate::DeviceError::Unexpected)+}++/// Owned PCWSTR+#[allow(clippy::upper_case_acronyms)]+struct OPCWSTR {+ inner: Vec<u16>,+}++impl OPCWSTR {+ fn new(s: &str) -> Self {+ let mut inner: Vec<_> = s.encode_utf16().collect();+ inner.push(0);+ Self { inner }+ }++ fn ptr(&self) -> PCWSTR {+ PCWSTR(self.inner.as_ptr())+ }+}++fn get_output<T: Interface>(+ res: &Dxc::IDxcResult,+ kind: Dxc::DXC_OUT_KIND,+) -> Result<T, crate::DeviceError> {+ let mut result__: Option<T> = None;+ unsafe { res.GetOutput::<T>(kind, &mut None, <*mut _>::cast(&mut result__)) }+ .into_device_result("GetOutput")?;+ result__.ok_or(crate::DeviceError::Unexpected)+}++fn as_err_str(blob: &Dxc::IDxcBlobUtf8) -> Result<&str, crate::DeviceError> {+ let ptr = unsafe { blob.GetStringPointer() };+ let len = unsafe { blob.GetStringLength() };+ core::str::from_utf8(unsafe { core::slice::from_raw_parts(ptr.0, len) })+ .map_err(|_| crate::DeviceError::Unexpected)+}++fn compile_dxc(+ device: &crate::dx12::Device,+ source: &str,+ source_name: Option<&CStr>,+ raw_ep: &str,+ stage_bit: wgt::ShaderStages,+ full_stage: &str,+ compiler: &Dxc::IDxcCompiler3,+) -> Result<crate::dx12::CompiledShader, crate::PipelineError> {+ profiling::scope!("compile_dxc");++ let source_name = source_name.and_then(|cstr| cstr.to_str().ok());++ let source_name = source_name.map(OPCWSTR::new);+ let raw_ep = OPCWSTR::new(raw_ep);+ let full_stage = OPCWSTR::new(full_stage);++ let mut compile_args = arrayvec::ArrayVec::<PCWSTR, 13>::new_const();++ if let Some(source_name) = source_name.as_ref() {+ compile_args.push(source_name.ptr())+ }++ compile_args.extend([+ windows::core::w!("-E"),+ raw_ep.ptr(),+ windows::core::w!("-T"),+ full_stage.ptr(),+ windows::core::w!("-HV"),+ windows::core::w!("2018"), // Use HLSL 2018, Naga doesn't supported 2021 yet.+ windows::core::w!("-no-warnings"),+ Dxc::DXC_ARG_ENABLE_STRICTNESS,+ ]);++ if device+ .shared+ .private_caps+ .instance_flags+ .contains(wgt::InstanceFlags::DEBUG)+ && !device+ .shared+ .private_caps+ .workarounds+ .avoid_shader_debug_info+ {+ compile_args.push(Dxc::DXC_ARG_DEBUG);+ compile_args.push(Dxc::DXC_ARG_SKIP_OPTIMIZATIONS);+ }++ if device+ .features+ .intersects(wgt::Features::SHADER_F16 | wgt::Features::SHADER_I16)+ {+ compile_args.push(windows::core::w!("-enable-16bit-types"));+ }++ let buffer = Dxc::DxcBuffer {+ Ptr: source.as_ptr().cast(),+ Size: source.len(),+ Encoding: Dxc::DXC_CP_UTF8.0,+ };++ let compile_res: Dxc::IDxcResult =+ unsafe { compiler.Compile(&buffer, Some(&compile_args), None) }+ .into_device_result("Compile")?;++ drop(compile_args);+ drop(source_name);+ drop(raw_ep);+ drop(full_stage);++ let err_blob = get_output::<Dxc::IDxcBlobUtf8>(&compile_res, Dxc::DXC_OUT_ERRORS)?;++ let len = unsafe { err_blob.GetStringLength() };+ if len != 0 {+ let err = as_err_str(&err_blob)?;+ return Err(crate::PipelineError::Linkage(+ stage_bit,+ format!("DXC compile error: {err}"),+ ));+ }++ let blob = get_output::<Dxc::IDxcBlob>(&compile_res, Dxc::DXC_OUT_OBJECT)?;++ Ok(crate::dx12::CompiledShader::Dxc(blob))+}diff --git a/vendor/wgpu-hal/src/dx12/suballocation.rs b/vendor/wgpu-hal/src/dx12/suballocation.rsnew file mode 100644index 00000000..89c7dc47--- /dev/null+++ b/vendor/wgpu-hal/src/dx12/suballocation.rs@@ -0,0 +1,603 @@+use alloc::sync::Arc;++use gpu_allocator::{d3d12::AllocationCreateDesc, MemoryLocation};+use parking_lot::Mutex;+use windows::Win32::Graphics::{Direct3D12, Dxgi};++use crate::{+ auxil::dxgi::{name::ObjectExt as _, result::HResult as _},+ dx12::conv,+ AllocationSizes,+};++#[derive(Debug)]+pub(crate) enum AllocationType {+ Buffer,+ Texture,+ AccelerationStructure,+}++#[derive(Debug)]+enum AllocationInner {+ /// This resource is suballocated from a heap.+ Placed {+ inner: gpu_allocator::d3d12::Allocation,+ },+ /// This resource is a committed resource and does not belong to a+ /// suballocated heap. We store an approximate size, so we can manage our counters+ /// correctly.+ ///+ /// This is only used for Intel Xe drivers, which have a bug that+ /// prevents suballocation from working correctly.+ Committed { size: u64 },+}++#[derive(Debug)]+pub(crate) struct Allocation {+ inner: AllocationInner,+ ty: AllocationType,+}++impl Allocation {+ pub fn placed(inner: gpu_allocator::d3d12::Allocation, ty: AllocationType) -> Self {+ Self {+ inner: AllocationInner::Placed { inner },+ ty,+ }+ }++ pub fn none(ty: AllocationType, size: u64) -> Self {+ Self {+ inner: AllocationInner::Committed { size },+ ty,+ }+ }++ pub fn size(&self) -> u64 {+ match self.inner {+ AllocationInner::Placed { ref inner } => inner.size(),+ AllocationInner::Committed { size } => size,+ }+ }+}++#[derive(Clone)]+pub(crate) struct Allocator {+ inner: Arc<Mutex<gpu_allocator::d3d12::Allocator>>,+ device_memblock_size: u64,+ host_memblock_size: u64,+ pub memory_budget_thresholds: wgt::MemoryBudgetThresholds,+}++impl Allocator {+ pub(crate) fn new(+ raw: &Direct3D12::ID3D12Device,+ memory_hints: &wgt::MemoryHints,+ memory_budget_thresholds: wgt::MemoryBudgetThresholds,+ ) -> Result<Self, crate::DeviceError> {+ let allocation_sizes = AllocationSizes::from_memory_hints(memory_hints);+ let device_memblock_size = allocation_sizes.min_device_memblock_size;+ let host_memblock_size = allocation_sizes.min_host_memblock_size;++ let allocator_desc = gpu_allocator::d3d12::AllocatorCreateDesc {+ device: gpu_allocator::d3d12::ID3D12DeviceVersion::Device(raw.clone()),+ debug_settings: Default::default(),+ allocation_sizes: allocation_sizes.into(),+ };++ let allocator = gpu_allocator::d3d12::Allocator::new(&allocator_desc).inspect_err(|e| {+ log::error!("Failed to create d3d12 allocator, error: {e}");+ })?;++ Ok(Self {+ inner: Arc::new(Mutex::new(allocator)),+ device_memblock_size,+ host_memblock_size,+ memory_budget_thresholds,+ })+ }++ pub(crate) fn generate_report(&self) -> wgt::AllocatorReport {+ let mut upstream = self.inner.lock().generate_report();++ let allocations = upstream+ .allocations+ .iter_mut()+ .map(|alloc| wgt::AllocationReport {+ name: core::mem::take(&mut alloc.name),+ offset: alloc.offset,+ size: alloc.size,+ })+ .collect();++ let blocks = upstream+ .blocks+ .iter()+ .map(|block| wgt::MemoryBlockReport {+ size: block.size,+ allocations: block.allocations.clone(),+ })+ .collect();++ wgt::AllocatorReport {+ allocations,+ blocks,+ total_allocated_bytes: upstream.total_allocated_bytes,+ total_reserved_bytes: upstream.total_capacity_bytes,+ }+ }+}++/// To allow us to construct buffers from both a `Device` and `CommandEncoder`+/// without needing each function to take a million arguments, we create a+/// borrowed context struct that contains the relevant members.+pub(crate) struct DeviceAllocationContext<'a> {+ pub(crate) raw: &'a Direct3D12::ID3D12Device,+ pub(crate) shared: &'a super::DeviceShared,+ pub(crate) mem_allocator: &'a Allocator,+ pub(crate) counters: &'a wgt::HalCounters,+}++impl<'a> From<&'a super::Device> for DeviceAllocationContext<'a> {+ fn from(device: &'a super::Device) -> Self {+ Self {+ raw: &device.raw,+ shared: &device.shared,+ mem_allocator: &device.mem_allocator,+ counters: &device.counters,+ }+ }+}++impl<'a> From<&'a super::CommandEncoder> for DeviceAllocationContext<'a> {+ fn from(encoder: &'a super::CommandEncoder) -> Self {+ Self {+ raw: &encoder.device,+ shared: &encoder.shared,+ mem_allocator: &encoder.mem_allocator,+ counters: &encoder.counters,+ }+ }+}++impl<'a> DeviceAllocationContext<'a> {+ ///////////////////////+ // Resource Creation //+ ///////////////////////++ pub(crate) fn create_buffer(+ &self,+ desc: &crate::BufferDescriptor,+ ) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {+ let is_cpu_read = desc.usage.contains(wgt::BufferUses::MAP_READ);+ let is_cpu_write = desc.usage.contains(wgt::BufferUses::MAP_WRITE);++ let location = match (is_cpu_read, is_cpu_write) {+ (true, true) => MemoryLocation::CpuToGpu,+ (true, false) => MemoryLocation::GpuToCpu,+ (false, true) => MemoryLocation::CpuToGpu,+ (false, false) => MemoryLocation::GpuOnly,+ };++ let raw_desc = conv::map_buffer_descriptor(desc);+ let allocation_info =+ self.error_if_would_oom_on_resource_allocation(&raw_desc, location)?;++ let (resource, allocation) = if self.shared.private_caps.suballocation_supported {+ self.create_placed_buffer(desc, raw_desc, allocation_info, location)?+ } else {+ self.create_committed_buffer(raw_desc, location)?+ };++ if let Some(label) = desc.label {+ resource.set_name(label)?;+ }++ self.counters.buffer_memory.add(allocation.size() as isize);++ Ok((resource, allocation))+ }++ pub(crate) fn create_texture(+ &self,+ desc: &crate::TextureDescriptor,+ raw_desc: Direct3D12::D3D12_RESOURCE_DESC,+ ) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {+ let location = MemoryLocation::GpuOnly;+ let allocation_info =+ self.error_if_would_oom_on_resource_allocation(&raw_desc, location)?;++ let (resource, allocation) = if self.shared.private_caps.suballocation_supported {+ self.create_placed_texture(desc, raw_desc, allocation_info, location)?+ } else {+ self.create_committed_texture(desc, raw_desc)?+ };++ if let Some(label) = desc.label {+ resource.set_name(label)?;+ }++ self.counters.texture_memory.add(allocation.size() as isize);++ Ok((resource, allocation))+ }++ pub(crate) fn create_acceleration_structure(+ &self,+ desc: &crate::AccelerationStructureDescriptor,+ raw_desc: Direct3D12::D3D12_RESOURCE_DESC,+ ) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {+ let location = MemoryLocation::GpuOnly;+ let allocation_info =+ self.error_if_would_oom_on_resource_allocation(&raw_desc, location)?;++ let (resource, allocation) = if self.shared.private_caps.suballocation_supported {+ self.create_placed_acceleration_structure(desc, raw_desc, allocation_info, location)?+ } else {+ self.create_committed_acceleration_structure(desc, raw_desc)?+ };++ if let Some(label) = desc.label {+ resource.set_name(label)?;+ }++ self.counters+ .acceleration_structure_memory+ .add(allocation.size() as isize);++ Ok((resource, allocation))+ }++ //////////////////////////+ // Resource Destruction //+ //////////////////////////++ pub(crate) fn free_resource(+ &self,+ resource: Direct3D12::ID3D12Resource,+ allocation: Allocation,+ ) {+ // Make sure the resource is released before we free the allocation.+ drop(resource);++ let counter = match allocation.ty {+ AllocationType::Buffer => &self.counters.buffer_memory,+ AllocationType::Texture => &self.counters.texture_memory,+ AllocationType::AccelerationStructure => &self.counters.acceleration_structure_memory,+ };+ counter.sub(allocation.size() as isize);++ if let AllocationInner::Placed { inner } = allocation.inner {+ match self.mem_allocator.inner.lock().free(inner) {+ Ok(_) => (),+ // TODO: Don't panic here+ Err(e) => panic!("Failed to destroy dx12 {:?}, {e}", allocation.ty),+ };+ }+ }++ ///////////////////////////////+ // Placed Resource Creation ///+ ///////////////////////////////++ fn create_placed_buffer(+ &self,+ desc: &crate::BufferDescriptor<'_>,+ raw_desc: Direct3D12::D3D12_RESOURCE_DESC,+ allocation_info: Direct3D12::D3D12_RESOURCE_ALLOCATION_INFO,+ location: MemoryLocation,+ ) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {+ let name = desc.label.unwrap_or("Unlabeled buffer");++ let mut allocator = self.mem_allocator.inner.lock();++ let allocation_desc = AllocationCreateDesc {+ name,+ location,+ size: allocation_info.SizeInBytes,+ alignment: allocation_info.Alignment,+ resource_category: gpu_allocator::d3d12::ResourceCategory::from(&raw_desc),+ };++ let allocation = allocator.allocate(&allocation_desc)?;+ let mut resource = None;+ unsafe {+ self.raw.CreatePlacedResource(+ allocation.heap(),+ allocation.offset(),+ &raw_desc,+ Direct3D12::D3D12_RESOURCE_STATE_COMMON,+ None,+ &mut resource,+ )+ }+ .into_device_result("Placed buffer creation")?;++ let resource = resource.ok_or(crate::DeviceError::Unexpected)?;+ let wrapped_allocation = Allocation::placed(allocation, AllocationType::Buffer);++ Ok((resource, wrapped_allocation))+ }++ fn create_placed_texture(+ &self,+ desc: &crate::TextureDescriptor<'_>,+ raw_desc: Direct3D12::D3D12_RESOURCE_DESC,+ allocation_info: Direct3D12::D3D12_RESOURCE_ALLOCATION_INFO,+ location: MemoryLocation,+ ) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {+ let name = desc.label.unwrap_or("Unlabeled texture");++ let mut allocator = self.mem_allocator.inner.lock();++ let allocation_desc = AllocationCreateDesc {+ name,+ location,+ size: allocation_info.SizeInBytes,+ alignment: allocation_info.Alignment,+ resource_category: gpu_allocator::d3d12::ResourceCategory::from(&raw_desc),+ };++ let allocation = allocator.allocate(&allocation_desc)?;+ let mut resource = None;+ unsafe {+ self.raw.CreatePlacedResource(+ allocation.heap(),+ allocation.offset(),+ &raw_desc,+ Direct3D12::D3D12_RESOURCE_STATE_COMMON,+ None, // clear value+ &mut resource,+ )+ }+ .into_device_result("Placed texture creation")?;++ let resource = resource.ok_or(crate::DeviceError::Unexpected)?;+ let wrapped_allocation = Allocation::placed(allocation, AllocationType::Texture);++ Ok((resource, wrapped_allocation))+ }++ fn create_placed_acceleration_structure(+ &self,+ desc: &crate::AccelerationStructureDescriptor<'_>,+ raw_desc: Direct3D12::D3D12_RESOURCE_DESC,+ allocation_info: Direct3D12::D3D12_RESOURCE_ALLOCATION_INFO,+ location: MemoryLocation,+ ) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {+ let name = desc.label.unwrap_or("Unlabeled acceleration structure");++ let mut allocator = self.mem_allocator.inner.lock();++ let allocation_desc = AllocationCreateDesc {+ name,+ location,+ size: allocation_info.SizeInBytes,+ alignment: allocation_info.Alignment,+ resource_category: gpu_allocator::d3d12::ResourceCategory::from(&raw_desc),+ };++ let allocation = allocator.allocate(&allocation_desc)?;+ let mut resource = None;+ unsafe {+ self.raw.CreatePlacedResource(+ allocation.heap(),+ allocation.offset(),+ &raw_desc,+ Direct3D12::D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE,+ None,+ &mut resource,+ )+ }+ .into_device_result("Placed acceleration structure creation")?;++ let resource = resource.ok_or(crate::DeviceError::Unexpected)?;+ let wrapped_allocation =+ Allocation::placed(allocation, AllocationType::AccelerationStructure);++ Ok((resource, wrapped_allocation))+ }++ /////////////////////////////////+ // Committed Resource Creation //+ /////////////////////////////////++ fn create_committed_buffer(+ &self,+ raw_desc: Direct3D12::D3D12_RESOURCE_DESC,+ location: MemoryLocation,+ ) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {+ let is_uma = matches!(+ self.shared.private_caps.memory_architecture,+ crate::dx12::MemoryArchitecture::Unified { .. }+ );++ let heap_properties = Direct3D12::D3D12_HEAP_PROPERTIES {+ Type: Direct3D12::D3D12_HEAP_TYPE_CUSTOM,+ CPUPageProperty: match location {+ MemoryLocation::GpuOnly => Direct3D12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE,+ MemoryLocation::CpuToGpu => Direct3D12::D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE,+ MemoryLocation::GpuToCpu => Direct3D12::D3D12_CPU_PAGE_PROPERTY_WRITE_BACK,+ _ => unreachable!(),+ },+ MemoryPoolPreference: match (is_uma, location) {+ // On dedicated GPUs, we only use L1 for GPU-only allocations.+ (false, MemoryLocation::GpuOnly) => Direct3D12::D3D12_MEMORY_POOL_L1,+ (_, _) => Direct3D12::D3D12_MEMORY_POOL_L0,+ },+ CreationNodeMask: 0,+ VisibleNodeMask: 0,+ };++ let mut resource = None;++ unsafe {+ self.raw.CreateCommittedResource(+ &heap_properties,+ if self.shared.private_caps.heap_create_not_zeroed {+ Direct3D12::D3D12_HEAP_FLAG_CREATE_NOT_ZEROED+ } else {+ Direct3D12::D3D12_HEAP_FLAG_NONE+ },+ &raw_desc,+ Direct3D12::D3D12_RESOURCE_STATE_COMMON,+ None,+ &mut resource,+ )+ }+ .into_device_result("Committed buffer creation")?;++ let resource = resource.ok_or(crate::DeviceError::Unexpected)?;+ let wrapped_allocation = Allocation::none(AllocationType::Buffer, raw_desc.Width);++ Ok((resource, wrapped_allocation))+ }++ fn create_committed_texture(+ &self,+ desc: &crate::TextureDescriptor,+ raw_desc: Direct3D12::D3D12_RESOURCE_DESC,+ ) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {+ let heap_properties = Direct3D12::D3D12_HEAP_PROPERTIES {+ Type: Direct3D12::D3D12_HEAP_TYPE_CUSTOM,+ CPUPageProperty: Direct3D12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE,+ MemoryPoolPreference: match self.shared.private_caps.memory_architecture {+ crate::dx12::MemoryArchitecture::NonUnified => Direct3D12::D3D12_MEMORY_POOL_L1,+ crate::dx12::MemoryArchitecture::Unified { .. } => Direct3D12::D3D12_MEMORY_POOL_L0,+ },+ CreationNodeMask: 0,+ VisibleNodeMask: 0,+ };++ let mut resource = None;++ unsafe {+ self.raw.CreateCommittedResource(+ &heap_properties,+ if self.shared.private_caps.heap_create_not_zeroed {+ Direct3D12::D3D12_HEAP_FLAG_CREATE_NOT_ZEROED+ } else {+ Direct3D12::D3D12_HEAP_FLAG_NONE+ },+ &raw_desc,+ Direct3D12::D3D12_RESOURCE_STATE_COMMON,+ None, // clear value+ &mut resource,+ )+ }+ .into_device_result("Committed texture creation")?;++ let resource = resource.ok_or(crate::DeviceError::Unexpected)?;+ let wrapped_allocation = Allocation::none(+ AllocationType::Texture,+ desc.format.theoretical_memory_footprint(desc.size),+ );++ Ok((resource, wrapped_allocation))+ }++ fn create_committed_acceleration_structure(+ &self,+ desc: &crate::AccelerationStructureDescriptor,+ raw_desc: Direct3D12::D3D12_RESOURCE_DESC,+ ) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {+ let heap_properties = Direct3D12::D3D12_HEAP_PROPERTIES {+ Type: Direct3D12::D3D12_HEAP_TYPE_CUSTOM,+ CPUPageProperty: Direct3D12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE,+ MemoryPoolPreference: match self.shared.private_caps.memory_architecture {+ crate::dx12::MemoryArchitecture::NonUnified => Direct3D12::D3D12_MEMORY_POOL_L1,+ crate::dx12::MemoryArchitecture::Unified { .. } => Direct3D12::D3D12_MEMORY_POOL_L0,+ },+ CreationNodeMask: 0,+ VisibleNodeMask: 0,+ };++ let mut resource = None;++ unsafe {+ self.raw.CreateCommittedResource(+ &heap_properties,+ if self.shared.private_caps.heap_create_not_zeroed {+ Direct3D12::D3D12_HEAP_FLAG_CREATE_NOT_ZEROED+ } else {+ Direct3D12::D3D12_HEAP_FLAG_NONE+ },+ &raw_desc,+ Direct3D12::D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE,+ None,+ &mut resource,+ )+ }+ .into_device_result("Committed acceleration structure creation")?;++ let resource = resource.ok_or(crate::DeviceError::Unexpected)?;+ let wrapped_allocation = Allocation::none(AllocationType::AccelerationStructure, desc.size);++ Ok((resource, wrapped_allocation))+ }++ fn error_if_would_oom_on_resource_allocation(+ &self,+ desc: &Direct3D12::D3D12_RESOURCE_DESC,+ location: MemoryLocation,+ ) -> Result<Direct3D12::D3D12_RESOURCE_ALLOCATION_INFO, crate::DeviceError> {+ let allocation_info = unsafe {+ self.raw+ .GetResourceAllocationInfo(0, core::slice::from_ref(desc))+ };++ // Some versions of WARP return SizeInBytes == 0 for very large+ // allocations. Proceeding to attempt to allocate a zero-sized resource+ // will result in a device lost error, so it seems preferable to return+ // an out of memory error now.+ if allocation_info.SizeInBytes == 0 {+ return Err(crate::DeviceError::OutOfMemory);+ }++ let Some(threshold) = self+ .mem_allocator+ .memory_budget_thresholds+ .for_resource_creation+ else {+ return Ok(allocation_info);+ };++ let memory_segment_group = match location {+ MemoryLocation::Unknown => unreachable!(),+ MemoryLocation::GpuOnly => Dxgi::DXGI_MEMORY_SEGMENT_GROUP_LOCAL,+ MemoryLocation::CpuToGpu | MemoryLocation::GpuToCpu => {+ match self.shared.private_caps.memory_architecture {+ super::MemoryArchitecture::Unified { .. } => {+ Dxgi::DXGI_MEMORY_SEGMENT_GROUP_LOCAL+ }+ super::MemoryArchitecture::NonUnified => {+ Dxgi::DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL+ }+ }+ }+ };++ let info = self+ .shared+ .adapter+ .query_video_memory_info(memory_segment_group)?;++ let memblock_size = match location {+ MemoryLocation::Unknown => unreachable!(),+ MemoryLocation::GpuOnly => self.mem_allocator.device_memblock_size,+ MemoryLocation::CpuToGpu | MemoryLocation::GpuToCpu => {+ self.mem_allocator.host_memblock_size+ }+ };++ if info+ .CurrentUsage+ .checked_add(allocation_info.SizeInBytes.max(memblock_size))+ .is_none_or(|usage| usage >= info.Budget / 100 * threshold as u64)+ {+ return Err(crate::DeviceError::OutOfMemory);+ }++ Ok(allocation_info)+ }+}diff --git a/vendor/wgpu-hal/src/dx12/types.rs b/vendor/wgpu-hal/src/dx12/types.rsnew file mode 100644index 00000000..5270c6ca--- /dev/null+++ b/vendor/wgpu-hal/src/dx12/types.rs@@ -0,0 +1,39 @@+#![allow(non_camel_case_types)]+#![allow(non_snake_case)]++use windows::Win32::Graphics::Dxgi;++windows_core::imp::define_interface!(+ ISwapChainPanelNative,+ ISwapChainPanelNative_Vtbl,+ 0x63aad0b8_7c24_40ff_85a8_640d944cc325+);+impl core::ops::Deref for ISwapChainPanelNative {+ type Target = windows_core::IUnknown;+ fn deref(&self) -> &Self::Target {+ unsafe { core::mem::transmute(self) }+ }+}+windows_core::imp::interface_hierarchy!(ISwapChainPanelNative, windows_core::IUnknown);+impl ISwapChainPanelNative {+ pub unsafe fn SetSwapChain<P0>(&self, swap_chain: P0) -> windows_core::Result<()>+ where+ P0: windows_core::Param<Dxgi::IDXGISwapChain1>,+ {+ unsafe {+ (windows_core::Interface::vtable(self).SetSwapChain)(+ windows_core::Interface::as_raw(self),+ swap_chain.param().abi(),+ )+ }+ .ok()+ }+}+#[repr(C)]+pub struct ISwapChainPanelNative_Vtbl {+ pub base__: windows_core::IUnknown_Vtbl,+ pub SetSwapChain: unsafe extern "system" fn(+ swap_chain_panel_native: *mut core::ffi::c_void,+ swap_chain: *mut core::ffi::c_void,+ ) -> windows_core::HRESULT,+}diff --git a/vendor/wgpu-hal/src/dx12/view.rs b/vendor/wgpu-hal/src/dx12/view.rsnew file mode 100644index 00000000..ba715833--- /dev/null+++ b/vendor/wgpu-hal/src/dx12/view.rs@@ -0,0 +1,370 @@+use windows::Win32::Graphics::{Direct3D12, Dxgi};++use crate::auxil;++pub(super) struct ViewDescriptor {+ dimension: wgt::TextureViewDimension,+ pub aspects: crate::FormatAspects,+ pub rtv_dsv_format: Dxgi::Common::DXGI_FORMAT,+ srv_uav_format: Option<Dxgi::Common::DXGI_FORMAT>,+ multisampled: bool,+ array_layer_base: u32,+ array_layer_count: u32,+ mip_level_base: u32,+ mip_level_count: u32,+ plane_slice_override: Option<u32>,+}++impl crate::TextureViewDescriptor<'_> {+ pub(super) fn to_internal(&self, texture: &super::Texture) -> ViewDescriptor {+ let aspects = crate::FormatAspects::new(texture.format, self.range.aspect);++ ViewDescriptor {+ dimension: self.dimension,+ aspects,+ rtv_dsv_format: auxil::dxgi::conv::map_texture_format(self.format),+ srv_uav_format: auxil::dxgi::conv::map_texture_format_for_srv_uav(self.format, aspects),+ multisampled: texture.sample_count > 1,+ mip_level_base: self.range.base_mip_level,+ mip_level_count: self.range.mip_level_count.unwrap_or(!0),+ array_layer_base: self.range.base_array_layer,+ array_layer_count: self.range.array_layer_count.unwrap_or(!0),+ plane_slice_override: texture.plane_slice_override(),+ }+ }+}++fn aspects_to_plane(aspects: crate::FormatAspects) -> u32 {+ match aspects {+ crate::FormatAspects::STENCIL => 1,+ crate::FormatAspects::PLANE_1 => 1,+ crate::FormatAspects::PLANE_2 => 2,+ _ => 0,+ }+}++impl ViewDescriptor {+ pub(super) fn plane_slice(&self) -> u32 {+ self.plane_slice_override+ .unwrap_or_else(|| aspects_to_plane(self.aspects))+ }+}++/// Shader component mapping for stencil views+///+/// Stencil views use `DXGI_FORMAT_X24_TYPELESS_G8_UINT` or+/// `DXGI_FORMAT_X32_TYPELESS_G8X24_UINT`, which have the stencil value in+/// the green component. WebGPU specifies that the stencil value be in the+/// red component. It also specifies that the remaining components _should_+/// be (0, 0, 1), but may have an unspecified value.+const STENCIL_COMPONENT_MAPPING: Direct3D12::D3D12_SHADER_COMPONENT_MAPPING =+ super::conv::make_shader_component_mapping(+ Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_FROM_MEMORY_COMPONENT_1,+ Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0,+ Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0,+ Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_1,+ );++impl ViewDescriptor {+ pub(crate) unsafe fn to_srv(&self) -> Option<Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC> {+ let swizzle = if self.aspects == crate::FormatAspects::STENCIL {+ STENCIL_COMPONENT_MAPPING.0 as u32+ } else {+ Direct3D12::D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING+ };++ let mut desc = Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC {+ Format: self.srv_uav_format?,+ ViewDimension: Direct3D12::D3D12_SRV_DIMENSION_UNKNOWN,+ Shader4ComponentMapping: swizzle,+ Anonymous: Default::default(),+ };++ match self.dimension {+ wgt::TextureViewDimension::D1 => {+ desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURE1D;+ desc.Anonymous.Texture1D = Direct3D12::D3D12_TEX1D_SRV {+ MostDetailedMip: self.mip_level_base,+ MipLevels: self.mip_level_count,+ ResourceMinLODClamp: 0.0,+ }+ }+ /*+ wgt::TextureViewDimension::D1Array => {+ desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURE1DARRAY;+ desc.Anonymous.Texture1DArray = Direct3D12::D3D12_TEX1D_ARRAY_SRV {+ MostDetailedMip: self.mip_level_base,+ MipLevels: self.mip_level_count,+ FirstArraySlice: self.array_layer_base,+ ArraySize: self.array_layer_count,+ ResourceMinLODClamp: 0.0,+ }+ }+ */+ wgt::TextureViewDimension::D2 if self.multisampled && self.array_layer_base == 0 => {+ desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURE2DMS;+ desc.Anonymous.Texture2DMS = Direct3D12::D3D12_TEX2DMS_SRV {+ UnusedField_NothingToDefine: 0,+ }+ }+ wgt::TextureViewDimension::D2 if self.array_layer_base == 0 => {+ desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURE2D;+ desc.Anonymous.Texture2D = Direct3D12::D3D12_TEX2D_SRV {+ MostDetailedMip: self.mip_level_base,+ MipLevels: self.mip_level_count,+ PlaneSlice: self.plane_slice(),+ ResourceMinLODClamp: 0.0,+ }+ }+ wgt::TextureViewDimension::D2 | wgt::TextureViewDimension::D2Array+ if self.multisampled =>+ {+ desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY;+ desc.Anonymous.Texture2DMSArray = Direct3D12::D3D12_TEX2DMS_ARRAY_SRV {+ FirstArraySlice: self.array_layer_base,+ ArraySize: self.array_layer_count,+ }+ }+ wgt::TextureViewDimension::D2 | wgt::TextureViewDimension::D2Array => {+ desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURE2DARRAY;+ desc.Anonymous.Texture2DArray = Direct3D12::D3D12_TEX2D_ARRAY_SRV {+ MostDetailedMip: self.mip_level_base,+ MipLevels: self.mip_level_count,+ FirstArraySlice: self.array_layer_base,+ ArraySize: self.array_layer_count,+ PlaneSlice: self.plane_slice(),+ ResourceMinLODClamp: 0.0,+ }+ }+ wgt::TextureViewDimension::D3 => {+ desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURE3D;+ desc.Anonymous.Texture3D = Direct3D12::D3D12_TEX3D_SRV {+ MostDetailedMip: self.mip_level_base,+ MipLevels: self.mip_level_count,+ ResourceMinLODClamp: 0.0,+ }+ }+ wgt::TextureViewDimension::Cube if self.array_layer_base == 0 => {+ desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURECUBE;+ desc.Anonymous.TextureCube = Direct3D12::D3D12_TEXCUBE_SRV {+ MostDetailedMip: self.mip_level_base,+ MipLevels: self.mip_level_count,+ ResourceMinLODClamp: 0.0,+ }+ }+ wgt::TextureViewDimension::Cube | wgt::TextureViewDimension::CubeArray => {+ desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURECUBEARRAY;+ desc.Anonymous.TextureCubeArray = Direct3D12::D3D12_TEXCUBE_ARRAY_SRV {+ MostDetailedMip: self.mip_level_base,+ MipLevels: self.mip_level_count,+ First2DArrayFace: self.array_layer_base,+ NumCubes: if self.array_layer_count == !0 {+ !0+ } else {+ self.array_layer_count / 6+ },+ ResourceMinLODClamp: 0.0,+ }+ }+ }++ Some(desc)+ }++ pub(crate) unsafe fn to_uav(&self) -> Option<Direct3D12::D3D12_UNORDERED_ACCESS_VIEW_DESC> {+ let mut desc = Direct3D12::D3D12_UNORDERED_ACCESS_VIEW_DESC {+ Format: self.srv_uav_format?,+ ViewDimension: Direct3D12::D3D12_UAV_DIMENSION_UNKNOWN,+ Anonymous: Default::default(),+ };++ match self.dimension {+ wgt::TextureViewDimension::D1 => {+ desc.ViewDimension = Direct3D12::D3D12_UAV_DIMENSION_TEXTURE1D;+ desc.Anonymous.Texture1D = Direct3D12::D3D12_TEX1D_UAV {+ MipSlice: self.mip_level_base,+ }+ }+ /*+ wgt::TextureViewDimension::D1Array => {+ desc.ViewDimension = Direct3D12::D3D12_UAV_DIMENSION_TEXTURE1DARRAY;+ desc.Anonymous.Texture1DArray = Direct3D12::D3D12_TEX1D_ARRAY_UAV {+ MipSlice: self.mip_level_base,+ FirstArraySlice: self.array_layer_base,+ ArraySize,+ }+ }*/+ wgt::TextureViewDimension::D2 if self.array_layer_base == 0 => {+ desc.ViewDimension = Direct3D12::D3D12_UAV_DIMENSION_TEXTURE2D;+ desc.Anonymous.Texture2D = Direct3D12::D3D12_TEX2D_UAV {+ MipSlice: self.mip_level_base,+ PlaneSlice: self.plane_slice(),+ }+ }+ wgt::TextureViewDimension::D2 | wgt::TextureViewDimension::D2Array => {+ desc.ViewDimension = Direct3D12::D3D12_UAV_DIMENSION_TEXTURE2DARRAY;+ desc.Anonymous.Texture2DArray = Direct3D12::D3D12_TEX2D_ARRAY_UAV {+ MipSlice: self.mip_level_base,+ FirstArraySlice: self.array_layer_base,+ ArraySize: self.array_layer_count,+ PlaneSlice: self.plane_slice(),+ }+ }+ wgt::TextureViewDimension::D3 => {+ desc.ViewDimension = Direct3D12::D3D12_UAV_DIMENSION_TEXTURE3D;+ desc.Anonymous.Texture3D = Direct3D12::D3D12_TEX3D_UAV {+ MipSlice: self.mip_level_base,+ FirstWSlice: 0,+ WSize: u32::MAX,+ }+ }+ wgt::TextureViewDimension::Cube | wgt::TextureViewDimension::CubeArray => {+ panic!("Unable to view texture as cube UAV")+ }+ }++ Some(desc)+ }++ pub(crate) unsafe fn to_rtv(&self) -> Direct3D12::D3D12_RENDER_TARGET_VIEW_DESC {+ let mut desc = Direct3D12::D3D12_RENDER_TARGET_VIEW_DESC {+ Format: self.rtv_dsv_format,+ ViewDimension: Direct3D12::D3D12_RTV_DIMENSION_UNKNOWN,+ Anonymous: Default::default(),+ };++ match self.dimension {+ wgt::TextureViewDimension::D1 => {+ desc.ViewDimension = Direct3D12::D3D12_RTV_DIMENSION_TEXTURE1D;+ desc.Anonymous.Texture1D = Direct3D12::D3D12_TEX1D_RTV {+ MipSlice: self.mip_level_base,+ }+ }+ /*+ wgt::TextureViewDimension::D1Array => {+ desc.ViewDimension = Direct3D12::D3D12_RTV_DIMENSION_TEXTURE1DARRAY;+ desc.Anonymous.Texture1DArray = Direct3D12::D3D12_TEX1D_ARRAY_RTV {+ MipSlice: self.mip_level_base,+ FirstArraySlice: self.array_layer_base,+ ArraySize,+ }+ }*/+ wgt::TextureViewDimension::D2 if self.multisampled && self.array_layer_base == 0 => {+ desc.ViewDimension = Direct3D12::D3D12_RTV_DIMENSION_TEXTURE2DMS;+ desc.Anonymous.Texture2DMS = Direct3D12::D3D12_TEX2DMS_RTV {+ UnusedField_NothingToDefine: 0,+ }+ }+ wgt::TextureViewDimension::D2 if self.array_layer_base == 0 => {+ desc.ViewDimension = Direct3D12::D3D12_RTV_DIMENSION_TEXTURE2D;+ desc.Anonymous.Texture2D = Direct3D12::D3D12_TEX2D_RTV {+ MipSlice: self.mip_level_base,+ PlaneSlice: self.plane_slice(),+ }+ }+ wgt::TextureViewDimension::D2 | wgt::TextureViewDimension::D2Array+ if self.multisampled =>+ {+ desc.ViewDimension = Direct3D12::D3D12_RTV_DIMENSION_TEXTURE2DMSARRAY;+ desc.Anonymous.Texture2DMSArray = Direct3D12::D3D12_TEX2DMS_ARRAY_RTV {+ FirstArraySlice: self.array_layer_base,+ ArraySize: self.array_layer_count,+ }+ }+ wgt::TextureViewDimension::D2 | wgt::TextureViewDimension::D2Array => {+ desc.ViewDimension = Direct3D12::D3D12_RTV_DIMENSION_TEXTURE2DARRAY;+ desc.Anonymous.Texture2DArray = Direct3D12::D3D12_TEX2D_ARRAY_RTV {+ MipSlice: self.mip_level_base,+ FirstArraySlice: self.array_layer_base,+ ArraySize: self.array_layer_count,+ PlaneSlice: self.plane_slice(),+ }+ }+ wgt::TextureViewDimension::D3+ | wgt::TextureViewDimension::Cube+ | wgt::TextureViewDimension::CubeArray => {+ panic!("Unable to view texture as cube or 3D RTV")+ }+ }++ desc+ }++ pub(crate) unsafe fn to_dsv(+ &self,+ read_only: bool,+ ) -> Direct3D12::D3D12_DEPTH_STENCIL_VIEW_DESC {+ let mut desc = Direct3D12::D3D12_DEPTH_STENCIL_VIEW_DESC {+ Format: self.rtv_dsv_format,+ ViewDimension: Direct3D12::D3D12_DSV_DIMENSION_UNKNOWN,+ Flags: {+ let mut flags = Direct3D12::D3D12_DSV_FLAG_NONE;+ if read_only {+ if self.aspects.contains(crate::FormatAspects::DEPTH) {+ flags |= Direct3D12::D3D12_DSV_FLAG_READ_ONLY_DEPTH;+ }+ if self.aspects.contains(crate::FormatAspects::STENCIL) {+ flags |= Direct3D12::D3D12_DSV_FLAG_READ_ONLY_STENCIL;+ }+ }+ flags+ },+ Anonymous: Default::default(),+ };++ match self.dimension {+ wgt::TextureViewDimension::D1 => {+ desc.ViewDimension = Direct3D12::D3D12_DSV_DIMENSION_TEXTURE1D;+ desc.Anonymous.Texture1D = Direct3D12::D3D12_TEX1D_DSV {+ MipSlice: self.mip_level_base,+ }+ }+ /*+ wgt::TextureViewDimension::D1Array => {+ desc.ViewDimension = Direct3D12::D3D12_DSV_DIMENSION_TEXTURE1DARRAY;+ desc.Anonymous.Texture1DArray = Direct3D12::D3D12_TEX1D_ARRAY_DSV {+ MipSlice: self.mip_level_base,+ FirstArraySlice: self.array_layer_base,+ ArraySize,+ }+ }*/+ wgt::TextureViewDimension::D2 if self.multisampled && self.array_layer_base == 0 => {+ desc.ViewDimension = Direct3D12::D3D12_DSV_DIMENSION_TEXTURE2DMS;+ desc.Anonymous.Texture2DMS = Direct3D12::D3D12_TEX2DMS_DSV {+ UnusedField_NothingToDefine: 0,+ }+ }+ wgt::TextureViewDimension::D2 if self.array_layer_base == 0 => {+ desc.ViewDimension = Direct3D12::D3D12_DSV_DIMENSION_TEXTURE2D;++ desc.Anonymous.Texture2D = Direct3D12::D3D12_TEX2D_DSV {+ MipSlice: self.mip_level_base,+ }+ }+ wgt::TextureViewDimension::D2 | wgt::TextureViewDimension::D2Array+ if self.multisampled =>+ {+ desc.ViewDimension = Direct3D12::D3D12_DSV_DIMENSION_TEXTURE2DMSARRAY;+ desc.Anonymous.Texture2DMSArray = Direct3D12::D3D12_TEX2DMS_ARRAY_DSV {+ FirstArraySlice: self.array_layer_base,+ ArraySize: self.array_layer_count,+ }+ }+ wgt::TextureViewDimension::D2 | wgt::TextureViewDimension::D2Array => {+ desc.ViewDimension = Direct3D12::D3D12_DSV_DIMENSION_TEXTURE2DARRAY;+ desc.Anonymous.Texture2DArray = Direct3D12::D3D12_TEX2D_ARRAY_DSV {+ MipSlice: self.mip_level_base,+ FirstArraySlice: self.array_layer_base,+ ArraySize: self.array_layer_count,+ }+ }+ wgt::TextureViewDimension::D3+ | wgt::TextureViewDimension::Cube+ | wgt::TextureViewDimension::CubeArray => {+ panic!("Unable to view texture as cube or 3D DSV")+ }+ }++ desc+ }+}diff --git a/vendor/wgpu-hal/src/dynamic/adapter.rs b/vendor/wgpu-hal/src/dynamic/adapter.rsnew file mode 100644index 00000000..4921d716--- /dev/null+++ b/vendor/wgpu-hal/src/dynamic/adapter.rs@@ -0,0 +1,95 @@+use alloc::boxed::Box;++use crate::{+ Adapter, Api, DeviceError, OpenDevice, SurfaceCapabilities, TextureFormatCapabilities,+};++use super::{DynDevice, DynQueue, DynResource, DynResourceExt, DynSurface};++#[expect(missing_debug_implementations, reason = "dyn")]+pub struct DynOpenDevice {+ pub device: Box<dyn DynDevice>,+ pub queue: Box<dyn DynQueue>,+}++impl<A: Api> From<OpenDevice<A>> for DynOpenDevice {+ fn from(open_device: OpenDevice<A>) -> Self {+ Self {+ device: Box::new(open_device.device),+ queue: Box::new(open_device.queue),+ }+ }+}++pub trait DynAdapter: DynResource {+ unsafe fn open(+ &self,+ features: wgt::Features,+ limits: &wgt::Limits,+ memory_hints: &wgt::MemoryHints,+ ) -> Result<DynOpenDevice, DeviceError>;++ unsafe fn texture_format_capabilities(+ &self,+ format: wgt::TextureFormat,+ ) -> TextureFormatCapabilities;++ unsafe fn surface_capabilities(&self, surface: &dyn DynSurface) -> Option<SurfaceCapabilities>;++ unsafe fn surface_display_hdr_info(+ &self,+ surface: &dyn DynSurface,+ ) -> Option<wgt::DisplayHdrInfo>;++ unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp;++ fn get_ordered_buffer_usages(&self) -> wgt::BufferUses;++ fn get_ordered_texture_usages(&self) -> wgt::TextureUses;+}++impl<A: Adapter + DynResource> DynAdapter for A {+ unsafe fn open(+ &self,+ features: wgt::Features,+ limits: &wgt::Limits,+ memory_hints: &wgt::MemoryHints,+ ) -> Result<DynOpenDevice, DeviceError> {+ unsafe { A::open(self, features, limits, memory_hints) }.map(|open_device| DynOpenDevice {+ device: Box::new(open_device.device),+ queue: Box::new(open_device.queue),+ })+ }++ unsafe fn texture_format_capabilities(+ &self,+ format: wgt::TextureFormat,+ ) -> TextureFormatCapabilities {+ unsafe { A::texture_format_capabilities(self, format) }+ }++ unsafe fn surface_capabilities(&self, surface: &dyn DynSurface) -> Option<SurfaceCapabilities> {+ let surface = surface.expect_downcast_ref();+ unsafe { A::surface_capabilities(self, surface) }+ }++ unsafe fn surface_display_hdr_info(+ &self,+ surface: &dyn DynSurface,+ ) -> Option<wgt::DisplayHdrInfo> {+ let surface = surface.expect_downcast_ref();+ unsafe { A::surface_display_hdr_info(self, surface) }+ }++ unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {+ unsafe { A::get_presentation_timestamp(self) }+ }++ fn get_ordered_buffer_usages(&self) -> wgt::BufferUses {+ A::get_ordered_buffer_usages(self)+ }++ fn get_ordered_texture_usages(&self) -> wgt::TextureUses {+ A::get_ordered_texture_usages(self)+ }+}diff --git a/vendor/wgpu-hal/src/dynamic/command.rs b/vendor/wgpu-hal/src/dynamic/command.rsnew file mode 100644index 00000000..aca5bbcb--- /dev/null+++ b/vendor/wgpu-hal/src/dynamic/command.rs@@ -0,0 +1,823 @@+use alloc::{boxed::Box, vec::Vec};+use core::ops::Range;++use crate::{+ AccelerationStructureBarrier, Api, Attachment, BufferBarrier, BufferBinding, BufferCopy,+ BufferTextureCopy, BuildAccelerationStructureDescriptor, ColorAttachment, CommandEncoder,+ ComputePassDescriptor, DepthStencilAttachment, DeviceError, DynRayTracingPipeline, Label,+ MemoryRange, PassTimestampWrites, RayTracingPassDescriptor, Rect, RenderPassDescriptor,+ TextureBarrier, TextureCopy,+};++use super::{+ DynAccelerationStructure, DynBindGroup, DynBuffer, DynCommandBuffer, DynComputePipeline,+ DynPipelineLayout, DynQuerySet, DynRenderPipeline, DynResource, DynResourceExt as _,+ DynTexture, DynTextureView,+};++pub trait DynCommandEncoder: DynResource + core::fmt::Debug {+ unsafe fn begin_encoding(&mut self, label: Label) -> Result<(), DeviceError>;++ unsafe fn discard_encoding(&mut self);++ unsafe fn end_encoding(&mut self) -> Result<Box<dyn DynCommandBuffer>, DeviceError>;++ unsafe fn reset_all(&mut self, command_buffers: Vec<Box<dyn DynCommandBuffer>>);++ unsafe fn transition_buffers(&mut self, barriers: &[BufferBarrier<'_, dyn DynBuffer>]);+ unsafe fn transition_textures(&mut self, barriers: &[TextureBarrier<'_, dyn DynTexture>]);++ unsafe fn clear_buffer(&mut self, buffer: &dyn DynBuffer, range: MemoryRange);++ unsafe fn copy_buffer_to_buffer(+ &mut self,+ src: &dyn DynBuffer,+ dst: &dyn DynBuffer,+ regions: &[BufferCopy],+ );++ unsafe fn copy_texture_to_texture(+ &mut self,+ src: &dyn DynTexture,+ src_usage: wgt::TextureUses,+ dst: &dyn DynTexture,+ regions: &[TextureCopy],+ );++ unsafe fn copy_buffer_to_texture(+ &mut self,+ src: &dyn DynBuffer,+ dst: &dyn DynTexture,+ regions: &[BufferTextureCopy],+ );++ unsafe fn copy_texture_to_buffer(+ &mut self,+ src: &dyn DynTexture,+ src_usage: wgt::TextureUses,+ dst: &dyn DynBuffer,+ regions: &[BufferTextureCopy],+ );++ unsafe fn set_bind_group(+ &mut self,+ layout: &dyn DynPipelineLayout,+ index: u32,+ group: &dyn DynBindGroup,+ dynamic_offsets: &[wgt::DynamicOffset],+ );++ unsafe fn set_immediates(+ &mut self,+ layout: &dyn DynPipelineLayout,+ offset_bytes: u32,+ data: &[u32],+ );++ unsafe fn insert_debug_marker(&mut self, label: &str);+ unsafe fn begin_debug_marker(&mut self, group_label: &str);+ unsafe fn end_debug_marker(&mut self);++ unsafe fn begin_query(&mut self, set: &dyn DynQuerySet, index: u32);+ unsafe fn end_query(&mut self, set: &dyn DynQuerySet, index: u32);+ unsafe fn write_timestamp(&mut self, set: &dyn DynQuerySet, index: u32);+ unsafe fn reset_queries(&mut self, set: &dyn DynQuerySet, range: Range<u32>);+ unsafe fn copy_query_results(+ &mut self,+ set: &dyn DynQuerySet,+ range: Range<u32>,+ buffer: &dyn DynBuffer,+ offset: wgt::BufferAddress,+ stride: wgt::BufferSize,+ );++ unsafe fn begin_render_pass(+ &mut self,+ desc: &RenderPassDescriptor<dyn DynQuerySet, dyn DynTextureView>,+ ) -> Result<(), DeviceError>;+ unsafe fn end_render_pass(&mut self);++ unsafe fn set_render_pipeline(&mut self, pipeline: &dyn DynRenderPipeline);++ unsafe fn set_index_buffer<'a>(+ &mut self,+ binding: BufferBinding<'a, dyn DynBuffer>,+ format: wgt::IndexFormat,+ );++ unsafe fn set_vertex_buffer<'a>(+ &mut self,+ index: u32,+ binding: BufferBinding<'a, dyn DynBuffer>,+ );+ unsafe fn set_viewport(&mut self, rect: &Rect<f32>, depth_range: Range<f32>);+ unsafe fn set_scissor_rect(&mut self, rect: &Rect<u32>);+ unsafe fn set_stencil_reference(&mut self, value: u32);+ unsafe fn set_blend_constants(&mut self, color: &[f32; 4]);++ unsafe fn draw(+ &mut self,+ first_vertex: u32,+ vertex_count: u32,+ first_instance: u32,+ instance_count: u32,+ );+ unsafe fn draw_indexed(+ &mut self,+ first_index: u32,+ index_count: u32,+ base_vertex: i32,+ first_instance: u32,+ instance_count: u32,+ );+ unsafe fn draw_mesh_tasks(+ &mut self,+ group_count_x: u32,+ group_count_y: u32,+ group_count_z: u32,+ );+ unsafe fn draw_indirect(+ &mut self,+ buffer: &dyn DynBuffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ );+ unsafe fn draw_indexed_indirect(+ &mut self,+ buffer: &dyn DynBuffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ );+ unsafe fn draw_mesh_tasks_indirect(+ &mut self,+ buffer: &dyn DynBuffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ );+ unsafe fn draw_indirect_count(+ &mut self,+ buffer: &dyn DynBuffer,+ offset: wgt::BufferAddress,+ count_buffer: &dyn DynBuffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ );+ unsafe fn draw_indexed_indirect_count(+ &mut self,+ buffer: &dyn DynBuffer,+ offset: wgt::BufferAddress,+ count_buffer: &dyn DynBuffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ );+ unsafe fn draw_mesh_tasks_indirect_count(+ &mut self,+ buffer: &dyn DynBuffer,+ offset: wgt::BufferAddress,+ count_buffer: &dyn DynBuffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ );++ unsafe fn begin_compute_pass(&mut self, desc: &ComputePassDescriptor<dyn DynQuerySet>);+ unsafe fn end_compute_pass(&mut self);++ unsafe fn set_compute_pipeline(&mut self, pipeline: &dyn DynComputePipeline);++ unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]);+ unsafe fn dispatch_workgroups_indirect(+ &mut self,+ buffer: &dyn DynBuffer,+ offset: wgt::BufferAddress,+ );++ unsafe fn begin_ray_tracing_pass(&mut self, desc: &RayTracingPassDescriptor);+ unsafe fn end_ray_tracing_pass(&mut self);++ unsafe fn trace_rays(+ &mut self,+ count: [u32; 3],+ ray_generation_group_data: crate::PipelineGroupData<dyn DynBuffer>,+ miss_group_data: crate::PipelineGroupData<dyn DynBuffer>,+ intersection_group_data: crate::PipelineGroupData<dyn DynBuffer>,+ );++ unsafe fn set_ray_tracing_pipeline(&mut self, pipeline: &dyn DynRayTracingPipeline);++ unsafe fn build_acceleration_structures<'a>(+ &mut self,+ descriptors: &'a [BuildAccelerationStructureDescriptor<+ 'a,+ dyn DynBuffer,+ dyn DynAccelerationStructure,+ >],+ );+ unsafe fn place_acceleration_structure_barrier(+ &mut self,+ barrier: AccelerationStructureBarrier,+ );+ unsafe fn copy_acceleration_structure_to_acceleration_structure(+ &mut self,+ src: &dyn DynAccelerationStructure,+ dst: &dyn DynAccelerationStructure,+ copy: wgt::AccelerationStructureCopy,+ );+ unsafe fn read_acceleration_structure_compact_size(+ &mut self,+ acceleration_structure: &dyn DynAccelerationStructure,+ buf: &dyn DynBuffer,+ );+ unsafe fn set_acceleration_structure_dependencies(+ &self,+ command_buffers: &[Box<dyn DynCommandBuffer>],+ dependencies: &[&dyn DynAccelerationStructure],+ );+}++impl<C: CommandEncoder + DynResource> DynCommandEncoder for C {+ unsafe fn begin_encoding(&mut self, label: Label) -> Result<(), DeviceError> {+ unsafe { C::begin_encoding(self, label) }+ }++ unsafe fn discard_encoding(&mut self) {+ unsafe { C::discard_encoding(self) }+ }++ unsafe fn end_encoding(&mut self) -> Result<Box<dyn DynCommandBuffer>, DeviceError> {+ unsafe { C::end_encoding(self) }.map(|cb| {+ let boxed_command_buffer: Box<<C::A as Api>::CommandBuffer> = Box::new(cb);+ let boxed_command_buffer: Box<dyn DynCommandBuffer> = boxed_command_buffer;+ boxed_command_buffer+ })+ }++ unsafe fn reset_all(&mut self, command_buffers: Vec<Box<dyn DynCommandBuffer>>) {+ unsafe { C::reset_all(self, command_buffers.into_iter().map(|cb| cb.unbox())) }+ }++ unsafe fn transition_buffers(&mut self, barriers: &[BufferBarrier<'_, dyn DynBuffer>]) {+ let barriers = barriers.iter().map(|barrier| BufferBarrier {+ buffer: barrier.buffer.expect_downcast_ref(),+ usage: barrier.usage.clone(),+ });+ unsafe { self.transition_buffers(barriers) };+ }++ unsafe fn transition_textures(&mut self, barriers: &[TextureBarrier<'_, dyn DynTexture>]) {+ let barriers = barriers.iter().map(|barrier| TextureBarrier {+ texture: barrier.texture.expect_downcast_ref(),+ usage: barrier.usage.clone(),+ range: barrier.range,+ });+ unsafe { self.transition_textures(barriers) };+ }++ unsafe fn clear_buffer(&mut self, buffer: &dyn DynBuffer, range: MemoryRange) {+ let buffer = buffer.expect_downcast_ref();+ unsafe { C::clear_buffer(self, buffer, range) };+ }++ unsafe fn copy_buffer_to_buffer(+ &mut self,+ src: &dyn DynBuffer,+ dst: &dyn DynBuffer,+ regions: &[BufferCopy],+ ) {+ let src = src.expect_downcast_ref();+ let dst = dst.expect_downcast_ref();+ unsafe {+ C::copy_buffer_to_buffer(self, src, dst, regions.iter().copied());+ }+ }++ unsafe fn copy_texture_to_texture(+ &mut self,+ src: &dyn DynTexture,+ src_usage: wgt::TextureUses,+ dst: &dyn DynTexture,+ regions: &[TextureCopy],+ ) {+ let src = src.expect_downcast_ref();+ let dst = dst.expect_downcast_ref();+ unsafe {+ C::copy_texture_to_texture(self, src, src_usage, dst, regions.iter().cloned());+ }+ }++ unsafe fn copy_buffer_to_texture(+ &mut self,+ src: &dyn DynBuffer,+ dst: &dyn DynTexture,+ regions: &[BufferTextureCopy],+ ) {+ let src = src.expect_downcast_ref();+ let dst = dst.expect_downcast_ref();+ unsafe {+ C::copy_buffer_to_texture(self, src, dst, regions.iter().cloned());+ }+ }++ unsafe fn copy_texture_to_buffer(+ &mut self,+ src: &dyn DynTexture,+ src_usage: wgt::TextureUses,+ dst: &dyn DynBuffer,+ regions: &[BufferTextureCopy],+ ) {+ let src = src.expect_downcast_ref();+ let dst = dst.expect_downcast_ref();+ unsafe {+ C::copy_texture_to_buffer(self, src, src_usage, dst, regions.iter().cloned());+ }+ }++ unsafe fn set_bind_group(+ &mut self,+ layout: &dyn DynPipelineLayout,+ index: u32,+ group: &dyn DynBindGroup,+ dynamic_offsets: &[wgt::DynamicOffset],+ ) {+ let layout = layout.expect_downcast_ref();+ let group = group.expect_downcast_ref();+ unsafe { C::set_bind_group(self, layout, index, group, dynamic_offsets) };+ }++ unsafe fn set_immediates(+ &mut self,+ layout: &dyn DynPipelineLayout,+ offset_bytes: u32,+ data: &[u32],+ ) {+ let layout = layout.expect_downcast_ref();+ unsafe { C::set_immediates(self, layout, offset_bytes, data) };+ }++ unsafe fn insert_debug_marker(&mut self, label: &str) {+ unsafe {+ C::insert_debug_marker(self, label);+ }+ }++ unsafe fn begin_debug_marker(&mut self, group_label: &str) {+ unsafe {+ C::begin_debug_marker(self, group_label);+ }+ }++ unsafe fn end_debug_marker(&mut self) {+ unsafe {+ C::end_debug_marker(self);+ }+ }++ unsafe fn begin_query(&mut self, set: &dyn DynQuerySet, index: u32) {+ let set = set.expect_downcast_ref();+ unsafe { C::begin_query(self, set, index) };+ }++ unsafe fn end_query(&mut self, set: &dyn DynQuerySet, index: u32) {+ let set = set.expect_downcast_ref();+ unsafe { C::end_query(self, set, index) };+ }++ unsafe fn write_timestamp(&mut self, set: &dyn DynQuerySet, index: u32) {+ let set = set.expect_downcast_ref();+ unsafe { C::write_timestamp(self, set, index) };+ }++ unsafe fn reset_queries(&mut self, set: &dyn DynQuerySet, range: Range<u32>) {+ let set = set.expect_downcast_ref();+ unsafe { C::reset_queries(self, set, range) };+ }++ unsafe fn copy_query_results(+ &mut self,+ set: &dyn DynQuerySet,+ range: Range<u32>,+ buffer: &dyn DynBuffer,+ offset: wgt::BufferAddress,+ stride: wgt::BufferSize,+ ) {+ let set = set.expect_downcast_ref();+ let buffer = buffer.expect_downcast_ref();+ unsafe { C::copy_query_results(self, set, range, buffer, offset, stride) };+ }++ unsafe fn begin_render_pass(+ &mut self,+ desc: &RenderPassDescriptor<dyn DynQuerySet, dyn DynTextureView>,+ ) -> Result<(), DeviceError> {+ let color_attachments = desc+ .color_attachments+ .iter()+ .map(|attachment| {+ attachment+ .as_ref()+ .map(|attachment| attachment.expect_downcast())+ })+ .collect::<Vec<_>>();++ let desc: RenderPassDescriptor<<C::A as Api>::QuerySet, <C::A as Api>::TextureView> =+ RenderPassDescriptor {+ label: desc.label,+ extent: desc.extent,+ sample_count: desc.sample_count,+ color_attachments: &color_attachments,+ depth_stencil_attachment: desc+ .depth_stencil_attachment+ .as_ref()+ .map(|ds| ds.expect_downcast()),+ multiview_mask: desc.multiview_mask,+ timestamp_writes: desc+ .timestamp_writes+ .as_ref()+ .map(|writes| writes.expect_downcast()),+ occlusion_query_set: desc+ .occlusion_query_set+ .map(|set| set.expect_downcast_ref()),+ };+ unsafe { C::begin_render_pass(self, &desc) }+ }++ unsafe fn end_render_pass(&mut self) {+ unsafe {+ C::end_render_pass(self);+ }+ }++ unsafe fn set_viewport(&mut self, rect: &Rect<f32>, depth_range: Range<f32>) {+ unsafe {+ C::set_viewport(self, rect, depth_range);+ }+ }++ unsafe fn set_scissor_rect(&mut self, rect: &Rect<u32>) {+ unsafe {+ C::set_scissor_rect(self, rect);+ }+ }++ unsafe fn set_stencil_reference(&mut self, value: u32) {+ unsafe {+ C::set_stencil_reference(self, value);+ }+ }++ unsafe fn set_blend_constants(&mut self, color: &[f32; 4]) {+ unsafe { C::set_blend_constants(self, color) };+ }++ unsafe fn draw(+ &mut self,+ first_vertex: u32,+ vertex_count: u32,+ first_instance: u32,+ instance_count: u32,+ ) {+ unsafe {+ C::draw(+ self,+ first_vertex,+ vertex_count,+ first_instance,+ instance_count,+ )+ };+ }++ unsafe fn draw_indexed(+ &mut self,+ first_index: u32,+ index_count: u32,+ base_vertex: i32,+ first_instance: u32,+ instance_count: u32,+ ) {+ unsafe {+ C::draw_indexed(+ self,+ first_index,+ index_count,+ base_vertex,+ first_instance,+ instance_count,+ )+ };+ }++ unsafe fn draw_mesh_tasks(+ &mut self,+ group_count_x: u32,+ group_count_y: u32,+ group_count_z: u32,+ ) {+ unsafe { C::draw_mesh_tasks(self, group_count_x, group_count_y, group_count_z) };+ }++ unsafe fn draw_indirect(+ &mut self,+ buffer: &dyn DynBuffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ ) {+ let buffer = buffer.expect_downcast_ref();+ unsafe { C::draw_indirect(self, buffer, offset, draw_count) };+ }++ unsafe fn draw_indexed_indirect(+ &mut self,+ buffer: &dyn DynBuffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ ) {+ let buffer = buffer.expect_downcast_ref();+ unsafe { C::draw_indexed_indirect(self, buffer, offset, draw_count) };+ }++ unsafe fn draw_mesh_tasks_indirect(+ &mut self,+ buffer: &dyn DynBuffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ ) {+ let buffer = buffer.expect_downcast_ref();+ unsafe { C::draw_mesh_tasks_indirect(self, buffer, offset, draw_count) };+ }++ unsafe fn draw_indirect_count(+ &mut self,+ buffer: &dyn DynBuffer,+ offset: wgt::BufferAddress,+ count_buffer: &dyn DynBuffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ ) {+ let buffer = buffer.expect_downcast_ref();+ let count_buffer = count_buffer.expect_downcast_ref();+ unsafe {+ C::draw_indirect_count(self, buffer, offset, count_buffer, count_offset, max_count)+ };+ }++ unsafe fn draw_indexed_indirect_count(+ &mut self,+ buffer: &dyn DynBuffer,+ offset: wgt::BufferAddress,+ count_buffer: &dyn DynBuffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ ) {+ let buffer = buffer.expect_downcast_ref();+ let count_buffer = count_buffer.expect_downcast_ref();+ unsafe {+ C::draw_indexed_indirect_count(+ self,+ buffer,+ offset,+ count_buffer,+ count_offset,+ max_count,+ )+ };+ }++ unsafe fn draw_mesh_tasks_indirect_count(+ &mut self,+ buffer: &dyn DynBuffer,+ offset: wgt::BufferAddress,+ count_buffer: &dyn DynBuffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ ) {+ let buffer = buffer.expect_downcast_ref();+ let count_buffer = count_buffer.expect_downcast_ref();+ unsafe {+ C::draw_mesh_tasks_indirect_count(+ self,+ buffer,+ offset,+ count_buffer,+ count_offset,+ max_count,+ )+ };+ }++ unsafe fn begin_compute_pass(&mut self, desc: &ComputePassDescriptor<dyn DynQuerySet>) {+ let desc = ComputePassDescriptor {+ label: desc.label,+ timestamp_writes: desc+ .timestamp_writes+ .as_ref()+ .map(|writes| writes.expect_downcast()),+ };+ unsafe { C::begin_compute_pass(self, &desc) };+ }++ unsafe fn end_compute_pass(&mut self) {+ unsafe { C::end_compute_pass(self) };+ }++ unsafe fn set_compute_pipeline(&mut self, pipeline: &dyn DynComputePipeline) {+ let pipeline = pipeline.expect_downcast_ref();+ unsafe { C::set_compute_pipeline(self, pipeline) };+ }++ unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]) {+ unsafe { C::dispatch_workgroups(self, count) };+ }++ unsafe fn dispatch_workgroups_indirect(+ &mut self,+ buffer: &dyn DynBuffer,+ offset: wgt::BufferAddress,+ ) {+ let buffer = buffer.expect_downcast_ref();+ unsafe { C::dispatch_workgroups_indirect(self, buffer, offset) };+ }++ unsafe fn set_render_pipeline(&mut self, pipeline: &dyn DynRenderPipeline) {+ let pipeline = pipeline.expect_downcast_ref();+ unsafe { C::set_render_pipeline(self, pipeline) };+ }++ unsafe fn set_index_buffer<'a>(+ &mut self,+ binding: BufferBinding<'a, dyn DynBuffer>,+ format: wgt::IndexFormat,+ ) {+ let binding = binding.expect_downcast();+ unsafe { self.set_index_buffer(binding, format) };+ }++ unsafe fn set_vertex_buffer<'a>(+ &mut self,+ index: u32,+ binding: BufferBinding<'a, dyn DynBuffer>,+ ) {+ let binding = binding.expect_downcast();+ unsafe { self.set_vertex_buffer(index, binding) };+ }++ unsafe fn begin_ray_tracing_pass(&mut self, desc: &RayTracingPassDescriptor) {+ let desc = RayTracingPassDescriptor { label: desc.label };+ unsafe { C::begin_ray_tracing_pass(self, &desc) };+ }++ unsafe fn end_ray_tracing_pass(&mut self) {+ unsafe { C::end_ray_tracing_pass(self) };+ }++ unsafe fn set_ray_tracing_pipeline(&mut self, pipeline: &dyn DynRayTracingPipeline) {+ let pipeline = pipeline.expect_downcast_ref();+ unsafe { C::set_ray_tracing_pipeline(self, pipeline) };+ }++ unsafe fn trace_rays<'a>(+ &mut self,+ count: [u32; 3],+ ray_generation_group_data: crate::PipelineGroupData<'a, dyn DynBuffer>,+ miss_group_data: crate::PipelineGroupData<'a, dyn DynBuffer>,+ intersection_group_data: crate::PipelineGroupData<'a, dyn DynBuffer>,+ ) {+ let downcast_group_data =+ |data: crate::PipelineGroupData<'a, dyn DynBuffer>| crate::PipelineGroupData {+ buffer: data.buffer.expect_downcast_ref(),+ offset: data.offset,+ stride: data.stride,+ count: data.count,+ };++ unsafe {+ C::trace_rays(+ self,+ count,+ downcast_group_data(ray_generation_group_data),+ downcast_group_data(miss_group_data),+ downcast_group_data(intersection_group_data),+ );+ }+ }++ unsafe fn build_acceleration_structures<'a>(+ &mut self,+ descriptors: &'a [BuildAccelerationStructureDescriptor<+ 'a,+ dyn DynBuffer,+ dyn DynAccelerationStructure,+ >],+ ) {+ // Need to collect entries here so we can reference them in the descriptor.+ // TODO: API should be redesigned to avoid this and other descriptor copies that happen due to the dyn api.+ let descriptor_entries = descriptors+ .iter()+ .map(|d| d.entries.expect_downcast())+ .collect::<Vec<_>>();+ let descriptors = descriptors+ .iter()+ .zip(descriptor_entries.iter())+ .map(|(d, entries)| BuildAccelerationStructureDescriptor::<+ <C::A as Api>::Buffer,+ <C::A as Api>::AccelerationStructure,+ > {+ entries,+ mode: d.mode,+ flags: d.flags,+ source_acceleration_structure: d+ .source_acceleration_structure+ .map(|a| a.expect_downcast_ref()),+ destination_acceleration_structure: d+ .destination_acceleration_structure+ .expect_downcast_ref(),+ scratch_buffer: d.scratch_buffer.expect_downcast_ref(),+ scratch_buffer_offset: d.scratch_buffer_offset,+ });+ unsafe { C::build_acceleration_structures(self, descriptors.len() as _, descriptors) };+ }++ unsafe fn place_acceleration_structure_barrier(+ &mut self,+ barrier: AccelerationStructureBarrier,+ ) {+ unsafe { C::place_acceleration_structure_barrier(self, barrier) };+ }++ unsafe fn copy_acceleration_structure_to_acceleration_structure(+ &mut self,+ src: &dyn DynAccelerationStructure,+ dst: &dyn DynAccelerationStructure,+ copy: wgt::AccelerationStructureCopy,+ ) {+ let src = src.expect_downcast_ref();+ let dst = dst.expect_downcast_ref();+ unsafe { C::copy_acceleration_structure_to_acceleration_structure(self, src, dst, copy) };+ }+ unsafe fn read_acceleration_structure_compact_size(+ &mut self,+ acceleration_structure: &dyn DynAccelerationStructure,+ buf: &dyn DynBuffer,+ ) {+ let acceleration_structure = acceleration_structure.expect_downcast_ref();+ let buf = buf.expect_downcast_ref();+ unsafe { C::read_acceleration_structure_compact_size(self, acceleration_structure, buf) }+ }++ unsafe fn set_acceleration_structure_dependencies(+ &self,+ command_buffers: &[Box<dyn DynCommandBuffer>],+ dependencies: &[&dyn DynAccelerationStructure],+ ) {+ let command_buffers: Vec<&<C::A as Api>::CommandBuffer> = command_buffers+ .iter()+ .map(|command_buffer| command_buffer.expect_downcast_ref())+ .collect();+ let dependencies: Vec<&<C::A as Api>::AccelerationStructure> = dependencies+ .iter()+ .map(|dependency| dependency.expect_downcast_ref())+ .collect();+ unsafe { C::set_acceleration_structure_dependencies(&command_buffers, &dependencies) }+ }+}++impl<'a> PassTimestampWrites<'a, dyn DynQuerySet> {+ pub fn expect_downcast<B: DynQuerySet>(&self) -> PassTimestampWrites<'a, B> {+ PassTimestampWrites {+ query_set: self.query_set.expect_downcast_ref(),+ beginning_of_pass_write_index: self.beginning_of_pass_write_index,+ end_of_pass_write_index: self.end_of_pass_write_index,+ }+ }+}++impl<'a> Attachment<'a, dyn DynTextureView> {+ pub fn expect_downcast<B: DynTextureView>(&self) -> Attachment<'a, B> {+ Attachment {+ view: self.view.expect_downcast_ref(),+ usage: self.usage,+ }+ }+}++impl<'a> ColorAttachment<'a, dyn DynTextureView> {+ pub fn expect_downcast<B: DynTextureView>(&self) -> ColorAttachment<'a, B> {+ ColorAttachment {+ target: self.target.expect_downcast(),+ depth_slice: self.depth_slice,+ resolve_target: self.resolve_target.as_ref().map(|rt| rt.expect_downcast()),+ ops: self.ops,+ clear_value: self.clear_value,+ }+ }+}++impl<'a> DepthStencilAttachment<'a, dyn DynTextureView> {+ pub fn expect_downcast<B: DynTextureView>(&self) -> DepthStencilAttachment<'a, B> {+ DepthStencilAttachment {+ target: self.target.expect_downcast(),+ depth_ops: self.depth_ops,+ stencil_ops: self.stencil_ops,+ clear_value: self.clear_value,+ }+ }+}diff --git a/vendor/wgpu-hal/src/dynamic/device.rs b/vendor/wgpu-hal/src/dynamic/device.rsnew file mode 100644index 00000000..f9b9fe68--- /dev/null+++ b/vendor/wgpu-hal/src/dynamic/device.rs@@ -0,0 +1,622 @@+use alloc::{borrow::ToOwned as _, boxed::Box, vec::Vec};++use crate::{+ AccelerationStructureBuildSizes, AccelerationStructureDescriptor, Api, BindGroupDescriptor,+ BindGroupLayoutDescriptor, BufferDescriptor, BufferMapping, CommandEncoderDescriptor,+ ComputePipelineDescriptor, Device, DeviceError, FenceValue,+ GetAccelerationStructureBuildSizesDescriptor, Label, MemoryRange, PipelineCacheDescriptor,+ PipelineCacheError, PipelineError, PipelineLayoutDescriptor, RayObjectIntersectionState,+ RayTracingPipelineDescriptor, RenderPipelineDescriptor, SamplerDescriptor, ShaderError,+ ShaderInput, ShaderModuleDescriptor, TextureDescriptor, TextureViewDescriptor, TlasInstance,+};++use super::{+ DynAccelerationStructure, DynBindGroup, DynBindGroupLayout, DynBuffer, DynCommandEncoder,+ DynComputePipeline, DynFence, DynPipelineCache, DynPipelineLayout, DynQuerySet, DynQueue,+ DynRayTracingPipeline, DynRenderPipeline, DynResource, DynResourceExt as _, DynSampler,+ DynShaderModule, DynTexture, DynTextureView,+};++pub trait DynDevice: DynResource {+ unsafe fn create_buffer(+ &self,+ desc: &BufferDescriptor,+ ) -> Result<Box<dyn DynBuffer>, DeviceError>;++ unsafe fn destroy_buffer(&self, buffer: Box<dyn DynBuffer>);+ unsafe fn add_raw_buffer(&self, buffer: &dyn DynBuffer);++ unsafe fn map_buffer(+ &self,+ buffer: &dyn DynBuffer,+ range: MemoryRange,+ ) -> Result<BufferMapping, DeviceError>;++ unsafe fn unmap_buffer(&self, buffer: &dyn DynBuffer);++ unsafe fn flush_mapped_ranges(&self, buffer: &dyn DynBuffer, ranges: &[MemoryRange]);+ unsafe fn invalidate_mapped_ranges(&self, buffer: &dyn DynBuffer, ranges: &[MemoryRange]);++ unsafe fn create_texture(+ &self,+ desc: &TextureDescriptor,+ ) -> Result<Box<dyn DynTexture>, DeviceError>;+ unsafe fn destroy_texture(&self, texture: Box<dyn DynTexture>);+ unsafe fn add_raw_texture(&self, texture: &dyn DynTexture);++ unsafe fn create_texture_view(+ &self,+ texture: &dyn DynTexture,+ desc: &TextureViewDescriptor,+ ) -> Result<Box<dyn DynTextureView>, DeviceError>;+ unsafe fn destroy_texture_view(&self, view: Box<dyn DynTextureView>);+ unsafe fn create_sampler(+ &self,+ desc: &SamplerDescriptor,+ ) -> Result<Box<dyn DynSampler>, DeviceError>;+ unsafe fn destroy_sampler(&self, sampler: Box<dyn DynSampler>);++ unsafe fn create_command_encoder(+ &self,+ desc: &CommandEncoderDescriptor<dyn DynQueue>,+ ) -> Result<Box<dyn DynCommandEncoder>, DeviceError>;++ unsafe fn create_bind_group_layout(+ &self,+ desc: &BindGroupLayoutDescriptor,+ ) -> Result<Box<dyn DynBindGroupLayout>, DeviceError>;+ unsafe fn destroy_bind_group_layout(&self, bg_layout: Box<dyn DynBindGroupLayout>);++ unsafe fn create_pipeline_layout(+ &self,+ desc: &PipelineLayoutDescriptor<dyn DynBindGroupLayout>,+ ) -> Result<Box<dyn DynPipelineLayout>, DeviceError>;+ unsafe fn destroy_pipeline_layout(&self, pipeline_layout: Box<dyn DynPipelineLayout>);++ unsafe fn create_bind_group(+ &self,+ desc: &BindGroupDescriptor<+ dyn DynBindGroupLayout,+ dyn DynBuffer,+ dyn DynSampler,+ dyn DynTextureView,+ dyn DynAccelerationStructure,+ >,+ ) -> Result<Box<dyn DynBindGroup>, DeviceError>;+ unsafe fn destroy_bind_group(&self, group: Box<dyn DynBindGroup>);++ unsafe fn create_shader_module(+ &self,+ desc: &ShaderModuleDescriptor,+ shader: ShaderInput,+ ) -> Result<Box<dyn DynShaderModule>, ShaderError>;+ unsafe fn destroy_shader_module(&self, module: Box<dyn DynShaderModule>);++ unsafe fn create_render_pipeline(+ &self,+ desc: &RenderPipelineDescriptor<+ dyn DynPipelineLayout,+ dyn DynShaderModule,+ dyn DynPipelineCache,+ >,+ ) -> Result<Box<dyn DynRenderPipeline>, PipelineError>;+ unsafe fn destroy_render_pipeline(&self, pipeline: Box<dyn DynRenderPipeline>);++ unsafe fn create_compute_pipeline(+ &self,+ desc: &ComputePipelineDescriptor<+ dyn DynPipelineLayout,+ dyn DynShaderModule,+ dyn DynPipelineCache,+ >,+ ) -> Result<Box<dyn DynComputePipeline>, PipelineError>;+ unsafe fn destroy_compute_pipeline(&self, pipeline: Box<dyn DynComputePipeline>);++ unsafe fn create_ray_tracing_pipeline(+ &self,+ desc: &RayTracingPipelineDescriptor<+ dyn DynPipelineLayout,+ dyn DynShaderModule,+ dyn DynPipelineCache,+ >,+ ) -> Result<Box<dyn DynRayTracingPipeline>, PipelineError>;+ unsafe fn destroy_ray_tracing_pipeline(&self, pipeline: Box<dyn DynRayTracingPipeline>);+ unsafe fn get_raytracing_pipeline_group_data(+ &self,+ pipeline: &dyn DynRayTracingPipeline,+ groups: core::ops::Range<u32>,+ ) -> Result<Vec<u8>, DeviceError>;++ unsafe fn create_pipeline_cache(+ &self,+ desc: &PipelineCacheDescriptor<'_>,+ ) -> Result<Box<dyn DynPipelineCache>, PipelineCacheError>;+ fn pipeline_cache_validation_key(&self) -> Option<[u8; 16]> {+ None+ }+ unsafe fn destroy_pipeline_cache(&self, cache: Box<dyn DynPipelineCache>);++ unsafe fn create_query_set(+ &self,+ desc: &wgt::QuerySetDescriptor<Label>,+ ) -> Result<Box<dyn DynQuerySet>, DeviceError>;+ unsafe fn destroy_query_set(&self, set: Box<dyn DynQuerySet>);++ unsafe fn create_fence(&self) -> Result<Box<dyn DynFence>, DeviceError>;+ unsafe fn destroy_fence(&self, fence: Box<dyn DynFence>);+ unsafe fn get_fence_value(&self, fence: &dyn DynFence) -> Result<FenceValue, DeviceError>;++ unsafe fn wait(+ &self,+ fence: &dyn DynFence,+ value: FenceValue,+ timeout: Option<core::time::Duration>,+ ) -> Result<bool, DeviceError>;++ unsafe fn start_graphics_debugger_capture(&self) -> bool;+ unsafe fn stop_graphics_debugger_capture(&self);++ unsafe fn pipeline_cache_get_data(&self, cache: &dyn DynPipelineCache) -> Option<Vec<u8>>;++ unsafe fn create_acceleration_structure(+ &self,+ desc: &AccelerationStructureDescriptor,+ ) -> Result<Box<dyn DynAccelerationStructure>, DeviceError>;+ unsafe fn get_acceleration_structure_build_sizes(+ &self,+ desc: &GetAccelerationStructureBuildSizesDescriptor<dyn DynBuffer>,+ ) -> AccelerationStructureBuildSizes;+ unsafe fn get_acceleration_structure_device_address(+ &self,+ acceleration_structure: &dyn DynAccelerationStructure,+ ) -> wgt::BufferAddress;+ unsafe fn destroy_acceleration_structure(+ &self,+ acceleration_structure: Box<dyn DynAccelerationStructure>,+ );+ fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8>;++ fn get_internal_counters(&self) -> wgt::HalCounters;+ fn generate_allocator_report(&self) -> Option<wgt::AllocatorReport>;++ fn check_if_oom(&self) -> Result<(), DeviceError>;+}++impl<D: Device + DynResource> DynDevice for D {+ unsafe fn create_buffer(+ &self,+ desc: &BufferDescriptor,+ ) -> Result<Box<dyn DynBuffer>, DeviceError> {+ unsafe { D::create_buffer(self, desc) }.map(|b| -> Box<dyn DynBuffer> { Box::new(b) })+ }++ unsafe fn destroy_buffer(&self, buffer: Box<dyn DynBuffer>) {+ unsafe { D::destroy_buffer(self, buffer.unbox()) };+ }+ unsafe fn add_raw_buffer(&self, buffer: &dyn DynBuffer) {+ let buffer = buffer.expect_downcast_ref();+ unsafe { D::add_raw_buffer(self, buffer) };+ }++ unsafe fn map_buffer(+ &self,+ buffer: &dyn DynBuffer,+ range: MemoryRange,+ ) -> Result<BufferMapping, DeviceError> {+ let buffer = buffer.expect_downcast_ref();+ unsafe { D::map_buffer(self, buffer, range) }+ }++ unsafe fn unmap_buffer(&self, buffer: &dyn DynBuffer) {+ let buffer = buffer.expect_downcast_ref();+ unsafe { D::unmap_buffer(self, buffer) }+ }++ unsafe fn flush_mapped_ranges(&self, buffer: &dyn DynBuffer, ranges: &[MemoryRange]) {+ let buffer = buffer.expect_downcast_ref();+ unsafe { D::flush_mapped_ranges(self, buffer, ranges.iter().cloned()) }+ }++ unsafe fn invalidate_mapped_ranges(&self, buffer: &dyn DynBuffer, ranges: &[MemoryRange]) {+ let buffer = buffer.expect_downcast_ref();+ unsafe { D::invalidate_mapped_ranges(self, buffer, ranges.iter().cloned()) }+ }++ unsafe fn create_texture(+ &self,+ desc: &TextureDescriptor,+ ) -> Result<Box<dyn DynTexture>, DeviceError> {+ unsafe { D::create_texture(self, desc) }.map(|b| {+ let boxed_texture: Box<<D::A as Api>::Texture> = Box::new(b);+ let boxed_texture: Box<dyn DynTexture> = boxed_texture;+ boxed_texture+ })+ }++ unsafe fn destroy_texture(&self, texture: Box<dyn DynTexture>) {+ unsafe { D::destroy_texture(self, texture.unbox()) };+ }++ unsafe fn add_raw_texture(&self, texture: &dyn DynTexture) {+ let texture = texture.expect_downcast_ref();+ unsafe { D::add_raw_texture(self, texture) };+ }++ unsafe fn create_texture_view(+ &self,+ texture: &dyn DynTexture,+ desc: &TextureViewDescriptor,+ ) -> Result<Box<dyn DynTextureView>, DeviceError> {+ let texture = texture.expect_downcast_ref();+ unsafe { D::create_texture_view(self, texture, desc) }.map(|b| {+ let boxed_texture_view: Box<<D::A as Api>::TextureView> = Box::new(b);+ let boxed_texture_view: Box<dyn DynTextureView> = boxed_texture_view;+ boxed_texture_view+ })+ }++ unsafe fn destroy_texture_view(&self, view: Box<dyn DynTextureView>) {+ unsafe { D::destroy_texture_view(self, view.unbox()) };+ }++ unsafe fn create_sampler(+ &self,+ desc: &SamplerDescriptor,+ ) -> Result<Box<dyn DynSampler>, DeviceError> {+ unsafe { D::create_sampler(self, desc) }.map(|b| {+ let boxed_sampler: Box<<D::A as Api>::Sampler> = Box::new(b);+ let boxed_sampler: Box<dyn DynSampler> = boxed_sampler;+ boxed_sampler+ })+ }++ unsafe fn destroy_sampler(&self, sampler: Box<dyn DynSampler>) {+ unsafe { D::destroy_sampler(self, sampler.unbox()) };+ }++ unsafe fn create_command_encoder(+ &self,+ desc: &CommandEncoderDescriptor<'_, dyn DynQueue>,+ ) -> Result<Box<dyn DynCommandEncoder>, DeviceError> {+ let desc = CommandEncoderDescriptor {+ label: desc.label,+ queue: desc.queue.expect_downcast_ref(),+ };+ unsafe { D::create_command_encoder(self, &desc) }+ .map(|b| -> Box<dyn DynCommandEncoder> { Box::new(b) })+ }++ unsafe fn create_bind_group_layout(+ &self,+ desc: &BindGroupLayoutDescriptor,+ ) -> Result<Box<dyn DynBindGroupLayout>, DeviceError> {+ unsafe { D::create_bind_group_layout(self, desc) }+ .map(|b| -> Box<dyn DynBindGroupLayout> { Box::new(b) })+ }++ unsafe fn destroy_bind_group_layout(&self, bg_layout: Box<dyn DynBindGroupLayout>) {+ unsafe { D::destroy_bind_group_layout(self, bg_layout.unbox()) };+ }++ unsafe fn create_pipeline_layout(+ &self,+ desc: &PipelineLayoutDescriptor<dyn DynBindGroupLayout>,+ ) -> Result<Box<dyn DynPipelineLayout>, DeviceError> {+ let bind_group_layouts: Vec<_> = desc+ .bind_group_layouts+ .iter()+ .map(|bgl| bgl.map(|bgl| bgl.expect_downcast_ref()))+ .collect();+ let desc = PipelineLayoutDescriptor {+ label: desc.label,+ bind_group_layouts: &bind_group_layouts,+ immediate_size: desc.immediate_size,+ flags: desc.flags,+ };++ unsafe { D::create_pipeline_layout(self, &desc) }+ .map(|b| -> Box<dyn DynPipelineLayout> { Box::new(b) })+ }++ unsafe fn destroy_pipeline_layout(&self, pipeline_layout: Box<dyn DynPipelineLayout>) {+ unsafe { D::destroy_pipeline_layout(self, pipeline_layout.unbox()) };+ }++ unsafe fn create_bind_group(+ &self,+ desc: &BindGroupDescriptor<+ dyn DynBindGroupLayout,+ dyn DynBuffer,+ dyn DynSampler,+ dyn DynTextureView,+ dyn DynAccelerationStructure,+ >,+ ) -> Result<Box<dyn DynBindGroup>, DeviceError> {+ let buffers: Vec<_> = desc+ .buffers+ .iter()+ .map(|b| b.clone().expect_downcast())+ .collect();+ let samplers: Vec<_> = desc+ .samplers+ .iter()+ .map(|s| s.expect_downcast_ref())+ .collect();+ let textures: Vec<_> = desc+ .textures+ .iter()+ .map(|t| t.clone().expect_downcast())+ .collect();+ let acceleration_structures: Vec<_> = desc+ .acceleration_structures+ .iter()+ .map(|a| a.expect_downcast_ref())+ .collect();+ let external_textures: Vec<_> = desc+ .external_textures+ .iter()+ .map(|et| et.clone().expect_downcast())+ .collect();++ let desc = BindGroupDescriptor {+ label: desc.label.to_owned(),+ layout: desc.layout.expect_downcast_ref(),+ buffers: &buffers,+ samplers: &samplers,+ textures: &textures,+ entries: desc.entries,+ acceleration_structures: &acceleration_structures,+ external_textures: &external_textures,+ };++ unsafe { D::create_bind_group(self, &desc) }+ .map(|b| -> Box<dyn DynBindGroup> { Box::new(b) })+ }++ unsafe fn destroy_bind_group(&self, group: Box<dyn DynBindGroup>) {+ unsafe { D::destroy_bind_group(self, group.unbox()) };+ }++ unsafe fn create_shader_module(+ &self,+ desc: &ShaderModuleDescriptor,+ shader: ShaderInput,+ ) -> Result<Box<dyn DynShaderModule>, ShaderError> {+ unsafe { D::create_shader_module(self, desc, shader) }+ .map(|b| -> Box<dyn DynShaderModule> { Box::new(b) })+ }++ unsafe fn destroy_shader_module(&self, module: Box<dyn DynShaderModule>) {+ unsafe { D::destroy_shader_module(self, module.unbox()) };+ }++ unsafe fn create_render_pipeline(+ &self,+ desc: &RenderPipelineDescriptor<+ dyn DynPipelineLayout,+ dyn DynShaderModule,+ dyn DynPipelineCache,+ >,+ ) -> Result<Box<dyn DynRenderPipeline>, PipelineError> {+ let desc = RenderPipelineDescriptor {+ label: desc.label,+ layout: desc.layout.expect_downcast_ref(),+ vertex_processor: match &desc.vertex_processor {+ crate::VertexProcessor::Standard {+ vertex_buffers,+ vertex_stage,+ } => crate::VertexProcessor::Standard {+ vertex_buffers,+ vertex_stage: vertex_stage.clone().expect_downcast(),+ },+ crate::VertexProcessor::Mesh {+ task_stage: task,+ mesh_stage: mesh,+ } => crate::VertexProcessor::Mesh {+ task_stage: task.as_ref().map(|a| a.clone().expect_downcast()),+ mesh_stage: mesh.clone().expect_downcast(),+ },+ },+ primitive: desc.primitive,+ depth_stencil: desc.depth_stencil.clone(),+ multisample: desc.multisample,+ fragment_stage: desc.fragment_stage.clone().map(|f| f.expect_downcast()),+ color_targets: desc.color_targets,+ multiview_mask: desc.multiview_mask,+ cache: desc.cache.map(|c| c.expect_downcast_ref()),+ };++ unsafe { D::create_render_pipeline(self, &desc) }+ .map(|b| -> Box<dyn DynRenderPipeline> { Box::new(b) })+ }++ unsafe fn destroy_render_pipeline(&self, pipeline: Box<dyn DynRenderPipeline>) {+ unsafe { D::destroy_render_pipeline(self, pipeline.unbox()) };+ }++ unsafe fn create_compute_pipeline(+ &self,+ desc: &ComputePipelineDescriptor<+ dyn DynPipelineLayout,+ dyn DynShaderModule,+ dyn DynPipelineCache,+ >,+ ) -> Result<Box<dyn DynComputePipeline>, PipelineError> {+ let desc = ComputePipelineDescriptor {+ label: desc.label,+ layout: desc.layout.expect_downcast_ref(),+ stage: desc.stage.clone().expect_downcast(),+ cache: desc.cache.as_ref().map(|c| c.expect_downcast_ref()),+ };++ unsafe { D::create_compute_pipeline(self, &desc) }+ .map(|b| -> Box<dyn DynComputePipeline> { Box::new(b) })+ }++ unsafe fn destroy_compute_pipeline(&self, pipeline: Box<dyn DynComputePipeline>) {+ unsafe { D::destroy_compute_pipeline(self, pipeline.unbox()) };+ }++ unsafe fn create_ray_tracing_pipeline(+ &self,+ desc: &RayTracingPipelineDescriptor<+ dyn DynPipelineLayout,+ dyn DynShaderModule,+ dyn DynPipelineCache,+ >,+ ) -> Result<Box<dyn DynRayTracingPipeline>, PipelineError> {+ let ray_intersection: Vec<_> = desc+ .intersection+ .iter()+ .map(|stage| RayObjectIntersectionState {+ closest_hit: stage.closest_hit.clone().expect_downcast(),+ any_hit: stage+ .any_hit+ .as_ref()+ .map(|stage| stage.clone().expect_downcast()),+ })+ .collect();++ let desc = RayTracingPipelineDescriptor {+ label: desc.label,+ layout: desc.layout.expect_downcast_ref(),+ ray_generation: desc.ray_generation.clone().expect_downcast(),+ miss: desc.miss.clone().expect_downcast(),+ intersection: &ray_intersection,+ max_recursion_depth: desc.max_recursion_depth,+ cache: desc.cache.as_ref().map(|c| c.expect_downcast_ref()),+ };++ unsafe { D::create_ray_tracing_pipeline(self, &desc) }+ .map(|b| -> Box<dyn DynRayTracingPipeline> { Box::new(b) })+ }++ unsafe fn destroy_ray_tracing_pipeline(&self, pipeline: Box<dyn DynRayTracingPipeline>) {+ unsafe {+ D::destroy_ray_tracing_pipeline(self, pipeline.unbox());+ };+ }+ unsafe fn get_raytracing_pipeline_group_data(+ &self,+ pipeline: &dyn DynRayTracingPipeline,+ groups: core::ops::Range<u32>,+ ) -> Result<Vec<u8>, DeviceError> {+ unsafe {+ D::get_raytracing_pipeline_group_data(self, pipeline.expect_downcast_ref(), groups)+ }+ }++ unsafe fn create_pipeline_cache(+ &self,+ desc: &PipelineCacheDescriptor<'_>,+ ) -> Result<Box<dyn DynPipelineCache>, PipelineCacheError> {+ unsafe { D::create_pipeline_cache(self, desc) }+ .map(|b| -> Box<dyn DynPipelineCache> { Box::new(b) })+ }++ fn pipeline_cache_validation_key(&self) -> Option<[u8; 16]> {+ D::pipeline_cache_validation_key(self)+ }++ unsafe fn destroy_pipeline_cache(&self, pipeline_cache: Box<dyn DynPipelineCache>) {+ unsafe { D::destroy_pipeline_cache(self, pipeline_cache.unbox()) };+ }++ unsafe fn create_query_set(+ &self,+ desc: &wgt::QuerySetDescriptor<Label>,+ ) -> Result<Box<dyn DynQuerySet>, DeviceError> {+ unsafe { D::create_query_set(self, desc) }.map(|b| -> Box<dyn DynQuerySet> { Box::new(b) })+ }++ unsafe fn destroy_query_set(&self, query_set: Box<dyn DynQuerySet>) {+ unsafe { D::destroy_query_set(self, query_set.unbox()) };+ }++ unsafe fn create_fence(&self) -> Result<Box<dyn DynFence>, DeviceError> {+ unsafe { D::create_fence(self) }.map(|b| -> Box<dyn DynFence> { Box::new(b) })+ }++ unsafe fn destroy_fence(&self, fence: Box<dyn DynFence>) {+ unsafe { D::destroy_fence(self, fence.unbox()) };+ }++ unsafe fn get_fence_value(&self, fence: &dyn DynFence) -> Result<FenceValue, DeviceError> {+ let fence = fence.expect_downcast_ref();+ unsafe { D::get_fence_value(self, fence) }+ }++ unsafe fn wait(+ &self,+ fence: &dyn DynFence,+ value: FenceValue,+ timeout: Option<core::time::Duration>,+ ) -> Result<bool, DeviceError> {+ let fence = fence.expect_downcast_ref();+ unsafe { D::wait(self, fence, value, timeout) }+ }++ unsafe fn start_graphics_debugger_capture(&self) -> bool {+ unsafe { D::start_graphics_debugger_capture(self) }+ }++ unsafe fn stop_graphics_debugger_capture(&self) {+ unsafe { D::stop_graphics_debugger_capture(self) }+ }++ unsafe fn pipeline_cache_get_data(&self, cache: &dyn DynPipelineCache) -> Option<Vec<u8>> {+ let cache = cache.expect_downcast_ref();+ unsafe { D::pipeline_cache_get_data(self, cache) }+ }++ unsafe fn create_acceleration_structure(+ &self,+ desc: &AccelerationStructureDescriptor,+ ) -> Result<Box<dyn DynAccelerationStructure>, DeviceError> {+ unsafe { D::create_acceleration_structure(self, desc) }+ .map(|b| -> Box<dyn DynAccelerationStructure> { Box::new(b) })+ }++ unsafe fn get_acceleration_structure_build_sizes(+ &self,+ desc: &GetAccelerationStructureBuildSizesDescriptor<dyn DynBuffer>,+ ) -> AccelerationStructureBuildSizes {+ let entries = desc.entries.expect_downcast();+ let desc = GetAccelerationStructureBuildSizesDescriptor {+ entries: &entries,+ flags: desc.flags,+ };+ unsafe { D::get_acceleration_structure_build_sizes(self, &desc) }+ }++ unsafe fn get_acceleration_structure_device_address(+ &self,+ acceleration_structure: &dyn DynAccelerationStructure,+ ) -> wgt::BufferAddress {+ let acceleration_structure = acceleration_structure.expect_downcast_ref();+ unsafe { D::get_acceleration_structure_device_address(self, acceleration_structure) }+ }++ unsafe fn destroy_acceleration_structure(+ &self,+ acceleration_structure: Box<dyn DynAccelerationStructure>,+ ) {+ unsafe { D::destroy_acceleration_structure(self, acceleration_structure.unbox()) }+ }++ fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8> {+ D::tlas_instance_to_bytes(self, instance)+ }++ fn get_internal_counters(&self) -> wgt::HalCounters {+ D::get_internal_counters(self)+ }++ fn generate_allocator_report(&self) -> Option<wgt::AllocatorReport> {+ D::generate_allocator_report(self)+ }++ fn check_if_oom(&self) -> Result<(), DeviceError> {+ D::check_if_oom(self)+ }+}diff --git a/vendor/wgpu-hal/src/dynamic/instance.rs b/vendor/wgpu-hal/src/dynamic/instance.rsnew file mode 100644index 00000000..cab14cdf--- /dev/null+++ b/vendor/wgpu-hal/src/dynamic/instance.rs@@ -0,0 +1,71 @@+use alloc::{boxed::Box, vec::Vec};++use crate::{Api, Capabilities, ExposedAdapter, Instance, InstanceError};++use super::{DynAdapter, DynResource, DynResourceExt as _, DynSurface};++#[expect(missing_debug_implementations, reason = "dyn")]+pub struct DynExposedAdapter {+ pub adapter: Box<dyn DynAdapter>,+ pub info: wgt::AdapterInfo,+ pub features: wgt::Features,+ pub capabilities: Capabilities,+}++impl DynExposedAdapter {+ /// Returns the backend this adapter is using.+ pub fn backend(&self) -> wgt::Backend {+ self.info.backend+ }+}++impl<A: Api> From<ExposedAdapter<A>> for DynExposedAdapter {+ fn from(exposed_adapter: ExposedAdapter<A>) -> Self {+ Self {+ adapter: Box::new(exposed_adapter.adapter),+ info: exposed_adapter.info,+ features: exposed_adapter.features,+ capabilities: exposed_adapter.capabilities,+ }+ }+}++pub trait DynInstance: DynResource {+ unsafe fn create_surface(+ &self,+ display_handle: raw_window_handle::RawDisplayHandle,+ window_handle: raw_window_handle::RawWindowHandle,+ ) -> Result<Box<dyn DynSurface>, InstanceError>;++ unsafe fn enumerate_adapters(+ &self,+ surface_hint: Option<&dyn DynSurface>,+ ) -> Vec<DynExposedAdapter>;+}++impl<I: Instance + DynResource> DynInstance for I {+ unsafe fn create_surface(+ &self,+ display_handle: raw_window_handle::RawDisplayHandle,+ window_handle: raw_window_handle::RawWindowHandle,+ ) -> Result<Box<dyn DynSurface>, InstanceError> {+ unsafe { I::create_surface(self, display_handle, window_handle) }+ .map(|surface| -> Box<dyn DynSurface> { Box::new(surface) })+ }++ unsafe fn enumerate_adapters(+ &self,+ surface_hint: Option<&dyn DynSurface>,+ ) -> Vec<DynExposedAdapter> {+ let surface_hint = surface_hint.map(|s| s.expect_downcast_ref());+ unsafe { I::enumerate_adapters(self, surface_hint) }+ .into_iter()+ .map(|exposed| DynExposedAdapter {+ adapter: Box::new(exposed.adapter),+ info: exposed.info,+ features: exposed.features,+ capabilities: exposed.capabilities,+ })+ .collect()+ }+}diff --git a/vendor/wgpu-hal/src/dynamic/mod.rs b/vendor/wgpu-hal/src/dynamic/mod.rsnew file mode 100644index 00000000..cce4d3b6--- /dev/null+++ b/vendor/wgpu-hal/src/dynamic/mod.rs@@ -0,0 +1,212 @@+mod adapter;+mod command;+mod device;+mod instance;+mod queue;+mod surface;++pub use adapter::{DynAdapter, DynOpenDevice};+pub use command::DynCommandEncoder;+pub use device::DynDevice;+pub use instance::{DynExposedAdapter, DynInstance};+pub use queue::DynQueue;+pub use surface::{DynAcquiredSurfaceTexture, DynSurface};++use alloc::boxed::Box;+use core::{+ any::{Any, TypeId},+ fmt,+};++use wgt::WasmNotSendSync;++use crate::{+ AccelerationStructureAABBs, AccelerationStructureEntries, AccelerationStructureInstances,+ AccelerationStructureTriangleIndices, AccelerationStructureTriangleTransform,+ AccelerationStructureTriangles, BufferBinding, ExternalTextureBinding, ProgrammableStage,+ TextureBinding,+};++/// Base trait for all resources, allows downcasting via [`Any`].+pub trait DynResource: Any + WasmNotSendSync + 'static {+ fn as_any(&self) -> &dyn Any;+ fn as_any_mut(&mut self) -> &mut dyn Any;+}++/// Utility macro for implementing `DynResource` for a list of types.+macro_rules! impl_dyn_resource {+ ($($type:ty),*) => {+ $(+ impl crate::DynResource for $type {+ fn as_any(&self) -> &dyn ::core::any::Any {+ self+ }++ fn as_any_mut(&mut self) -> &mut dyn ::core::any::Any {+ self+ }+ }+ )*+ };+}+pub(crate) use impl_dyn_resource;++/// Extension trait for `DynResource` used by implementations of various dynamic resource traits.+trait DynResourceExt {+ /// # Panics+ ///+ /// - Panics if `self` is not downcastable to `T`.+ fn expect_downcast_ref<T: DynResource>(&self) -> &T;++ /// Unboxes a `Box<dyn DynResource>` to a concrete type.+ ///+ /// # Safety+ ///+ /// - `self` must be the correct concrete type.+ unsafe fn unbox<T: DynResource + 'static>(self: Box<Self>) -> T;+}++impl<R: DynResource + ?Sized> DynResourceExt for R {+ fn expect_downcast_ref<'a, T: DynResource>(&'a self) -> &'a T {+ self.as_any()+ .downcast_ref()+ .expect("Resource doesn't have the expected backend type.")+ }++ unsafe fn unbox<T: DynResource + 'static>(self: Box<Self>) -> T {+ debug_assert!(+ <Self as Any>::type_id(self.as_ref()) == TypeId::of::<T>(),+ "Resource doesn't have the expected type, expected {:?}, got {:?}",+ TypeId::of::<T>(),+ <Self as Any>::type_id(self.as_ref())+ );++ let casted_ptr = Box::into_raw(self).cast::<T>();+ // SAFETY: This is adheres to the safety contract of `Box::from_raw` because:+ //+ // - We are casting the value of a previously `Box`ed value, which guarantees:+ // - `casted_ptr` is not null.+ // - `casted_ptr` is valid for reads and writes, though by itself this does not mean+ // valid reads and writes for `T` (read on for that).+ // - We don't change the allocator.+ // - The contract of `Box::from_raw` requires that an initialized and aligned `T` is stored+ // within `casted_ptr`.+ *unsafe { Box::from_raw(casted_ptr) }+ }+}++pub trait DynAccelerationStructure: DynResource + fmt::Debug {}+pub trait DynBindGroup: DynResource + fmt::Debug {}+pub trait DynBindGroupLayout: DynResource + fmt::Debug {}+pub trait DynBuffer: DynResource + fmt::Debug {}+pub trait DynCommandBuffer: DynResource + fmt::Debug {}+pub trait DynComputePipeline: DynResource + fmt::Debug {}+pub trait DynFence: DynResource + fmt::Debug {}+pub trait DynPipelineCache: DynResource + fmt::Debug {}+pub trait DynPipelineLayout: DynResource + fmt::Debug {}+pub trait DynQuerySet: DynResource + fmt::Debug {}+pub trait DynRenderPipeline: DynResource + fmt::Debug {}+pub trait DynRayTracingPipeline: DynResource + fmt::Debug {}+pub trait DynSampler: DynResource + fmt::Debug {}+pub trait DynShaderModule: DynResource + fmt::Debug {}+pub trait DynSurfaceTexture:+ DynResource + core::borrow::Borrow<dyn DynTexture> + fmt::Debug+{+}+pub trait DynTexture: DynResource + fmt::Debug {}+pub trait DynTextureView: DynResource + fmt::Debug {}++impl<'a> BufferBinding<'a, dyn DynBuffer> {+ pub fn expect_downcast<B: DynBuffer>(self) -> BufferBinding<'a, B> {+ BufferBinding {+ buffer: self.buffer.expect_downcast_ref(),+ offset: self.offset,+ size: self.size,+ }+ }+}++impl<'a> TextureBinding<'a, dyn DynTextureView> {+ pub fn expect_downcast<T: DynTextureView>(self) -> TextureBinding<'a, T> {+ TextureBinding {+ view: self.view.expect_downcast_ref(),+ usage: self.usage,+ }+ }+}++impl<'a> ExternalTextureBinding<'a, dyn DynBuffer, dyn DynTextureView> {+ pub fn expect_downcast<B: DynBuffer, T: DynTextureView>(+ self,+ ) -> ExternalTextureBinding<'a, B, T> {+ let planes = self.planes.map(|plane| plane.expect_downcast());+ let params = self.params.expect_downcast();+ ExternalTextureBinding { planes, params }+ }+}++impl<'a> ProgrammableStage<'a, dyn DynShaderModule> {+ fn expect_downcast<T: DynShaderModule>(self) -> ProgrammableStage<'a, T> {+ ProgrammableStage {+ module: self.module.expect_downcast_ref(),+ entry_point: self.entry_point,+ constants: self.constants,+ zero_initialize_workgroup_memory: self.zero_initialize_workgroup_memory,+ }+ }+}++impl<'a> AccelerationStructureEntries<'a, dyn DynBuffer> {+ fn expect_downcast<B: DynBuffer>(&self) -> AccelerationStructureEntries<'a, B> {+ match self {+ AccelerationStructureEntries::Instances(instances) => {+ AccelerationStructureEntries::Instances(AccelerationStructureInstances {+ buffer: instances.buffer.map(|b| b.expect_downcast_ref()),+ offset: instances.offset,+ count: instances.count,+ })+ }+ AccelerationStructureEntries::Triangles(triangles) => {+ AccelerationStructureEntries::Triangles(+ triangles+ .iter()+ .map(|t| AccelerationStructureTriangles {+ vertex_buffer: t.vertex_buffer.map(|b| b.expect_downcast_ref()),+ vertex_format: t.vertex_format,+ first_vertex: t.first_vertex,+ vertex_count: t.vertex_count,+ vertex_stride: t.vertex_stride,+ indices: t.indices.as_ref().map(|i| {+ AccelerationStructureTriangleIndices {+ buffer: i.buffer.map(|b| b.expect_downcast_ref()),+ format: i.format,+ offset: i.offset,+ count: i.count,+ }+ }),+ transform: t.transform.as_ref().map(|t| {+ AccelerationStructureTriangleTransform {+ buffer: t.buffer.expect_downcast_ref(),+ offset: t.offset,+ }+ }),+ flags: t.flags,+ })+ .collect(),+ )+ }+ AccelerationStructureEntries::AABBs(entries) => AccelerationStructureEntries::AABBs(+ entries+ .iter()+ .map(|e| AccelerationStructureAABBs {+ buffer: e.buffer.map(|b| b.expect_downcast_ref()),+ offset: e.offset,+ count: e.count,+ stride: e.stride,+ flags: e.flags,+ })+ .collect(),+ ),+ }+ }+}diff --git a/vendor/wgpu-hal/src/dynamic/queue.rs b/vendor/wgpu-hal/src/dynamic/queue.rsnew file mode 100644index 00000000..5425b2d1--- /dev/null+++ b/vendor/wgpu-hal/src/dynamic/queue.rs@@ -0,0 +1,61 @@+use alloc::{boxed::Box, vec::Vec};++use crate::{+ DeviceError, DynCommandBuffer, DynFence, DynResource, DynSurface, DynSurfaceTexture,+ FenceValue, Queue, SurfaceError,+};++use super::DynResourceExt as _;++pub trait DynQueue: DynResource {+ unsafe fn submit(+ &self,+ command_buffers: &[&dyn DynCommandBuffer],+ surface_textures: &[&dyn DynSurfaceTexture],+ signal_fence: (&dyn DynFence, FenceValue),+ ) -> Result<(), DeviceError>;+ unsafe fn present(+ &self,+ surface: &dyn DynSurface,+ texture: Box<dyn DynSurfaceTexture>,+ ) -> Result<(), SurfaceError>;+ unsafe fn get_timestamp_period(&self) -> f32;+ unsafe fn wait_for_idle(&self) -> Result<(), DeviceError>;+}++impl<Q: Queue + DynResource> DynQueue for Q {+ unsafe fn submit(+ &self,+ command_buffers: &[&dyn DynCommandBuffer],+ surface_textures: &[&dyn DynSurfaceTexture],+ signal_fence: (&dyn DynFence, FenceValue),+ ) -> Result<(), DeviceError> {+ let command_buffers = command_buffers+ .iter()+ .map(|cb| (*cb).expect_downcast_ref())+ .collect::<Vec<_>>();+ let surface_textures = surface_textures+ .iter()+ .map(|surface| (*surface).expect_downcast_ref())+ .collect::<Vec<_>>();+ let signal_fence = (signal_fence.0.expect_downcast_ref(), signal_fence.1);+ unsafe { Q::submit(self, &command_buffers, &surface_textures, signal_fence) }+ }++ unsafe fn present(+ &self,+ surface: &dyn DynSurface,+ texture: Box<dyn DynSurfaceTexture>,+ ) -> Result<(), SurfaceError> {+ let surface = surface.expect_downcast_ref();+ unsafe { Q::present(self, surface, texture.unbox()) }+ }++ unsafe fn get_timestamp_period(&self) -> f32 {+ unsafe { Q::get_timestamp_period(self) }+ }++ unsafe fn wait_for_idle(&self) -> Result<(), DeviceError> {+ unsafe { Q::wait_for_idle(self) }+ }+}diff --git a/vendor/wgpu-hal/src/dynamic/surface.rs b/vendor/wgpu-hal/src/dynamic/surface.rsnew file mode 100644index 00000000..17fa8ee8--- /dev/null+++ b/vendor/wgpu-hal/src/dynamic/surface.rs@@ -0,0 +1,72 @@+use alloc::boxed::Box;+use core::time::Duration;++use crate::{+ DynDevice, DynFence, DynResource, DynSurfaceTexture, Surface, SurfaceConfiguration,+ SurfaceError,+};++use super::DynResourceExt as _;++#[derive(Debug)]+pub struct DynAcquiredSurfaceTexture {+ pub texture: Box<dyn DynSurfaceTexture>,+ /// The presentation configuration no longer matches+ /// the surface properties exactly, but can still be used to present+ /// to the surface successfully.+ pub suboptimal: bool,+}++pub trait DynSurface: DynResource {+ unsafe fn configure(+ &self,+ device: &dyn DynDevice,+ config: &SurfaceConfiguration,+ ) -> Result<(), SurfaceError>;++ unsafe fn unconfigure(&self, device: &dyn DynDevice);++ unsafe fn acquire_texture(+ &self,+ timeout: Option<Duration>,+ fence: &dyn DynFence,+ ) -> Result<DynAcquiredSurfaceTexture, SurfaceError>;++ unsafe fn discard_texture(&self, texture: Box<dyn DynSurfaceTexture>);+}++impl<S: Surface + DynResource> DynSurface for S {+ unsafe fn configure(+ &self,+ device: &dyn DynDevice,+ config: &SurfaceConfiguration,+ ) -> Result<(), SurfaceError> {+ let device = device.expect_downcast_ref();+ unsafe { S::configure(self, device, config) }+ }++ unsafe fn unconfigure(&self, device: &dyn DynDevice) {+ let device = device.expect_downcast_ref();+ unsafe { S::unconfigure(self, device) }+ }++ unsafe fn acquire_texture(+ &self,+ timeout: Option<Duration>,+ fence: &dyn DynFence,+ ) -> Result<DynAcquiredSurfaceTexture, SurfaceError> {+ let fence = fence.expect_downcast_ref();+ unsafe { S::acquire_texture(self, timeout, fence) }.map(|ast| {+ let texture = Box::new(ast.texture);+ let suboptimal = ast.suboptimal;+ DynAcquiredSurfaceTexture {+ texture,+ suboptimal,+ }+ })+ }++ unsafe fn discard_texture(&self, texture: Box<dyn DynSurfaceTexture>) {+ unsafe { S::discard_texture(self, texture.unbox()) }+ }+}diff --git a/vendor/wgpu-hal/src/gles/adapter.rs b/vendor/wgpu-hal/src/gles/adapter.rsnew file mode 100644index 00000000..47b8115a--- /dev/null+++ b/vendor/wgpu-hal/src/gles/adapter.rs@@ -0,0 +1,1470 @@+use alloc::{borrow::ToOwned as _, format, string::String, sync::Arc, vec, vec::Vec};+use core::sync::atomic::AtomicU8;++use glow::HasContext;+use parking_lot::Mutex;+use wgt::AstcChannel;++use crate::auxil::db;+use crate::gles::ShaderClearProgram;++// https://webgl2fundamentals.org/webgl/lessons/webgl-data-textures.html++const GL_UNMASKED_VENDOR_WEBGL: u32 = 0x9245;+const GL_UNMASKED_RENDERER_WEBGL: u32 = 0x9246;++impl super::Adapter {+ pub fn get_glsl_version(&self) -> naga::back::glsl::Version {+ self.shared.shading_language_version+ }++ /// Note that this function is intentionally lenient in regards to parsing,+ /// and will try to recover at least the first two version numbers without+ /// resulting in an `Err`.+ /// # Notes+ /// `WebGL 2` version returned as `OpenGL ES 3.0`+ fn parse_version(mut src: &str) -> Result<(u8, u8), crate::InstanceError> {+ let webgl_sig = "WebGL ";+ // According to the WebGL specification+ // VERSION WebGL<space>1.0<space><vendor-specific information>+ // SHADING_LANGUAGE_VERSION WebGL<space>GLSL<space>ES<space>1.0<space><vendor-specific information>+ let is_webgl = src.starts_with(webgl_sig);+ if is_webgl {+ let pos = src.rfind(webgl_sig).unwrap_or(0);+ src = &src[pos + webgl_sig.len()..];+ } else {+ let es_sig = " ES ";+ match src.rfind(es_sig) {+ Some(pos) => {+ src = &src[pos + es_sig.len()..];+ }+ None => {+ return Err(crate::InstanceError::new(format!(+ "OpenGL version {src:?} does not contain 'ES'"+ )));+ }+ }+ };++ let glsl_es_sig = "GLSL ES ";+ let is_glsl = match src.find(glsl_es_sig) {+ Some(pos) => {+ src = &src[pos + glsl_es_sig.len()..];+ true+ }+ None => false,+ };++ Self::parse_full_version(src).map(|(major, minor)| {+ (+ // Return WebGL 2.0 version as OpenGL ES 3.0+ if is_webgl && !is_glsl {+ major + 1+ } else {+ major+ },+ minor,+ )+ })+ }++ /// According to the OpenGL specification, the version information is+ /// expected to follow the following syntax:+ ///+ /// ~~~bnf+ /// <major> ::= <number>+ /// <minor> ::= <number>+ /// <revision> ::= <number>+ /// <vendor-info> ::= <string>+ /// <release> ::= <major> "." <minor> ["." <release>]+ /// <version> ::= <release> [" " <vendor-info>]+ /// ~~~+ ///+ /// Note that this function is intentionally lenient in regards to parsing,+ /// and will try to recover at least the first two version numbers without+ /// resulting in an `Err`.+ pub(super) fn parse_full_version(src: &str) -> Result<(u8, u8), crate::InstanceError> {+ let (version, _vendor_info) = match src.find(' ') {+ Some(i) => (&src[..i], src[i + 1..].to_owned()),+ None => (src, String::new()),+ };++ // TODO: make this even more lenient so that we can also accept+ // `<major> "." <minor> [<???>]`+ let mut it = version.split('.');+ let major = it.next().and_then(|s| s.parse().ok());+ let minor = it.next().and_then(|s| {+ let trimmed = if s.starts_with('0') {+ "0"+ } else {+ s.trim_end_matches('0')+ };+ trimmed.parse().ok()+ });++ match (major, minor) {+ (Some(major), Some(minor)) => Ok((major, minor)),+ _ => Err(crate::InstanceError::new(format!(+ "unable to extract OpenGL version from {version:?}"+ ))),+ }+ }++ fn make_info(vendor_orig: String, renderer_orig: String, version: String) -> wgt::AdapterInfo {+ let vendor = vendor_orig.to_lowercase();+ let renderer = renderer_orig.to_lowercase();++ // opengl has no way to discern device_type, so we can try to infer it from the renderer string+ let strings_that_imply_integrated = [+ " xpress", // space here is on purpose so we don't match express+ "amd renoir",+ "radeon hd 4200",+ "radeon hd 4250",+ "radeon hd 4290",+ "radeon hd 4270",+ "radeon hd 4225",+ "radeon hd 3100",+ "radeon hd 3200",+ "radeon hd 3000",+ "radeon hd 3300",+ "radeon(tm) r4 graphics",+ "radeon(tm) r5 graphics",+ "radeon(tm) r6 graphics",+ "radeon(tm) r7 graphics",+ "radeon r7 graphics",+ "nforce", // all nvidia nforce are integrated+ "tegra", // all nvidia tegra are integrated+ "shield", // all nvidia shield are integrated+ "igp",+ "mali",+ "intel",+ "v3d",+ "apple m", // all apple m are integrated+ ];+ let strings_that_imply_cpu = ["mesa offscreen", "swiftshader", "llvmpipe"];++ //TODO: handle Intel Iris XE as discreet+ let inferred_device_type = if vendor.contains("qualcomm")+ || vendor.contains("intel")+ || strings_that_imply_integrated+ .iter()+ .any(|&s| renderer.contains(s))+ {+ wgt::DeviceType::IntegratedGpu+ } else if strings_that_imply_cpu.iter().any(|&s| renderer.contains(s)) {+ wgt::DeviceType::Cpu+ } else {+ // At this point the Device type is Unknown.+ // It's most likely DiscreteGpu, but we do not know for sure.+ // Use "Other" to avoid possibly making incorrect assumptions.+ // Note that if this same device is available under some other API (ex: Vulkan),+ // It will mostly likely get a different device type (probably DiscreteGpu).+ wgt::DeviceType::Other+ };++ // source: Sascha Willems at Vulkan+ let vendor_id = if vendor.contains("amd") {+ db::amd::VENDOR+ } else if vendor.contains("imgtec") {+ db::imgtec::VENDOR+ } else if vendor.contains("nvidia") {+ db::nvidia::VENDOR+ } else if vendor.contains("arm") {+ db::arm::VENDOR+ } else if vendor.contains("qualcomm") {+ db::qualcomm::VENDOR+ } else if vendor.contains("intel") {+ db::intel::VENDOR+ } else if vendor.contains("broadcom") {+ db::broadcom::VENDOR+ } else if vendor.contains("mesa") {+ db::mesa::VENDOR+ } else if vendor.contains("apple") {+ db::apple::VENDOR+ } else {+ 0+ };++ wgt::AdapterInfo {+ name: renderer_orig,+ vendor: vendor_id,+ driver_info: version,+ ..wgt::AdapterInfo::new(inferred_device_type, wgt::Backend::Gl)+ }+ }++ pub(super) unsafe fn expose(+ context: super::AdapterContext,+ backend_options: wgt::GlBackendOptions,+ ) -> Option<crate::ExposedAdapter<super::Api>> {+ let gl = context.lock();+ let extensions = gl.supported_extensions();++ let (vendor_const, renderer_const) = if extensions.contains("WEBGL_debug_renderer_info") {+ // emscripten doesn't enable "WEBGL_debug_renderer_info" extension by default. so, we do it manually.+ // See https://github.com/gfx-rs/wgpu/issues/3245 for context+ #[cfg(Emscripten)]+ if unsafe {+ super::emscripten::enable_extension(c"WEBGL_debug_renderer_info".to_str().unwrap())+ } {+ (GL_UNMASKED_VENDOR_WEBGL, GL_UNMASKED_RENDERER_WEBGL)+ } else {+ (glow::VENDOR, glow::RENDERER)+ }+ // glow already enables WEBGL_debug_renderer_info on wasm32-unknown-unknown target by default.+ #[cfg(not(Emscripten))]+ (GL_UNMASKED_VENDOR_WEBGL, GL_UNMASKED_RENDERER_WEBGL)+ } else {+ (glow::VENDOR, glow::RENDERER)+ };++ let vendor = unsafe { gl.get_parameter_string(vendor_const) };+ let renderer = unsafe { gl.get_parameter_string(renderer_const) };+ let version = unsafe { gl.get_parameter_string(glow::VERSION) };+ log::debug!("Vendor: {vendor}");+ log::debug!("Renderer: {renderer}");+ log::debug!("Version: {version}");++ let full_ver = Self::parse_full_version(&version).ok();+ let es_ver = full_ver.map_or_else(|| Self::parse_version(&version).ok(), |_| None);++ if let Some(full_ver) = full_ver {+ let core_profile = (full_ver >= (3, 2)).then(|| unsafe {+ gl.get_parameter_i32(glow::CONTEXT_PROFILE_MASK)+ & glow::CONTEXT_CORE_PROFILE_BIT as i32+ != 0+ });+ log::trace!(+ "Profile: {}",+ core_profile+ .map(|core_profile| if core_profile {+ "Core"+ } else {+ "Compatibility"+ })+ .unwrap_or("Legacy")+ );+ }++ if es_ver.is_none() && full_ver.is_none() {+ log::warn!("Unable to parse OpenGL version");+ return None;+ }++ if let Some(es_ver) = es_ver {+ if es_ver < (3, 0) {+ log::warn!(+ "Returned GLES context is {}.{}, when 3.0+ was requested",+ es_ver.0,+ es_ver.1+ );+ return None;+ }+ }++ if let Some(full_ver) = full_ver {+ if full_ver < (3, 3) {+ log::warn!(+ "Returned GL context is {}.{}, when 3.3+ is needed",+ full_ver.0,+ full_ver.1+ );+ return None;+ }+ }++ let shading_language_version = {+ let sl_version = unsafe { gl.get_parameter_string(glow::SHADING_LANGUAGE_VERSION) };+ log::debug!("SL version: {}", &sl_version);+ if full_ver.is_some() {+ let (sl_major, sl_minor) = Self::parse_full_version(&sl_version).ok()?;+ let mut value = sl_major as u16 * 100 + sl_minor as u16 * 10;+ // Naga doesn't think it supports GL 460+, so we cap it at 450+ if value > 450 {+ value = 450;+ }+ naga::back::glsl::Version::Desktop(value)+ } else {+ let (sl_major, sl_minor) = Self::parse_version(&sl_version).ok()?;+ let value = sl_major as u16 * 100 + sl_minor as u16 * 10;+ naga::back::glsl::Version::Embedded {+ version: value,+ is_webgl: cfg!(any(webgl, Emscripten)),+ }+ }+ };++ log::debug!("Supported GL Extensions: {extensions:#?}");++ let supported = |(req_es_major, req_es_minor), (req_full_major, req_full_minor)| {+ let es_supported = es_ver+ .map(|es_ver| es_ver >= (req_es_major, req_es_minor))+ .unwrap_or_default();++ let full_supported = full_ver+ .map(|full_ver| full_ver >= (req_full_major, req_full_minor))+ .unwrap_or_default();++ es_supported || full_supported+ };++ let supports_storage =+ supported((3, 1), (4, 3)) || extensions.contains("GL_ARB_shader_storage_buffer_object");+ let supports_compute =+ supported((3, 1), (4, 3)) || extensions.contains("GL_ARB_compute_shader");+ let supports_work_group_params = supports_compute;++ // ANGLE provides renderer strings like: "ANGLE (Apple, Apple M1 Pro, OpenGL 4.1)"+ let is_angle = renderer.contains("ANGLE");++ let vertex_shader_storage_blocks = if supports_storage {+ let value =+ (unsafe { gl.get_parameter_i32(glow::MAX_VERTEX_SHADER_STORAGE_BLOCKS) } as u32);++ if value == 0 && extensions.contains("GL_ARB_shader_storage_buffer_object") {+ // The driver for AMD Radeon HD 5870 returns zero here, so assume the value matches the compute shader storage block count.+ // Windows doesn't recognize `GL_MAX_VERTEX_ATTRIB_STRIDE`.+ let new = (unsafe { gl.get_parameter_i32(glow::MAX_COMPUTE_SHADER_STORAGE_BLOCKS) }+ as u32);+ log::debug!("Max vertex shader storage blocks is zero, but GL_ARB_shader_storage_buffer_object is specified. Assuming the compute value {new}");+ new+ } else {+ value+ }+ } else {+ 0+ };+ let fragment_shader_storage_blocks = if supports_storage {+ (unsafe { gl.get_parameter_i32(glow::MAX_FRAGMENT_SHADER_STORAGE_BLOCKS) } as u32)+ } else {+ 0+ };+ let vertex_shader_storage_textures = if supports_storage {+ (unsafe { gl.get_parameter_i32(glow::MAX_VERTEX_IMAGE_UNIFORMS) } as u32)+ } else {+ 0+ };+ let fragment_shader_storage_textures = if supports_storage {+ (unsafe { gl.get_parameter_i32(glow::MAX_FRAGMENT_IMAGE_UNIFORMS) } as u32)+ } else {+ 0+ };+ let max_storage_block_size = if supports_storage {+ (unsafe { gl.get_parameter_i32(glow::MAX_SHADER_STORAGE_BLOCK_SIZE) } as u32)+ } else {+ 0+ };+ let max_element_index = unsafe { gl.get_parameter_i32(glow::MAX_ELEMENT_INDEX) } as u32;++ // WORKAROUND: In order to work around an issue with GL on RPI4 and similar, we ignore a+ // zero vertex ssbo count if there are vertex sstos. (more info:+ // https://github.com/gfx-rs/wgpu/pull/1607#issuecomment-874938961) The hardware does not+ // want us to write to these SSBOs, but GLES cannot express that. We detect this case and+ // disable writing to SSBOs.+ let vertex_ssbo_false_zero =+ vertex_shader_storage_blocks == 0 && vertex_shader_storage_textures != 0;+ if vertex_ssbo_false_zero {+ // We only care about fragment here as the 0 is a lie.+ log::debug!("Max vertex shader SSBO == 0 and SSTO != 0. Interpreting as false zero.");+ }++ let max_storage_buffers_per_shader_stage = if vertex_shader_storage_blocks == 0 {+ fragment_shader_storage_blocks+ } else {+ vertex_shader_storage_blocks.min(fragment_shader_storage_blocks)+ };+ let max_storage_textures_per_shader_stage = if vertex_shader_storage_textures == 0 {+ fragment_shader_storage_textures+ } else {+ vertex_shader_storage_textures.min(fragment_shader_storage_textures)+ };+ // NOTE: GL_ARB_compute_shader adds support for indirect dispatch+ let indirect_execution = supported((3, 1), (4, 3))+ || (extensions.contains("GL_ARB_draw_indirect") && supports_compute);+ let supports_cube_array = supported((3, 2), (4, 0))+ || (supported((3, 1), (4, 0)) && extensions.contains("GL_EXT_texture_cube_map_array"));++ let mut downlevel_flags = wgt::DownlevelFlags::empty()+ | wgt::DownlevelFlags::NON_POWER_OF_TWO_MIPMAPPED_TEXTURES+ | wgt::DownlevelFlags::COMPARISON_SAMPLERS+ | wgt::DownlevelFlags::SHADER_F16_IN_F32+ | wgt::DownlevelFlags::MSL2_1;+ downlevel_flags.set(+ wgt::DownlevelFlags::CUBE_ARRAY_TEXTURES,+ supports_cube_array,+ );+ downlevel_flags.set(wgt::DownlevelFlags::COMPUTE_SHADERS, supports_compute);+ downlevel_flags.set(+ wgt::DownlevelFlags::FRAGMENT_WRITABLE_STORAGE,+ max_storage_block_size != 0,+ );+ downlevel_flags.set(wgt::DownlevelFlags::INDIRECT_EXECUTION, indirect_execution);+ downlevel_flags.set(wgt::DownlevelFlags::BASE_VERTEX, supported((3, 2), (3, 2)));+ downlevel_flags.set(+ wgt::DownlevelFlags::INDEPENDENT_BLEND,+ supported((3, 2), (4, 0)) || extensions.contains("GL_EXT_draw_buffers_indexed"),+ );+ downlevel_flags.set(+ wgt::DownlevelFlags::VERTEX_STORAGE,+ max_storage_block_size != 0+ && max_storage_buffers_per_shader_stage != 0+ && (vertex_shader_storage_blocks != 0 || vertex_ssbo_false_zero),+ );+ downlevel_flags.set(wgt::DownlevelFlags::FRAGMENT_STORAGE, supports_storage);+ if extensions.contains("EXT_texture_filter_anisotropic")+ || extensions.contains("GL_EXT_texture_filter_anisotropic")+ {+ let max_aniso =+ unsafe { gl.get_parameter_i32(glow::MAX_TEXTURE_MAX_ANISOTROPY_EXT) } as u32;+ downlevel_flags.set(wgt::DownlevelFlags::ANISOTROPIC_FILTERING, max_aniso >= 16);+ }+ downlevel_flags.set(+ wgt::DownlevelFlags::BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED,+ !(cfg!(any(webgl, Emscripten)) || is_angle),+ );+ // see https://registry.khronos.org/webgl/specs/latest/2.0/#BUFFER_OBJECT_BINDING+ downlevel_flags.set(+ wgt::DownlevelFlags::UNRESTRICTED_INDEX_BUFFER,+ !cfg!(any(webgl, Emscripten)),+ );+ downlevel_flags.set(+ wgt::DownlevelFlags::UNRESTRICTED_EXTERNAL_TEXTURE_COPIES,+ !cfg!(any(webgl, Emscripten)),+ );+ downlevel_flags.set(+ wgt::DownlevelFlags::FULL_DRAW_INDEX_UINT32,+ max_element_index == u32::MAX,+ );+ downlevel_flags.set(+ wgt::DownlevelFlags::MULTISAMPLED_SHADING,+ supported((3, 2), (4, 0)) || extensions.contains("OES_sample_variables"),+ );+ let query_buffers = extensions.contains("GL_ARB_query_buffer_object")+ || extensions.contains("GL_AMD_query_buffer_object");+ if query_buffers {+ downlevel_flags.set(wgt::DownlevelFlags::NONBLOCKING_QUERY_RESOLVE, true);+ }++ // Desktop GL: norm16 is core since GL 3.0/3.1; we minimum-version+ // to GL 3.3, so always on. GLES/WebGL2: needs `EXT_texture_norm16`.+ let supports_16bit_norm = if es_ver.is_some() {+ extensions.contains("GL_EXT_texture_norm16")+ || extensions.contains("EXT_texture_norm16")+ } else {+ true+ };+ // SNORM color-rendering is not spec-guaranteed on either path+ // (GLES Table 8.13 marks it not-renderable; desktop GL exposes+ // it as only "optionally renderable"), so gate it on+ // `EXT_render_snorm` for both - matching how `COLOR_BUFFER_FLOAT`+ // is probed above.+ let supports_16bit_snorm_renderable = supports_16bit_norm+ && (extensions.contains("GL_EXT_render_snorm")+ || extensions.contains("EXT_render_snorm"));+ // Storage on norm16. ARB_shader_image_load_store / GL 4.2 Table X.2 and+ // NV_image_formats Table 8.27 both list `r16/rg16/rgba16` and the SNORM+ // variants as image-unit formats, so one gate covers UNORM and SNORM.+ // Paths:+ // * Desktop: core in GL 4.2+; on 3.3..=4.1 via `GL_ARB_shader_image_load_store`.+ // * GLES/WebGL2: only via `GL_NV_image_formats` - the ES 3.1/3.2 core+ // Table 8.27 omits these formats, and `GL_EXT_texture_norm16` does not+ // extend Table 8.27 (NV_image_formats itself depends on EXT_texture_norm16+ // for the norm16 entries, so an ES driver exposing NV_image_formats+ // without EXT_texture_norm16 still wouldn't accept them - the+ // `supports_16bit_norm` prerequisite below catches that).+ let supports_16bit_norm_storage = supports_16bit_norm+ && if es_ver.is_some() {+ extensions.contains("GL_NV_image_formats")+ } else {+ full_ver.is_some_and(|v| v >= (4, 2))+ || extensions.contains("GL_ARB_shader_image_load_store")+ };++ let mut features = wgt::Features::empty()+ | wgt::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES+ | wgt::Features::CLEAR_TEXTURE+ | wgt::Features::IMMEDIATES+ | wgt::Features::DEPTH32FLOAT_STENCIL8+ | wgt::Features::PASSTHROUGH_SHADERS;+ features.set(+ wgt::Features::TEXTURE_FORMAT_16BIT_NORM,+ supports_16bit_norm,+ );+ features.set(+ wgt::Features::ADDRESS_MODE_CLAMP_TO_BORDER | wgt::Features::ADDRESS_MODE_CLAMP_TO_ZERO,+ extensions.contains("GL_EXT_texture_border_clamp")+ || extensions.contains("GL_ARB_texture_border_clamp"),+ );+ features.set(+ wgt::Features::DEPTH_CLIP_CONTROL,+ extensions.contains("GL_EXT_depth_clamp") || extensions.contains("GL_ARB_depth_clamp"),+ );+ features.set(+ wgt::Features::VERTEX_WRITABLE_STORAGE,+ downlevel_flags.contains(wgt::DownlevelFlags::VERTEX_STORAGE)+ && vertex_shader_storage_textures != 0,+ );+ features.set(+ wgt::Features::MULTIVIEW,+ extensions.contains("OVR_multiview2") || extensions.contains("GL_OVR_multiview2"),+ );+ features.set(+ wgt::Features::DUAL_SOURCE_BLENDING,+ extensions.contains("GL_EXT_blend_func_extended")+ || extensions.contains("GL_ARB_blend_func_extended"),+ );+ features.set(+ wgt::Features::CLIP_DISTANCES,+ full_ver.is_some() || extensions.contains("GL_EXT_clip_cull_distance"),+ );+ features.set(+ wgt::Features::PRIMITIVE_INDEX,+ supported((3, 2), (3, 2))+ || extensions.contains("OES_geometry_shader")+ || extensions.contains("GL_ARB_geometry_shader4"),+ );+ features.set(+ wgt::Features::SHADER_EARLY_DEPTH_TEST,+ supported((3, 1), (4, 2)) || extensions.contains("GL_ARB_shader_image_load_store"),+ );+ if extensions.contains("GL_ARB_timer_query") {+ features.set(wgt::Features::TIMESTAMP_QUERY, true);+ features.set(wgt::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS, true);+ features.set(wgt::Features::TIMESTAMP_QUERY_INSIDE_PASSES, true);+ }+ let gl_bcn_exts = [+ "GL_EXT_texture_compression_s3tc",+ "GL_EXT_texture_compression_rgtc",+ "GL_ARB_texture_compression_bptc",+ ];+ let gles_bcn_exts = [+ "GL_EXT_texture_compression_s3tc_srgb",+ "GL_EXT_texture_compression_rgtc",+ "GL_EXT_texture_compression_bptc",+ ];+ let webgl_bcn_exts = [+ "WEBGL_compressed_texture_s3tc",+ "WEBGL_compressed_texture_s3tc_srgb",+ "EXT_texture_compression_rgtc",+ "EXT_texture_compression_bptc",+ ];+ let bcn_exts = if cfg!(any(webgl, Emscripten)) {+ &webgl_bcn_exts[..]+ } else if es_ver.is_some() {+ &gles_bcn_exts[..]+ } else {+ &gl_bcn_exts[..]+ };+ features.set(+ wgt::Features::TEXTURE_COMPRESSION_BC,+ bcn_exts.iter().all(|&ext| extensions.contains(ext)),+ );+ features.set(+ wgt::Features::TEXTURE_COMPRESSION_BC_SLICED_3D,+ bcn_exts.iter().all(|&ext| extensions.contains(ext)), // BC guaranteed Sliced 3D+ );+ let has_etc = if cfg!(any(webgl, Emscripten)) {+ extensions.contains("WEBGL_compressed_texture_etc")+ } else {+ es_ver.is_some() || extensions.contains("GL_ARB_ES3_compatibility")+ };+ features.set(wgt::Features::TEXTURE_COMPRESSION_ETC2, has_etc);++ // `OES_texture_compression_astc` provides 2D + 3D, LDR + HDR support+ if extensions.contains("WEBGL_compressed_texture_astc")+ || extensions.contains("GL_OES_texture_compression_astc")+ {+ #[cfg(webgl)]+ {+ if context+ .glow_context+ .compressed_texture_astc_supports_ldr_profile()+ {+ features.insert(wgt::Features::TEXTURE_COMPRESSION_ASTC);+ features.insert(wgt::Features::TEXTURE_COMPRESSION_ASTC_SLICED_3D);+ }+ if context+ .glow_context+ .compressed_texture_astc_supports_hdr_profile()+ {+ features.insert(wgt::Features::TEXTURE_COMPRESSION_ASTC_HDR);+ }+ }++ #[cfg(any(native, Emscripten))]+ {+ features.insert(wgt::Features::TEXTURE_COMPRESSION_ASTC);+ features.insert(wgt::Features::TEXTURE_COMPRESSION_ASTC_SLICED_3D);+ features.insert(wgt::Features::TEXTURE_COMPRESSION_ASTC_HDR);+ }+ } else {+ features.set(+ wgt::Features::TEXTURE_COMPRESSION_ASTC,+ extensions.contains("GL_KHR_texture_compression_astc_ldr"),+ );+ features.set(+ wgt::Features::TEXTURE_COMPRESSION_ASTC_SLICED_3D,+ extensions.contains("GL_KHR_texture_compression_astc_ldr")+ && extensions.contains("GL_KHR_texture_compression_astc_sliced_3d"),+ );+ features.set(+ wgt::Features::TEXTURE_COMPRESSION_ASTC_HDR,+ extensions.contains("GL_KHR_texture_compression_astc_hdr"),+ );+ }++ downlevel_flags.set(+ wgt::DownlevelFlags::TEXTURE_COMPRESSION,+ features.contains(wgt::Features::TEXTURE_COMPRESSION_BC)+ || features.contains(+ wgt::Features::TEXTURE_COMPRESSION_ETC2+ | wgt::Features::TEXTURE_COMPRESSION_ASTC,+ ),+ );++ features.set(+ wgt::Features::FLOAT32_FILTERABLE,+ extensions.contains("GL_ARB_color_buffer_float")+ || extensions.contains("GL_EXT_color_buffer_float")+ || extensions.contains("OES_texture_float_linear"),+ );++ if es_ver.is_none() {+ features |= wgt::Features::POLYGON_MODE_LINE | wgt::Features::POLYGON_MODE_POINT;+ }++ // We *might* be able to emulate bgra8unorm-storage but currently don't attempt to.++ let mut private_caps = super::PrivateCapabilities::empty();+ private_caps.set(+ super::PrivateCapabilities::BUFFER_ALLOCATION,+ extensions.contains("GL_EXT_buffer_storage")+ || extensions.contains("GL_ARB_buffer_storage"),+ );+ private_caps.set(+ super::PrivateCapabilities::SHADER_BINDING_LAYOUT,+ supports_compute,+ );+ private_caps.set(+ super::PrivateCapabilities::SHADER_TEXTURE_SHADOW_LOD,+ extensions.contains("GL_EXT_texture_shadow_lod"),+ );+ private_caps.set(+ super::PrivateCapabilities::MEMORY_BARRIERS,+ supported((3, 1), (4, 2)),+ );+ private_caps.set(+ super::PrivateCapabilities::VERTEX_BUFFER_LAYOUT,+ supported((3, 1), (4, 3)) || extensions.contains("GL_ARB_vertex_attrib_binding"),+ );+ private_caps.set(+ super::PrivateCapabilities::INDEX_BUFFER_ROLE_CHANGE,+ !cfg!(any(webgl, Emscripten)),+ );+ private_caps.set(+ super::PrivateCapabilities::GET_BUFFER_SUB_DATA,+ cfg!(any(webgl, Emscripten)) || full_ver.is_some(),+ );+ let color_buffer_float = extensions.contains("GL_EXT_color_buffer_float")+ || extensions.contains("GL_ARB_color_buffer_float")+ || extensions.contains("EXT_color_buffer_float");+ let color_buffer_half_float = extensions.contains("GL_EXT_color_buffer_half_float")+ || extensions.contains("GL_ARB_half_float_pixel");+ private_caps.set(+ super::PrivateCapabilities::COLOR_BUFFER_HALF_FLOAT,+ color_buffer_half_float || color_buffer_float,+ );+ private_caps.set(+ super::PrivateCapabilities::COLOR_BUFFER_FLOAT,+ color_buffer_float,+ );+ private_caps.set(super::PrivateCapabilities::QUERY_BUFFERS, query_buffers);+ private_caps.set(super::PrivateCapabilities::QUERY_64BIT, full_ver.is_some());+ private_caps.set(+ super::PrivateCapabilities::TEXTURE_STORAGE,+ supported((3, 0), (4, 2)),+ );+ let is_mali = renderer.to_lowercase().contains("mali");+ let debug_fns_enabled = match backend_options.debug_fns {+ wgt::GlDebugFns::Auto => gl.supports_debug() && !is_mali,+ wgt::GlDebugFns::ForceEnabled => gl.supports_debug(),+ wgt::GlDebugFns::Disabled => false,+ };+ private_caps.set(super::PrivateCapabilities::DEBUG_FNS, debug_fns_enabled);+ private_caps.set(+ super::PrivateCapabilities::INVALIDATE_FRAMEBUFFER,+ supported((3, 0), (4, 3)),+ );+ if let Some(full_ver) = full_ver {+ let supported =+ full_ver >= (4, 2) && extensions.contains("GL_ARB_shader_draw_parameters");+ private_caps.set(+ super::PrivateCapabilities::FULLY_FEATURED_INSTANCING,+ supported,+ );+ // Desktop 4.2 and greater specify the first instance parameter.+ //+ // For all other versions, the behavior is undefined.+ //+ // We only support indirect first instance when we also have ARB_shader_draw_parameters as+ // that's the only way to get gl_InstanceID to work correctly.+ features.set(wgt::Features::INDIRECT_FIRST_INSTANCE, supported);+ }+ private_caps.set(+ super::PrivateCapabilities::MULTISAMPLED_RENDER_TO_TEXTURE,+ extensions.contains("GL_EXT_multisampled_render_to_texture"),+ );+ private_caps.set(+ super::PrivateCapabilities::TEXTURE_FORMAT_NORM16,+ supports_16bit_norm,+ );+ private_caps.set(+ super::PrivateCapabilities::TEXTURE_FORMAT_SNORM16_RENDERABLE,+ supports_16bit_snorm_renderable,+ );+ private_caps.set(+ super::PrivateCapabilities::TEXTURE_FORMAT_NORM16_STORAGE,+ supports_16bit_norm_storage,+ );++ // GLSL ES 3.10+ / GLSL 4.30+ natively support coherent/volatile qualifiers+ // on storage buffers. These were introduced alongside storage buffer support.+ if supports_storage {+ features |= wgt::Features::MEMORY_DECORATION_COHERENT+ | wgt::Features::MEMORY_DECORATION_VOLATILE;+ }++ let max_texture_size = unsafe { gl.get_parameter_i32(glow::MAX_TEXTURE_SIZE) } as u32;+ let max_texture_3d_size = unsafe { gl.get_parameter_i32(glow::MAX_3D_TEXTURE_SIZE) } as u32;++ let min_uniform_buffer_offset_alignment =+ (unsafe { gl.get_parameter_i32(glow::UNIFORM_BUFFER_OFFSET_ALIGNMENT) } as u32);+ let min_storage_buffer_offset_alignment = if supports_storage {+ (unsafe { gl.get_parameter_i32(glow::SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT) } as u32)+ } else {+ 256+ };+ let max_uniform_buffers_per_shader_stage =+ unsafe { gl.get_parameter_i32(glow::MAX_VERTEX_UNIFORM_BLOCKS) }+ .min(unsafe { gl.get_parameter_i32(glow::MAX_FRAGMENT_UNIFORM_BLOCKS) })+ as u32;++ let max_compute_workgroups_per_dimension = if supports_work_group_params {+ unsafe { gl.get_parameter_indexed_i32(glow::MAX_COMPUTE_WORK_GROUP_COUNT, 0) }+ .min(unsafe { gl.get_parameter_indexed_i32(glow::MAX_COMPUTE_WORK_GROUP_COUNT, 1) })+ .min(unsafe { gl.get_parameter_indexed_i32(glow::MAX_COMPUTE_WORK_GROUP_COUNT, 2) })+ as u32+ } else {+ 0+ };++ let max_color_attachments = unsafe {+ gl.get_parameter_i32(glow::MAX_COLOR_ATTACHMENTS)+ .min(gl.get_parameter_i32(glow::MAX_DRAW_BUFFERS)) as u32+ };++ // 16 bytes per sample is the maximum size of a color attachment.+ let max_color_attachment_bytes_per_sample =+ max_color_attachments * wgt::TextureFormat::MAX_TARGET_PIXEL_BYTE_COST;++ let limits = crate::auxil::adjust_raw_limits(wgt::Limits {+ max_texture_dimension_1d: max_texture_size,+ max_texture_dimension_2d: max_texture_size,+ max_texture_dimension_3d: max_texture_3d_size,+ max_texture_array_layers: unsafe {+ gl.get_parameter_i32(glow::MAX_ARRAY_TEXTURE_LAYERS)+ } as u32,+ max_bind_groups: u32::MAX,+ // No limit.+ max_bind_groups_plus_vertex_buffers: u32::MAX,+ // No limit.+ max_bindings_per_bind_group: u32::MAX,+ max_dynamic_uniform_buffers_per_pipeline_layout: max_uniform_buffers_per_shader_stage,+ max_dynamic_storage_buffers_per_pipeline_layout: max_storage_buffers_per_shader_stage,+ max_sampled_textures_per_shader_stage: super::MAX_TEXTURE_SLOTS as u32,+ max_samplers_per_shader_stage: super::MAX_SAMPLERS as u32,+ max_storage_buffers_per_shader_stage,+ max_storage_textures_per_shader_stage,+ max_uniform_buffers_per_shader_stage,+ max_binding_array_elements_per_shader_stage: 0,+ max_binding_array_sampler_elements_per_shader_stage: 0,+ max_binding_array_acceleration_structure_elements_per_shader_stage: 0,+ max_uniform_buffer_binding_size: unsafe {+ gl.get_parameter_i32(glow::MAX_UNIFORM_BLOCK_SIZE)+ } as u64,+ max_storage_buffer_binding_size: if supports_storage {+ unsafe { gl.get_parameter_i32(glow::MAX_SHADER_STORAGE_BLOCK_SIZE) }+ } else {+ 0+ } as u64,+ max_vertex_buffers: if private_caps+ .contains(super::PrivateCapabilities::VERTEX_BUFFER_LAYOUT)+ {+ (unsafe { gl.get_parameter_i32(glow::MAX_VERTEX_ATTRIB_BINDINGS) } as u32)+ } else {+ 16 // should this be different?+ },+ max_vertex_attributes: (unsafe { gl.get_parameter_i32(glow::MAX_VERTEX_ATTRIBS) }+ as u32)+ .min(super::MAX_VERTEX_ATTRIBUTES as u32),+ max_vertex_buffer_array_stride: if private_caps+ .contains(super::PrivateCapabilities::VERTEX_BUFFER_LAYOUT)+ {+ if let Some(full_ver) = full_ver {+ if full_ver >= (4, 4) {+ // We can query `GL_MAX_VERTEX_ATTRIB_STRIDE` in OpenGL 4.4++ let value =+ (unsafe { gl.get_parameter_i32(glow::MAX_VERTEX_ATTRIB_STRIDE) })+ as u32;++ if value == 0 {+ // This should be at least 2048, but the driver for AMD Radeon HD 5870 on+ // Windows doesn't recognize `GL_MAX_VERTEX_ATTRIB_STRIDE`.++ log::debug!("Max vertex attribute stride is 0. Assuming it is the OpenGL minimum spec 2048");+ 2048+ } else {+ value+ }+ } else {+ log::debug!("Max vertex attribute stride unknown. Assuming it is the OpenGL minimum spec 2048");+ 2048+ }+ } else {+ (unsafe { gl.get_parameter_i32(glow::MAX_VERTEX_ATTRIB_STRIDE) }) as u32+ }+ } else {+ !0+ },+ max_immediate_size: super::MAX_IMMEDIATES as u32 * 4,+ min_uniform_buffer_offset_alignment,+ min_storage_buffer_offset_alignment,+ max_inter_stage_shader_variables: {+ // MAX_VARYING_COMPONENTS may return 0, because it is deprecated since OpenGL 3.2 core,+ // and an OpenGL Context with the core profile and with forward-compatibility=true,+ // will make deprecated constants unavailable.+ let max_varying_components =+ unsafe { gl.get_parameter_i32(glow::MAX_VARYING_COMPONENTS) } as u32;+ if max_varying_components == 0 {+ // default value for max_inter_stage_shader_variables+ 15+ } else {+ max_varying_components / 4+ }+ },+ max_color_attachments,+ max_color_attachment_bytes_per_sample,+ max_compute_workgroup_storage_size: if supports_work_group_params {+ (unsafe { gl.get_parameter_i32(glow::MAX_COMPUTE_SHARED_MEMORY_SIZE) } as u32)+ } else {+ 0+ },+ max_compute_invocations_per_workgroup: if supports_work_group_params {+ (unsafe { gl.get_parameter_i32(glow::MAX_COMPUTE_WORK_GROUP_INVOCATIONS) } as u32)+ } else {+ 0+ },+ max_compute_workgroup_size_x: if supports_work_group_params {+ (unsafe { gl.get_parameter_indexed_i32(glow::MAX_COMPUTE_WORK_GROUP_SIZE, 0) }+ as u32)+ } else {+ 0+ },+ max_compute_workgroup_size_y: if supports_work_group_params {+ (unsafe { gl.get_parameter_indexed_i32(glow::MAX_COMPUTE_WORK_GROUP_SIZE, 1) }+ as u32)+ } else {+ 0+ },+ max_compute_workgroup_size_z: if supports_work_group_params {+ (unsafe { gl.get_parameter_indexed_i32(glow::MAX_COMPUTE_WORK_GROUP_SIZE, 2) }+ as u32)+ } else {+ 0+ },+ max_compute_workgroups_per_dimension,+ max_buffer_size: i32::MAX as u64,+ max_non_sampler_bindings: u32::MAX,++ max_task_workgroup_total_count: 0,+ max_task_workgroups_per_dimension: 0,+ max_mesh_workgroup_total_count: 0,+ max_mesh_workgroups_per_dimension: 0,+ max_task_invocations_per_workgroup: 0,+ max_task_invocations_per_dimension: 0,+ max_mesh_invocations_per_workgroup: 0,+ max_mesh_invocations_per_dimension: 0,+ max_task_payload_size: 0,+ max_mesh_output_vertices: 0,+ max_mesh_output_primitives: 0,+ max_mesh_output_layers: 0,+ max_mesh_multiview_view_count: 0,++ max_blas_primitive_count: 0,+ max_blas_geometry_count: 0,+ max_tlas_instance_count: 0,+ max_acceleration_structures_per_shader_stage: 0,+ max_buffers_and_acceleration_structures_per_shader_stage: u32::MAX,++ max_multiview_view_count: 0,++ max_ray_dispatch_count: 0,+ max_ray_recursion_depth: 0,+ });++ let mut workarounds = super::Workarounds::empty();++ workarounds.set(+ super::Workarounds::EMULATE_BUFFER_MAP,+ cfg!(any(webgl, Emscripten)),+ );++ let r = renderer.to_lowercase();+ // Check for Mesa sRGB clear bug. See+ // [`super::PrivateCapabilities::MESA_I915_SRGB_SHADER_CLEAR`].+ if context.is_owned()+ && r.contains("mesa")+ && r.contains("intel")+ && r.split(&[' ', '(', ')'][..])+ .any(|substr| substr.len() == 3 && substr.chars().nth(2) == Some('l'))+ {+ log::debug!(+ "Detected skylake derivative running on mesa i915. Clears to srgb textures will \+ use manual shader clears."+ );+ workarounds.set(super::Workarounds::MESA_I915_SRGB_SHADER_CLEAR, true);+ }++ let downlevel_defaults = wgt::DownlevelLimits {};+ let max_samples = unsafe { gl.get_parameter_i32(glow::MAX_SAMPLES) };++ // Drop the GL guard so we can move the context into AdapterShared+ // ( on Wasm the gl handle is just a ref so we tell clippy to allow+ // dropping the ref )+ #[cfg_attr(target_arch = "wasm32", allow(dropping_references))]+ drop(gl);++ Some(crate::ExposedAdapter {+ adapter: super::Adapter {+ shared: Arc::new(super::AdapterShared {+ context,+ private_caps,+ workarounds,+ features,+ limits: limits.clone(),+ options: backend_options,+ shading_language_version,+ next_shader_id: Default::default(),+ program_cache: Default::default(),+ es: es_ver.is_some(),+ max_msaa_samples: max_samples,+ }),+ },+ info: Self::make_info(vendor, renderer, version),+ features,+ capabilities: crate::Capabilities {+ limits,+ downlevel: wgt::DownlevelCapabilities {+ flags: downlevel_flags,+ limits: downlevel_defaults,+ shader_model: wgt::ShaderModel::Sm5,+ },+ alignments: crate::Alignments {+ buffer_copy_offset: wgt::BufferSize::new(4).unwrap(),+ buffer_copy_pitch: wgt::BufferSize::new(4).unwrap(),+ // #6151: `wgpu_hal::gles` doesn't ask Naga to inject bounds+ // checks in GLSL, and it doesn't request extensions like+ // `KHR_robust_buffer_access_behavior` that would provide+ // them, so we can't really implement the checks promised by+ // [`crate::BufferBinding`].+ //+ // Since this is a pre-existing condition, for the time+ // being, provide 1 as the value here, to cause as little+ // trouble as possible.+ uniform_bounds_check_alignment: wgt::BufferSize::new(1).unwrap(),+ raw_tlas_instance_size: 0,+ ray_tracing_scratch_buffer_alignment: 0,+ ray_tracing_pipeline_group_data_size: 0,+ ray_tracing_pipeline_group_data_alignment: 0,+ ray_tracing_pipeline_data_offset_alignment: 0,+ },+ cooperative_matrix_properties: Vec::new(),+ },+ })+ }++ unsafe fn compile_shader(+ source: &str,+ gl: &glow::Context,+ shader_type: u32,+ es: bool,+ ) -> Option<glow::Shader> {+ let source = if es {+ format!("#version 300 es\nprecision lowp float;\n{source}")+ } else {+ let version = gl.version();+ if version.major == 3 && version.minor == 0 {+ // OpenGL 3.0 only supports this format+ format!("#version 130\n{source}")+ } else {+ // OpenGL 3.1+ support this format+ format!("#version 140\n{source}")+ }+ };+ let shader = unsafe { gl.create_shader(shader_type) }.expect("Could not create shader");+ unsafe { gl.shader_source(shader, &source) };+ unsafe { gl.compile_shader(shader) };++ if !unsafe { gl.get_shader_compile_status(shader) } {+ let msg = unsafe { gl.get_shader_info_log(shader) };+ if !msg.is_empty() {+ log::error!("\tShader compile error: {msg}");+ }+ unsafe { gl.delete_shader(shader) };+ None+ } else {+ Some(shader)+ }+ }++ unsafe fn create_shader_clear_program(+ gl: &glow::Context,+ es: bool,+ ) -> Option<ShaderClearProgram> {+ let program = unsafe { gl.create_program() }.expect("Could not create shader program");+ let vertex = unsafe {+ Self::compile_shader(+ include_str!("./shaders/clear.vert"),+ gl,+ glow::VERTEX_SHADER,+ es,+ )?+ };+ let fragment = unsafe {+ Self::compile_shader(+ include_str!("./shaders/clear.frag"),+ gl,+ glow::FRAGMENT_SHADER,+ es,+ )?+ };+ unsafe { gl.attach_shader(program, vertex) };+ unsafe { gl.attach_shader(program, fragment) };+ unsafe { gl.link_program(program) };++ let linked_ok = unsafe { gl.get_program_link_status(program) };+ let msg = unsafe { gl.get_program_info_log(program) };+ if !msg.is_empty() {+ log::error!("Shader link error: {msg}");+ }+ if !linked_ok {+ return None;+ }++ let color_uniform_location = unsafe { gl.get_uniform_location(program, "color") }+ .expect("Could not find color uniform in shader clear shader");+ unsafe { gl.delete_shader(vertex) };+ unsafe { gl.delete_shader(fragment) };++ Some(ShaderClearProgram {+ program,+ color_uniform_location,+ })+ }+}++impl crate::Adapter for super::Adapter {+ type A = super::Api;++ unsafe fn open(+ &self,+ features: wgt::Features,+ _limits: &wgt::Limits,+ _memory_hints: &wgt::MemoryHints,+ ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> {+ let gl = &self.shared.context.lock();+ unsafe { gl.pixel_store_i32(glow::UNPACK_ALIGNMENT, 1) };+ unsafe { gl.pixel_store_i32(glow::PACK_ALIGNMENT, 1) };+ let main_vao =+ unsafe { gl.create_vertex_array() }.map_err(|_| crate::DeviceError::OutOfMemory)?;+ unsafe { gl.bind_vertex_array(Some(main_vao)) };++ let zero_buffer =+ unsafe { gl.create_buffer() }.map_err(|_| crate::DeviceError::OutOfMemory)?;+ unsafe { gl.bind_buffer(glow::COPY_READ_BUFFER, Some(zero_buffer)) };+ let zeroes = vec![0u8; super::ZERO_BUFFER_SIZE];+ unsafe { gl.buffer_data_u8_slice(glow::COPY_READ_BUFFER, &zeroes, glow::STATIC_DRAW) };++ // Compile the shader program we use for doing manual clears to work around Mesa fastclear+ // bug.++ let shader_clear_program = if self+ .shared+ .workarounds+ .contains(super::Workarounds::MESA_I915_SRGB_SHADER_CLEAR)+ {+ Some(unsafe {+ Self::create_shader_clear_program(gl, self.shared.es)+ .ok_or(crate::DeviceError::Lost)?+ })+ } else {+ // If we don't need the workaround, don't waste time and resources compiling the clear program+ None+ };++ Ok(crate::OpenDevice {+ device: super::Device {+ shared: Arc::clone(&self.shared),+ main_vao,+ #[cfg(all(native, feature = "renderdoc"))]+ render_doc: Default::default(),+ counters: Default::default(),+ },+ queue: super::Queue {+ shared: Arc::clone(&self.shared),+ features,+ draw_fbo: unsafe { gl.create_framebuffer() }+ .map_err(|_| crate::DeviceError::OutOfMemory)?,+ copy_fbo: unsafe { gl.create_framebuffer() }+ .map_err(|_| crate::DeviceError::OutOfMemory)?,+ shader_clear_program,+ zero_buffer,+ temp_query_results: Mutex::new(Vec::new()),+ draw_buffer_count: AtomicU8::new(1),+ current_index_buffer: Mutex::new(None),+ },+ })+ }++ unsafe fn texture_format_capabilities(+ &self,+ format: wgt::TextureFormat,+ ) -> crate::TextureFormatCapabilities {+ use crate::TextureFormatCapabilities as Tfc;+ use wgt::TextureFormat as Tf;++ let sample_count = {+ let max_samples = self.shared.max_msaa_samples;+ if max_samples >= 16 {+ Tfc::MULTISAMPLE_X2+ | Tfc::MULTISAMPLE_X4+ | Tfc::MULTISAMPLE_X8+ | Tfc::MULTISAMPLE_X16+ } else if max_samples >= 8 {+ Tfc::MULTISAMPLE_X2 | Tfc::MULTISAMPLE_X4 | Tfc::MULTISAMPLE_X8+ } else {+ // The lowest supported level in GLE3.0/WebGL2 is 4X+ // (see GL_MAX_SAMPLES in https://registry.khronos.org/OpenGL-Refpages/es3.0/html/glGet.xhtml).+ // On some platforms, like iOS Safari, `get_parameter_i32(MAX_SAMPLES)` returns 0,+ // so we always fall back to supporting 4x here.+ Tfc::MULTISAMPLE_X2 | Tfc::MULTISAMPLE_X4+ }+ };++ // Base types are pulled from the table in the OpenGLES 3.0 spec in section 3.8.+ //+ // The storage types are based on table 8.26, in section+ // "TEXTURE IMAGE LOADS AND STORES" of OpenGLES-3.2 spec.+ let empty = Tfc::empty();+ let base = Tfc::COPY_SRC | Tfc::COPY_DST;+ let unfilterable = base | Tfc::SAMPLED;+ let depth = base | Tfc::SAMPLED | sample_count | Tfc::DEPTH_STENCIL_ATTACHMENT;+ let filterable = unfilterable | Tfc::SAMPLED_LINEAR;+ let renderable =+ unfilterable | Tfc::COLOR_ATTACHMENT | sample_count | Tfc::MULTISAMPLE_RESOLVE;+ let filterable_renderable = filterable | renderable | Tfc::COLOR_ATTACHMENT_BLEND;+ let storage =+ base | Tfc::STORAGE_READ_WRITE | Tfc::STORAGE_READ_ONLY | Tfc::STORAGE_WRITE_ONLY;++ let feature_fn = |f, caps| {+ if self.shared.features.contains(f) {+ caps+ } else {+ empty+ }+ };++ let bcn_features = feature_fn(wgt::Features::TEXTURE_COMPRESSION_BC, filterable);+ let etc2_features = feature_fn(wgt::Features::TEXTURE_COMPRESSION_ETC2, filterable);+ let astc_features = feature_fn(wgt::Features::TEXTURE_COMPRESSION_ASTC, filterable);+ let astc_hdr_features = feature_fn(wgt::Features::TEXTURE_COMPRESSION_ASTC_HDR, filterable);++ let private_caps_fn = |f, caps| {+ if self.shared.private_caps.contains(f) {+ caps+ } else {+ empty+ }+ };++ let half_float_renderable = private_caps_fn(+ super::PrivateCapabilities::COLOR_BUFFER_HALF_FLOAT,+ Tfc::COLOR_ATTACHMENT+ | Tfc::COLOR_ATTACHMENT_BLEND+ | sample_count+ | Tfc::MULTISAMPLE_RESOLVE,+ );++ let float_renderable = private_caps_fn(+ super::PrivateCapabilities::COLOR_BUFFER_FLOAT,+ Tfc::COLOR_ATTACHMENT+ | Tfc::COLOR_ATTACHMENT_BLEND+ | sample_count+ | Tfc::MULTISAMPLE_RESOLVE,+ );++ let texture_float_linear = feature_fn(wgt::Features::FLOAT32_FILTERABLE, filterable);++ let image_atomic = feature_fn(wgt::Features::TEXTURE_ATOMIC, Tfc::STORAGE_ATOMIC);+ let image_64_atomic = feature_fn(wgt::Features::TEXTURE_INT64_ATOMIC, Tfc::STORAGE_ATOMIC);++ // UNORM gets full filterable+renderable; SNORM splits because+ // `EXT_texture_norm16` marks only UNORM as color-renderable.+ // Storage rides on a separate cap (desktop GL >= 4.2 core / pre-4.2+ // `GL_ARB_shader_image_load_store`, GLES `GL_NV_image_formats`).+ let norm16_unorm = private_caps_fn(+ super::PrivateCapabilities::TEXTURE_FORMAT_NORM16,+ filterable_renderable,+ );+ let norm16_snorm = if self+ .shared+ .private_caps+ .contains(super::PrivateCapabilities::TEXTURE_FORMAT_SNORM16_RENDERABLE)+ {+ norm16_unorm+ } else {+ private_caps_fn(+ super::PrivateCapabilities::TEXTURE_FORMAT_NORM16,+ filterable,+ )+ };+ let norm16_storage = private_caps_fn(+ super::PrivateCapabilities::TEXTURE_FORMAT_NORM16_STORAGE,+ storage,+ );++ match format {+ Tf::R8Unorm => filterable_renderable,+ Tf::R8Snorm => filterable,+ Tf::R8Uint => renderable,+ Tf::R8Sint => renderable,+ Tf::R16Uint => renderable,+ Tf::R16Sint => renderable,+ Tf::R16Unorm => norm16_unorm | norm16_storage,+ Tf::R16Snorm => norm16_snorm | norm16_storage,+ Tf::R16Float => filterable | half_float_renderable,+ Tf::Rg8Unorm => filterable_renderable,+ Tf::Rg8Snorm => filterable,+ Tf::Rg8Uint => renderable,+ Tf::Rg8Sint => renderable,+ Tf::R32Uint => renderable | storage | image_atomic,+ Tf::R32Sint => renderable | storage | image_atomic,+ Tf::R32Float => unfilterable | storage | float_renderable | texture_float_linear,+ Tf::Rg16Uint => renderable,+ Tf::Rg16Sint => renderable,+ Tf::Rg16Unorm => norm16_unorm | norm16_storage,+ Tf::Rg16Snorm => norm16_snorm | norm16_storage,+ Tf::Rg16Float => filterable | half_float_renderable,+ Tf::Rgba8Unorm => filterable_renderable | storage,+ Tf::Rgba8UnormSrgb => filterable_renderable,+ Tf::Bgra8Unorm | Tf::Bgra8UnormSrgb => filterable_renderable,+ Tf::Rgba8Snorm => filterable | storage,+ Tf::Rgba8Uint => renderable | storage,+ Tf::Rgba8Sint => renderable | storage,+ Tf::Rgb10a2Uint => renderable,+ Tf::Rgb10a2Unorm => filterable_renderable,+ Tf::Rg11b10Ufloat => filterable | float_renderable,+ Tf::R64Uint => image_64_atomic,+ Tf::Rg32Uint => renderable,+ Tf::Rg32Sint => renderable,+ Tf::Rg32Float => unfilterable | float_renderable | texture_float_linear,+ Tf::Rgba16Uint => renderable | storage,+ Tf::Rgba16Sint => renderable | storage,+ Tf::Rgba16Unorm => norm16_unorm | norm16_storage,+ Tf::Rgba16Snorm => norm16_snorm | norm16_storage,+ Tf::Rgba16Float => filterable | storage | half_float_renderable,+ Tf::Rgba32Uint => renderable | storage,+ Tf::Rgba32Sint => renderable | storage,+ Tf::Rgba32Float => unfilterable | storage | float_renderable | texture_float_linear,+ Tf::Stencil8+ | Tf::Depth16Unorm+ | Tf::Depth32Float+ | Tf::Depth32FloatStencil8+ | Tf::Depth24Plus+ | Tf::Depth24PlusStencil8 => depth,+ Tf::NV12 => empty,+ Tf::P010 => empty,+ Tf::Rgb9e5Ufloat => filterable,+ Tf::Bc1RgbaUnorm+ | Tf::Bc1RgbaUnormSrgb+ | Tf::Bc2RgbaUnorm+ | Tf::Bc2RgbaUnormSrgb+ | Tf::Bc3RgbaUnorm+ | Tf::Bc3RgbaUnormSrgb+ | Tf::Bc4RUnorm+ | Tf::Bc4RSnorm+ | Tf::Bc5RgUnorm+ | Tf::Bc5RgSnorm+ | Tf::Bc6hRgbFloat+ | Tf::Bc6hRgbUfloat+ | Tf::Bc7RgbaUnorm+ | Tf::Bc7RgbaUnormSrgb => bcn_features,+ Tf::Etc2Rgb8Unorm+ | Tf::Etc2Rgb8UnormSrgb+ | Tf::Etc2Rgb8A1Unorm+ | Tf::Etc2Rgb8A1UnormSrgb+ | Tf::Etc2Rgba8Unorm+ | Tf::Etc2Rgba8UnormSrgb+ | Tf::EacR11Unorm+ | Tf::EacR11Snorm+ | Tf::EacRg11Unorm+ | Tf::EacRg11Snorm => etc2_features,+ Tf::Astc {+ block: _,+ channel: AstcChannel::Unorm | AstcChannel::UnormSrgb,+ } => astc_features,+ Tf::Astc {+ block: _,+ channel: AstcChannel::Hdr,+ } => astc_hdr_features,+ }+ }++ unsafe fn surface_capabilities(+ &self,+ surface: &super::Surface,+ ) -> Option<crate::SurfaceCapabilities> {+ #[cfg(webgl)]+ if self.shared.context.webgl2_context != surface.webgl2_context {+ return None;+ }++ if surface.presentable {+ // There is no extended-range or wide-gamut path in the GLES+ // backend; everything is presented as sRGB.+ let format_caps = |format: wgt::TextureFormat| wgt::SurfaceFormatCapabilities {+ format,+ color_spaces: wgt::SurfaceColorSpaces::SRGB,+ };+ let mut formats = vec![+ format_caps(wgt::TextureFormat::Rgba8Unorm),+ #[cfg(native)]+ format_caps(wgt::TextureFormat::Bgra8Unorm),+ ];+ if surface.supports_srgb() {+ formats.extend([+ format_caps(wgt::TextureFormat::Rgba8UnormSrgb),+ #[cfg(native)]+ format_caps(wgt::TextureFormat::Bgra8UnormSrgb),+ ])+ }+ if self+ .shared+ .private_caps+ .contains(super::PrivateCapabilities::COLOR_BUFFER_HALF_FLOAT)+ {+ formats.push(format_caps(wgt::TextureFormat::Rgba16Float))+ }++ Some(crate::SurfaceCapabilities {+ formats,+ present_modes: if cfg!(windows) {+ vec![wgt::PresentMode::Fifo, wgt::PresentMode::Immediate]+ } else {+ vec![wgt::PresentMode::Fifo] //TODO+ },+ composite_alpha_modes: vec![wgt::CompositeAlphaMode::Opaque], //TODO+ maximum_frame_latency: 2..=2, //TODO, unused currently+ current_extent: None,+ usage: wgt::TextureUses::COLOR_TARGET,+ })+ } else {+ None+ }+ }++ unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {+ wgt::PresentationTimestamp::INVALID_TIMESTAMP+ }++ fn get_ordered_buffer_usages(&self) -> wgt::BufferUses {+ wgt::BufferUses::INCLUSIVE | wgt::BufferUses::MAP_WRITE+ }++ // Don't put barriers between inclusive uses+ fn get_ordered_texture_usages(&self) -> wgt::TextureUses {+ wgt::TextureUses::INCLUSIVE+ | wgt::TextureUses::COLOR_TARGET+ | wgt::TextureUses::DEPTH_STENCIL_WRITE+ }+}++impl super::AdapterShared {+ pub(super) unsafe fn get_buffer_sub_data(+ &self,+ gl: &glow::Context,+ target: u32,+ offset: i32,+ dst_data: &mut [u8],+ ) {+ if self+ .private_caps+ .contains(super::PrivateCapabilities::GET_BUFFER_SUB_DATA)+ {+ unsafe { gl.get_buffer_sub_data(target, offset, dst_data) };+ } else {+ log::error!("Fake map");+ let length = dst_data.len();+ // glMapBufferRange throws an error if length is 0.+ if length != 0 {+ let buffer_mapping =+ unsafe { gl.map_buffer_range(target, offset, length as _, glow::MAP_READ_BIT) };++ unsafe {+ core::ptr::copy_nonoverlapping(buffer_mapping, dst_data.as_mut_ptr(), length)+ };++ unsafe { gl.unmap_buffer(target) };+ }+ }+ }+}++#[cfg(send_sync)]+unsafe impl Sync for super::Adapter {}+#[cfg(send_sync)]+unsafe impl Send for super::Adapter {}++#[cfg(test)]+mod tests {+ use super::super::Adapter;++ #[test]+ fn test_version_parse() {+ Adapter::parse_version("1").unwrap_err();+ Adapter::parse_version("1.").unwrap_err();+ Adapter::parse_version("1 h3l1o. W0rld").unwrap_err();+ Adapter::parse_version("1. h3l1o. W0rld").unwrap_err();+ Adapter::parse_version("1.2.3").unwrap_err();++ assert_eq!(Adapter::parse_version("OpenGL ES 3.1").unwrap(), (3, 1));+ assert_eq!(+ Adapter::parse_version("OpenGL ES 2.0 Google Nexus").unwrap(),+ (2, 0)+ );+ assert_eq!(Adapter::parse_version("GLSL ES 1.1").unwrap(), (1, 1));+ assert_eq!(+ Adapter::parse_version("OpenGL ES GLSL ES 3.20").unwrap(),+ (3, 2)+ );+ assert_eq!(+ // WebGL 2.0 should parse as OpenGL ES 3.0+ Adapter::parse_version("WebGL 2.0 (OpenGL ES 3.0 Chromium)").unwrap(),+ (3, 0)+ );+ assert_eq!(+ Adapter::parse_version("WebGL GLSL ES 3.00 (OpenGL ES GLSL ES 3.0 Chromium)").unwrap(),+ (3, 0)+ );+ }+}diff --git a/vendor/wgpu-hal/src/gles/command.rs b/vendor/wgpu-hal/src/gles/command.rsnew file mode 100644index 00000000..427551ce--- /dev/null+++ b/vendor/wgpu-hal/src/gles/command.rs@@ -0,0 +1,1345 @@+use alloc::string::String;+use core::{mem, ops::Range};++use arrayvec::ArrayVec;++use super::{conv, Command as C};++#[derive(Clone, Copy, Debug, Default)]+struct TextureSlotDesc {+ tex_target: super::BindTarget,+ sampler_index: Option<u8>,+}++pub(super) struct State {+ topology: u32,+ primitive: super::PrimitiveState,+ index_format: wgt::IndexFormat,+ index_offset: wgt::BufferAddress,+ vertex_buffers: [(+ Option<super::VertexBufferDesc>,+ Option<super::BufferBinding>,+ ); crate::MAX_VERTEX_BUFFERS],+ vertex_attributes: ArrayVec<super::AttributeDesc, { super::MAX_VERTEX_ATTRIBUTES }>,+ color_targets: ArrayVec<super::ColorTargetDesc, { crate::MAX_COLOR_ATTACHMENTS }>,+ stencil: super::StencilState,+ depth_bias: wgt::DepthBiasState,+ alpha_to_coverage_enabled: bool,+ samplers: [Option<glow::Sampler>; super::MAX_SAMPLERS],+ texture_slots: [TextureSlotDesc; super::MAX_TEXTURE_SLOTS],+ render_size: wgt::Extent3d,+ resolve_attachments: ArrayVec<(u32, super::TextureView), { crate::MAX_COLOR_ATTACHMENTS }>,+ invalidate_attachments: ArrayVec<u32, { crate::MAX_COLOR_ATTACHMENTS + 2 }>,+ has_pass_label: bool,+ instance_vbuf_mask: usize,+ dirty_vbuf_mask: usize,+ active_first_instance: u32,+ first_instance_location: Option<glow::UniformLocation>,+ immediates_descs: ArrayVec<super::ImmediateDesc, { super::MAX_IMMEDIATES_COMMANDS }>,+ // The current state of the immediate data block.+ current_immediates_data: [u32; super::MAX_IMMEDIATES],+ end_of_pass_timestamp: Option<glow::Query>,+ clip_distance_count: u32,+}++impl Default for State {+ fn default() -> Self {+ Self {+ topology: Default::default(),+ primitive: Default::default(),+ index_format: Default::default(),+ index_offset: Default::default(),+ vertex_buffers: Default::default(),+ vertex_attributes: Default::default(),+ color_targets: Default::default(),+ stencil: Default::default(),+ depth_bias: Default::default(),+ alpha_to_coverage_enabled: Default::default(),+ samplers: Default::default(),+ texture_slots: Default::default(),+ render_size: Default::default(),+ resolve_attachments: Default::default(),+ invalidate_attachments: Default::default(),+ has_pass_label: Default::default(),+ instance_vbuf_mask: Default::default(),+ dirty_vbuf_mask: Default::default(),+ active_first_instance: Default::default(),+ first_instance_location: Default::default(),+ immediates_descs: Default::default(),+ current_immediates_data: [0; super::MAX_IMMEDIATES],+ end_of_pass_timestamp: Default::default(),+ clip_distance_count: Default::default(),+ }+ }+}++impl super::CommandBuffer {+ fn clear(&mut self) {+ self.label = None;+ self.commands.clear();+ self.data_bytes.clear();+ self.queries.clear();+ }++ fn add_marker(&mut self, marker: &str) -> Range<u32> {+ let start = self.data_bytes.len() as u32;+ self.data_bytes.extend(marker.as_bytes());+ start..self.data_bytes.len() as u32+ }++ fn add_immediates_data(&mut self, data: &[u32]) -> Range<u32> {+ let data_raw = bytemuck::cast_slice(data);+ let start = self.data_bytes.len();+ assert!(start < u32::MAX as usize);+ self.data_bytes.extend_from_slice(data_raw);+ let end = self.data_bytes.len();+ assert!(end < u32::MAX as usize);+ (start as u32)..(end as u32)+ }+}++impl Drop for super::CommandEncoder {+ fn drop(&mut self) {+ use crate::CommandEncoder;+ unsafe { self.discard_encoding() }+ self.counters.command_encoders.sub(1);+ }+}++impl super::CommandEncoder {+ fn rebind_stencil_func(&mut self) {+ fn make(s: &super::StencilSide, face: u32) -> C {+ C::SetStencilFunc {+ face,+ function: s.function,+ reference: s.reference,+ read_mask: s.mask_read,+ }+ }++ let s = &self.state.stencil;+ if s.front.function == s.back.function+ && s.front.mask_read == s.back.mask_read+ && s.front.reference == s.back.reference+ {+ self.cmd_buffer+ .commands+ .push(make(&s.front, glow::FRONT_AND_BACK));+ } else {+ self.cmd_buffer.commands.push(make(&s.front, glow::FRONT));+ self.cmd_buffer.commands.push(make(&s.back, glow::BACK));+ }+ }++ fn rebind_vertex_data(&mut self, first_instance: u32) {+ if self+ .private_caps+ .contains(super::PrivateCapabilities::VERTEX_BUFFER_LAYOUT)+ {+ for (index, pair) in self.state.vertex_buffers.iter().enumerate() {+ if self.state.dirty_vbuf_mask & (1 << index) == 0 {+ continue;+ }+ let (buffer_desc, vb) = match *pair {+ (Some(ref vb_desc), Some(ref vb)) => (vb_desc.clone(), vb),+ // Not all dirty bindings are necessarily filled. Some may be unused.+ (_, _) => continue,+ };+ let instance_offset = match buffer_desc.step {+ wgt::VertexStepMode::Vertex => 0,+ wgt::VertexStepMode::Instance => first_instance * buffer_desc.stride,+ };++ self.cmd_buffer.commands.push(C::SetVertexBuffer {+ index: index as u32,+ buffer: super::BufferBinding {+ raw: vb.raw,+ offset: vb.offset + instance_offset as wgt::BufferAddress,+ },+ buffer_desc,+ });+ self.state.dirty_vbuf_mask ^= 1 << index;+ }+ } else {+ let mut vbuf_mask = 0;+ for attribute in self.state.vertex_attributes.iter() {+ if self.state.dirty_vbuf_mask & (1 << attribute.buffer_index) == 0 {+ continue;+ }+ let (buffer_desc, vb) =+ match self.state.vertex_buffers[attribute.buffer_index as usize] {+ (Some(ref vb_desc), Some(ref vb)) => (vb_desc.clone(), vb),+ // Not all dirty bindings are necessarily filled. Some may be unused.+ (_, _) => continue,+ };++ let mut attribute_desc = attribute.clone();+ attribute_desc.offset += vb.offset as u32;+ if buffer_desc.step == wgt::VertexStepMode::Instance {+ attribute_desc.offset += buffer_desc.stride * first_instance;+ }++ self.cmd_buffer.commands.push(C::SetVertexAttribute {+ buffer: Some(vb.raw),+ buffer_desc,+ attribute_desc,+ });+ vbuf_mask |= 1 << attribute.buffer_index;+ }+ self.state.dirty_vbuf_mask ^= vbuf_mask;+ }+ }++ fn rebind_sampler_states(&mut self, dirty_textures: u32, dirty_samplers: u32) {+ for (texture_index, slot) in self.state.texture_slots.iter().enumerate() {+ if dirty_textures & (1 << texture_index) != 0+ || slot+ .sampler_index+ .is_some_and(|si| dirty_samplers & (1 << si) != 0)+ {+ let sampler = slot+ .sampler_index+ .and_then(|si| self.state.samplers[si as usize]);+ self.cmd_buffer+ .commands+ .push(C::BindSampler(texture_index as u32, sampler));+ }+ }+ }++ fn prepare_draw(&mut self, first_instance: u32) {+ // If we support fully featured instancing, we want to bind everything as normal+ // and let the draw call sort it out.+ let emulated_first_instance_value = if self+ .private_caps+ .contains(super::PrivateCapabilities::FULLY_FEATURED_INSTANCING)+ {+ 0+ } else {+ first_instance+ };++ if emulated_first_instance_value != self.state.active_first_instance {+ // rebind all per-instance buffers on first-instance change+ self.state.dirty_vbuf_mask |= self.state.instance_vbuf_mask;+ self.state.active_first_instance = emulated_first_instance_value;+ }+ if self.state.dirty_vbuf_mask != 0 {+ self.rebind_vertex_data(emulated_first_instance_value);+ }+ }++ fn set_pipeline_inner(&mut self, inner: &super::PipelineInner) {+ self.cmd_buffer.commands.push(C::SetProgram(inner.program));++ self.state+ .first_instance_location+ .clone_from(&inner.first_instance_location);+ self.state+ .immediates_descs+ .clone_from(&inner.immediates_descs);++ // rebind textures, if needed+ let mut dirty_textures = 0u32;+ for (texture_index, (slot, &sampler_index)) in self+ .state+ .texture_slots+ .iter_mut()+ .zip(inner.sampler_map.iter())+ .enumerate()+ {+ if slot.sampler_index != sampler_index {+ slot.sampler_index = sampler_index;+ dirty_textures |= 1 << texture_index;+ }+ }+ if dirty_textures != 0 {+ self.rebind_sampler_states(dirty_textures, 0);+ }+ }+}++impl crate::CommandEncoder for super::CommandEncoder {+ type A = super::Api;++ unsafe fn begin_encoding(&mut self, label: crate::Label) -> Result<(), crate::DeviceError> {+ self.state = State::default();+ self.cmd_buffer.label = label.map(String::from);+ Ok(())+ }+ unsafe fn discard_encoding(&mut self) {+ self.cmd_buffer.clear();+ }+ unsafe fn end_encoding(&mut self) -> Result<super::CommandBuffer, crate::DeviceError> {+ Ok(mem::take(&mut self.cmd_buffer))+ }+ unsafe fn reset_all<I>(&mut self, _command_buffers: I) {+ //TODO: could re-use the allocations in all these command buffers+ }++ unsafe fn transition_buffers<'a, T>(&mut self, barriers: T)+ where+ T: Iterator<Item = crate::BufferBarrier<'a, super::Buffer>>,+ {+ if !self+ .private_caps+ .contains(super::PrivateCapabilities::MEMORY_BARRIERS)+ {+ return;+ }+ for bar in barriers {+ // GLES only synchronizes storage -> anything explicitly+ if !bar.usage.from.contains(wgt::BufferUses::STORAGE_READ_WRITE) {+ continue;+ }+ self.cmd_buffer+ .commands+ .push(C::BufferBarrier(bar.buffer.raw.unwrap(), bar.usage.to));+ }+ }++ unsafe fn transition_textures<'a, T>(&mut self, barriers: T)+ where+ T: Iterator<Item = crate::TextureBarrier<'a, super::Texture>>,+ {+ if !self+ .private_caps+ .contains(super::PrivateCapabilities::MEMORY_BARRIERS)+ {+ return;+ }++ let mut combined_usage = wgt::TextureUses::empty();+ for bar in barriers {+ // GLES only synchronizes storage -> anything explicitly+ // if shader writes to a texture then barriers should be placed+ if !bar.usage.from.intersects(+ wgt::TextureUses::STORAGE_READ_WRITE | wgt::TextureUses::STORAGE_WRITE_ONLY,+ ) {+ continue;+ }+ // unlike buffers, there is no need for a concrete texture+ // object to be bound anywhere for a barrier+ combined_usage |= bar.usage.to;+ }++ if !combined_usage.is_empty() {+ self.cmd_buffer+ .commands+ .push(C::TextureBarrier(combined_usage));+ }+ }++ unsafe fn clear_buffer(&mut self, buffer: &super::Buffer, range: crate::MemoryRange) {+ self.cmd_buffer.commands.push(C::ClearBuffer {+ dst: buffer.clone(),+ dst_target: buffer.target,+ range,+ });+ }++ unsafe fn copy_buffer_to_buffer<T>(+ &mut self,+ src: &super::Buffer,+ dst: &super::Buffer,+ regions: T,+ ) where+ T: Iterator<Item = crate::BufferCopy>,+ {+ let (src_target, dst_target) = if src.target == dst.target {+ (glow::COPY_READ_BUFFER, glow::COPY_WRITE_BUFFER)+ } else {+ (src.target, dst.target)+ };+ for copy in regions {+ self.cmd_buffer.commands.push(C::CopyBufferToBuffer {+ src: src.clone(),+ src_target,+ dst: dst.clone(),+ dst_target,+ copy,+ })+ }+ }++ #[cfg(webgl)]+ unsafe fn copy_external_image_to_texture<T>(+ &mut self,+ src: &wgt::CopyExternalImageSourceInfo,+ dst: &super::Texture,+ dst_premultiplication: bool,+ regions: T,+ ) where+ T: Iterator<Item = crate::TextureCopy>,+ {+ let (dst_raw, dst_target) = dst.inner.as_native();+ for copy in regions {+ self.cmd_buffer+ .commands+ .push(C::CopyExternalImageToTexture {+ src: src.clone(),+ dst: dst_raw,+ dst_target,+ dst_format: dst.format,+ dst_premultiplication,+ copy,+ })+ }+ }++ unsafe fn copy_texture_to_texture<T>(+ &mut self,+ src: &super::Texture,+ _src_usage: wgt::TextureUses,+ dst: &super::Texture,+ regions: T,+ ) where+ T: Iterator<Item = crate::TextureCopy>,+ {+ let (src_raw, src_target) = src.inner.as_native();+ let (dst_raw, dst_target) = dst.inner.as_native();+ for mut copy in regions {+ copy.clamp_size_to_virtual(&src.copy_size, &dst.copy_size);+ self.cmd_buffer.commands.push(C::CopyTextureToTexture {+ src: src_raw,+ src_target,+ dst: dst_raw,+ dst_target,+ copy,+ })+ }+ }++ unsafe fn copy_buffer_to_texture<T>(+ &mut self,+ src: &super::Buffer,+ dst: &super::Texture,+ regions: T,+ ) where+ T: Iterator<Item = crate::BufferTextureCopy>,+ {+ let (dst_raw, dst_target) = dst.inner.as_native();++ for mut copy in regions {+ copy.clamp_size_to_virtual(&dst.copy_size);+ self.cmd_buffer.commands.push(C::CopyBufferToTexture {+ src: src.clone(),+ src_target: src.target,+ dst: dst_raw,+ dst_target,+ dst_format: dst.format,+ copy,+ })+ }+ }++ unsafe fn copy_texture_to_buffer<T>(+ &mut self,+ src: &super::Texture,+ _src_usage: wgt::TextureUses,+ dst: &super::Buffer,+ regions: T,+ ) where+ T: Iterator<Item = crate::BufferTextureCopy>,+ {+ let (src_raw, src_target) = src.inner.as_native();+ for mut copy in regions {+ copy.clamp_size_to_virtual(&src.copy_size);+ self.cmd_buffer.commands.push(C::CopyTextureToBuffer {+ src: src_raw,+ src_target,+ src_format: src.format,+ dst: dst.clone(),+ dst_target: dst.target,+ copy,+ })+ }+ }++ unsafe fn begin_query(&mut self, set: &super::QuerySet, index: u32) {+ let query = set.queries[index as usize];+ self.cmd_buffer+ .commands+ .push(C::BeginQuery(query, set.target));+ }+ unsafe fn end_query(&mut self, set: &super::QuerySet, _index: u32) {+ self.cmd_buffer.commands.push(C::EndQuery(set.target));+ }+ unsafe fn write_timestamp(&mut self, set: &super::QuerySet, index: u32) {+ let query = set.queries[index as usize];+ self.cmd_buffer.commands.push(C::TimestampQuery(query));+ }+ unsafe fn reset_queries(&mut self, _set: &super::QuerySet, _range: Range<u32>) {+ //TODO: what do we do here?+ }+ unsafe fn copy_query_results(+ &mut self,+ set: &super::QuerySet,+ range: Range<u32>,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ _stride: wgt::BufferSize,+ ) {+ let start = self.cmd_buffer.queries.len();+ self.cmd_buffer+ .queries+ .extend_from_slice(&set.queries[range.start as usize..range.end as usize]);+ let query_range = start as u32..self.cmd_buffer.queries.len() as u32;+ self.cmd_buffer.commands.push(C::CopyQueryResults {+ query_range,+ dst: buffer.clone(),+ dst_target: buffer.target,+ dst_offset: offset,+ });+ }++ // render++ unsafe fn begin_render_pass(+ &mut self,+ desc: &crate::RenderPassDescriptor<super::QuerySet, super::TextureView>,+ ) -> Result<(), crate::DeviceError> {+ debug_assert!(self.state.end_of_pass_timestamp.is_none());+ if let Some(ref t) = desc.timestamp_writes {+ if let Some(index) = t.beginning_of_pass_write_index {+ unsafe { self.write_timestamp(t.query_set, index) }+ }+ self.state.end_of_pass_timestamp = t+ .end_of_pass_write_index+ .map(|index| t.query_set.queries[index as usize]);+ }++ self.state.render_size = desc.extent;+ self.state.resolve_attachments.clear();+ self.state.invalidate_attachments.clear();+ if let Some(label) = desc.label {+ let range = self.cmd_buffer.add_marker(label);+ self.cmd_buffer.commands.push(C::PushDebugGroup(range));+ self.state.has_pass_label = true;+ }++ let rendering_to_external_framebuffer = desc+ .color_attachments+ .iter()+ .filter_map(|at| at.as_ref())+ .any(|at| match at.target.view.inner {+ #[cfg(webgl)]+ super::TextureInner::ExternalFramebuffer { .. } => true,+ #[cfg(native)]+ super::TextureInner::ExternalNativeFramebuffer { .. } => true,+ _ => false,+ });++ if rendering_to_external_framebuffer && desc.color_attachments.len() != 1 {+ panic!("Multiple render attachments with external framebuffers are not supported.");+ }++ // `COLOR_ATTACHMENT0` to `COLOR_ATTACHMENT31` gives 32 possible color attachments.+ assert!(desc.color_attachments.len() <= 32);++ match desc+ .color_attachments+ .first()+ .filter(|at| at.is_some())+ .and_then(|at| at.as_ref().map(|at| &at.target.view.inner))+ {+ // default framebuffer (provided externally)+ Some(&super::TextureInner::DefaultRenderbuffer) => {+ self.cmd_buffer+ .commands+ .push(C::ResetFramebuffer { is_default: true });+ }+ _ => {+ // set the framebuffer+ self.cmd_buffer+ .commands+ .push(C::ResetFramebuffer { is_default: false });++ for (i, cat) in desc.color_attachments.iter().enumerate() {+ if let Some(cat) = cat.as_ref() {+ let attachment = glow::COLOR_ATTACHMENT0 + i as u32;+ // Try to use the multisampled render-to-texture extension to avoid resolving+ if let Some(ref rat) = cat.resolve_target {+ if matches!(rat.view.inner, super::TextureInner::Texture { .. })+ && self.private_caps.contains(+ super::PrivateCapabilities::MULTISAMPLED_RENDER_TO_TEXTURE,+ )+ && !cat.ops.contains(crate::AttachmentOps::STORE)+ // Extension specifies that only COLOR_ATTACHMENT0 is valid+ && i == 0+ {+ self.cmd_buffer.commands.push(C::BindAttachment {+ attachment,+ view: rat.view.clone(),+ depth_slice: None,+ sample_count: desc.sample_count,+ });+ continue;+ }+ }+ self.cmd_buffer.commands.push(C::BindAttachment {+ attachment,+ view: cat.target.view.clone(),+ depth_slice: cat.depth_slice,+ sample_count: 1,+ });+ if let Some(ref rat) = cat.resolve_target {+ self.state+ .resolve_attachments+ .push((attachment, rat.view.clone()));+ }+ if cat.ops.contains(crate::AttachmentOps::STORE_DISCARD) {+ self.state.invalidate_attachments.push(attachment);+ }+ }+ }+ if let Some(ref dsat) = desc.depth_stencil_attachment {+ let aspects = dsat.target.view.aspects;+ let attachment = match aspects {+ crate::FormatAspects::DEPTH => glow::DEPTH_ATTACHMENT,+ crate::FormatAspects::STENCIL => glow::STENCIL_ATTACHMENT,+ _ => glow::DEPTH_STENCIL_ATTACHMENT,+ };+ self.cmd_buffer.commands.push(C::BindAttachment {+ attachment,+ view: dsat.target.view.clone(),+ depth_slice: None,+ sample_count: 1,+ });+ if aspects.contains(crate::FormatAspects::DEPTH)+ && dsat.depth_ops.contains(crate::AttachmentOps::STORE_DISCARD)+ {+ self.state+ .invalidate_attachments+ .push(glow::DEPTH_ATTACHMENT);+ }+ if aspects.contains(crate::FormatAspects::STENCIL)+ && dsat+ .stencil_ops+ .contains(crate::AttachmentOps::STORE_DISCARD)+ {+ self.state+ .invalidate_attachments+ .push(glow::STENCIL_ATTACHMENT);+ }+ }+ }+ }++ let rect = crate::Rect {+ x: 0,+ y: 0,+ w: desc.extent.width as i32,+ h: desc.extent.height as i32,+ };+ self.cmd_buffer.commands.push(C::SetScissor(rect.clone()));+ self.cmd_buffer.commands.push(C::SetViewport {+ rect,+ depth: 0.0..1.0,+ });++ if !rendering_to_external_framebuffer {+ // set the draw buffers and states+ self.cmd_buffer+ .commands+ .push(C::SetDrawColorBuffers(desc.color_attachments.len() as u8));+ }++ // issue the clears+ for (i, cat) in desc+ .color_attachments+ .iter()+ .filter_map(|at| at.as_ref())+ .enumerate()+ {+ if cat.ops.contains(crate::AttachmentOps::LOAD_CLEAR) {+ let c = &cat.clear_value;+ self.cmd_buffer.commands.push(+ match cat.target.view.format.sample_type(None, None).unwrap() {+ wgt::TextureSampleType::Float { .. } => C::ClearColorF {+ draw_buffer: i as u32,+ color: [c.r as f32, c.g as f32, c.b as f32, c.a as f32],+ is_srgb: cat.target.view.format.is_srgb(),+ },+ wgt::TextureSampleType::Uint => C::ClearColorU(+ i as u32,+ [c.r as u32, c.g as u32, c.b as u32, c.a as u32],+ ),+ wgt::TextureSampleType::Sint => C::ClearColorI(+ i as u32,+ [c.r as i32, c.g as i32, c.b as i32, c.a as i32],+ ),+ wgt::TextureSampleType::Depth => unreachable!(),+ },+ );+ }+ }++ if let Some(ref dsat) = desc.depth_stencil_attachment {+ let clear_depth = dsat.depth_ops.contains(crate::AttachmentOps::LOAD_CLEAR);+ let clear_stencil = dsat.stencil_ops.contains(crate::AttachmentOps::LOAD_CLEAR);++ if clear_depth && clear_stencil {+ self.cmd_buffer.commands.push(C::ClearDepthAndStencil(+ dsat.clear_value.0,+ dsat.clear_value.1,+ ));+ } else if clear_depth {+ self.cmd_buffer+ .commands+ .push(C::ClearDepth(dsat.clear_value.0));+ } else if clear_stencil {+ self.cmd_buffer+ .commands+ .push(C::ClearStencil(dsat.clear_value.1));+ }+ }+ Ok(())+ }+ unsafe fn end_render_pass(&mut self) {+ for (attachment, dst) in self.state.resolve_attachments.drain(..) {+ self.cmd_buffer.commands.push(C::ResolveAttachment {+ attachment,+ dst,+ size: self.state.render_size,+ });+ }+ if !self.state.invalidate_attachments.is_empty() {+ self.cmd_buffer.commands.push(C::InvalidateAttachments(+ self.state.invalidate_attachments.clone(),+ ));+ self.state.invalidate_attachments.clear();+ }+ if self.state.has_pass_label {+ self.cmd_buffer.commands.push(C::PopDebugGroup);+ self.state.has_pass_label = false;+ }+ self.state.instance_vbuf_mask = 0;+ self.state.dirty_vbuf_mask = 0;+ self.state.active_first_instance = 0;+ self.state.color_targets.clear();+ for vat in &self.state.vertex_attributes {+ self.cmd_buffer+ .commands+ .push(C::UnsetVertexAttribute(vat.location));+ }+ self.state.vertex_attributes.clear();+ self.state.primitive = super::PrimitiveState::default();++ if let Some(query) = self.state.end_of_pass_timestamp.take() {+ self.cmd_buffer.commands.push(C::TimestampQuery(query));+ }+ }++ unsafe fn set_bind_group(+ &mut self,+ layout: &super::PipelineLayout,+ index: u32,+ group: &super::BindGroup,+ dynamic_offsets: &[wgt::DynamicOffset],+ ) {+ let mut do_index = 0;+ let mut dirty_textures = 0u32;+ let mut dirty_samplers = 0u32;+ let group_info = layout.group_infos[index as usize].as_ref().unwrap();++ for (binding_layout, raw_binding) in group_info.entries.iter().zip(group.contents.iter()) {+ let slot = group_info.binding_to_slot[binding_layout.binding as usize] as u32;+ match *raw_binding {+ super::RawBinding::Buffer {+ raw,+ offset: base_offset,+ size,+ } => {+ let mut offset = base_offset;+ let target = match binding_layout.ty {+ wgt::BindingType::Buffer {+ ty,+ has_dynamic_offset,+ min_binding_size: _,+ } => {+ if has_dynamic_offset {+ offset += dynamic_offsets[do_index] as i32;+ do_index += 1;+ }+ match ty {+ wgt::BufferBindingType::Uniform => glow::UNIFORM_BUFFER,+ wgt::BufferBindingType::Storage { .. } => {+ glow::SHADER_STORAGE_BUFFER+ }+ }+ }+ _ => unreachable!(),+ };+ self.cmd_buffer.commands.push(C::BindBuffer {+ target,+ slot,+ buffer: raw,+ offset,+ size,+ });+ }+ super::RawBinding::Sampler(sampler) => {+ dirty_samplers |= 1 << slot;+ self.state.samplers[slot as usize] = Some(sampler);+ }+ super::RawBinding::Texture {+ raw,+ target,+ aspects,+ ref mip_levels,+ } => {+ dirty_textures |= 1 << slot;+ self.state.texture_slots[slot as usize].tex_target = target;+ self.cmd_buffer.commands.push(C::BindTexture {+ slot,+ texture: raw,+ target,+ aspects,+ mip_levels: mip_levels.clone(),+ });+ }+ super::RawBinding::Image(ref binding) => {+ self.cmd_buffer.commands.push(C::BindImage {+ slot,+ binding: binding.clone(),+ });+ }+ }+ }++ self.rebind_sampler_states(dirty_textures, dirty_samplers);+ }++ unsafe fn set_immediates(+ &mut self,+ _layout: &super::PipelineLayout,+ offset_bytes: u32,+ data: &[u32],+ ) {+ // There is nothing preventing the user from trying to update a single value within+ // a vector or matrix in the set_immediates call, as to the user, all of this is+ // just memory. However OpenGL does not allow partial uniform updates.+ //+ // As such, we locally keep a copy of the current state of the immediate data memory+ // block. If the user tries to update a single value, we have the data to update the entirety+ // of the uniform.+ let start_words = offset_bytes / 4;+ let end_words = start_words + data.len() as u32;+ self.state.current_immediates_data[start_words as usize..end_words as usize]+ .copy_from_slice(data);++ // We iterate over the uniform list as there may be multiple uniforms that need+ // updating from the same immediate data memory (one for each shader stage).+ //+ // Additionally, any statically unused uniform descs will have been removed from this list+ // by OpenGL, so the uniform list is not contiguous.+ for uniform in self.state.immediates_descs.iter().cloned() {+ let uniform_size_words = uniform.size_bytes / 4;+ let uniform_start_words = uniform.offset / 4;+ let uniform_end_words = uniform_start_words + uniform_size_words;++ // Is true if any word within the uniform binding was updated+ let needs_updating =+ start_words < uniform_end_words || uniform_start_words <= end_words;++ if needs_updating {+ let uniform_data = &self.state.current_immediates_data+ [uniform_start_words as usize..uniform_end_words as usize];++ let range = self.cmd_buffer.add_immediates_data(uniform_data);++ self.cmd_buffer.commands.push(C::SetImmediates {+ uniform,+ offset: range.start,+ });+ }+ }+ }++ unsafe fn insert_debug_marker(&mut self, label: &str) {+ let range = self.cmd_buffer.add_marker(label);+ self.cmd_buffer.commands.push(C::InsertDebugMarker(range));+ }+ unsafe fn begin_debug_marker(&mut self, group_label: &str) {+ let range = self.cmd_buffer.add_marker(group_label);+ self.cmd_buffer.commands.push(C::PushDebugGroup(range));+ }+ unsafe fn end_debug_marker(&mut self) {+ self.cmd_buffer.commands.push(C::PopDebugGroup);+ }++ unsafe fn set_render_pipeline(&mut self, pipeline: &super::RenderPipeline) {+ self.state.topology = conv::map_primitive_topology(pipeline.primitive.topology);++ if self+ .private_caps+ .contains(super::PrivateCapabilities::VERTEX_BUFFER_LAYOUT)+ {+ for vat in pipeline.vertex_attributes.iter() {+ let vb = pipeline.vertex_buffers[vat.buffer_index as usize]+ .as_ref()+ .unwrap();+ // set the layout+ self.cmd_buffer.commands.push(C::SetVertexAttribute {+ buffer: None,+ buffer_desc: vb.clone(),+ attribute_desc: vat.clone(),+ });+ }+ } else {+ for vat in &self.state.vertex_attributes {+ self.cmd_buffer+ .commands+ .push(C::UnsetVertexAttribute(vat.location));+ }+ self.state.vertex_attributes.clear();++ self.state.dirty_vbuf_mask = 0;+ // copy vertex attributes+ for vat in pipeline.vertex_attributes.iter() {+ //Note: we can invalidate more carefully here.+ self.state.dirty_vbuf_mask |= 1 << vat.buffer_index;+ self.state.vertex_attributes.push(vat.clone());+ }+ }++ self.state.instance_vbuf_mask = 0;+ // copy vertex state+ for (index, (&mut (ref mut state_desc, _), pipe_desc)) in self+ .state+ .vertex_buffers+ .iter_mut()+ .zip(pipeline.vertex_buffers.iter())+ .enumerate()+ {+ let Some(pipe_desc) = pipe_desc else {+ continue;+ };+ if pipe_desc.step == wgt::VertexStepMode::Instance {+ self.state.instance_vbuf_mask |= 1 << index;+ }+ if state_desc.as_ref() != Some(pipe_desc) {+ self.state.dirty_vbuf_mask |= 1 << index;+ *state_desc = Some(pipe_desc.clone());+ }+ }++ self.set_pipeline_inner(&pipeline.inner);++ // set primitive state+ let prim_state = conv::map_primitive_state(&pipeline.primitive);+ if prim_state != self.state.primitive {+ self.cmd_buffer+ .commands+ .push(C::SetPrimitive(prim_state.clone()));+ self.state.primitive = prim_state;+ }++ // set depth/stencil states+ let mut aspects = crate::FormatAspects::empty();+ if pipeline.depth_bias != self.state.depth_bias {+ self.state.depth_bias = pipeline.depth_bias;+ self.cmd_buffer+ .commands+ .push(C::SetDepthBias(pipeline.depth_bias));+ }+ if let Some(ref depth) = pipeline.depth {+ aspects |= crate::FormatAspects::DEPTH;+ self.cmd_buffer.commands.push(C::SetDepth(depth.clone()));+ }+ if let Some(ref stencil) = pipeline.stencil {+ aspects |= crate::FormatAspects::STENCIL;+ self.state.stencil = stencil.clone();+ self.rebind_stencil_func();+ if stencil.front.ops == stencil.back.ops+ && stencil.front.mask_write == stencil.back.mask_write+ {+ self.cmd_buffer.commands.push(C::SetStencilOps {+ face: glow::FRONT_AND_BACK,+ write_mask: stencil.front.mask_write,+ ops: stencil.front.ops.clone(),+ });+ } else {+ self.cmd_buffer.commands.push(C::SetStencilOps {+ face: glow::FRONT,+ write_mask: stencil.front.mask_write,+ ops: stencil.front.ops.clone(),+ });+ self.cmd_buffer.commands.push(C::SetStencilOps {+ face: glow::BACK,+ write_mask: stencil.back.mask_write,+ ops: stencil.back.ops.clone(),+ });+ }+ }+ self.cmd_buffer+ .commands+ .push(C::ConfigureDepthStencil(aspects));++ // set multisampling state+ if pipeline.alpha_to_coverage_enabled != self.state.alpha_to_coverage_enabled {+ self.state.alpha_to_coverage_enabled = pipeline.alpha_to_coverage_enabled;+ self.cmd_buffer+ .commands+ .push(C::SetAlphaToCoverage(pipeline.alpha_to_coverage_enabled));+ }++ // set blend states+ if self.state.color_targets[..] != pipeline.color_targets[..] {+ if pipeline+ .color_targets+ .iter()+ .skip(1)+ .any(|ct| *ct != pipeline.color_targets[0])+ {+ for (index, ct) in pipeline.color_targets.iter().enumerate() {+ self.cmd_buffer.commands.push(C::SetColorTarget {+ draw_buffer_index: Some(index as u32),+ desc: ct.clone(),+ });+ }+ } else {+ self.cmd_buffer.commands.push(C::SetColorTarget {+ draw_buffer_index: None,+ desc: pipeline.color_targets.first().cloned().unwrap_or_default(),+ });+ }+ }+ self.state.color_targets.clear();+ for ct in pipeline.color_targets.iter() {+ self.state.color_targets.push(ct.clone());+ }++ // set clip plane count+ if pipeline.inner.clip_distance_count != self.state.clip_distance_count {+ self.cmd_buffer.commands.push(C::SetClipDistances {+ old_count: self.state.clip_distance_count,+ new_count: pipeline.inner.clip_distance_count,+ });+ self.state.clip_distance_count = pipeline.inner.clip_distance_count;+ }+ }++ unsafe fn set_index_buffer<'a>(+ &mut self,+ binding: crate::BufferBinding<'a, super::Buffer>,+ format: wgt::IndexFormat,+ ) {+ self.state.index_offset = binding.offset;+ self.state.index_format = format;+ self.cmd_buffer+ .commands+ .push(C::SetIndexBuffer(binding.buffer.raw.unwrap()));+ }+ unsafe fn set_vertex_buffer<'a>(+ &mut self,+ index: u32,+ binding: crate::BufferBinding<'a, super::Buffer>,+ ) {+ self.state.dirty_vbuf_mask |= 1 << index;+ let (_, ref mut vb) = self.state.vertex_buffers[index as usize];+ *vb = Some(super::BufferBinding {+ raw: binding.buffer.raw.unwrap(),+ offset: binding.offset,+ });+ }+ unsafe fn set_viewport(&mut self, rect: &crate::Rect<f32>, depth: Range<f32>) {+ self.cmd_buffer.commands.push(C::SetViewport {+ rect: crate::Rect {+ x: rect.x as i32,+ y: rect.y as i32,+ w: rect.w as i32,+ h: rect.h as i32,+ },+ depth,+ });+ }+ unsafe fn set_scissor_rect(&mut self, rect: &crate::Rect<u32>) {+ self.cmd_buffer.commands.push(C::SetScissor(crate::Rect {+ x: rect.x as i32,+ y: rect.y as i32,+ w: rect.w as i32,+ h: rect.h as i32,+ }));+ }+ unsafe fn set_stencil_reference(&mut self, value: u32) {+ self.state.stencil.front.reference = value;+ self.state.stencil.back.reference = value;+ self.rebind_stencil_func();+ }+ unsafe fn set_blend_constants(&mut self, color: &[f32; 4]) {+ self.cmd_buffer.commands.push(C::SetBlendConstant(*color));+ }++ unsafe fn draw(+ &mut self,+ first_vertex: u32,+ vertex_count: u32,+ first_instance: u32,+ instance_count: u32,+ ) {+ self.prepare_draw(first_instance);+ #[allow(+ clippy::clone_on_copy,+ reason = "False positive when cloning glow::UniformLocation"+ )]+ self.cmd_buffer.commands.push(C::Draw {+ topology: self.state.topology,+ first_vertex,+ vertex_count,+ first_instance,+ instance_count,+ first_instance_location: self.state.first_instance_location.clone(),+ });+ }+ unsafe fn draw_indexed(+ &mut self,+ first_index: u32,+ index_count: u32,+ base_vertex: i32,+ first_instance: u32,+ instance_count: u32,+ ) {+ self.prepare_draw(first_instance);+ let (index_size, index_type) = match self.state.index_format {+ wgt::IndexFormat::Uint16 => (2, glow::UNSIGNED_SHORT),+ wgt::IndexFormat::Uint32 => (4, glow::UNSIGNED_INT),+ };+ let index_offset = self.state.index_offset + index_size * first_index as wgt::BufferAddress;+ #[allow(+ clippy::clone_on_copy,+ reason = "False positive when cloning glow::UniformLocation"+ )]+ self.cmd_buffer.commands.push(C::DrawIndexed {+ topology: self.state.topology,+ index_type,+ index_offset,+ index_count,+ base_vertex,+ first_instance,+ instance_count,+ first_instance_location: self.state.first_instance_location.clone(),+ });+ }+ unsafe fn draw_mesh_tasks(+ &mut self,+ _group_count_x: u32,+ _group_count_y: u32,+ _group_count_z: u32,+ ) {+ unreachable!()+ }+ unsafe fn draw_indirect(+ &mut self,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ ) {+ self.prepare_draw(0);+ for draw in 0..draw_count as wgt::BufferAddress {+ let indirect_offset =+ offset + draw * size_of::<wgt::DrawIndirectArgs>() as wgt::BufferAddress;+ #[allow(+ clippy::clone_on_copy,+ reason = "False positive when cloning glow::UniformLocation"+ )]+ self.cmd_buffer.commands.push(C::DrawIndirect {+ topology: self.state.topology,+ indirect_buf: buffer.raw.unwrap(),+ indirect_offset,+ first_instance_location: self.state.first_instance_location.clone(),+ });+ }+ }+ unsafe fn draw_indexed_indirect(+ &mut self,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ ) {+ self.prepare_draw(0);+ let index_type = match self.state.index_format {+ wgt::IndexFormat::Uint16 => glow::UNSIGNED_SHORT,+ wgt::IndexFormat::Uint32 => glow::UNSIGNED_INT,+ };+ for draw in 0..draw_count as wgt::BufferAddress {+ let indirect_offset =+ offset + draw * size_of::<wgt::DrawIndexedIndirectArgs>() as wgt::BufferAddress;+ #[allow(+ clippy::clone_on_copy,+ reason = "False positive when cloning glow::UniformLocation"+ )]+ self.cmd_buffer.commands.push(C::DrawIndexedIndirect {+ topology: self.state.topology,+ index_type,+ indirect_buf: buffer.raw.unwrap(),+ indirect_offset,+ first_instance_location: self.state.first_instance_location.clone(),+ });+ }+ }+ unsafe fn draw_mesh_tasks_indirect(+ &mut self,+ _buffer: &<Self::A as crate::Api>::Buffer,+ _offset: wgt::BufferAddress,+ _draw_count: u32,+ ) {+ unreachable!()+ }+ unsafe fn draw_indirect_count(+ &mut self,+ _buffer: &super::Buffer,+ _offset: wgt::BufferAddress,+ _count_buffer: &super::Buffer,+ _count_offset: wgt::BufferAddress,+ _max_count: u32,+ ) {+ unreachable!()+ }+ unsafe fn draw_indexed_indirect_count(+ &mut self,+ _buffer: &super::Buffer,+ _offset: wgt::BufferAddress,+ _count_buffer: &super::Buffer,+ _count_offset: wgt::BufferAddress,+ _max_count: u32,+ ) {+ unreachable!()+ }+ unsafe fn draw_mesh_tasks_indirect_count(+ &mut self,+ _buffer: &<Self::A as crate::Api>::Buffer,+ _offset: wgt::BufferAddress,+ _count_buffer: &<Self::A as crate::Api>::Buffer,+ _count_offset: wgt::BufferAddress,+ _max_count: u32,+ ) {+ unreachable!()+ }++ // compute++ unsafe fn begin_compute_pass(&mut self, desc: &crate::ComputePassDescriptor<super::QuerySet>) {+ debug_assert!(self.state.end_of_pass_timestamp.is_none());+ if let Some(ref t) = desc.timestamp_writes {+ if let Some(index) = t.beginning_of_pass_write_index {+ unsafe { self.write_timestamp(t.query_set, index) }+ }+ self.state.end_of_pass_timestamp = t+ .end_of_pass_write_index+ .map(|index| t.query_set.queries[index as usize]);+ }++ if let Some(label) = desc.label {+ let range = self.cmd_buffer.add_marker(label);+ self.cmd_buffer.commands.push(C::PushDebugGroup(range));+ self.state.has_pass_label = true;+ }+ }+ unsafe fn end_compute_pass(&mut self) {+ if self.state.has_pass_label {+ self.cmd_buffer.commands.push(C::PopDebugGroup);+ self.state.has_pass_label = false;+ }++ if let Some(query) = self.state.end_of_pass_timestamp.take() {+ self.cmd_buffer.commands.push(C::TimestampQuery(query));+ }+ }++ unsafe fn set_compute_pipeline(&mut self, pipeline: &super::ComputePipeline) {+ self.set_pipeline_inner(&pipeline.inner);+ }++ unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]) {+ // Empty dispatches are invalid in OpenGL, but valid in WebGPU.+ if count.contains(&0) {+ return;+ }+ self.cmd_buffer.commands.push(C::Dispatch(count));+ }+ unsafe fn dispatch_workgroups_indirect(+ &mut self,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ ) {+ self.cmd_buffer.commands.push(C::DispatchIndirect {+ indirect_buf: buffer.raw.unwrap(),+ indirect_offset: offset,+ });+ }++ unsafe fn build_acceleration_structures<'a, T>(+ &mut self,+ _descriptor_count: u32,+ _descriptors: T,+ ) where+ super::Api: 'a,+ T: IntoIterator<+ Item = crate::BuildAccelerationStructureDescriptor<+ 'a,+ super::Buffer,+ super::AccelerationStructure,+ >,+ >,+ {+ unimplemented!()+ }++ unsafe fn place_acceleration_structure_barrier(+ &mut self,+ _barriers: crate::AccelerationStructureBarrier,+ ) {+ unimplemented!()+ }++ unsafe fn copy_acceleration_structure_to_acceleration_structure(+ &mut self,+ _src: &super::AccelerationStructure,+ _dst: &super::AccelerationStructure,+ _copy: wgt::AccelerationStructureCopy,+ ) {+ unimplemented!()+ }++ unsafe fn read_acceleration_structure_compact_size(+ &mut self,+ _acceleration_structure: &super::AccelerationStructure,+ _buf: &super::Buffer,+ ) {+ unimplemented!()+ }++ unsafe fn set_acceleration_structure_dependencies(+ _command_buffers: &[&super::CommandBuffer],+ _dependencies: &[&super::AccelerationStructure],+ ) {+ unimplemented!()+ }++ unsafe fn begin_ray_tracing_pass(&mut self, _desc: &crate::RayTracingPassDescriptor) {+ unimplemented!()+ }++ unsafe fn end_ray_tracing_pass(&mut self) {+ unimplemented!()+ }++ unsafe fn set_ray_tracing_pipeline(+ &mut self,+ _pipeline: &<Self::A as crate::Api>::RayTracingPipeline,+ ) {+ unimplemented!()+ }++ unsafe fn trace_rays(+ &mut self,+ _count: [u32; 3],+ _ray_generation_group_data: crate::PipelineGroupData<super::Buffer>,+ _miss_group_data: crate::PipelineGroupData<super::Buffer>,+ _intersection_group_data: crate::PipelineGroupData<super::Buffer>,+ ) {+ unimplemented!()+ }+}diff --git a/vendor/wgpu-hal/src/gles/conv.rs b/vendor/wgpu-hal/src/gles/conv.rsnew file mode 100644index 00000000..5b54f8f1--- /dev/null+++ b/vendor/wgpu-hal/src/gles/conv.rs@@ -0,0 +1,430 @@+impl super::AdapterShared {+ pub(super) fn describe_texture_format(+ &self,+ texture_format: wgt::TextureFormat,+ ) -> super::TextureFormatDesc {+ use wgt::TextureFormat as Tf;+ use wgt::{AstcBlock, AstcChannel};++ let (internal, external, data_type) = match texture_format {+ Tf::R8Unorm => (glow::R8, glow::RED, glow::UNSIGNED_BYTE),+ Tf::R8Snorm => (glow::R8_SNORM, glow::RED, glow::BYTE),+ Tf::R8Uint => (glow::R8UI, glow::RED_INTEGER, glow::UNSIGNED_BYTE),+ Tf::R8Sint => (glow::R8I, glow::RED_INTEGER, glow::BYTE),+ Tf::R16Uint => (glow::R16UI, glow::RED_INTEGER, glow::UNSIGNED_SHORT),+ Tf::R16Sint => (glow::R16I, glow::RED_INTEGER, glow::SHORT),+ Tf::R16Unorm => (glow::R16, glow::RED, glow::UNSIGNED_SHORT),+ Tf::R16Snorm => (glow::R16_SNORM, glow::RED, glow::SHORT),+ Tf::R16Float => (glow::R16F, glow::RED, glow::HALF_FLOAT),+ Tf::Rg8Unorm => (glow::RG8, glow::RG, glow::UNSIGNED_BYTE),+ Tf::Rg8Snorm => (glow::RG8_SNORM, glow::RG, glow::BYTE),+ Tf::Rg8Uint => (glow::RG8UI, glow::RG_INTEGER, glow::UNSIGNED_BYTE),+ Tf::Rg8Sint => (glow::RG8I, glow::RG_INTEGER, glow::BYTE),+ Tf::R32Uint => (glow::R32UI, glow::RED_INTEGER, glow::UNSIGNED_INT),+ Tf::R32Sint => (glow::R32I, glow::RED_INTEGER, glow::INT),+ Tf::R32Float => (glow::R32F, glow::RED, glow::FLOAT),+ Tf::Rg16Uint => (glow::RG16UI, glow::RG_INTEGER, glow::UNSIGNED_SHORT),+ Tf::Rg16Sint => (glow::RG16I, glow::RG_INTEGER, glow::SHORT),+ Tf::Rg16Unorm => (glow::RG16, glow::RG, glow::UNSIGNED_SHORT),+ Tf::Rg16Snorm => (glow::RG16_SNORM, glow::RG, glow::SHORT),+ Tf::Rg16Float => (glow::RG16F, glow::RG, glow::HALF_FLOAT),+ Tf::Rgba8Unorm => (glow::RGBA8, glow::RGBA, glow::UNSIGNED_BYTE),+ Tf::Rgba8UnormSrgb => (glow::SRGB8_ALPHA8, glow::RGBA, glow::UNSIGNED_BYTE),+ Tf::Bgra8UnormSrgb => (glow::SRGB8_ALPHA8, glow::BGRA, glow::UNSIGNED_BYTE), //TODO?+ Tf::Rgba8Snorm => (glow::RGBA8_SNORM, glow::RGBA, glow::BYTE),+ Tf::Bgra8Unorm => (glow::RGBA8, glow::BGRA, glow::UNSIGNED_BYTE), //TODO?+ Tf::Rgba8Uint => (glow::RGBA8UI, glow::RGBA_INTEGER, glow::UNSIGNED_BYTE),+ Tf::Rgba8Sint => (glow::RGBA8I, glow::RGBA_INTEGER, glow::BYTE),+ Tf::Rgb10a2Uint => (+ glow::RGB10_A2UI,+ glow::RGBA_INTEGER,+ glow::UNSIGNED_INT_2_10_10_10_REV,+ ),+ Tf::Rgb10a2Unorm => (+ glow::RGB10_A2,+ glow::RGBA,+ glow::UNSIGNED_INT_2_10_10_10_REV,+ ),+ Tf::Rg11b10Ufloat => (+ glow::R11F_G11F_B10F,+ glow::RGB,+ glow::UNSIGNED_INT_10F_11F_11F_REV,+ ),+ Tf::R64Uint => (glow::RG32UI, glow::RED_INTEGER, glow::UNSIGNED_INT),+ Tf::Rg32Uint => (glow::RG32UI, glow::RG_INTEGER, glow::UNSIGNED_INT),+ Tf::Rg32Sint => (glow::RG32I, glow::RG_INTEGER, glow::INT),+ Tf::Rg32Float => (glow::RG32F, glow::RG, glow::FLOAT),+ Tf::Rgba16Uint => (glow::RGBA16UI, glow::RGBA_INTEGER, glow::UNSIGNED_SHORT),+ Tf::Rgba16Sint => (glow::RGBA16I, glow::RGBA_INTEGER, glow::SHORT),+ Tf::Rgba16Unorm => (glow::RGBA16, glow::RGBA, glow::UNSIGNED_SHORT),+ Tf::Rgba16Snorm => (glow::RGBA16_SNORM, glow::RGBA, glow::SHORT),+ Tf::Rgba16Float => (glow::RGBA16F, glow::RGBA, glow::HALF_FLOAT),+ Tf::Rgba32Uint => (glow::RGBA32UI, glow::RGBA_INTEGER, glow::UNSIGNED_INT),+ Tf::Rgba32Sint => (glow::RGBA32I, glow::RGBA_INTEGER, glow::INT),+ Tf::Rgba32Float => (glow::RGBA32F, glow::RGBA, glow::FLOAT),+ Tf::Stencil8 => (+ glow::STENCIL_INDEX8,+ glow::STENCIL_INDEX,+ glow::UNSIGNED_BYTE,+ ),+ Tf::Depth16Unorm => (+ glow::DEPTH_COMPONENT16,+ glow::DEPTH_COMPONENT,+ glow::UNSIGNED_SHORT,+ ),+ Tf::Depth32Float => (glow::DEPTH_COMPONENT32F, glow::DEPTH_COMPONENT, glow::FLOAT),+ Tf::Depth32FloatStencil8 => (+ glow::DEPTH32F_STENCIL8,+ glow::DEPTH_STENCIL,+ glow::FLOAT_32_UNSIGNED_INT_24_8_REV,+ ),+ Tf::Depth24Plus => (+ glow::DEPTH_COMPONENT24,+ glow::DEPTH_COMPONENT,+ glow::UNSIGNED_INT,+ ),+ Tf::Depth24PlusStencil8 => (+ glow::DEPTH24_STENCIL8,+ glow::DEPTH_STENCIL,+ glow::UNSIGNED_INT_24_8,+ ),+ Tf::NV12 => unreachable!(),+ Tf::P010 => unreachable!(),+ Tf::Rgb9e5Ufloat => (glow::RGB9_E5, glow::RGB, glow::UNSIGNED_INT_5_9_9_9_REV),+ Tf::Bc1RgbaUnorm => (glow::COMPRESSED_RGBA_S3TC_DXT1_EXT, glow::RGBA, 0),+ Tf::Bc1RgbaUnormSrgb => (glow::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, glow::RGBA, 0),+ Tf::Bc2RgbaUnorm => (glow::COMPRESSED_RGBA_S3TC_DXT3_EXT, glow::RGBA, 0),+ Tf::Bc2RgbaUnormSrgb => (glow::COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, glow::RGBA, 0),+ Tf::Bc3RgbaUnorm => (glow::COMPRESSED_RGBA_S3TC_DXT5_EXT, glow::RGBA, 0),+ Tf::Bc3RgbaUnormSrgb => (glow::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, glow::RGBA, 0),+ Tf::Bc4RUnorm => (glow::COMPRESSED_RED_RGTC1, glow::RED, 0),+ Tf::Bc4RSnorm => (glow::COMPRESSED_SIGNED_RED_RGTC1, glow::RED, 0),+ Tf::Bc5RgUnorm => (glow::COMPRESSED_RG_RGTC2, glow::RG, 0),+ Tf::Bc5RgSnorm => (glow::COMPRESSED_SIGNED_RG_RGTC2, glow::RG, 0),+ Tf::Bc6hRgbUfloat => (glow::COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT, glow::RGB, 0),+ Tf::Bc6hRgbFloat => (glow::COMPRESSED_RGB_BPTC_SIGNED_FLOAT, glow::RGB, 0),+ Tf::Bc7RgbaUnorm => (glow::COMPRESSED_RGBA_BPTC_UNORM, glow::RGBA, 0),+ Tf::Bc7RgbaUnormSrgb => (glow::COMPRESSED_SRGB_ALPHA_BPTC_UNORM, glow::RGBA, 0),+ Tf::Etc2Rgb8Unorm => (glow::COMPRESSED_RGB8_ETC2, glow::RGB, 0),+ Tf::Etc2Rgb8UnormSrgb => (glow::COMPRESSED_SRGB8_ETC2, glow::RGB, 0),+ Tf::Etc2Rgb8A1Unorm => (+ glow::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,+ glow::RGBA,+ 0,+ ),+ Tf::Etc2Rgb8A1UnormSrgb => (+ glow::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,+ glow::RGBA,+ 0,+ ),+ Tf::Etc2Rgba8Unorm => (glow::COMPRESSED_RGBA8_ETC2_EAC, glow::RGBA, 0),+ Tf::Etc2Rgba8UnormSrgb => (glow::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, glow::RGBA, 0),+ Tf::EacR11Unorm => (glow::COMPRESSED_R11_EAC, glow::RED, 0),+ Tf::EacR11Snorm => (glow::COMPRESSED_SIGNED_R11_EAC, glow::RED, 0),+ Tf::EacRg11Unorm => (glow::COMPRESSED_RG11_EAC, glow::RG, 0),+ Tf::EacRg11Snorm => (glow::COMPRESSED_SIGNED_RG11_EAC, glow::RG, 0),+ Tf::Astc { block, channel } => match channel {+ AstcChannel::Unorm | AstcChannel::Hdr => match block {+ AstcBlock::B4x4 => (glow::COMPRESSED_RGBA_ASTC_4x4_KHR, glow::RGBA, 0),+ AstcBlock::B5x4 => (glow::COMPRESSED_RGBA_ASTC_5x4_KHR, glow::RGBA, 0),+ AstcBlock::B5x5 => (glow::COMPRESSED_RGBA_ASTC_5x5_KHR, glow::RGBA, 0),+ AstcBlock::B6x5 => (glow::COMPRESSED_RGBA_ASTC_6x5_KHR, glow::RGBA, 0),+ AstcBlock::B6x6 => (glow::COMPRESSED_RGBA_ASTC_6x6_KHR, glow::RGBA, 0),+ AstcBlock::B8x5 => (glow::COMPRESSED_RGBA_ASTC_8x5_KHR, glow::RGBA, 0),+ AstcBlock::B8x6 => (glow::COMPRESSED_RGBA_ASTC_8x6_KHR, glow::RGBA, 0),+ AstcBlock::B8x8 => (glow::COMPRESSED_RGBA_ASTC_8x8_KHR, glow::RGBA, 0),+ AstcBlock::B10x5 => (glow::COMPRESSED_RGBA_ASTC_10x5_KHR, glow::RGBA, 0),+ AstcBlock::B10x6 => (glow::COMPRESSED_RGBA_ASTC_10x6_KHR, glow::RGBA, 0),+ AstcBlock::B10x8 => (glow::COMPRESSED_RGBA_ASTC_10x8_KHR, glow::RGBA, 0),+ AstcBlock::B10x10 => (glow::COMPRESSED_RGBA_ASTC_10x10_KHR, glow::RGBA, 0),+ AstcBlock::B12x10 => (glow::COMPRESSED_RGBA_ASTC_12x10_KHR, glow::RGBA, 0),+ AstcBlock::B12x12 => (glow::COMPRESSED_RGBA_ASTC_12x12_KHR, glow::RGBA, 0),+ },+ AstcChannel::UnormSrgb => match block {+ AstcBlock::B4x4 => (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR, glow::RGBA, 0),+ AstcBlock::B5x4 => (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR, glow::RGBA, 0),+ AstcBlock::B5x5 => (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR, glow::RGBA, 0),+ AstcBlock::B6x5 => (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR, glow::RGBA, 0),+ AstcBlock::B6x6 => (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR, glow::RGBA, 0),+ AstcBlock::B8x5 => (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR, glow::RGBA, 0),+ AstcBlock::B8x6 => (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR, glow::RGBA, 0),+ AstcBlock::B8x8 => (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR, glow::RGBA, 0),+ AstcBlock::B10x5 => {+ (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR, glow::RGBA, 0)+ }+ AstcBlock::B10x6 => {+ (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR, glow::RGBA, 0)+ }+ AstcBlock::B10x8 => {+ (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR, glow::RGBA, 0)+ }+ AstcBlock::B10x10 => {+ (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR, glow::RGBA, 0)+ }+ AstcBlock::B12x10 => {+ (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR, glow::RGBA, 0)+ }+ AstcBlock::B12x12 => {+ (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR, glow::RGBA, 0)+ }+ },+ },+ };++ super::TextureFormatDesc {+ internal,+ external,+ data_type,+ }+ }+}++pub(super) fn describe_vertex_format(vertex_format: wgt::VertexFormat) -> super::VertexFormatDesc {+ use super::VertexAttribKind as Vak;+ use wgt::VertexFormat as Vf;++ let (element_count, element_format, attrib_kind) = match vertex_format {+ Vf::Unorm8 => (1, glow::UNSIGNED_BYTE, Vak::Float),+ Vf::Snorm8 => (1, glow::BYTE, Vak::Float),+ Vf::Uint8 => (1, glow::UNSIGNED_BYTE, Vak::Integer),+ Vf::Sint8 => (1, glow::BYTE, Vak::Integer),+ Vf::Unorm8x2 => (2, glow::UNSIGNED_BYTE, Vak::Float),+ Vf::Snorm8x2 => (2, glow::BYTE, Vak::Float),+ Vf::Uint8x2 => (2, glow::UNSIGNED_BYTE, Vak::Integer),+ Vf::Sint8x2 => (2, glow::BYTE, Vak::Integer),+ Vf::Unorm8x4 => (4, glow::UNSIGNED_BYTE, Vak::Float),+ Vf::Snorm8x4 => (4, glow::BYTE, Vak::Float),+ Vf::Uint8x4 => (4, glow::UNSIGNED_BYTE, Vak::Integer),+ Vf::Sint8x4 => (4, glow::BYTE, Vak::Integer),+ Vf::Unorm16 => (1, glow::UNSIGNED_SHORT, Vak::Float),+ Vf::Snorm16 => (1, glow::SHORT, Vak::Float),+ Vf::Uint16 => (1, glow::UNSIGNED_SHORT, Vak::Integer),+ Vf::Sint16 => (1, glow::SHORT, Vak::Integer),+ Vf::Float16 => (1, glow::HALF_FLOAT, Vak::Float),+ Vf::Unorm16x2 => (2, glow::UNSIGNED_SHORT, Vak::Float),+ Vf::Snorm16x2 => (2, glow::SHORT, Vak::Float),+ Vf::Uint16x2 => (2, glow::UNSIGNED_SHORT, Vak::Integer),+ Vf::Sint16x2 => (2, glow::SHORT, Vak::Integer),+ Vf::Float16x2 => (2, glow::HALF_FLOAT, Vak::Float),+ Vf::Unorm16x4 => (4, glow::UNSIGNED_SHORT, Vak::Float),+ Vf::Snorm16x4 => (4, glow::SHORT, Vak::Float),+ Vf::Uint16x4 => (4, glow::UNSIGNED_SHORT, Vak::Integer),+ Vf::Sint16x4 => (4, glow::SHORT, Vak::Integer),+ Vf::Float16x4 => (4, glow::HALF_FLOAT, Vak::Float),+ Vf::Uint32 => (1, glow::UNSIGNED_INT, Vak::Integer),+ Vf::Sint32 => (1, glow::INT, Vak::Integer),+ Vf::Float32 => (1, glow::FLOAT, Vak::Float),+ Vf::Uint32x2 => (2, glow::UNSIGNED_INT, Vak::Integer),+ Vf::Sint32x2 => (2, glow::INT, Vak::Integer),+ Vf::Float32x2 => (2, glow::FLOAT, Vak::Float),+ Vf::Uint32x3 => (3, glow::UNSIGNED_INT, Vak::Integer),+ Vf::Sint32x3 => (3, glow::INT, Vak::Integer),+ Vf::Float32x3 => (3, glow::FLOAT, Vak::Float),+ Vf::Uint32x4 => (4, glow::UNSIGNED_INT, Vak::Integer),+ Vf::Sint32x4 => (4, glow::INT, Vak::Integer),+ Vf::Float32x4 => (4, glow::FLOAT, Vak::Float),+ Vf::Unorm10_10_10_2 => (4, glow::UNSIGNED_INT_2_10_10_10_REV, Vak::Float),+ Vf::Unorm8x4Bgra => (glow::BGRA as i32, glow::UNSIGNED_BYTE, Vak::Float),+ Vf::Float64 | Vf::Float64x2 | Vf::Float64x3 | Vf::Float64x4 => unimplemented!(),+ };++ super::VertexFormatDesc {+ element_count,+ element_format,+ attrib_kind,+ }+}++pub fn map_filter_modes(+ min: wgt::FilterMode,+ mag: wgt::FilterMode,+ mip: wgt::MipmapFilterMode,+) -> (u32, u32) {+ use wgt::FilterMode as Fm;+ use wgt::MipmapFilterMode as Mfm;++ let mag_filter = match mag {+ Fm::Nearest => glow::NEAREST,+ Fm::Linear => glow::LINEAR,+ };++ let min_filter = match (min, mip) {+ (Fm::Nearest, Mfm::Nearest) => glow::NEAREST_MIPMAP_NEAREST,+ (Fm::Nearest, Mfm::Linear) => glow::NEAREST_MIPMAP_LINEAR,+ (Fm::Linear, Mfm::Nearest) => glow::LINEAR_MIPMAP_NEAREST,+ (Fm::Linear, Mfm::Linear) => glow::LINEAR_MIPMAP_LINEAR,+ };++ (min_filter, mag_filter)+}++pub fn map_address_mode(mode: wgt::AddressMode) -> u32 {+ match mode {+ wgt::AddressMode::Repeat => glow::REPEAT,+ wgt::AddressMode::MirrorRepeat => glow::MIRRORED_REPEAT,+ wgt::AddressMode::ClampToEdge => glow::CLAMP_TO_EDGE,+ wgt::AddressMode::ClampToBorder => glow::CLAMP_TO_BORDER,+ //wgt::AddressMode::MirrorClamp => glow::MIRROR_CLAMP_TO_EDGE,+ }+}++pub fn map_compare_func(fun: wgt::CompareFunction) -> u32 {+ use wgt::CompareFunction as Cf;+ match fun {+ Cf::Never => glow::NEVER,+ Cf::Less => glow::LESS,+ Cf::LessEqual => glow::LEQUAL,+ Cf::Equal => glow::EQUAL,+ Cf::GreaterEqual => glow::GEQUAL,+ Cf::Greater => glow::GREATER,+ Cf::NotEqual => glow::NOTEQUAL,+ Cf::Always => glow::ALWAYS,+ }+}++pub fn map_primitive_topology(topology: wgt::PrimitiveTopology) -> u32 {+ use wgt::PrimitiveTopology as Pt;+ match topology {+ Pt::PointList => glow::POINTS,+ Pt::LineList => glow::LINES,+ Pt::LineStrip => glow::LINE_STRIP,+ Pt::TriangleList => glow::TRIANGLES,+ Pt::TriangleStrip => glow::TRIANGLE_STRIP,+ }+}++pub(super) fn map_primitive_state(state: &wgt::PrimitiveState) -> super::PrimitiveState {+ super::PrimitiveState {+ //Note: we are flipping the front face, so that+ // the Y-flip in the generated GLSL keeps the same visibility.+ // See `naga::back::glsl::WriterFlags::ADJUST_COORDINATE_SPACE`.+ front_face: match state.front_face {+ wgt::FrontFace::Cw => glow::CCW,+ wgt::FrontFace::Ccw => glow::CW,+ },+ cull_face: match state.cull_mode {+ Some(wgt::Face::Front) => glow::FRONT,+ Some(wgt::Face::Back) => glow::BACK,+ None => 0,+ },+ unclipped_depth: state.unclipped_depth,+ polygon_mode: match state.polygon_mode {+ wgt::PolygonMode::Fill => glow::FILL,+ wgt::PolygonMode::Line => glow::LINE,+ wgt::PolygonMode::Point => glow::POINT,+ },+ }+}++pub fn _map_view_dimension(dim: wgt::TextureViewDimension) -> u32 {+ use wgt::TextureViewDimension as Tvd;+ match dim {+ Tvd::D1 | Tvd::D2 => glow::TEXTURE_2D,+ Tvd::D2Array => glow::TEXTURE_2D_ARRAY,+ Tvd::Cube => glow::TEXTURE_CUBE_MAP,+ Tvd::CubeArray => glow::TEXTURE_CUBE_MAP_ARRAY,+ Tvd::D3 => glow::TEXTURE_3D,+ }+}++fn map_stencil_op(operation: wgt::StencilOperation) -> u32 {+ use wgt::StencilOperation as So;+ match operation {+ So::Keep => glow::KEEP,+ So::Zero => glow::ZERO,+ So::Replace => glow::REPLACE,+ So::Invert => glow::INVERT,+ So::IncrementClamp => glow::INCR,+ So::DecrementClamp => glow::DECR,+ So::IncrementWrap => glow::INCR_WRAP,+ So::DecrementWrap => glow::DECR_WRAP,+ }+}++fn map_stencil_ops(face: &wgt::StencilFaceState) -> super::StencilOps {+ super::StencilOps {+ pass: map_stencil_op(face.pass_op),+ fail: map_stencil_op(face.fail_op),+ depth_fail: map_stencil_op(face.depth_fail_op),+ }+}++pub(super) fn map_stencil(state: &wgt::StencilState) -> super::StencilState {+ super::StencilState {+ front: super::StencilSide {+ function: map_compare_func(state.front.compare),+ mask_read: state.read_mask,+ mask_write: state.write_mask,+ reference: 0,+ ops: map_stencil_ops(&state.front),+ },+ back: super::StencilSide {+ function: map_compare_func(state.back.compare),+ mask_read: state.read_mask,+ mask_write: state.write_mask,+ reference: 0,+ ops: map_stencil_ops(&state.back),+ },+ }+}++fn map_blend_factor(factor: wgt::BlendFactor) -> u32 {+ use wgt::BlendFactor as Bf;+ match factor {+ Bf::Zero => glow::ZERO,+ Bf::One => glow::ONE,+ Bf::Src => glow::SRC_COLOR,+ Bf::OneMinusSrc => glow::ONE_MINUS_SRC_COLOR,+ Bf::Dst => glow::DST_COLOR,+ Bf::OneMinusDst => glow::ONE_MINUS_DST_COLOR,+ Bf::SrcAlpha => glow::SRC_ALPHA,+ Bf::OneMinusSrcAlpha => glow::ONE_MINUS_SRC_ALPHA,+ Bf::DstAlpha => glow::DST_ALPHA,+ Bf::OneMinusDstAlpha => glow::ONE_MINUS_DST_ALPHA,+ Bf::Constant => glow::CONSTANT_COLOR,+ Bf::OneMinusConstant => glow::ONE_MINUS_CONSTANT_COLOR,+ Bf::SrcAlphaSaturated => glow::SRC_ALPHA_SATURATE,+ Bf::Src1 => glow::SRC1_COLOR,+ Bf::OneMinusSrc1 => glow::ONE_MINUS_SRC1_COLOR,+ Bf::Src1Alpha => glow::SRC1_ALPHA,+ Bf::OneMinusSrc1Alpha => glow::ONE_MINUS_SRC1_ALPHA,+ }+}++fn map_blend_component(component: &wgt::BlendComponent) -> super::BlendComponent {+ super::BlendComponent {+ src: map_blend_factor(component.src_factor),+ dst: map_blend_factor(component.dst_factor),+ equation: match component.operation {+ wgt::BlendOperation::Add => glow::FUNC_ADD,+ wgt::BlendOperation::Subtract => glow::FUNC_SUBTRACT,+ wgt::BlendOperation::ReverseSubtract => glow::FUNC_REVERSE_SUBTRACT,+ wgt::BlendOperation::Min => glow::MIN,+ wgt::BlendOperation::Max => glow::MAX,+ },+ }+}++pub(super) fn map_blend(blend: &wgt::BlendState) -> super::BlendDesc {+ super::BlendDesc {+ color: map_blend_component(&blend.color),+ alpha: map_blend_component(&blend.alpha),+ }+}++pub(super) fn map_storage_access(access: wgt::StorageTextureAccess) -> u32 {+ match access {+ wgt::StorageTextureAccess::ReadOnly => glow::READ_ONLY,+ wgt::StorageTextureAccess::WriteOnly => glow::WRITE_ONLY,+ wgt::StorageTextureAccess::ReadWrite => glow::READ_WRITE,+ wgt::StorageTextureAccess::Atomic => glow::READ_WRITE,+ }+}++pub(super) fn is_layered_target(target: u32) -> bool {+ match target {+ glow::TEXTURE_2D | glow::TEXTURE_CUBE_MAP => false,+ glow::TEXTURE_2D_ARRAY | glow::TEXTURE_CUBE_MAP_ARRAY | glow::TEXTURE_3D => true,+ _ => unreachable!(),+ }+}diff --git a/vendor/wgpu-hal/src/gles/device.rs b/vendor/wgpu-hal/src/gles/device.rsnew file mode 100644index 00000000..1c61cf3b--- /dev/null+++ b/vendor/wgpu-hal/src/gles/device.rs@@ -0,0 +1,1823 @@+use alloc::{+ borrow::{Cow, ToOwned},+ format,+ string::String,+ string::ToString as _,+ sync::Arc,+ vec,+ vec::Vec,+};+use core::{cmp::max, convert::TryInto, num::NonZeroU32, ptr, sync::atomic::Ordering};++use arrayvec::ArrayVec;+use glow::HasContext;+use naga::FastHashMap;++use super::{conv, lock, MaybeMutex, PrivateCapabilities};+use crate::auxil::map_naga_stage;+use crate::TlasInstance;++type ShaderStage<'a> = (+ naga::ShaderStage,+ &'a crate::ProgrammableStage<'a, super::ShaderModule>,+);+type NameBindingMap = FastHashMap<String, (super::BindingRegister, u8)>;++struct CompilationContext<'a> {+ layout: &'a super::PipelineLayout,+ sampler_map: &'a mut super::SamplerBindMap,+ name_binding_map: &'a mut NameBindingMap,+ immediates_items: &'a mut Vec<naga::back::glsl::ImmediateItem>,+ multiview_mask: Option<NonZeroU32>,+ clip_distance_count: &'a mut u32,+}++impl CompilationContext<'_> {+ fn consume_reflection(+ self,+ gl: &glow::Context,+ module: &naga::Module,+ ep_info: &naga::valid::FunctionInfo,+ reflection_info: naga::back::glsl::ReflectionInfo,+ naga_stage: naga::ShaderStage,+ program: glow::Program,+ ) {+ for (handle, var) in module.global_variables.iter() {+ if ep_info[handle].is_empty() {+ continue;+ }+ let register = match var.space {+ naga::AddressSpace::Uniform => super::BindingRegister::UniformBuffers,+ naga::AddressSpace::Storage { .. } => super::BindingRegister::StorageBuffers,+ _ => continue,+ };++ let br = var.binding.as_ref().unwrap();+ let slot = self.layout.get_slot(br);++ let name = match reflection_info.uniforms.get(&handle) {+ Some(name) => name.clone(),+ None => continue,+ };+ log::trace!(+ "Rebind buffer: {:?} -> {}, register={:?}, slot={}",+ var.name.as_ref(),+ &name,+ register,+ slot+ );+ self.name_binding_map.insert(name, (register, slot));+ }++ for (name, mapping) in reflection_info.texture_mapping {+ let var = &module.global_variables[mapping.texture];+ let register = match module.types[var.ty].inner {+ naga::TypeInner::Image {+ class: naga::ImageClass::Storage { .. },+ ..+ } => super::BindingRegister::Images,+ _ => super::BindingRegister::Textures,+ };++ let tex_br = var.binding.as_ref().unwrap();+ let texture_linear_index = self.layout.get_slot(tex_br);++ self.name_binding_map+ .insert(name, (register, texture_linear_index));+ if let Some(sampler_handle) = mapping.sampler {+ let sam_br = module.global_variables[sampler_handle]+ .binding+ .as_ref()+ .unwrap();+ let sampler_linear_index = self.layout.get_slot(sam_br);+ self.sampler_map[texture_linear_index as usize] = Some(sampler_linear_index);+ }+ }++ for (name, location) in reflection_info.varying {+ match naga_stage {+ naga::ShaderStage::Vertex => {+ assert_eq!(location.index, 0);+ unsafe { gl.bind_attrib_location(program, location.location, &name) }+ }+ naga::ShaderStage::Fragment => {+ assert_eq!(location.index, 0);+ unsafe { gl.bind_frag_data_location(program, location.location, &name) }+ }+ naga::ShaderStage::Compute => {}+ naga::ShaderStage::Task+ | naga::ShaderStage::Mesh+ | naga::ShaderStage::RayGeneration+ | naga::ShaderStage::AnyHit+ | naga::ShaderStage::ClosestHit+ | naga::ShaderStage::Miss => unreachable!(),+ }+ }++ *self.immediates_items = reflection_info.immediates_items;++ if naga_stage == naga::ShaderStage::Vertex {+ *self.clip_distance_count = reflection_info.clip_distance_count;+ }+ }+}++impl super::Device {+ /// # Safety+ ///+ /// - `name` must be created respecting `desc`+ /// - `name` must be a texture+ /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of the texture. If+ /// `drop_callback` is [`Some`], the texture must be valid until the callback is called.+ #[cfg(any(native, Emscripten))]+ pub unsafe fn texture_from_raw(+ &self,+ name: NonZeroU32,+ desc: &crate::TextureDescriptor,+ drop_callback: Option<crate::DropCallback>,+ ) -> super::Texture {+ super::Texture {+ inner: super::TextureInner::Texture {+ raw: glow::NativeTexture(name),+ target: super::Texture::get_info_from_desc(desc),+ },+ drop_guard: crate::DropGuard::from_option(drop_callback),+ mip_level_count: desc.mip_level_count,+ array_layer_count: desc.array_layer_count(),+ format: desc.format,+ format_desc: self.shared.describe_texture_format(desc.format),+ copy_size: desc.copy_extent(),+ }+ }++ /// # Safety+ ///+ /// - `name` must be created respecting `desc`+ /// - `name` must be a renderbuffer+ /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of the renderbuffer. If+ /// `drop_callback` is [`Some`], the renderbuffer must be valid until the callback is called.+ #[cfg(any(native, Emscripten))]+ pub unsafe fn texture_from_raw_renderbuffer(+ &self,+ name: NonZeroU32,+ desc: &crate::TextureDescriptor,+ drop_callback: Option<crate::DropCallback>,+ ) -> super::Texture {+ super::Texture {+ inner: super::TextureInner::Renderbuffer {+ raw: glow::NativeRenderbuffer(name),+ },+ drop_guard: crate::DropGuard::from_option(drop_callback),+ mip_level_count: desc.mip_level_count,+ array_layer_count: desc.array_layer_count(),+ format: desc.format,+ format_desc: self.shared.describe_texture_format(desc.format),+ copy_size: desc.copy_extent(),+ }+ }++ /// # Safety+ ///+ /// - `name` must be a non-zero GL buffer name created respecting `desc`.+ /// - The buffer's storage size must be at least `desc.size`.+ /// - If `desc.usage` includes [`BufferUses::MAP_READ`](wgt::BufferUses::MAP_READ) or+ /// [`BufferUses::MAP_WRITE`](wgt::BufferUses::MAP_WRITE), the GL buffer must have+ /// been allocated with `glBufferStorage` (or equivalent) using flags compatible+ /// with persistent mapping (`GL_MAP_PERSISTENT_BIT` plus matching read/write/coherent+ /// bits). Buffers created with the legacy `glBufferData` family cannot be mapped+ /// through this path.+ /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of the buffer and+ /// call `glDeleteBuffers` on it. If `drop_callback` is [`Some`], the buffer must+ /// remain valid until the callback is invoked.+ #[cfg(any(native, Emscripten))]+ pub unsafe fn buffer_from_raw(+ &self,+ name: NonZeroU32,+ desc: &crate::BufferDescriptor,+ drop_callback: Option<crate::DropCallback>,+ ) -> super::Buffer {+ let target = if desc.usage.contains(wgt::BufferUses::INDEX) {+ glow::ELEMENT_ARRAY_BUFFER+ } else {+ glow::ARRAY_BUFFER+ };++ let is_host_visible = desc+ .usage+ .intersects(wgt::BufferUses::MAP_READ | wgt::BufferUses::MAP_WRITE);+ let is_coherent = desc+ .memory_flags+ .contains(crate::MemoryFlags::PREFER_COHERENT);++ let mut map_flags = 0;+ if desc.usage.contains(wgt::BufferUses::MAP_READ) {+ map_flags |= glow::MAP_READ_BIT;+ }+ if desc.usage.contains(wgt::BufferUses::MAP_WRITE) {+ map_flags |= glow::MAP_WRITE_BIT;+ }+ if is_host_visible {+ map_flags |= glow::MAP_PERSISTENT_BIT;+ if is_coherent {+ map_flags |= glow::MAP_COHERENT_BIT;+ }+ }+ if !is_coherent && desc.usage.contains(wgt::BufferUses::MAP_WRITE) {+ map_flags |= glow::MAP_FLUSH_EXPLICIT_BIT;+ }++ self.counters.buffers.add(1);++ super::Buffer {+ raw: Some(glow::NativeBuffer(name)),+ target,+ size: desc.size,+ map_flags,+ map_state: Arc::new(MaybeMutex::new(super::BufferMapState {+ mapped: false,+ data: None,+ offset_of_current_mapping: 0,+ })),+ drop_guard: crate::DropGuard::from_option(drop_callback).map(Arc::new),+ }+ }++ unsafe fn compile_shader(+ gl: &glow::Context,+ shader: &str,+ naga_stage: naga::ShaderStage,+ #[cfg_attr(target_arch = "wasm32", allow(unused))] label: Option<&str>,+ ) -> Result<glow::Shader, crate::PipelineError> {+ let target = match naga_stage {+ naga::ShaderStage::Vertex => glow::VERTEX_SHADER,+ naga::ShaderStage::Fragment => glow::FRAGMENT_SHADER,+ naga::ShaderStage::Compute => glow::COMPUTE_SHADER,+ naga::ShaderStage::Task+ | naga::ShaderStage::Mesh+ | naga::ShaderStage::RayGeneration+ | naga::ShaderStage::AnyHit+ | naga::ShaderStage::ClosestHit+ | naga::ShaderStage::Miss => unreachable!(),+ };++ let raw = unsafe { gl.create_shader(target) }.unwrap();+ #[cfg(native)]+ if gl.supports_debug() {+ let name = raw.0.get();+ unsafe { gl.object_label(glow::SHADER, name, label) };+ }++ unsafe { gl.shader_source(raw, shader) };+ unsafe { gl.compile_shader(raw) };++ log::debug!("\tCompiled shader {raw:?}");++ let compiled_ok = unsafe { gl.get_shader_compile_status(raw) };+ let msg = unsafe { gl.get_shader_info_log(raw) };+ if compiled_ok {+ if !msg.is_empty() {+ log::debug!("\tCompile message: {msg}");+ }+ Ok(raw)+ } else {+ log::error!("\tShader compilation failed: {msg}");+ unsafe { gl.delete_shader(raw) };+ Err(crate::PipelineError::Linkage(+ map_naga_stage(naga_stage),+ msg,+ ))+ }+ }++ fn create_shader(+ gl: &glow::Context,+ naga_stage: naga::ShaderStage,+ stage: &crate::ProgrammableStage<super::ShaderModule>,+ context: CompilationContext,+ program: glow::Program,+ ) -> Result<glow::Shader, crate::PipelineError> {+ let source = 'outer: {+ use naga::back::glsl;+ let pipeline_options = glsl::PipelineOptions {+ shader_stage: naga_stage,+ entry_point: stage.entry_point.to_owned(),+ multiview: context+ .multiview_mask+ .map(|a| NonZeroU32::new(a.get().count_ones()).unwrap()),+ };++ let naga = match stage.module.source {+ super::ShaderModuleSource::Naga(ref naga) => naga,+ super::ShaderModuleSource::Passthrough { ref source } => {+ break 'outer Cow::Borrowed(source);+ }+ };++ let (module, info) = naga::back::pipeline_constants::process_overrides(+ &naga.module,+ &naga.info,+ Some((naga_stage, stage.entry_point)),+ stage.constants,+ )+ .map_err(|e| {+ let msg = format!("{e}");+ crate::PipelineError::PipelineConstants(map_naga_stage(naga_stage), msg)+ })?;++ let entry_point_index = module+ .entry_points+ .iter()+ .position(|ep| ep.name.as_str() == stage.entry_point)+ .ok_or(crate::PipelineError::EntryPoint(naga_stage))?;++ use naga::proc::BoundsCheckPolicy;+ // The image bounds checks require the TEXTURE_LEVELS feature available in GL core 4.3+.+ let version = gl.version();+ let image_check = if !version.is_embedded && (version.major, version.minor) >= (4, 3) {+ BoundsCheckPolicy::ReadZeroSkipWrite+ } else {+ BoundsCheckPolicy::Unchecked+ };++ // Other bounds check are either provided by glsl or not implemented yet.+ let policies = naga::proc::BoundsCheckPolicies {+ index: BoundsCheckPolicy::Unchecked,+ buffer: BoundsCheckPolicy::Unchecked,+ image_load: image_check,+ binding_array: BoundsCheckPolicy::Unchecked,+ };++ let mut output = String::new();+ let needs_temp_options = stage.zero_initialize_workgroup_memory+ != context.layout.naga_options.zero_initialize_workgroup_memory;+ let mut temp_options;+ let naga_options = if needs_temp_options {+ // We use a conditional here, as cloning the naga_options could be expensive+ // That is, we want to avoid doing that unless we cannot avoid it+ temp_options = context.layout.naga_options.clone();+ temp_options.zero_initialize_workgroup_memory =+ stage.zero_initialize_workgroup_memory;+ &temp_options+ } else {+ &context.layout.naga_options+ };+ let mut writer = glsl::Writer::new(+ &mut output,+ &module,+ &info,+ naga_options,+ &pipeline_options,+ policies,+ )+ .map_err(|e| {+ let msg = format!("{e}");+ crate::PipelineError::Linkage(map_naga_stage(naga_stage), msg)+ })?;++ let reflection_info = writer.write().map_err(|e| {+ let msg = format!("{e}");+ crate::PipelineError::Linkage(map_naga_stage(naga_stage), msg)+ })?;++ log::debug!("Naga generated shader:\n{output}");++ context.consume_reflection(+ gl,+ &module,+ info.get_entry_point(entry_point_index),+ reflection_info,+ naga_stage,+ program,+ );+ Cow::Owned(output)+ };++ unsafe { Self::compile_shader(gl, &source, naga_stage, stage.module.label.as_deref()) }+ }++ unsafe fn create_pipeline<'a>(+ &self,+ gl: &glow::Context,+ shaders: ArrayVec<ShaderStage<'a>, { crate::MAX_CONCURRENT_SHADER_STAGES }>,+ layout: &super::PipelineLayout,+ #[cfg_attr(target_arch = "wasm32", allow(unused))] label: Option<&str>,+ multiview_mask: Option<NonZeroU32>,+ ) -> Result<Arc<super::PipelineInner>, crate::PipelineError> {+ let mut program_stages = ArrayVec::new();+ let group_to_binding_to_slot = layout+ .group_infos+ .iter()+ .map(|group| group.as_ref().map(|group| group.binding_to_slot.clone()))+ .collect::<Vec<_>>();+ for &(naga_stage, stage) in &shaders {+ program_stages.push(super::ProgramStage {+ naga_stage: naga_stage.to_owned(),+ shader_id: stage.module.id,+ entry_point: stage.entry_point.to_owned(),+ zero_initialize_workgroup_memory: stage.zero_initialize_workgroup_memory,+ constant_hash: Self::create_constant_hash(stage),+ });+ }+ let mut guard = self+ .shared+ .program_cache+ .try_lock()+ .expect("Couldn't acquire program_cache lock");+ // This guard ensures that we can't accidentally destroy a program whilst we're about to reuse it+ // The only place that destroys a pipeline is also locking on `program_cache`+ let program = guard+ .entry(super::ProgramCacheKey {+ stages: program_stages,+ group_to_binding_to_slot: group_to_binding_to_slot.into_boxed_slice(),+ })+ .or_insert_with(|| unsafe {+ Self::create_program(+ gl,+ shaders,+ layout,+ label,+ multiview_mask,+ self.shared.shading_language_version,+ self.shared.private_caps,+ )+ })+ .to_owned()?;+ drop(guard);++ Ok(program)+ }++ fn create_constant_hash(stage: &crate::ProgrammableStage<super::ShaderModule>) -> Vec<u8> {+ let mut buf: Vec<u8> = Vec::new();++ for (key, value) in stage.constants.iter() {+ buf.extend_from_slice(key.as_bytes());+ buf.extend_from_slice(&value.to_ne_bytes());+ }++ buf+ }++ unsafe fn create_program<'a>(+ gl: &glow::Context,+ shaders: ArrayVec<ShaderStage<'a>, { crate::MAX_CONCURRENT_SHADER_STAGES }>,+ layout: &super::PipelineLayout,+ #[cfg_attr(target_arch = "wasm32", allow(unused))] label: Option<&str>,+ multiview_mask: Option<NonZeroU32>,+ glsl_version: naga::back::glsl::Version,+ private_caps: PrivateCapabilities,+ ) -> Result<Arc<super::PipelineInner>, crate::PipelineError> {+ let glsl_version = match glsl_version {+ naga::back::glsl::Version::Embedded { version, .. } => format!("{version} es"),+ naga::back::glsl::Version::Desktop(version) => format!("{version}"),+ };+ let program = unsafe { gl.create_program() }.unwrap();+ #[cfg(native)]+ if let Some(label) = label {+ if private_caps.contains(PrivateCapabilities::DEBUG_FNS) {+ let name = program.0.get();+ unsafe { gl.object_label(glow::PROGRAM, name, Some(label)) };+ }+ }++ let mut name_binding_map = NameBindingMap::default();+ let mut immediates_items = ArrayVec::<_, { crate::MAX_CONCURRENT_SHADER_STAGES }>::new();+ let mut sampler_map = [None; super::MAX_TEXTURE_SLOTS];+ let mut has_stages = wgt::ShaderStages::empty();+ let mut shaders_to_delete = ArrayVec::<_, { crate::MAX_CONCURRENT_SHADER_STAGES }>::new();+ let mut clip_distance_count = 0;++ for &(naga_stage, stage) in &shaders {+ has_stages |= map_naga_stage(naga_stage);+ let pc_item = {+ immediates_items.push(Vec::new());+ immediates_items.last_mut().unwrap()+ };+ let context = CompilationContext {+ layout,+ sampler_map: &mut sampler_map,+ name_binding_map: &mut name_binding_map,+ immediates_items: pc_item,+ multiview_mask,+ clip_distance_count: &mut clip_distance_count,+ };++ let shader = Self::create_shader(gl, naga_stage, stage, context, program)?;+ shaders_to_delete.push(shader);+ }++ // Create empty fragment shader if only vertex shader is present+ if has_stages == wgt::ShaderStages::VERTEX {+ let shader_src = format!("#version {glsl_version}\n void main(void) {{}}",);+ log::debug!("Only vertex shader is present. Creating an empty fragment shader",);+ let shader = unsafe {+ Self::compile_shader(+ gl,+ &shader_src,+ naga::ShaderStage::Fragment,+ Some("(wgpu internal) dummy fragment shader"),+ )+ }?;+ shaders_to_delete.push(shader);+ }++ for &shader in shaders_to_delete.iter() {+ unsafe { gl.attach_shader(program, shader) };+ }+ unsafe { gl.link_program(program) };++ for shader in shaders_to_delete {+ unsafe { gl.delete_shader(shader) };+ }++ log::debug!("\tLinked program {program:?}");++ let linked_ok = unsafe { gl.get_program_link_status(program) };+ let msg = unsafe { gl.get_program_info_log(program) };+ if !linked_ok {+ return Err(crate::PipelineError::Linkage(has_stages, msg));+ }+ if !msg.is_empty() {+ log::debug!("\tLink message: {msg}");+ }++ if !private_caps.contains(PrivateCapabilities::SHADER_BINDING_LAYOUT) {+ // This remapping is only needed if we aren't able to put the binding layout+ // in the shader. We can't remap storage buffers this way.+ unsafe { gl.use_program(Some(program)) };+ for (ref name, (register, slot)) in name_binding_map {+ log::trace!("Get binding {name:?} from program {program:?}");+ match register {+ super::BindingRegister::UniformBuffers => {+ let index = unsafe { gl.get_uniform_block_index(program, name) }.unwrap();+ log::trace!("\tBinding slot {slot} to block index {index}");+ unsafe { gl.uniform_block_binding(program, index, slot as _) };+ }+ super::BindingRegister::StorageBuffers => {+ let index =+ unsafe { gl.get_shader_storage_block_index(program, name) }.unwrap();+ log::error!("Unable to re-map shader storage block {name} to {index}");+ return Err(crate::DeviceError::Lost.into());+ }+ super::BindingRegister::Textures | super::BindingRegister::Images => {+ let location = unsafe { gl.get_uniform_location(program, name) };+ unsafe { gl.uniform_1_i32(location.as_ref(), slot as _) };+ }+ }+ }+ }++ let mut uniforms = ArrayVec::new();++ for stage_items in immediates_items {+ for item in stage_items {+ let location = unsafe { gl.get_uniform_location(program, &item.access_path) };++ log::trace!(+ "immediate data item: name={}, ty={:?}, offset={}, location={:?}",+ item.access_path,+ item.ty,+ item.offset,+ location,+ );++ if let Some(location) = location {+ uniforms.push(super::ImmediateDesc {+ location,+ offset: item.offset,+ size_bytes: item.size_bytes,+ ty: item.ty,+ });+ }+ }+ }++ let first_instance_location = if has_stages.contains(wgt::ShaderStages::VERTEX) {+ // If this returns none (the uniform isn't active), that's fine, we just won't set it.+ unsafe { gl.get_uniform_location(program, naga::back::glsl::FIRST_INSTANCE_BINDING) }+ } else {+ None+ };++ Ok(Arc::new(super::PipelineInner {+ program,+ sampler_map,+ first_instance_location,+ immediates_descs: uniforms,+ clip_distance_count,+ }))+ }+}++impl crate::Device for super::Device {+ type A = super::Api;++ unsafe fn create_buffer(+ &self,+ desc: &crate::BufferDescriptor,+ ) -> Result<super::Buffer, crate::DeviceError> {+ let target = if desc.usage.contains(wgt::BufferUses::INDEX) {+ glow::ELEMENT_ARRAY_BUFFER+ } else {+ glow::ARRAY_BUFFER+ };++ let emulate_map = self+ .shared+ .workarounds+ .contains(super::Workarounds::EMULATE_BUFFER_MAP)+ || !self+ .shared+ .private_caps+ .contains(PrivateCapabilities::BUFFER_ALLOCATION);++ if emulate_map && desc.usage.intersects(wgt::BufferUses::MAP_WRITE) {+ return Ok(super::Buffer {+ raw: None,+ target,+ size: desc.size,+ map_flags: 0,+ map_state: Arc::new(MaybeMutex::new(super::BufferMapState {+ mapped: false,+ data: Some(vec![0; desc.size as usize]),+ offset_of_current_mapping: 0,+ })),+ drop_guard: None,+ });+ }++ let gl = &self.shared.context.lock();++ let target = if desc.usage.contains(wgt::BufferUses::INDEX) {+ glow::ELEMENT_ARRAY_BUFFER+ } else {+ glow::ARRAY_BUFFER+ };++ let is_host_visible = desc+ .usage+ .intersects(wgt::BufferUses::MAP_READ | wgt::BufferUses::MAP_WRITE);+ let is_coherent = desc+ .memory_flags+ .contains(crate::MemoryFlags::PREFER_COHERENT);++ let mut map_flags = 0;+ if desc.usage.contains(wgt::BufferUses::MAP_READ) {+ map_flags |= glow::MAP_READ_BIT;+ }+ if desc.usage.contains(wgt::BufferUses::MAP_WRITE) {+ map_flags |= glow::MAP_WRITE_BIT;+ }++ let raw = Some(unsafe { gl.create_buffer() }.map_err(|_| crate::DeviceError::OutOfMemory)?);+ unsafe { gl.bind_buffer(target, raw) };+ let raw_size = desc+ .size+ .try_into()+ .map_err(|_| crate::DeviceError::OutOfMemory)?;++ if self+ .shared+ .private_caps+ .contains(PrivateCapabilities::BUFFER_ALLOCATION)+ {+ if is_host_visible {+ map_flags |= glow::MAP_PERSISTENT_BIT;+ if is_coherent {+ map_flags |= glow::MAP_COHERENT_BIT;+ }+ }+ // TODO: may also be required for other calls involving `buffer_sub_data_u8_slice` (e.g. copy buffer to buffer and clear buffer)+ if desc.usage.intersects(wgt::BufferUses::QUERY_RESOLVE) {+ map_flags |= glow::DYNAMIC_STORAGE_BIT;+ }+ unsafe { gl.buffer_storage(target, raw_size, None, map_flags) };+ } else {+ assert!(!is_coherent);+ let usage = if is_host_visible {+ if desc.usage.contains(wgt::BufferUses::MAP_READ) {+ glow::STREAM_READ+ } else {+ glow::DYNAMIC_DRAW+ }+ } else {+ // Even if the usage doesn't contain SRC_READ, we update it internally at least once+ // Some vendors take usage very literally and STATIC_DRAW will freeze us with an empty buffer+ // https://github.com/gfx-rs/wgpu/issues/3371+ glow::DYNAMIC_DRAW+ };+ unsafe { gl.buffer_data_size(target, raw_size, usage) };+ }++ unsafe { gl.bind_buffer(target, None) };++ if !is_coherent && desc.usage.contains(wgt::BufferUses::MAP_WRITE) {+ map_flags |= glow::MAP_FLUSH_EXPLICIT_BIT;+ }+ //TODO: do we need `glow::MAP_UNSYNCHRONIZED_BIT`?++ #[cfg(native)]+ if let Some(label) = desc.label {+ if self+ .shared+ .private_caps+ .contains(PrivateCapabilities::DEBUG_FNS)+ {+ let name = raw.map_or(0, |buf| buf.0.get());+ unsafe { gl.object_label(glow::BUFFER, name, Some(label)) };+ }+ }++ let data = if emulate_map && desc.usage.contains(wgt::BufferUses::MAP_READ) {+ Some(vec![0; desc.size as usize])+ } else {+ None+ };++ self.counters.buffers.add(1);++ Ok(super::Buffer {+ raw,+ target,+ size: desc.size,+ map_flags,+ map_state: Arc::new(MaybeMutex::new(super::BufferMapState {+ mapped: false,+ data,+ offset_of_current_mapping: 0,+ })),+ drop_guard: None,+ })+ }++ unsafe fn destroy_buffer(&self, buffer: super::Buffer) {+ if buffer.drop_guard.is_none() {+ if let Some(raw) = buffer.raw {+ let gl = &self.shared.context.lock();+ unsafe { gl.delete_buffer(raw) };+ }+ }++ // For clarity, we explicitly drop the drop guard. Although this has no real semantic effect as the+ // end of the scope will drop the drop guard since this function takes ownership of the buffer.+ drop(buffer.drop_guard);++ self.counters.buffers.sub(1);+ }++ unsafe fn add_raw_buffer(&self, _buffer: &super::Buffer) {+ self.counters.buffers.add(1);+ }++ unsafe fn map_buffer(+ &self,+ buffer: &super::Buffer,+ range: crate::MemoryRange,+ ) -> Result<crate::BufferMapping, crate::DeviceError> {+ let is_coherent = buffer.map_flags & glow::MAP_COHERENT_BIT != 0;+ let ptr = match buffer.raw {+ None => {+ let mut map_state = lock(&buffer.map_state);+ let vec = map_state.data.as_mut().unwrap();+ let slice = &mut vec.as_mut_slice()[range.start as usize..range.end as usize];+ slice.as_mut_ptr()+ }+ Some(raw) => {+ let gl = &self.shared.context.lock();+ unsafe { gl.bind_buffer(buffer.target, Some(raw)) };+ let mut map_state = lock(&buffer.map_state);+ let ptr = if let Some(map_read_allocation) = map_state.data.as_mut() {+ let slice = map_read_allocation.as_mut_slice();+ unsafe { self.shared.get_buffer_sub_data(gl, buffer.target, 0, slice) };+ slice.as_mut_ptr()+ } else {+ map_state.offset_of_current_mapping = range.start;+ // glMapBufferRange throws an error if length is 0.+ // We want to allow mapping 0-sized buffer slices, so perform a workaround+ // if the range length is 0. The resulting pointer must never be dereferenced.+ let range_start: i32 = range+ .start+ .try_into()+ .expect("Buffer range invalid for GLES");+ let range_length: i32 = (range.end - range.start)+ .try_into()+ .expect("Buffer range invalid for GLES");+ if range_length != 0 {+ map_state.mapped = true;+ unsafe {+ gl.map_buffer_range(+ buffer.target,+ range_start,+ range_length,+ buffer.map_flags,+ )+ }+ } else {+ ptr::dangling_mut()+ }+ };+ unsafe { gl.bind_buffer(buffer.target, None) };+ ptr+ }+ };+ Ok(crate::BufferMapping {+ ptr: ptr::NonNull::new(ptr).ok_or(crate::DeviceError::Lost)?,+ is_coherent,+ })+ }+ unsafe fn unmap_buffer(&self, buffer: &super::Buffer) {+ let gl = &self.shared.context.lock();+ let mut map_state = lock(&buffer.map_state);+ if core::mem::replace(&mut map_state.mapped, false) {+ if let Some(raw) = buffer.raw {+ if map_state.data.is_none() {+ unsafe { gl.bind_buffer(buffer.target, Some(raw)) };+ unsafe { gl.unmap_buffer(buffer.target) };+ unsafe { gl.bind_buffer(buffer.target, None) };+ map_state.offset_of_current_mapping = 0;+ }+ }+ }+ }+ unsafe fn flush_mapped_ranges<I>(&self, buffer: &super::Buffer, ranges: I)+ where+ I: Iterator<Item = crate::MemoryRange>,+ {+ let gl = &self.shared.context.lock();+ let map_state = lock(&buffer.map_state);+ if map_state.mapped {+ if let Some(raw) = buffer.raw {+ if map_state.data.is_none() {+ unsafe { gl.bind_buffer(buffer.target, Some(raw)) };+ for range in ranges {+ let offset_of_current_mapping = map_state.offset_of_current_mapping;+ unsafe {+ gl.flush_mapped_buffer_range(+ buffer.target,+ (range.start - offset_of_current_mapping) as i32,+ (range.end - range.start) as i32,+ )+ };+ }+ }+ }+ }+ }+ unsafe fn invalidate_mapped_ranges<I>(&self, _buffer: &super::Buffer, _ranges: I) {+ //TODO: do we need to do anything?+ }++ unsafe fn create_texture(+ &self,+ desc: &crate::TextureDescriptor,+ ) -> Result<super::Texture, crate::DeviceError> {+ let gl = &self.shared.context.lock();++ let render_usage = wgt::TextureUses::COLOR_TARGET+ | wgt::TextureUses::DEPTH_STENCIL_WRITE+ | wgt::TextureUses::DEPTH_STENCIL_READ+ | wgt::TextureUses::TRANSIENT;+ let format_desc = self.shared.describe_texture_format(desc.format);++ let inner = if render_usage.contains(desc.usage)+ && desc.dimension == wgt::TextureDimension::D2+ && desc.size.depth_or_array_layers == 1+ {+ let raw = unsafe { gl.create_renderbuffer().unwrap() };+ unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, Some(raw)) };+ if desc.sample_count > 1 {+ unsafe {+ gl.renderbuffer_storage_multisample(+ glow::RENDERBUFFER,+ desc.sample_count as i32,+ format_desc.internal,+ desc.size.width as i32,+ desc.size.height as i32,+ )+ };+ } else {+ unsafe {+ gl.renderbuffer_storage(+ glow::RENDERBUFFER,+ format_desc.internal,+ desc.size.width as i32,+ desc.size.height as i32,+ )+ };+ }++ #[cfg(native)]+ if let Some(label) = desc.label {+ if self+ .shared+ .private_caps+ .contains(PrivateCapabilities::DEBUG_FNS)+ {+ let name = raw.0.get();+ unsafe { gl.object_label(glow::RENDERBUFFER, name, Some(label)) };+ }+ }++ unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, None) };+ super::TextureInner::Renderbuffer { raw }+ } else {+ let raw = unsafe { gl.create_texture().unwrap() };+ let target = super::Texture::get_info_from_desc(desc);++ unsafe { gl.bind_texture(target, Some(raw)) };+ //Note: this has to be done before defining the storage!+ match desc.format.sample_type(None, Some(self.shared.features)) {+ Some(+ wgt::TextureSampleType::Float { filterable: false }+ | wgt::TextureSampleType::Uint+ | wgt::TextureSampleType::Sint,+ ) => {+ // reset default filtering mode+ unsafe {+ gl.tex_parameter_i32(target, glow::TEXTURE_MIN_FILTER, glow::NEAREST as i32)+ };+ unsafe {+ gl.tex_parameter_i32(target, glow::TEXTURE_MAG_FILTER, glow::NEAREST as i32)+ };+ }+ _ => {}+ }++ if conv::is_layered_target(target) {+ unsafe {+ if self+ .shared+ .private_caps+ .contains(PrivateCapabilities::TEXTURE_STORAGE)+ {+ gl.tex_storage_3d(+ target,+ desc.mip_level_count as i32,+ format_desc.internal,+ desc.size.width as i32,+ desc.size.height as i32,+ desc.size.depth_or_array_layers as i32,+ )+ } else if target == glow::TEXTURE_3D {+ let mut width = desc.size.width;+ let mut height = desc.size.height;+ let mut depth = desc.size.depth_or_array_layers;+ for i in 0..desc.mip_level_count {+ gl.tex_image_3d(+ target,+ i as i32,+ format_desc.internal as i32,+ width as i32,+ height as i32,+ depth as i32,+ 0,+ format_desc.external,+ format_desc.data_type,+ glow::PixelUnpackData::Slice(None),+ );+ width = max(1, width / 2);+ height = max(1, height / 2);+ depth = max(1, depth / 2);+ }+ } else {+ let mut width = desc.size.width;+ let mut height = desc.size.height;+ for i in 0..desc.mip_level_count {+ gl.tex_image_3d(+ target,+ i as i32,+ format_desc.internal as i32,+ width as i32,+ height as i32,+ desc.size.depth_or_array_layers as i32,+ 0,+ format_desc.external,+ format_desc.data_type,+ glow::PixelUnpackData::Slice(None),+ );+ width = max(1, width / 2);+ height = max(1, height / 2);+ }+ }+ };+ } else if desc.sample_count > 1 {+ unsafe {+ gl.tex_storage_2d_multisample(+ target,+ desc.sample_count as i32,+ format_desc.internal,+ desc.size.width as i32,+ desc.size.height as i32,+ true,+ )+ };+ } else {+ unsafe {+ if self+ .shared+ .private_caps+ .contains(PrivateCapabilities::TEXTURE_STORAGE)+ {+ gl.tex_storage_2d(+ target,+ desc.mip_level_count as i32,+ format_desc.internal,+ desc.size.width as i32,+ desc.size.height as i32,+ )+ } else if target == glow::TEXTURE_CUBE_MAP {+ let mut width = desc.size.width;+ let mut height = desc.size.height;+ for i in 0..desc.mip_level_count {+ for face in [+ glow::TEXTURE_CUBE_MAP_POSITIVE_X,+ glow::TEXTURE_CUBE_MAP_NEGATIVE_X,+ glow::TEXTURE_CUBE_MAP_POSITIVE_Y,+ glow::TEXTURE_CUBE_MAP_NEGATIVE_Y,+ glow::TEXTURE_CUBE_MAP_POSITIVE_Z,+ glow::TEXTURE_CUBE_MAP_NEGATIVE_Z,+ ] {+ gl.tex_image_2d(+ face,+ i as i32,+ format_desc.internal as i32,+ width as i32,+ height as i32,+ 0,+ format_desc.external,+ format_desc.data_type,+ glow::PixelUnpackData::Slice(None),+ );+ }+ width = max(1, width / 2);+ height = max(1, height / 2);+ }+ } else {+ let mut width = desc.size.width;+ let mut height = desc.size.height;+ for i in 0..desc.mip_level_count {+ gl.tex_image_2d(+ target,+ i as i32,+ format_desc.internal as i32,+ width as i32,+ height as i32,+ 0,+ format_desc.external,+ format_desc.data_type,+ glow::PixelUnpackData::Slice(None),+ );+ width = max(1, width / 2);+ height = max(1, height / 2);+ }+ }+ };+ }++ #[cfg(native)]+ if let Some(label) = desc.label {+ if self+ .shared+ .private_caps+ .contains(PrivateCapabilities::DEBUG_FNS)+ {+ let name = raw.0.get();+ unsafe { gl.object_label(glow::TEXTURE, name, Some(label)) };+ }+ }++ unsafe { gl.bind_texture(target, None) };+ super::TextureInner::Texture { raw, target }+ };++ self.counters.textures.add(1);++ Ok(super::Texture {+ inner,+ drop_guard: None,+ mip_level_count: desc.mip_level_count,+ array_layer_count: desc.array_layer_count(),+ format: desc.format,+ format_desc,+ copy_size: desc.copy_extent(),+ })+ }++ unsafe fn destroy_texture(&self, texture: super::Texture) {+ if texture.drop_guard.is_none() {+ let gl = &self.shared.context.lock();+ match texture.inner {+ super::TextureInner::Renderbuffer { raw, .. } => {+ unsafe { gl.delete_renderbuffer(raw) };+ }+ super::TextureInner::DefaultRenderbuffer => {}+ super::TextureInner::Texture { raw, .. } => {+ unsafe { gl.delete_texture(raw) };+ }+ #[cfg(webgl)]+ super::TextureInner::ExternalFramebuffer { .. } => {}+ #[cfg(native)]+ super::TextureInner::ExternalNativeFramebuffer { .. } => {}+ }+ }++ // For clarity, we explicitly drop the drop guard. Although this has no real semantic effect as the+ // end of the scope will drop the drop guard since this function takes ownership of the texture.+ drop(texture.drop_guard);++ self.counters.textures.sub(1);+ }++ unsafe fn add_raw_texture(&self, _texture: &super::Texture) {+ self.counters.textures.add(1);+ }++ unsafe fn create_texture_view(+ &self,+ texture: &super::Texture,+ desc: &crate::TextureViewDescriptor,+ ) -> Result<super::TextureView, crate::DeviceError> {+ self.counters.texture_views.add(1);+ Ok(super::TextureView {+ //TODO: use `conv::map_view_dimension(desc.dimension)`?+ inner: texture.inner.clone(),+ aspects: crate::FormatAspects::new(texture.format, desc.range.aspect),+ mip_levels: desc.range.mip_range(texture.mip_level_count),+ array_layers: desc.range.layer_range(texture.array_layer_count),+ format: texture.format,+ })+ }++ unsafe fn destroy_texture_view(&self, _view: super::TextureView) {+ self.counters.texture_views.sub(1);+ }++ unsafe fn create_sampler(+ &self,+ desc: &crate::SamplerDescriptor,+ ) -> Result<super::Sampler, crate::DeviceError> {+ let gl = &self.shared.context.lock();++ let raw = unsafe { gl.create_sampler().unwrap() };++ let (min, mag) =+ conv::map_filter_modes(desc.min_filter, desc.mag_filter, desc.mipmap_filter);++ unsafe { gl.sampler_parameter_i32(raw, glow::TEXTURE_MIN_FILTER, min as i32) };+ unsafe { gl.sampler_parameter_i32(raw, glow::TEXTURE_MAG_FILTER, mag as i32) };++ unsafe {+ gl.sampler_parameter_i32(+ raw,+ glow::TEXTURE_WRAP_S,+ conv::map_address_mode(desc.address_modes[0]) as i32,+ )+ };+ unsafe {+ gl.sampler_parameter_i32(+ raw,+ glow::TEXTURE_WRAP_T,+ conv::map_address_mode(desc.address_modes[1]) as i32,+ )+ };+ unsafe {+ gl.sampler_parameter_i32(+ raw,+ glow::TEXTURE_WRAP_R,+ conv::map_address_mode(desc.address_modes[2]) as i32,+ )+ };++ if let Some(border_color) = desc.border_color {+ let border = match border_color {+ wgt::SamplerBorderColor::TransparentBlack | wgt::SamplerBorderColor::Zero => {+ [0.0; 4]+ }+ wgt::SamplerBorderColor::OpaqueBlack => [0.0, 0.0, 0.0, 1.0],+ wgt::SamplerBorderColor::OpaqueWhite => [1.0; 4],+ };+ unsafe { gl.sampler_parameter_f32_slice(raw, glow::TEXTURE_BORDER_COLOR, &border) };+ }++ unsafe { gl.sampler_parameter_f32(raw, glow::TEXTURE_MIN_LOD, desc.lod_clamp.start) };+ unsafe { gl.sampler_parameter_f32(raw, glow::TEXTURE_MAX_LOD, desc.lod_clamp.end) };++ // If clamp is not 1, we know anisotropy is supported up to 16x+ if desc.anisotropy_clamp != 1 {+ unsafe {+ gl.sampler_parameter_i32(+ raw,+ glow::TEXTURE_MAX_ANISOTROPY,+ desc.anisotropy_clamp as i32,+ )+ };+ }++ //set_param_float(glow::TEXTURE_LOD_BIAS, info.lod_bias.0);++ if let Some(compare) = desc.compare {+ unsafe {+ gl.sampler_parameter_i32(+ raw,+ glow::TEXTURE_COMPARE_MODE,+ glow::COMPARE_REF_TO_TEXTURE as i32,+ )+ };+ unsafe {+ gl.sampler_parameter_i32(+ raw,+ glow::TEXTURE_COMPARE_FUNC,+ conv::map_compare_func(compare) as i32,+ )+ };+ }++ #[cfg(native)]+ if let Some(label) = desc.label {+ if self+ .shared+ .private_caps+ .contains(PrivateCapabilities::DEBUG_FNS)+ {+ let name = raw.0.get();+ unsafe { gl.object_label(glow::SAMPLER, name, Some(label)) };+ }+ }++ self.counters.samplers.add(1);++ Ok(super::Sampler { raw })+ }++ unsafe fn destroy_sampler(&self, sampler: super::Sampler) {+ let gl = &self.shared.context.lock();+ unsafe { gl.delete_sampler(sampler.raw) };+ self.counters.samplers.sub(1);+ }++ unsafe fn create_command_encoder(+ &self,+ _desc: &crate::CommandEncoderDescriptor<super::Queue>,+ ) -> Result<super::CommandEncoder, crate::DeviceError> {+ self.counters.command_encoders.add(1);++ Ok(super::CommandEncoder {+ cmd_buffer: super::CommandBuffer::default(),+ state: Default::default(),+ private_caps: self.shared.private_caps,+ counters: Arc::clone(&self.counters),+ })+ }++ unsafe fn create_bind_group_layout(+ &self,+ desc: &crate::BindGroupLayoutDescriptor,+ ) -> Result<super::BindGroupLayout, crate::DeviceError> {+ self.counters.bind_group_layouts.add(1);+ Ok(super::BindGroupLayout {+ entries: Arc::from(desc.entries),+ })+ }++ unsafe fn destroy_bind_group_layout(&self, _bg_layout: super::BindGroupLayout) {+ self.counters.bind_group_layouts.sub(1);+ }++ unsafe fn create_pipeline_layout(+ &self,+ desc: &crate::PipelineLayoutDescriptor<super::BindGroupLayout>,+ ) -> Result<super::PipelineLayout, crate::DeviceError> {+ use naga::back::glsl;++ let mut group_infos = Vec::with_capacity(desc.bind_group_layouts.len());+ let mut num_samplers = 0u8;+ let mut num_textures = 0u8;+ let mut num_images = 0u8;+ let mut num_uniform_buffers = 0u8;+ let mut num_storage_buffers = 0u8;++ let mut writer_flags = glsl::WriterFlags::ADJUST_COORDINATE_SPACE;+ writer_flags.set(+ glsl::WriterFlags::TEXTURE_SHADOW_LOD,+ self.shared+ .private_caps+ .contains(PrivateCapabilities::SHADER_TEXTURE_SHADOW_LOD),+ );+ writer_flags.set(+ glsl::WriterFlags::DRAW_PARAMETERS,+ self.shared+ .private_caps+ .contains(PrivateCapabilities::FULLY_FEATURED_INSTANCING),+ );+ // We always force point size to be written and it will be ignored by the driver if it's not a point list primitive.+ // https://github.com/gfx-rs/wgpu/pull/3440/files#r1095726950+ writer_flags.set(glsl::WriterFlags::FORCE_POINT_SIZE, true);+ let mut binding_map = glsl::BindingMap::default();++ for (group_index, bg_layout) in desc.bind_group_layouts.iter().enumerate() {+ let Some(bg_layout) = bg_layout else {+ group_infos.push(None);+ continue;+ };++ // create a vector with the size enough to hold all the bindings, filled with `!0`+ let mut binding_to_slot = vec![+ !0;+ bg_layout+ .entries+ .iter()+ .map(|b| b.binding)+ .max()+ .map_or(0, |idx| idx as usize + 1)+ ]+ .into_boxed_slice();++ for entry in bg_layout.entries.iter() {+ let counter = match entry.ty {+ wgt::BindingType::Sampler { .. } => &mut num_samplers,+ wgt::BindingType::Texture { .. } => &mut num_textures,+ wgt::BindingType::StorageTexture { .. } => &mut num_images,+ wgt::BindingType::Buffer {+ ty: wgt::BufferBindingType::Uniform,+ ..+ } => &mut num_uniform_buffers,+ wgt::BindingType::Buffer {+ ty: wgt::BufferBindingType::Storage { .. },+ ..+ } => &mut num_storage_buffers,+ wgt::BindingType::AccelerationStructure { .. } => unimplemented!(),+ wgt::BindingType::ExternalTexture => unimplemented!(),+ };++ binding_to_slot[entry.binding as usize] = *counter;+ let br = naga::ResourceBinding {+ group: group_index as u32,+ binding: entry.binding,+ };+ binding_map.insert(br, *counter);+ *counter += entry.count.map_or(1, |c| c.get() as u8);+ }++ group_infos.push(Some(super::BindGroupLayoutInfo {+ entries: Arc::clone(&bg_layout.entries),+ binding_to_slot,+ }));+ }++ self.counters.pipeline_layouts.add(1);++ Ok(super::PipelineLayout {+ group_infos: group_infos.into_boxed_slice(),+ naga_options: glsl::Options {+ version: self.shared.shading_language_version,+ writer_flags,+ binding_map,+ zero_initialize_workgroup_memory: true,+ },+ })+ }++ unsafe fn destroy_pipeline_layout(&self, _pipeline_layout: super::PipelineLayout) {+ self.counters.pipeline_layouts.sub(1);+ }++ unsafe fn create_bind_group(+ &self,+ desc: &crate::BindGroupDescriptor<+ super::BindGroupLayout,+ super::Buffer,+ super::Sampler,+ super::TextureView,+ super::AccelerationStructure,+ >,+ ) -> Result<super::BindGroup, crate::DeviceError> {+ let mut contents = Vec::new();++ let layout_and_entry_iter = desc.entries.iter().map(|entry| {+ let layout = desc+ .layout+ .entries+ .iter()+ .find(|layout_entry| layout_entry.binding == entry.binding)+ .expect("internal error: no layout entry found with binding slot");+ (entry, layout)+ });+ for (entry, layout) in layout_and_entry_iter {+ let binding = match layout.ty {+ wgt::BindingType::Buffer { .. } => {+ let bb = &desc.buffers[entry.resource_index as usize];+ super::RawBinding::Buffer {+ raw: bb.buffer.raw.unwrap(),+ offset: bb.offset as i32,+ size: match bb.size {+ Some(s) => s.get() as i32,+ None => (bb.buffer.size - bb.offset) as i32,+ },+ }+ }+ wgt::BindingType::Sampler { .. } => {+ let sampler = desc.samplers[entry.resource_index as usize];+ super::RawBinding::Sampler(sampler.raw)+ }+ wgt::BindingType::Texture { view_dimension, .. } => {+ let view = desc.textures[entry.resource_index as usize].view;+ if view.array_layers.start != 0 {+ log::error!("Unable to create a sampled texture binding for non-zero array layer.\n{}",+ "This is an implementation problem of wgpu-hal/gles backend.")+ }+ let (raw, target) = view.inner.as_native();++ super::Texture::log_failing_target_heuristics(view_dimension, target);++ super::RawBinding::Texture {+ raw,+ target,+ aspects: view.aspects,+ mip_levels: view.mip_levels.clone(),+ }+ }+ wgt::BindingType::StorageTexture {+ access,+ format,+ view_dimension,+ } => {+ let view = desc.textures[entry.resource_index as usize].view;+ let format_desc = self.shared.describe_texture_format(format);+ let (raw, _target) = view.inner.as_native();+ super::RawBinding::Image(super::ImageBinding {+ raw,+ mip_level: view.mip_levels.start,+ array_layer: match view_dimension {+ wgt::TextureViewDimension::D2Array+ | wgt::TextureViewDimension::CubeArray => None,+ _ => Some(view.array_layers.start),+ },+ access: conv::map_storage_access(access),+ format: format_desc.internal,+ })+ }+ wgt::BindingType::AccelerationStructure { .. } => unimplemented!(),+ wgt::BindingType::ExternalTexture => unimplemented!(),+ };+ contents.push(binding);+ }++ self.counters.bind_groups.add(1);++ Ok(super::BindGroup {+ contents: contents.into_boxed_slice(),+ })+ }++ unsafe fn destroy_bind_group(&self, _group: super::BindGroup) {+ self.counters.bind_groups.sub(1);+ }++ unsafe fn create_shader_module(+ &self,+ desc: &crate::ShaderModuleDescriptor,+ shader: crate::ShaderInput,+ ) -> Result<super::ShaderModule, crate::ShaderError> {+ self.counters.shader_modules.add(1);++ Ok(super::ShaderModule {+ source: match shader {+ crate::ShaderInput::Naga(naga) => super::ShaderModuleSource::Naga(naga),+ // The backend doesn't yet expose this feature so it should be fine+ crate::ShaderInput::Glsl { shader, .. } => super::ShaderModuleSource::Passthrough {+ source: shader.to_owned(),+ },+ crate::ShaderInput::SpirV(_)+ | crate::ShaderInput::MetalLib { .. }+ | crate::ShaderInput::Msl { .. }+ | crate::ShaderInput::Dxil { .. }+ | crate::ShaderInput::Hlsl { .. } => {+ unreachable!()+ }+ },+ label: desc.label.map(|str| str.to_string()),+ id: self.shared.next_shader_id.fetch_add(1, Ordering::Relaxed),+ })+ }++ unsafe fn destroy_shader_module(&self, _module: super::ShaderModule) {+ self.counters.shader_modules.sub(1);+ }++ unsafe fn create_render_pipeline(+ &self,+ desc: &crate::RenderPipelineDescriptor<+ super::PipelineLayout,+ super::ShaderModule,+ super::PipelineCache,+ >,+ ) -> Result<super::RenderPipeline, crate::PipelineError> {+ let (vertex_stage, vertex_buffers) = match &desc.vertex_processor {+ crate::VertexProcessor::Standard {+ vertex_buffers,+ ref vertex_stage,+ } => (vertex_stage, vertex_buffers),+ crate::VertexProcessor::Mesh { .. } => unreachable!(),+ };+ let gl = &self.shared.context.lock();+ let mut shaders = ArrayVec::new();+ shaders.push((naga::ShaderStage::Vertex, vertex_stage));+ if let Some(ref fs) = desc.fragment_stage {+ shaders.push((naga::ShaderStage::Fragment, fs));+ }+ let inner = unsafe {+ self.create_pipeline(gl, shaders, desc.layout, desc.label, desc.multiview_mask)+ }?;++ let (vertex_buffers, vertex_attributes) = {+ let mut buffers = Vec::new();+ let mut attributes = Vec::new();+ for (index, vb_layout) in vertex_buffers.iter().enumerate() {+ let vb_desc = if let Some(vb_layout) = vb_layout {+ for vat in vb_layout.attributes.iter() {+ let format_desc = conv::describe_vertex_format(vat.format);+ attributes.push(super::AttributeDesc {+ location: vat.shader_location,+ offset: vat.offset as u32,+ buffer_index: index as u32,+ format_desc,+ });+ }+ Some(super::VertexBufferDesc {+ step: vb_layout.step_mode,+ stride: vb_layout.array_stride as u32,+ })+ } else {+ None+ };+ buffers.push(vb_desc);+ }+ (buffers.into_boxed_slice(), attributes.into_boxed_slice())+ };++ let color_targets = {+ let mut targets = Vec::new();+ for ct in desc.color_targets.iter().filter_map(|at| at.as_ref()) {+ targets.push(super::ColorTargetDesc {+ mask: ct.write_mask,+ blend: ct.blend.as_ref().map(conv::map_blend),+ });+ }+ //Note: if any of the states are different, and `INDEPENDENT_BLEND` flag+ // is not exposed, then this pipeline will not bind correctly.+ targets.into_boxed_slice()+ };++ self.counters.render_pipelines.add(1);++ Ok(super::RenderPipeline {+ inner,+ primitive: desc.primitive,+ vertex_buffers,+ vertex_attributes,+ color_targets,+ depth: desc.depth_stencil.as_ref().map(|ds| super::DepthState {+ function: conv::map_compare_func(ds.depth_compare.unwrap_or_default()),+ mask: ds.depth_write_enabled.unwrap_or_default(),+ }),+ depth_bias: desc+ .depth_stencil+ .as_ref()+ .map(|ds| ds.bias)+ .unwrap_or_default(),+ stencil: desc+ .depth_stencil+ .as_ref()+ .map(|ds| conv::map_stencil(&ds.stencil)),+ alpha_to_coverage_enabled: desc.multisample.alpha_to_coverage_enabled,+ })+ }++ unsafe fn destroy_render_pipeline(&self, pipeline: super::RenderPipeline) {+ // If the pipeline only has 2 strong references remaining, they're `pipeline` and `program_cache`+ // This is safe to assume as long as:+ // - `RenderPipeline` can't be cloned+ // - The only place that we can get a new reference is during `program_cache.lock()`+ if Arc::strong_count(&pipeline.inner) == 2 {+ let gl = &self.shared.context.lock();+ let mut program_cache = self.shared.program_cache.lock();+ program_cache.retain(|_, v| match *v {+ Ok(ref p) => p.program != pipeline.inner.program,+ Err(_) => false,+ });+ unsafe { gl.delete_program(pipeline.inner.program) };+ }++ self.counters.render_pipelines.sub(1);+ }++ unsafe fn create_compute_pipeline(+ &self,+ desc: &crate::ComputePipelineDescriptor<+ super::PipelineLayout,+ super::ShaderModule,+ super::PipelineCache,+ >,+ ) -> Result<super::ComputePipeline, crate::PipelineError> {+ let gl = &self.shared.context.lock();+ let mut shaders = ArrayVec::new();+ shaders.push((naga::ShaderStage::Compute, &desc.stage));+ let inner = unsafe { self.create_pipeline(gl, shaders, desc.layout, desc.label, None) }?;++ self.counters.compute_pipelines.add(1);++ Ok(super::ComputePipeline { inner })+ }++ unsafe fn destroy_compute_pipeline(&self, pipeline: super::ComputePipeline) {+ // If the pipeline only has 2 strong references remaining, they're `pipeline` and `program_cache``+ // This is safe to assume as long as:+ // - `ComputePipeline` can't be cloned+ // - The only place that we can get a new reference is during `program_cache.lock()`+ if Arc::strong_count(&pipeline.inner) == 2 {+ let gl = &self.shared.context.lock();+ let mut program_cache = self.shared.program_cache.lock();+ program_cache.retain(|_, v| match *v {+ Ok(ref p) => p.program != pipeline.inner.program,+ Err(_) => false,+ });+ unsafe { gl.delete_program(pipeline.inner.program) };+ }++ self.counters.compute_pipelines.sub(1);+ }++ unsafe fn create_ray_tracing_pipeline(+ &self,+ _desc: &crate::RayTracingPipelineDescriptor<+ super::PipelineLayout,+ super::ShaderModule,+ super::PipelineCache,+ >,+ ) -> Result<super::RayTracingPipeline, crate::PipelineError> {+ unimplemented!("Ray tracing is unsupported on GL")+ }++ unsafe fn destroy_ray_tracing_pipeline(&self, _pipeline: super::RayTracingPipeline) {+ unimplemented!("Ray tracing is unsupported on GL")+ }++ unsafe fn get_raytracing_pipeline_group_data(+ &self,+ _pipeline: &super::RayTracingPipeline,+ _groups: core::ops::Range<u32>,+ ) -> Result<Vec<u8>, crate::DeviceError> {+ unimplemented!("Ray tracing is unsupported on GL")+ }++ unsafe fn create_pipeline_cache(+ &self,+ _: &crate::PipelineCacheDescriptor<'_>,+ ) -> Result<super::PipelineCache, crate::PipelineCacheError> {+ // Even though the cache doesn't do anything, we still return something here+ // as the least bad option+ Ok(super::PipelineCache)+ }+ unsafe fn destroy_pipeline_cache(&self, _: super::PipelineCache) {}++ #[cfg_attr(target_arch = "wasm32", allow(unused))]+ unsafe fn create_query_set(+ &self,+ desc: &wgt::QuerySetDescriptor<crate::Label>,+ ) -> Result<super::QuerySet, crate::DeviceError> {+ let gl = &self.shared.context.lock();++ let mut queries = Vec::with_capacity(desc.count as usize);+ for _ in 0..desc.count {+ let query =+ unsafe { gl.create_query() }.map_err(|_| crate::DeviceError::OutOfMemory)?;++ // We aren't really able to, in general, label queries.+ //+ // We could take a timestamp here to "initialize" the query,+ // but that's a bit of a hack, and we don't want to insert+ // random timestamps into the command stream of we don't have to.++ queries.push(query);+ }++ self.counters.query_sets.add(1);++ Ok(super::QuerySet {+ queries: queries.into_boxed_slice(),+ target: match desc.ty {+ wgt::QueryType::Occlusion => glow::ANY_SAMPLES_PASSED_CONSERVATIVE,+ wgt::QueryType::Timestamp => glow::TIMESTAMP,+ _ => unimplemented!(),+ },+ })+ }++ unsafe fn destroy_query_set(&self, set: super::QuerySet) {+ let gl = &self.shared.context.lock();+ for &query in set.queries.iter() {+ unsafe { gl.delete_query(query) };+ }+ self.counters.query_sets.sub(1);+ }++ unsafe fn create_fence(&self) -> Result<super::Fence, crate::DeviceError> {+ self.counters.fences.add(1);+ Ok(super::Fence::new(&self.shared.options))+ }++ unsafe fn destroy_fence(&self, fence: super::Fence) {+ let gl = &self.shared.context.lock();+ fence.destroy(gl);+ self.counters.fences.sub(1);+ }++ unsafe fn get_fence_value(+ &self,+ fence: &super::Fence,+ ) -> Result<crate::FenceValue, crate::DeviceError> {+ #[cfg_attr(target_arch = "wasm32", allow(clippy::needless_borrow))]+ Ok(fence.get_latest(&self.shared.context.lock()))+ }+ unsafe fn wait(+ &self,+ fence: &super::Fence,+ wait_value: crate::FenceValue,+ timeout: Option<core::time::Duration>,+ ) -> Result<bool, crate::DeviceError> {+ if fence.satisfied(wait_value) {+ return Ok(true);+ }++ let gl = &self.shared.context.lock();+ // MAX_CLIENT_WAIT_TIMEOUT_WEBGL is:+ // - 1s in Gecko https://searchfox.org/mozilla-central/rev/754074e05178e017ef6c3d8e30428ffa8f1b794d/dom/canvas/WebGLTypes.h#1386+ // - 0 in WebKit https://github.com/WebKit/WebKit/blob/4ef90d4672ca50267c0971b85db403d9684508ea/Source/WebCore/html/canvas/WebGL2RenderingContext.cpp#L110+ // - 0 in Chromium https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/modules/webgl/webgl2_rendering_context_base.cc;l=112;drc=a3cb0ac4c71ec04abfeaed199e5d63230eca2551+ let timeout_ns = if cfg!(any(webgl, Emscripten)) {+ 0+ } else {+ timeout+ .map(|t| t.as_nanos().min(u32::MAX as u128) as u32)+ .unwrap_or(u32::MAX)+ };+ fence.wait(gl, wait_value, timeout_ns)+ }++ unsafe fn start_graphics_debugger_capture(&self) -> bool {+ #[cfg(all(native, feature = "renderdoc"))]+ return unsafe {+ self.render_doc+ .start_frame_capture(self.shared.context.raw_context(), ptr::null_mut())+ };+ #[allow(unreachable_code)]+ false+ }+ unsafe fn stop_graphics_debugger_capture(&self) {+ #[cfg(all(native, feature = "renderdoc"))]+ unsafe {+ self.render_doc+ .end_frame_capture(ptr::null_mut(), ptr::null_mut())+ }+ }+ unsafe fn create_acceleration_structure(+ &self,+ _desc: &crate::AccelerationStructureDescriptor,+ ) -> Result<super::AccelerationStructure, crate::DeviceError> {+ unimplemented!()+ }+ unsafe fn get_acceleration_structure_build_sizes<'a>(+ &self,+ _desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, super::Buffer>,+ ) -> crate::AccelerationStructureBuildSizes {+ unimplemented!()+ }+ unsafe fn get_acceleration_structure_device_address(+ &self,+ _acceleration_structure: &super::AccelerationStructure,+ ) -> wgt::BufferAddress {+ unimplemented!()+ }+ unsafe fn destroy_acceleration_structure(+ &self,+ _acceleration_structure: super::AccelerationStructure,+ ) {+ }++ fn tlas_instance_to_bytes(&self, _instance: TlasInstance) -> Vec<u8> {+ unimplemented!()+ }++ fn get_internal_counters(&self) -> wgt::HalCounters {+ self.counters.as_ref().clone()+ }++ fn check_if_oom(&self) -> Result<(), crate::DeviceError> {+ Ok(())+ }+}++#[cfg(send_sync)]+unsafe impl Sync for super::Device {}+#[cfg(send_sync)]+unsafe impl Send for super::Device {}diff --git a/vendor/wgpu-hal/src/gles/egl.rs b/vendor/wgpu-hal/src/gles/egl.rsnew file mode 100644index 00000000..eeb73694--- /dev/null+++ b/vendor/wgpu-hal/src/gles/egl.rs@@ -0,0 +1,1470 @@+use alloc::{string::String, sync::Arc, vec::Vec};+use core::{ffi, mem::ManuallyDrop, ptr, time::Duration};+use std::sync::LazyLock;++use glow::HasContext;+use hashbrown::HashMap;+use parking_lot::{MappedMutexGuard, Mutex, MutexGuard, RwLock};++/// The amount of time to wait while trying to obtain a lock to the adapter context+const CONTEXT_LOCK_TIMEOUT_SECS: u64 = 6;++const EGL_CONTEXT_FLAGS_KHR: i32 = 0x30FC;+const EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR: i32 = 0x0001;+const EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT: i32 = 0x30BF;+const EGL_PLATFORM_WAYLAND_KHR: u32 = 0x31D8;+const EGL_PLATFORM_X11_KHR: u32 = 0x31D5;+const EGL_PLATFORM_XCB_EXT: u32 = 0x31DC;+const EGL_PLATFORM_XCB_SCREEN_EXT: u32 = 0x31DE;+const EGL_PLATFORM_ANGLE_ANGLE: u32 = 0x3202;+const EGL_PLATFORM_ANGLE_NATIVE_PLATFORM_TYPE_ANGLE: u32 = 0x348F;+const EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED: u32 = 0x3451;+const EGL_PLATFORM_SURFACELESS_MESA: u32 = 0x31DD;+const EGL_GL_COLORSPACE_KHR: u32 = 0x309D;+const EGL_GL_COLORSPACE_SRGB_KHR: u32 = 0x3089;++#[cfg(not(Emscripten))]+type EglInstance = khronos_egl::DynamicInstance<khronos_egl::EGL1_4>;++#[cfg(Emscripten)]+type EglInstance = khronos_egl::Instance<khronos_egl::Static>;++type EglLabel = *const ffi::c_void;++#[allow(clippy::upper_case_acronyms)]+type EGLDEBUGPROCKHR = Option<+ unsafe extern "system" fn(+ error: khronos_egl::Enum,+ command: *const ffi::c_char,+ message_type: u32,+ thread_label: EglLabel,+ object_label: EglLabel,+ message: *const ffi::c_char,+ ),+>;++const EGL_DEBUG_MSG_CRITICAL_KHR: u32 = 0x33B9;+const EGL_DEBUG_MSG_ERROR_KHR: u32 = 0x33BA;+const EGL_DEBUG_MSG_WARN_KHR: u32 = 0x33BB;+const EGL_DEBUG_MSG_INFO_KHR: u32 = 0x33BC;++type EglDebugMessageControlFun = unsafe extern "system" fn(+ proc: EGLDEBUGPROCKHR,+ attrib_list: *const khronos_egl::Attrib,+) -> ffi::c_int;++unsafe extern "system" fn egl_debug_proc(+ error: khronos_egl::Enum,+ command_raw: *const ffi::c_char,+ message_type: u32,+ _thread_label: EglLabel,+ _object_label: EglLabel,+ message_raw: *const ffi::c_char,+) {+ let log_severity = match message_type {+ EGL_DEBUG_MSG_CRITICAL_KHR | EGL_DEBUG_MSG_ERROR_KHR => log::Level::Error,+ EGL_DEBUG_MSG_WARN_KHR => log::Level::Warn,+ // We intentionally suppress info messages down to debug+ // so that users are not inundated with info messages from+ // the runtime.+ EGL_DEBUG_MSG_INFO_KHR => log::Level::Debug,+ _ => log::Level::Trace,+ };+ let command = unsafe { ffi::CStr::from_ptr(command_raw) }.to_string_lossy();+ let message = if message_raw.is_null() {+ "".into()+ } else {+ unsafe { ffi::CStr::from_ptr(message_raw) }.to_string_lossy()+ };++ log::log!(log_severity, "EGL '{command}' code 0x{error:x}: {message}",);+}++#[derive(Clone, Copy, Debug)]+enum SrgbFrameBufferKind {+ /// No support for SRGB surface+ None,+ /// Using EGL 1.5's support for colorspaces+ Core,+ /// Using EGL_KHR_gl_colorspace+ Khr,+}++/// Choose GLES framebuffer configuration.+fn choose_config(+ egl: &EglInstance,+ display: khronos_egl::Display,+ srgb_kind: SrgbFrameBufferKind,+) -> Result<(khronos_egl::Config, bool), crate::InstanceError> {+ //TODO: EGL_SLOW_CONFIG+ let tiers = [+ (+ "off-screen",+ &[+ khronos_egl::SURFACE_TYPE,+ khronos_egl::PBUFFER_BIT,+ khronos_egl::RENDERABLE_TYPE,+ khronos_egl::OPENGL_ES2_BIT,+ ][..],+ ),+ (+ "presentation",+ &[khronos_egl::SURFACE_TYPE, khronos_egl::WINDOW_BIT][..],+ ),+ #[cfg(not(target_os = "android"))]+ (+ "native-render",+ &[khronos_egl::NATIVE_RENDERABLE, khronos_egl::TRUE as _][..],+ ),+ ];++ let mut attributes = Vec::with_capacity(9);+ for tier_max in (0..tiers.len()).rev() {+ let name = tiers[tier_max].0;+ log::debug!("\tTrying {name}");++ attributes.clear();+ for &(_, tier_attr) in tiers[..=tier_max].iter() {+ attributes.extend_from_slice(tier_attr);+ }+ // make sure the Alpha is enough to support sRGB+ match srgb_kind {+ SrgbFrameBufferKind::None => {}+ _ => {+ attributes.push(khronos_egl::ALPHA_SIZE);+ attributes.push(8);+ }+ }+ attributes.push(khronos_egl::NONE);++ match egl.choose_first_config(display, &attributes) {+ Ok(Some(config)) => {+ if tier_max == 1 {+ //Note: this has been confirmed to malfunction on Intel+NV laptops,+ // but also on Angle.+ log::info!("EGL says it can present to the window but not natively",);+ }+ // Android emulator can't natively present either.+ let tier_threshold =+ if cfg!(target_os = "android") || cfg!(windows) || cfg!(target_env = "ohos") {+ 1+ } else {+ 2+ };+ return Ok((config, tier_max >= tier_threshold));+ }+ Ok(None) => {+ log::debug!("No config found!");+ }+ Err(e) => {+ log::error!("error in choose_first_config: {e:?}");+ }+ }+ }++ // TODO: include diagnostic details that are currently logged+ Err(crate::InstanceError::new(String::from(+ "unable to find an acceptable EGL framebuffer configuration",+ )))+}++#[derive(Clone, Debug)]+struct EglContext {+ instance: Arc<EglInstance>,+ version: (i32, i32),+ display: khronos_egl::Display,+ raw: khronos_egl::Context,+ pbuffer: Option<khronos_egl::Surface>,+}++impl EglContext {+ fn make_current(&self) {+ self.instance+ .make_current(self.display, self.pbuffer, self.pbuffer, Some(self.raw))+ .unwrap();+ }++ fn unmake_current(&self) {+ self.instance+ .make_current(self.display, None, None, None)+ .unwrap();+ }+}++/// A wrapper around a [`glow::Context`] and the required EGL context that uses locking to guarantee+/// exclusive access when shared with multiple threads.+#[derive(Debug)]+pub struct AdapterContext {+ glow: Mutex<ManuallyDrop<glow::Context>>,+ egl: Option<EglContext>,+}++unsafe impl Sync for AdapterContext {}+unsafe impl Send for AdapterContext {}++impl AdapterContext {+ pub fn is_owned(&self) -> bool {+ self.egl.is_some()+ }++ /// Returns the EGL instance.+ ///+ /// This provides access to EGL functions and the ability to load GL and EGL extension functions.+ pub fn egl_instance(&self) -> Option<&EglInstance> {+ self.egl.as_ref().map(|egl| &*egl.instance)+ }++ /// Returns the EGLDisplay corresponding to the adapter context.+ ///+ /// Returns [`None`] if the adapter was externally created.+ pub fn raw_display(&self) -> Option<&khronos_egl::Display> {+ self.egl.as_ref().map(|egl| &egl.display)+ }++ /// Returns the EGL version the adapter context was created with.+ ///+ /// Returns [`None`] if the adapter was externally created.+ pub fn egl_version(&self) -> Option<(i32, i32)> {+ self.egl.as_ref().map(|egl| egl.version)+ }++ pub fn raw_context(&self) -> *mut ffi::c_void {+ match self.egl {+ Some(ref egl) => egl.raw.as_ptr(),+ None => ptr::null_mut(),+ }+ }+}++impl Drop for AdapterContext {+ fn drop(&mut self) {+ struct CurrentGuard<'a>(&'a EglContext);+ impl Drop for CurrentGuard<'_> {+ fn drop(&mut self) {+ self.0.unmake_current();+ }+ }++ // Context must be current when dropped. See safety docs on+ // `glow::HasContext`.+ //+ // NOTE: This is only set to `None` by `Adapter::new_external` which+ // requires the context to be current when anything that may be holding+ // the `Arc<AdapterShared>` is dropped.+ let _guard = self.egl.as_ref().map(|egl| {+ egl.make_current();+ CurrentGuard(egl)+ });+ let glow = self.glow.get_mut();+ // SAFETY: Field not used after this.+ unsafe { ManuallyDrop::drop(glow) };+ }+}++struct EglContextLock<'a> {+ instance: &'a Arc<EglInstance>,+ display: khronos_egl::Display,+}++/// A guard containing a lock to an [`AdapterContext`], while the GL context is kept current.+#[expect(missing_debug_implementations)]+pub struct AdapterContextLock<'a> {+ glow: MutexGuard<'a, ManuallyDrop<glow::Context>>,+ egl: Option<EglContextLock<'a>>,+}++impl<'a> core::ops::Deref for AdapterContextLock<'a> {+ type Target = glow::Context;++ fn deref(&self) -> &Self::Target {+ &self.glow+ }+}++impl<'a> Drop for AdapterContextLock<'a> {+ fn drop(&mut self) {+ if let Some(egl) = self.egl.take() {+ if let Err(err) = egl.instance.make_current(egl.display, None, None, None) {+ log::error!("Failed to make EGL context current: {err:?}");+ }+ }+ }+}++impl AdapterContext {+ /// Get's the [`glow::Context`] without waiting for a lock+ ///+ /// # Safety+ ///+ /// This should only be called when you have manually made sure that the current thread has made+ /// the EGL context current and that no other thread also has the EGL context current.+ /// Additionally, you must manually make the EGL context **not** current after you are done with+ /// it, so that future calls to `lock()` will not fail.+ ///+ /// > **Note:** Calling this function **will** still lock the [`glow::Context`] which adds an+ /// > extra safe-guard against accidental concurrent access to the context.+ pub unsafe fn get_without_egl_lock(&self) -> MappedMutexGuard<'_, glow::Context> {+ let guard = self+ .glow+ .try_lock_for(Duration::from_secs(CONTEXT_LOCK_TIMEOUT_SECS))+ .expect("Could not lock adapter context. This is most-likely a deadlock.");+ MutexGuard::map(guard, |glow| &mut **glow)+ }++ /// Obtain a lock to the EGL context and get handle to the [`glow::Context`] that can be used to+ /// do rendering.+ #[track_caller]+ pub fn lock<'a>(&'a self) -> AdapterContextLock<'a> {+ let glow = self+ .glow+ // Don't lock forever. If it takes longer than 1 second to get the lock we've got a+ // deadlock and should panic to show where we got stuck+ .try_lock_for(Duration::from_secs(CONTEXT_LOCK_TIMEOUT_SECS))+ .expect("Could not lock adapter context. This is most-likely a deadlock.");++ let egl = self.egl.as_ref().map(|egl| {+ egl.make_current();+ EglContextLock {+ instance: &egl.instance,+ display: egl.display,+ }+ });++ AdapterContextLock { glow, egl }+ }+}++#[derive(Debug)]+struct Inner {+ /// Note: the context contains a dummy pbuffer (1x1).+ /// Required for `eglMakeCurrent` on platforms that doesn't supports `EGL_KHR_surfaceless_context`.+ egl: EglContext,+ version: (i32, i32),+ supports_native_window: bool,+ config: khronos_egl::Config,+ /// Method by which the framebuffer should support srgb+ srgb_kind: SrgbFrameBufferKind,+}++#[cfg(send_sync)]+unsafe impl Send for Inner {}+#[cfg(send_sync)]+unsafe impl Sync for Inner {}++// Different calls to `eglGetPlatformDisplay` may return the same `Display`, making it a global+// state of all our `EglContext`s. This forces us to track the number of such context to prevent+// terminating the display if it's currently used by another `EglContext`.+static DISPLAYS_REFERENCE_COUNT: LazyLock<Mutex<HashMap<usize, usize>>> =+ LazyLock::new(Default::default);++fn initialize_display(+ egl: &EglInstance,+ display: khronos_egl::Display,+) -> Result<(i32, i32), khronos_egl::Error> {+ let mut guard = DISPLAYS_REFERENCE_COUNT.lock();+ *guard.entry(display.as_ptr() as usize).or_default() += 1;++ // We don't need to check the reference count here since according to the `eglInitialize`+ // documentation, initializing an already initialized EGL display connection has no effect+ // besides returning the version numbers.+ egl.initialize(display)+}++fn terminate_display(+ egl: &EglInstance,+ display: khronos_egl::Display,+) -> Result<(), khronos_egl::Error> {+ let key = &(display.as_ptr() as usize);+ let mut guard = DISPLAYS_REFERENCE_COUNT.lock();+ let count_ref = guard+ .get_mut(key)+ .expect("Attempted to decref a display before incref was called");++ if *count_ref > 1 {+ *count_ref -= 1;++ Ok(())+ } else {+ guard.remove(key);++ egl.terminate(display)+ }+}++fn instance_err<E: core::error::Error + Send + Sync + 'static>(+ message: impl Into<String>,+) -> impl FnOnce(E) -> crate::InstanceError {+ move |e| crate::InstanceError::with_source(message.into(), e)+}++impl Inner {+ fn create(+ flags: wgt::InstanceFlags,+ egl: Arc<EglInstance>,+ display: khronos_egl::Display,+ force_gles_minor_version: wgt::Gles3MinorVersion,+ ) -> Result<Self, crate::InstanceError> {+ let version = initialize_display(&egl, display)+ .map_err(instance_err("failed to initialize EGL display connection"))?;+ let vendor = egl+ .query_string(Some(display), khronos_egl::VENDOR)+ .map_err(instance_err("failed to query EGL vendor"))?;+ let display_extensions = egl+ .query_string(Some(display), khronos_egl::EXTENSIONS)+ .map_err(instance_err("failed to query EGL display extensions"))?+ .to_string_lossy();+ log::debug!("Display vendor {vendor:?}, version {version:?}",);+ log::debug!(+ "Display extensions: {:#?}",+ display_extensions.split_whitespace().collect::<Vec<_>>()+ );++ let srgb_kind = if version >= (1, 5) {+ log::debug!("\tEGL surface: +srgb");+ SrgbFrameBufferKind::Core+ } else if display_extensions.contains("EGL_KHR_gl_colorspace") {+ log::debug!("\tEGL surface: +srgb khr");+ SrgbFrameBufferKind::Khr+ } else {+ log::debug!("\tEGL surface: -srgb");+ SrgbFrameBufferKind::None+ };++ if log::max_level() >= log::LevelFilter::Trace {+ log::trace!("Configurations:");+ let config_count = egl+ .get_config_count(display)+ .map_err(instance_err("failed to get config count"))?;+ let mut configurations = Vec::with_capacity(config_count);+ egl.get_configs(display, &mut configurations)+ .map_err(instance_err("failed to get configs"))?;+ for &config in configurations.iter() {+ log::trace!("\tCONFORMANT=0x{:X?}, RENDERABLE=0x{:X?}, NATIVE_RENDERABLE=0x{:X?}, SURFACE_TYPE=0x{:X?}, ALPHA_SIZE={:?}",+ egl.get_config_attrib(display, config, khronos_egl::CONFORMANT),+ egl.get_config_attrib(display, config, khronos_egl::RENDERABLE_TYPE),+ egl.get_config_attrib(display, config, khronos_egl::NATIVE_RENDERABLE),+ egl.get_config_attrib(display, config, khronos_egl::SURFACE_TYPE),+ egl.get_config_attrib(display, config, khronos_egl::ALPHA_SIZE),+ );+ }+ }++ let (config, supports_native_window) = choose_config(&egl, display, srgb_kind)?;++ let supports_opengl = if version >= (1, 4) {+ let client_apis = egl+ .query_string(Some(display), khronos_egl::CLIENT_APIS)+ .map_err(instance_err("failed to query EGL client APIs string"))?+ .to_string_lossy();+ client_apis+ .split(' ')+ .any(|client_api| client_api == "OpenGL")+ } else {+ false+ };++ let mut khr_context_flags = 0;+ let supports_khr_context = display_extensions.contains("EGL_KHR_create_context");++ let mut context_attributes = vec![];+ let mut gl_context_attributes = vec![];+ let mut gles_context_attributes = vec![];+ gl_context_attributes.push(khronos_egl::CONTEXT_MAJOR_VERSION);+ gl_context_attributes.push(3);+ gl_context_attributes.push(khronos_egl::CONTEXT_MINOR_VERSION);+ gl_context_attributes.push(3);+ if supports_opengl && force_gles_minor_version != wgt::Gles3MinorVersion::Automatic {+ log::warn!("Ignoring specified GLES minor version as OpenGL is used");+ }+ gles_context_attributes.push(khronos_egl::CONTEXT_MAJOR_VERSION);+ gles_context_attributes.push(3); // Request GLES 3.0 or higher+ if force_gles_minor_version != wgt::Gles3MinorVersion::Automatic {+ gles_context_attributes.push(khronos_egl::CONTEXT_MINOR_VERSION);+ gles_context_attributes.push(match force_gles_minor_version {+ wgt::Gles3MinorVersion::Automatic => unreachable!(),+ wgt::Gles3MinorVersion::Version0 => 0,+ wgt::Gles3MinorVersion::Version1 => 1,+ wgt::Gles3MinorVersion::Version2 => 2,+ });+ }+ if flags.contains(wgt::InstanceFlags::DEBUG) {+ if version >= (1, 5) {+ log::debug!("\tEGL context: +debug");+ context_attributes.push(khronos_egl::CONTEXT_OPENGL_DEBUG);+ context_attributes.push(khronos_egl::TRUE as _);+ } else if supports_khr_context {+ log::debug!("\tEGL context: +debug KHR");+ khr_context_flags |= EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR;+ } else {+ log::debug!("\tEGL context: -debug");+ }+ }++ if khr_context_flags != 0 {+ context_attributes.push(EGL_CONTEXT_FLAGS_KHR);+ context_attributes.push(khr_context_flags);+ }++ gl_context_attributes.extend(&context_attributes);+ gles_context_attributes.extend(&context_attributes);++ let context = {+ #[derive(Copy, Clone)]+ enum Robustness {+ Core,+ Ext,+ }++ let robustness = if version >= (1, 5) {+ Some(Robustness::Core)+ } else if display_extensions.contains("EGL_EXT_create_context_robustness") {+ Some(Robustness::Ext)+ } else {+ None+ };++ let create_context = |api, base_attributes: &[khronos_egl::Int]| {+ egl.bind_api(api)?;++ let mut robustness = robustness;+ loop {+ let robustness_attributes = match robustness {+ Some(Robustness::Core) => {+ vec![+ khronos_egl::CONTEXT_OPENGL_ROBUST_ACCESS,+ khronos_egl::TRUE as _,+ khronos_egl::NONE,+ ]+ }+ Some(Robustness::Ext) => {+ vec![+ EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT,+ khronos_egl::TRUE as _,+ khronos_egl::NONE,+ ]+ }+ None => vec![khronos_egl::NONE],+ };++ let mut context_attributes = base_attributes.to_vec();+ context_attributes.extend(&robustness_attributes);++ match egl.create_context(display, config, None, &context_attributes) {+ Ok(context) => {+ match robustness {+ Some(Robustness::Core) => {+ log::debug!("\tEGL context: +robust access");+ }+ Some(Robustness::Ext) => {+ log::debug!("\tEGL context: +robust access EXT");+ }+ None => {+ log::debug!("\tEGL context: -robust access");+ }+ }+ return Ok(context);+ }++ // Robust access context creation can fail with different error codes+ // depending on the EGL path. Retry with a lower robustness level.+ Err(+ khronos_egl::Error::BadAttribute+ | khronos_egl::Error::BadMatch+ | khronos_egl::Error::BadConfig,+ ) if robustness.is_some() => {+ robustness = match robustness {+ Some(Robustness::Core)+ if display_extensions+ .contains("EGL_EXT_create_context_robustness") =>+ {+ Some(Robustness::Ext)+ }+ _ => None,+ };+ continue;+ }++ Err(e) => return Err(e),+ }+ }+ };++ let result = if supports_opengl {+ create_context(khronos_egl::OPENGL_API, &gl_context_attributes).or_else(+ |gl_error| {+ log::debug!("Failed to create desktop OpenGL context: {gl_error}, falling back to OpenGL ES");+ create_context(khronos_egl::OPENGL_ES_API, &gles_context_attributes)+ },+ )+ } else {+ create_context(khronos_egl::OPENGL_ES_API, &gles_context_attributes)+ };++ result.map_err(|e| {+ crate::InstanceError::with_source(+ String::from("unable to create OpenGL or GLES 3.x context"),+ e,+ )+ })+ }?;++ // Testing if context can be binded without surface+ // and creating dummy pbuffer surface if not.+ let pbuffer = if version >= (1, 5)+ || display_extensions.contains("EGL_KHR_surfaceless_context")+ || cfg!(Emscripten)+ {+ log::debug!("\tEGL context: +surfaceless");+ None+ } else {+ let attributes = [+ khronos_egl::WIDTH,+ 1,+ khronos_egl::HEIGHT,+ 1,+ khronos_egl::NONE,+ ];+ egl.create_pbuffer_surface(display, config, &attributes)+ .map(Some)+ .map_err(|e| {+ crate::InstanceError::with_source(+ String::from("error in create_pbuffer_surface"),+ e,+ )+ })?+ };++ Ok(Self {+ egl: EglContext {+ instance: egl,+ display,+ raw: context,+ pbuffer,+ version,+ },+ version,+ supports_native_window,+ config,+ srgb_kind,+ })+ }+}++impl Drop for Inner {+ fn drop(&mut self) {+ // ERROR: Since EglContext is erroneously Clone, these handles could be copied and+ // accidentally used elsewhere outside of Inner, despite us assuming ownership and+ // destroying the handles here.+ if let Err(e) = self+ .egl+ .instance+ .destroy_context(self.egl.display, self.egl.raw)+ {+ log::warn!("Error in destroy_context: {e:?}");+ }++ if let Err(e) = terminate_display(&self.egl.instance, self.egl.display) {+ log::warn!("Error in terminate: {e:?}");+ }+ }+}++#[derive(Clone, Copy, Debug, PartialEq)]+enum WindowKind {+ Wayland,+ X11,+ AngleX11,+ Unknown,+}++#[derive(Clone, Debug)]+struct WindowSystemInterface {+ kind: WindowKind,+}++#[derive(Debug)]+pub struct Instance {+ wsi: WindowSystemInterface,+ flags: wgt::InstanceFlags,+ options: wgt::GlBackendOptions,+ inner: Mutex<Inner>,+}++impl Instance {+ pub fn raw_display(&self) -> khronos_egl::Display {+ self.inner+ .try_lock()+ .expect("Could not lock instance. This is most-likely a deadlock.")+ .egl+ .display+ }++ /// Returns the version of the EGL display.+ pub fn egl_version(&self) -> (i32, i32) {+ self.inner+ .try_lock()+ .expect("Could not lock instance. This is most-likely a deadlock.")+ .version+ }++ pub fn egl_config(&self) -> khronos_egl::Config {+ self.inner+ .try_lock()+ .expect("Could not lock instance. This is most-likely a deadlock.")+ .config+ }+}++#[cfg(send_sync)]+static_assertions::assert_impl_all!(Instance: Send, Sync);++impl crate::Instance for Instance {+ type A = super::Api;++ unsafe fn init(desc: &crate::InstanceDescriptor<'_>) -> Result<Self, crate::InstanceError> {+ use raw_window_handle::RawDisplayHandle as Rdh;++ profiling::scope!("Init OpenGL (EGL) Backend");+ #[cfg(Emscripten)]+ let egl_result: Result<EglInstance, khronos_egl::Error> =+ Ok(khronos_egl::Instance::new(khronos_egl::Static));++ #[cfg(not(Emscripten))]+ let egl_result = if cfg!(windows) {+ unsafe {+ khronos_egl::DynamicInstance::<khronos_egl::EGL1_4>::load_required_from_filename(+ "libEGL.dll",+ )+ }+ } else if cfg!(target_vendor = "apple") {+ unsafe {+ khronos_egl::DynamicInstance::<khronos_egl::EGL1_4>::load_required_from_filename(+ "libEGL.dylib",+ )+ }+ } else {+ unsafe { khronos_egl::DynamicInstance::<khronos_egl::EGL1_4>::load_required() }+ };+ let egl = egl_result+ .map(Arc::new)+ .map_err(instance_err("unable to open libEGL"))?;++ let client_extensions = egl.query_string(None, khronos_egl::EXTENSIONS);++ let client_ext_str = match client_extensions {+ Ok(ext) => ext.to_string_lossy().into_owned(),+ Err(_) => String::new(),+ };+ log::debug!(+ "Client extensions: {:#?}",+ client_ext_str.split_whitespace().collect::<Vec<_>>()+ );++ #[cfg(not(Emscripten))]+ let egl1_5 = egl.upcast::<khronos_egl::EGL1_5>();++ #[cfg(Emscripten)]+ let egl1_5: Option<&Arc<EglInstance>> = Some(&egl);++ let (display, wsi_kind) = match (desc.display.map(|d| d.as_raw()), egl1_5) {+ (Some(Rdh::Wayland(wayland_display_handle)), Some(egl))+ if client_ext_str.contains("EGL_EXT_platform_wayland") =>+ {+ log::debug!("Using Wayland platform");+ let display_attributes = [khronos_egl::ATTRIB_NONE];+ let display = unsafe {+ egl.get_platform_display(+ EGL_PLATFORM_WAYLAND_KHR,+ wayland_display_handle.display.as_ptr(),+ &display_attributes,+ )+ }+ .map_err(instance_err("failed to get Wayland display"))?;+ (display, WindowKind::Wayland)+ }+ (Some(Rdh::Xlib(xlib_display_handle)), Some(egl))+ if client_ext_str.contains("EGL_EXT_platform_x11") =>+ {+ log::debug!("Using X11 platform");+ let display_attributes = [khronos_egl::ATTRIB_NONE];+ let display = unsafe {+ egl.get_platform_display(+ EGL_PLATFORM_X11_KHR,+ xlib_display_handle+ .display+ .map_or(khronos_egl::DEFAULT_DISPLAY, ptr::NonNull::as_ptr),+ &display_attributes,+ )+ }+ .map_err(instance_err("failed to get X11 display"))?;+ (display, WindowKind::X11)+ }+ (Some(Rdh::Xlib(xlib_display_handle)), Some(egl))+ if client_ext_str.contains("EGL_ANGLE_platform_angle") =>+ {+ log::debug!("Using Angle platform with X11");+ let display_attributes = [+ EGL_PLATFORM_ANGLE_NATIVE_PLATFORM_TYPE_ANGLE as khronos_egl::Attrib,+ EGL_PLATFORM_X11_KHR as khronos_egl::Attrib,+ EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED as khronos_egl::Attrib,+ usize::from(desc.flags.contains(wgt::InstanceFlags::VALIDATION)),+ khronos_egl::ATTRIB_NONE,+ ];+ let display = unsafe {+ egl.get_platform_display(+ EGL_PLATFORM_ANGLE_ANGLE,+ xlib_display_handle+ .display+ .map_or(khronos_egl::DEFAULT_DISPLAY, ptr::NonNull::as_ptr),+ &display_attributes,+ )+ }+ .map_err(instance_err("failed to get Angle display"))?;+ (display, WindowKind::AngleX11)+ }+ (Some(Rdh::Xcb(xcb_display_handle)), Some(egl))+ if client_ext_str.contains("EGL_EXT_platform_xcb") =>+ {+ log::debug!("Using XCB platform");+ let display_attributes = [+ EGL_PLATFORM_XCB_SCREEN_EXT as khronos_egl::Attrib,+ xcb_display_handle.screen as khronos_egl::Attrib,+ khronos_egl::ATTRIB_NONE,+ ];+ let display = unsafe {+ egl.get_platform_display(+ EGL_PLATFORM_XCB_EXT,+ xcb_display_handle+ .connection+ .map_or(khronos_egl::DEFAULT_DISPLAY, ptr::NonNull::as_ptr),+ &display_attributes,+ )+ }+ .map_err(instance_err("failed to get XCB display"))?;+ (display, WindowKind::X11)+ }+ x if client_ext_str.contains("EGL_MESA_platform_surfaceless") => {+ log::debug!(+ "No (or unknown) windowing system ({x:?}) present. Using surfaceless platform"+ );+ #[allow(+ clippy::unnecessary_literal_unwrap,+ reason = "this is only a literal on Emscripten"+ )]+ // TODO: This extension is also supported on EGL 1.4 with EGL_EXT_platform_base: https://registry.khronos.org/EGL/extensions/MESA/EGL_MESA_platform_surfaceless.txt+ let egl = egl1_5.expect("Failed to get EGL 1.5 for surfaceless");+ let display = unsafe {+ egl.get_platform_display(+ EGL_PLATFORM_SURFACELESS_MESA,+ khronos_egl::DEFAULT_DISPLAY,+ &[khronos_egl::ATTRIB_NONE],+ )+ }+ .map_err(instance_err("failed to get MESA surfaceless display"))?;+ (display, WindowKind::Unknown)+ }+ x => {+ log::debug!(+ "No (or unknown) windowing system {x:?} and EGL_MESA_platform_surfaceless not available. Using default platform"+ );+ let display =+ unsafe { egl.get_display(khronos_egl::DEFAULT_DISPLAY) }.ok_or_else(|| {+ crate::InstanceError::new("Failed to get default display".into())+ })?;+ (display, WindowKind::Unknown)+ }+ };++ if desc.flags.contains(wgt::InstanceFlags::VALIDATION)+ && client_ext_str.contains("EGL_KHR_debug")+ {+ log::debug!("Enabling EGL debug output");+ let function: EglDebugMessageControlFun = {+ let addr = egl+ .get_proc_address("eglDebugMessageControlKHR")+ .ok_or_else(|| {+ crate::InstanceError::new(+ "failed to get `eglDebugMessageControlKHR` proc address".into(),+ )+ })?;+ unsafe { core::mem::transmute(addr) }+ };+ let attributes = [+ EGL_DEBUG_MSG_CRITICAL_KHR as khronos_egl::Attrib,+ 1,+ EGL_DEBUG_MSG_ERROR_KHR as khronos_egl::Attrib,+ 1,+ EGL_DEBUG_MSG_WARN_KHR as khronos_egl::Attrib,+ 1,+ EGL_DEBUG_MSG_INFO_KHR as khronos_egl::Attrib,+ 1,+ khronos_egl::ATTRIB_NONE,+ ];+ unsafe { (function)(Some(egl_debug_proc), attributes.as_ptr()) };+ }++ let inner = Inner::create(+ desc.flags,+ egl,+ display,+ desc.backend_options.gl.gles_minor_version,+ )?;++ Ok(Instance {+ wsi: WindowSystemInterface { kind: wsi_kind },+ flags: desc.flags,+ options: desc.backend_options.gl.clone(),+ inner: Mutex::new(inner),+ })+ }++ unsafe fn create_surface(+ &self,+ display_handle: raw_window_handle::RawDisplayHandle,+ window_handle: raw_window_handle::RawWindowHandle,+ ) -> Result<Surface, crate::InstanceError> {+ use raw_window_handle::RawWindowHandle as Rwh;++ let inner = self.inner.lock();++ match (window_handle, display_handle) {+ (Rwh::Xlib(_), _) => {}+ (Rwh::Xcb(_), _) => {}+ (Rwh::Win32(_), _) => {}+ (Rwh::AppKit(_), _) => {}+ (Rwh::OhosNdk(_), _) => {}+ #[cfg(target_os = "android")]+ (Rwh::AndroidNdk(handle), _) => {+ let format = inner+ .egl+ .instance+ .get_config_attrib(+ inner.egl.display,+ inner.config,+ khronos_egl::NATIVE_VISUAL_ID,+ )+ .map_err(instance_err("failed to get config NATIVE_VISUAL_ID"))?;++ let ret = unsafe {+ ndk_sys::ANativeWindow_setBuffersGeometry(+ handle+ .a_native_window+ .as_ptr()+ .cast::<ndk_sys::ANativeWindow>(),+ 0,+ 0,+ format,+ )+ };++ if ret != 0 {+ return Err(crate::InstanceError::new(format!(+ "error {ret} returned from ANativeWindow_setBuffersGeometry",+ )));+ }+ }+ (Rwh::Wayland(_), _) => {}+ #[cfg(Emscripten)]+ (Rwh::Web(_), _) => {}+ other => {+ return Err(crate::InstanceError::new(format!(+ "unsupported window: {other:?}"+ )));+ }+ };++ inner.egl.unmake_current();++ Ok(Surface {+ egl: inner.egl.clone(),+ wsi: self.wsi.clone(),+ config: inner.config,+ presentable: inner.supports_native_window,+ raw_window_handle: window_handle,+ swapchain: RwLock::new(None),+ srgb_kind: inner.srgb_kind,+ })+ }++ unsafe fn enumerate_adapters(+ &self,+ _surface_hint: Option<&Surface>,+ ) -> Vec<crate::ExposedAdapter<super::Api>> {+ let inner = self.inner.lock();+ inner.egl.make_current();++ let mut gl = unsafe {+ glow::Context::from_loader_function(|name| {+ inner+ .egl+ .instance+ .get_proc_address(name)+ .map_or(ptr::null(), |p| p as *const _)+ })+ };++ // In contrast to OpenGL ES, OpenGL requires explicitly enabling sRGB conversions,+ // as otherwise the user has to do the sRGB conversion.+ if !matches!(inner.srgb_kind, SrgbFrameBufferKind::None) {+ unsafe { gl.enable(glow::FRAMEBUFFER_SRGB) };+ }++ if self.flags.contains(wgt::InstanceFlags::DEBUG) && gl.supports_debug() {+ log::debug!("Max label length: {}", unsafe {+ gl.get_parameter_i32(glow::MAX_LABEL_LENGTH)+ });+ }++ if self.flags.contains(wgt::InstanceFlags::VALIDATION) && gl.supports_debug() {+ log::debug!("Enabling GLES debug output");+ unsafe { gl.enable(glow::DEBUG_OUTPUT) };+ unsafe { gl.debug_message_callback(super::gl_debug_message_callback) };+ }++ // Wrap in ManuallyDrop to make it easier to "current" the GL context before dropping this+ // GLOW context, which could also happen if a panic occurs after we uncurrent the context+ // below but before AdapterContext is constructed.+ let gl = ManuallyDrop::new(gl);+ inner.egl.unmake_current();++ unsafe {+ super::Adapter::expose(+ AdapterContext {+ glow: Mutex::new(gl),+ // ERROR: Copying owned reference handles here, be careful to not drop them!+ egl: Some(inner.egl.clone()),+ },+ self.options.clone(),+ )+ }+ .into_iter()+ .collect()+ }+}++impl super::Adapter {+ /// Creates a new external adapter using the specified loader function.+ ///+ /// # Safety+ ///+ /// - The underlying OpenGL ES context must be current.+ /// - The underlying OpenGL ES context must be current when interfacing with any objects returned by+ /// wgpu-hal from this adapter.+ /// - The underlying OpenGL ES context must be current when dropping this adapter and when+ /// dropping any objects returned from this adapter.+ pub unsafe fn new_external(+ fun: impl FnMut(&str) -> *const ffi::c_void,+ options: wgt::GlBackendOptions,+ ) -> Option<crate::ExposedAdapter<super::Api>> {+ let context = unsafe { glow::Context::from_loader_function(fun) };+ unsafe {+ Self::expose(+ AdapterContext {+ glow: Mutex::new(ManuallyDrop::new(context)),+ egl: None,+ },+ options,+ )+ }+ }++ pub fn adapter_context(&self) -> &AdapterContext {+ &self.shared.context+ }+}++impl super::Device {+ /// Returns the underlying EGL context.+ pub fn context(&self) -> &AdapterContext {+ &self.shared.context+ }+}++#[derive(Debug)]+pub struct Swapchain {+ surface: khronos_egl::Surface,+ wl_window: Option<*mut wayland_sys::egl::wl_egl_window>,+ framebuffer: glow::Framebuffer,+ renderbuffer: glow::Renderbuffer,+ /// Extent because the window lies+ extent: wgt::Extent3d,+ format: wgt::TextureFormat,+ format_desc: super::TextureFormatDesc,+ #[allow(unused)]+ sample_type: wgt::TextureSampleType,+}++#[derive(Debug)]+pub struct Surface {+ egl: EglContext,+ wsi: WindowSystemInterface,+ config: khronos_egl::Config,+ pub(super) presentable: bool,+ raw_window_handle: raw_window_handle::RawWindowHandle,+ swapchain: RwLock<Option<Swapchain>>,+ srgb_kind: SrgbFrameBufferKind,+}++unsafe impl Send for Surface {}+unsafe impl Sync for Surface {}++impl Surface {+ pub(super) unsafe fn present(+ &self,+ _suf_texture: super::Texture,+ context: &AdapterContext,+ ) -> Result<(), crate::SurfaceError> {+ let gl = unsafe { context.get_without_egl_lock() };+ let swapchain = self.swapchain.read();+ let sc = swapchain.as_ref().ok_or(crate::SurfaceError::Other(+ "Surface has no swap-chain configured",+ ))?;++ self.egl+ .instance+ .make_current(+ self.egl.display,+ Some(sc.surface),+ Some(sc.surface),+ Some(self.egl.raw),+ )+ .map_err(|e| {+ log::error!("make_current(surface) failed: {e}");+ crate::SurfaceError::Lost+ })?;++ unsafe { gl.disable(glow::SCISSOR_TEST) };+ unsafe { gl.color_mask(true, true, true, true) };++ unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, None) };+ unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(sc.framebuffer)) };++ if !matches!(self.srgb_kind, SrgbFrameBufferKind::None) {+ // Disable sRGB conversions for `glBlitFramebuffer` as behavior does diverge between+ // drivers and formats otherwise and we want to ensure no sRGB conversions happen.+ unsafe { gl.disable(glow::FRAMEBUFFER_SRGB) };+ }++ // Note the Y-flipping here. GL's presentation is not flipped,+ // but main rendering is. Therefore, we Y-flip the output positions+ // in the shader, and also this blit.+ unsafe {+ gl.blit_framebuffer(+ 0,+ sc.extent.height as i32,+ sc.extent.width as i32,+ 0,+ 0,+ 0,+ sc.extent.width as i32,+ sc.extent.height as i32,+ glow::COLOR_BUFFER_BIT,+ glow::NEAREST,+ )+ };++ if !matches!(self.srgb_kind, SrgbFrameBufferKind::None) {+ unsafe { gl.enable(glow::FRAMEBUFFER_SRGB) };+ }++ unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, None) };++ self.egl+ .instance+ .swap_buffers(self.egl.display, sc.surface)+ .map_err(|e| {+ log::error!("swap_buffers failed: {e}");+ crate::SurfaceError::Lost+ // TODO: should we unset the current context here?+ })?;+ self.egl+ .instance+ .make_current(self.egl.display, None, None, None)+ .map_err(|e| {+ log::error!("make_current(null) failed: {e}");+ crate::SurfaceError::Lost+ })?;++ Ok(())+ }++ unsafe fn unconfigure_impl(+ &self,+ device: &super::Device,+ ) -> Option<(+ khronos_egl::Surface,+ Option<*mut wayland_sys::egl::wl_egl_window>,+ )> {+ let gl = &device.shared.context.lock();+ match self.swapchain.write().take() {+ Some(sc) => {+ unsafe { gl.delete_renderbuffer(sc.renderbuffer) };+ unsafe { gl.delete_framebuffer(sc.framebuffer) };+ Some((sc.surface, sc.wl_window))+ }+ None => None,+ }+ }++ pub fn supports_srgb(&self) -> bool {+ match self.srgb_kind {+ SrgbFrameBufferKind::None => false,+ _ => true,+ }+ }+}++impl crate::Surface for Surface {+ type A = super::Api;++ unsafe fn configure(+ &self,+ device: &super::Device,+ config: &crate::SurfaceConfiguration,+ ) -> Result<(), crate::SurfaceError> {+ use raw_window_handle::RawWindowHandle as Rwh;++ let (surface, wl_window) = match unsafe { self.unconfigure_impl(device) } {+ Some((sc, wl_window)) => {+ if let Some(window) = wl_window {+ wayland_sys::ffi_dispatch!(+ wayland_sys::egl::wayland_egl_handle(),+ wl_egl_window_resize,+ window,+ config.extent.width as i32,+ config.extent.height as i32,+ 0,+ 0,+ );+ }++ (sc, wl_window)+ }+ None => {+ let mut wl_window = None;+ let (mut temp_xlib_handle, mut temp_xcb_handle);+ let native_window_ptr = match (self.wsi.kind, self.raw_window_handle) {+ (WindowKind::Unknown | WindowKind::X11, Rwh::Xlib(handle)) => {+ temp_xlib_handle = handle.window;+ ptr::from_mut(&mut temp_xlib_handle).cast::<ffi::c_void>()+ }+ (WindowKind::AngleX11, Rwh::Xlib(handle)) => handle.window as *mut ffi::c_void,+ (WindowKind::Unknown | WindowKind::X11, Rwh::Xcb(handle)) => {+ temp_xcb_handle = handle.window;+ ptr::from_mut(&mut temp_xcb_handle).cast::<ffi::c_void>()+ }+ (WindowKind::AngleX11, Rwh::Xcb(handle)) => {+ handle.window.get() as *mut ffi::c_void+ }+ (WindowKind::Unknown, Rwh::AndroidNdk(handle)) => {+ handle.a_native_window.as_ptr()+ }+ (WindowKind::Unknown, Rwh::OhosNdk(handle)) => handle.native_window.as_ptr(),+ #[cfg(unix)]+ (WindowKind::Wayland, Rwh::Wayland(handle)) => {+ let window = wayland_sys::ffi_dispatch!(+ wayland_sys::egl::wayland_egl_handle(),+ wl_egl_window_create,+ handle.surface.as_ptr().cast(),+ config.extent.width as i32,+ config.extent.height as i32,+ );+ wl_window = Some(window);+ window.cast()+ }+ #[cfg(Emscripten)]+ (WindowKind::Unknown, Rwh::Web(handle)) => handle.id as *mut ffi::c_void,+ (WindowKind::Unknown, Rwh::Win32(handle)) => {+ handle.hwnd.get() as *mut ffi::c_void+ }+ (WindowKind::Unknown, Rwh::AppKit(handle)) => {+ #[cfg(not(target_os = "macos"))]+ let window_ptr = handle.ns_view.as_ptr();+ #[cfg(target_os = "macos")]+ let window_ptr = {+ use objc2::msg_send;+ use objc2::runtime::AnyObject;+ // ns_view always have a layer and don't need to verify that it exists.+ let layer: *mut AnyObject =+ msg_send![handle.ns_view.as_ptr().cast::<AnyObject>(), layer];+ layer.cast::<ffi::c_void>()+ };+ window_ptr+ }+ _ => {+ log::warn!(+ "Initialized platform {:?} doesn't work with window {:?}",+ self.wsi.kind,+ self.raw_window_handle+ );+ return Err(crate::SurfaceError::Other("incompatible window kind"));+ }+ };++ let mut attributes = vec![+ khronos_egl::RENDER_BUFFER,+ // We don't want any of the buffering done by the driver, because we+ // manage a swapchain on our side.+ // Some drivers just fail on surface creation seeing `EGL_SINGLE_BUFFER`.+ if cfg!(any(+ target_os = "android",+ target_os = "macos",+ target_env = "ohos"+ )) || cfg!(windows)+ || self.wsi.kind == WindowKind::AngleX11+ {+ khronos_egl::BACK_BUFFER+ } else {+ khronos_egl::SINGLE_BUFFER+ },+ ];+ if config.format.is_srgb() {+ match self.srgb_kind {+ SrgbFrameBufferKind::None => {}+ SrgbFrameBufferKind::Core => {+ attributes.push(khronos_egl::GL_COLORSPACE);+ attributes.push(khronos_egl::GL_COLORSPACE_SRGB);+ }+ SrgbFrameBufferKind::Khr => {+ attributes.push(EGL_GL_COLORSPACE_KHR as i32);+ attributes.push(EGL_GL_COLORSPACE_SRGB_KHR as i32);+ }+ }+ }+ attributes.push(khronos_egl::ATTRIB_NONE as i32);++ #[cfg(not(Emscripten))]+ let egl1_5 = self.egl.instance.upcast::<khronos_egl::EGL1_5>();++ #[cfg(Emscripten)]+ let egl1_5: Option<&Arc<EglInstance>> = Some(&self.egl.instance);++ // Careful, we can still be in 1.4 version even if `upcast` succeeds+ let raw_result = match egl1_5 {+ Some(egl) if self.wsi.kind != WindowKind::Unknown => {+ let attributes_usize = attributes+ .into_iter()+ .map(|v| v as usize)+ .collect::<Vec<_>>();+ unsafe {+ egl.create_platform_window_surface(+ self.egl.display,+ self.config,+ native_window_ptr,+ &attributes_usize,+ )+ }+ }+ _ => unsafe {+ self.egl.instance.create_window_surface(+ self.egl.display,+ self.config,+ native_window_ptr,+ Some(&attributes),+ )+ },+ };++ match raw_result {+ Ok(raw) => (raw, wl_window),+ Err(e) => {+ log::warn!("Error in create_window_surface: {e:?}");+ return Err(crate::SurfaceError::Lost);+ }+ }+ }+ };++ let format_desc = device.shared.describe_texture_format(config.format);+ let gl = &device.shared.context.lock();+ let renderbuffer = unsafe { gl.create_renderbuffer() }.map_err(|error| {+ log::error!("Internal swapchain renderbuffer creation failed: {error}");+ crate::DeviceError::OutOfMemory+ })?;+ unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, Some(renderbuffer)) };+ unsafe {+ gl.renderbuffer_storage(+ glow::RENDERBUFFER,+ format_desc.internal,+ config.extent.width as _,+ config.extent.height as _,+ )+ };+ let framebuffer = unsafe { gl.create_framebuffer() }.map_err(|error| {+ log::error!("Internal swapchain framebuffer creation failed: {error}");+ crate::DeviceError::OutOfMemory+ })?;+ unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(framebuffer)) };+ unsafe {+ gl.framebuffer_renderbuffer(+ glow::READ_FRAMEBUFFER,+ glow::COLOR_ATTACHMENT0,+ glow::RENDERBUFFER,+ Some(renderbuffer),+ )+ };+ unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, None) };+ unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, None) };++ let mut swapchain = self.swapchain.write();+ *swapchain = Some(Swapchain {+ surface,+ wl_window,+ renderbuffer,+ framebuffer,+ extent: config.extent,+ format: config.format,+ format_desc,+ sample_type: wgt::TextureSampleType::Float { filterable: false },+ });++ Ok(())+ }++ unsafe fn unconfigure(&self, device: &super::Device) {+ if let Some((surface, wl_window)) = unsafe { self.unconfigure_impl(device) } {+ self.egl+ .instance+ .destroy_surface(self.egl.display, surface)+ .unwrap();+ if let Some(window) = wl_window {+ wayland_sys::ffi_dispatch!(+ wayland_sys::egl::wayland_egl_handle(),+ wl_egl_window_destroy,+ window,+ );+ }+ }+ }++ unsafe fn acquire_texture(+ &self,+ _timeout_ms: Option<Duration>, //TODO+ _fence: &super::Fence,+ ) -> Result<crate::AcquiredSurfaceTexture<super::Api>, crate::SurfaceError> {+ let swapchain = self.swapchain.read();+ let sc = swapchain.as_ref().ok_or(crate::SurfaceError::Other(+ "Surface has no swap-chain configured",+ ))?;+ let texture = super::Texture {+ inner: super::TextureInner::Renderbuffer {+ raw: sc.renderbuffer,+ },+ drop_guard: None,+ array_layer_count: 1,+ mip_level_count: 1,+ format: sc.format,+ format_desc: sc.format_desc.clone(),+ copy_size: crate::CopyExtent {+ width: sc.extent.width,+ height: sc.extent.height,+ depth: 1,+ },+ };+ Ok(crate::AcquiredSurfaceTexture {+ texture,+ suboptimal: false,+ })+ }+ unsafe fn discard_texture(&self, _texture: super::Texture) {}+}diff --git a/vendor/wgpu-hal/src/gles/emscripten.rs b/vendor/wgpu-hal/src/gles/emscripten.rsnew file mode 100644index 00000000..88cc0bec--- /dev/null+++ b/vendor/wgpu-hal/src/gles/emscripten.rs@@ -0,0 +1,26 @@+extern "C" {+ /// returns 1 if success. 0 if failure. extension name must be null terminated+ fn emscripten_webgl_enable_extension(+ context: core::ffi::c_int,+ extension: *const core::ffi::c_char,+ ) -> core::ffi::c_int;+ fn emscripten_webgl_get_current_context() -> core::ffi::c_int;+}+/// Webgl requires extensions to be enabled before using them.+/// This function can be used to enable webgl extension on emscripten target.+///+/// returns true on success+///+/// # Safety+///+/// - opengl context MUST BE current+/// - extension_name_null_terminated argument must be a valid string with null terminator.+/// - extension must be present. check `glow_context.supported_extensions()`+pub unsafe fn enable_extension(extension_name_null_terminated: &str) -> bool {+ unsafe {+ emscripten_webgl_enable_extension(+ emscripten_webgl_get_current_context(),+ extension_name_null_terminated.as_ptr().cast(),+ ) == 1+ }+}diff --git a/vendor/wgpu-hal/src/gles/fence.rs b/vendor/wgpu-hal/src/gles/fence.rsnew file mode 100644index 00000000..0470e5ba--- /dev/null+++ b/vendor/wgpu-hal/src/gles/fence.rs@@ -0,0 +1,198 @@+use alloc::{sync::Arc, vec::Vec};+use core::sync::atomic::Ordering;+use parking_lot::RwLock;++use glow::HasContext;++use crate::AtomicFenceValue;++#[derive(Debug)]+struct GLFence {+ // Since a fence can be `Copy`ed, there can exist some+ // cases where a fence could be destroyed while something+ // else is still using it. Therefore, while a function is+ // using this fence (and doesn't keep pending read locked),+ // it should clone the `Arc` to show it needs this to+ // stay alive.+ //+ // The arc should not be kept after a function has finished+ sync: Arc<glow::Fence>,+ value: crate::FenceValue,+}++#[derive(Debug)]+pub struct Fence {+ last_completed: AtomicFenceValue,+ pending: RwLock<Vec<GLFence>>,+ fence_behavior: wgt::GlFenceBehavior,+}++impl crate::DynFence for Fence {}++#[cfg(send_sync)]+unsafe impl Send for Fence {}+#[cfg(send_sync)]+unsafe impl Sync for Fence {}++impl Fence {+ pub fn new(options: &wgt::GlBackendOptions) -> Self {+ Self {+ last_completed: AtomicFenceValue::new(0),+ pending: RwLock::new(Vec::new()),+ fence_behavior: options.fence_behavior,+ }+ }++ pub fn signal(+ &self,+ gl: &glow::Context,+ value: crate::FenceValue,+ ) -> Result<(), crate::DeviceError> {+ if self.fence_behavior.is_auto_finish() {+ self.last_completed.store(value, Ordering::Release);+ return Ok(());+ }++ let sync = unsafe { gl.fence_sync(glow::SYNC_GPU_COMMANDS_COMPLETE, 0) }+ .map_err(|_| crate::DeviceError::OutOfMemory)?;+ self.pending.write().push(GLFence {+ sync: Arc::new(sync),+ value,+ });++ Ok(())+ }++ pub fn satisfied(&self, value: crate::FenceValue) -> bool {+ self.last_completed.load(Ordering::Acquire) >= value+ }++ pub fn get_latest(&self, gl: &glow::Context) -> crate::FenceValue {+ let mut max_value = self.last_completed.load(Ordering::Acquire);++ if self.fence_behavior.is_auto_finish() {+ return max_value;+ }++ let pending = self.pending.read();++ for gl_fence in pending.iter() {+ if gl_fence.value <= max_value {+ // We already know this was good, no need to check again+ continue;+ }+ // We have pending `read` locked, so we shouldn't have to clone it.+ let status = unsafe { gl.get_sync_status(*gl_fence.sync) };+ if status == glow::SIGNALED {+ max_value = gl_fence.value;+ } else {+ // Anything after the first unsignalled is guaranteed to also be unsignalled+ break;+ }+ }++ // Track the latest value, to save ourselves some querying later+ self.last_completed.fetch_max(max_value, Ordering::AcqRel);++ max_value+ }++ pub fn maintain(&self, gl: &glow::Context) {+ if self.fence_behavior.is_auto_finish() {+ return;+ }++ let latest = self.get_latest(gl);+ let mut pending = self.pending.write();+ pending.retain_mut(|gl_fence| {+ if gl_fence.value > latest {+ true+ } else if let Some(fence) = Arc::get_mut(&mut gl_fence.sync) {+ unsafe {+ gl.delete_sync(*fence);+ }+ false+ } else {+ // Another function is currently using this value. In general, these should finish+ // very quickly (for wait because the fence should already be signaled, an all+ // others are just fast), but submit should be very fast, so we shouldn't block on+ // this.+ true+ }+ });+ }++ pub fn wait(+ &self,+ gl: &glow::Context,+ wait_value: crate::FenceValue,+ timeout_ns: u32,+ ) -> Result<bool, crate::DeviceError> {+ let last_completed = self.last_completed.load(Ordering::Acquire);++ if self.fence_behavior.is_auto_finish() {+ return Ok(last_completed >= wait_value);+ }++ // We already know this fence has been signalled to that value. Return signalled.+ if last_completed >= wait_value {+ return Ok(true);+ }++ let pending = self.pending.read();++ // Find a matching fence+ let gl_fence = pending.iter().find(|gl_fence| gl_fence.value >= wait_value);++ let Some(gl_fence) = gl_fence else {+ log::warn!("Tried to wait for {wait_value} but that value has not been signalled yet");+ return Ok(false);+ };++ // clone to show we're using the fence+ let sync = gl_fence.sync.clone();+ let fence_value = gl_fence.value;++ drop(pending);++ let status = unsafe {+ gl.client_wait_sync(+ *sync,+ glow::SYNC_FLUSH_COMMANDS_BIT,+ timeout_ns.min(i32::MAX as u32) as i32,+ )+ };++ drop(sync);++ let signalled = match status {+ glow::ALREADY_SIGNALED | glow::CONDITION_SATISFIED => true,+ glow::TIMEOUT_EXPIRED | glow::WAIT_FAILED => false,+ _ => {+ log::warn!("Unexpected result from client_wait_sync: {status}");+ false+ }+ };++ if signalled {+ self.last_completed.fetch_max(fence_value, Ordering::AcqRel);+ }++ Ok(signalled)+ }++ pub fn destroy(self, gl: &glow::Context) {+ if self.fence_behavior.is_auto_finish() {+ return;+ }++ for gl_fence in self.pending.into_inner() {+ unsafe {+ gl.delete_sync(+ Arc::into_inner(gl_fence.sync)+ .expect("A function has failed to drop all its references to this"),+ );+ }+ }+ }+}diff --git a/vendor/wgpu-hal/src/gles/mod.rs b/vendor/wgpu-hal/src/gles/mod.rsnew file mode 100644index 00000000..24e26c67--- /dev/null+++ b/vendor/wgpu-hal/src/gles/mod.rs@@ -0,0 +1,1215 @@+/*!+# OpenGL ES3 API (aka GLES3).++Designed to work on Linux and Android, with context provided by EGL.++## Texture views++GLES3 doesn't really have separate texture view objects. We have to remember the+original texture and the sub-range into it. Problem is, however, that there is+no way to expose a subset of array layers or mip levels of a sampled texture.++## Binding model++Binding model is very different from WebGPU, especially with regards to samplers.+GLES3 has sampler objects, but they aren't separately bindable to the shaders.+Each sampled texture is exposed to the shader as a combined texture-sampler binding.++When building the pipeline layout, we linearize binding entries based on the groups+(uniform/storage buffers, uniform/storage textures), and record the mapping into+`BindGroupLayoutInfo`.+When a pipeline gets created, and we track all the texture-sampler associations+from the static use in the shader.+We only support at most one sampler used with each texture so far. The linear index+of this sampler is stored per texture slot in `SamplerBindMap` array.++The texture-sampler pairs get potentially invalidated in 2 places:+ - when a new pipeline is set, we update the linear indices of associated samplers+ - when a new bind group is set, we update both the textures and the samplers++We expect that the changes to sampler states between any 2 pipelines of the same layout+will be minimal, if any.++## Vertex data++Generally, vertex buffers are marked as dirty and lazily bound on draw.++GLES3 doesn't support `first_instance` semantics. However, it's easy to support,+since we are forced to do late binding anyway. We just adjust the offsets+into the vertex data.++### Old path++In GLES-3.0 and WebGL2, vertex buffer layout is provided+together with the actual buffer binding.+We invalidate the attributes on the vertex buffer change, and re-bind them.++### New path++In GLES-3.1 and higher, the vertex buffer layout can be declared separately+from the vertex data itself. This mostly matches WebGPU, however there is a catch:+`stride` needs to be specified with the data, not as a part of the layout.++To address this, we invalidate the vertex buffers based on:+ - whether or not `first_instance` is used+ - stride has changed++## Handling of `base_vertex`, `first_instance`, and `first_vertex`++Between indirect, the lack of `first_instance` semantics, and the availability of `gl_BaseInstance`+in shaders, getting buffers and builtins to work correctly is a bit tricky.++We never emulate `base_vertex` and gl_VertexID behaves as `@builtin(vertex_index)` does, so we+never need to do anything about that.++### GL 4.2+ with ARB_shader_draw_parameters++- `@builtin(instance_index)` translates to `gl_InstanceID + gl_BaseInstance`+- We bind instance buffers without any offset emulation.+- We advertise support for the `INDIRECT_FIRST_INSTANCE` feature.++While we can theoretically have a card with 4.2+ support but without ARB_shader_draw_parameters,+we don't bother with that combination.++### GLES & GL 4.1++- `@builtin(instance_index)` translates to `gl_InstanceID + naga_vs_first_instance`+- We bind instance buffers with offset emulation.+- We _do not_ advertise support for `INDIRECT_FIRST_INSTANCE` and cpu-side pretend the `first_instance` is 0 on indirect calls.++*/++///cbindgen:ignore+#[cfg(not(any(windows, webgl)))]+mod egl;+#[cfg(Emscripten)]+mod emscripten;+#[cfg(webgl)]+mod web;+#[cfg(windows)]+mod wgl;++mod adapter;+mod command;+mod conv;+mod device;+mod fence;+mod queue;++pub use fence::Fence;++#[cfg(not(any(windows, webgl)))]+pub use self::egl::{AdapterContext, AdapterContextLock};+#[cfg(not(any(windows, webgl)))]+pub use self::egl::{Instance, Surface};++#[cfg(webgl)]+pub use self::web::AdapterContext;+#[cfg(webgl)]+pub use self::web::{Instance, Surface};++#[cfg(windows)]+use self::wgl::AdapterContext;+#[cfg(windows)]+pub use self::wgl::{Instance, Surface};++use alloc::{boxed::Box, string::String, string::ToString as _, sync::Arc, vec::Vec};+use core::{+ fmt,+ ops::Range,+ sync::atomic::{AtomicU32, AtomicU8},+};+use parking_lot::Mutex;++use arrayvec::ArrayVec;+use glow::HasContext;+use naga::FastHashMap;++use crate::{CopyExtent, TextureDescriptor};++#[derive(Clone, Debug)]+pub struct Api;++//Note: we can support more samplers if not every one of them is used at a time,+// but it probably doesn't worth it.+const MAX_TEXTURE_SLOTS: usize = 16;+const MAX_SAMPLERS: usize = 16;+const MAX_VERTEX_ATTRIBUTES: usize = 16;+const ZERO_BUFFER_SIZE: usize = 256 << 10;+const MAX_IMMEDIATES: usize = 64;+// We have to account for each immediate data may need to be set for every shader.+const MAX_IMMEDIATES_COMMANDS: usize = MAX_IMMEDIATES * crate::MAX_CONCURRENT_SHADER_STAGES;++impl crate::Api for Api {+ const VARIANT: wgt::Backend = wgt::Backend::Gl;++ type Instance = Instance;+ type Surface = Surface;+ type Adapter = Adapter;+ type Device = Device;++ type Queue = Queue;+ type CommandEncoder = CommandEncoder;+ type CommandBuffer = CommandBuffer;++ type Buffer = Buffer;+ type Texture = Texture;+ type SurfaceTexture = Texture;+ type TextureView = TextureView;+ type Sampler = Sampler;+ type QuerySet = QuerySet;+ type Fence = Fence;+ type AccelerationStructure = AccelerationStructure;+ type PipelineCache = PipelineCache;++ type BindGroupLayout = BindGroupLayout;+ type BindGroup = BindGroup;+ type PipelineLayout = PipelineLayout;+ type ShaderModule = ShaderModule;+ type RenderPipeline = RenderPipeline;+ type ComputePipeline = ComputePipeline;+ type RayTracingPipeline = RayTracingPipeline;+}++crate::impl_dyn_resource!(+ Adapter,+ AccelerationStructure,+ BindGroup,+ BindGroupLayout,+ Buffer,+ CommandBuffer,+ CommandEncoder,+ ComputePipeline,+ Device,+ Fence,+ Instance,+ PipelineCache,+ PipelineLayout,+ QuerySet,+ Queue,+ RenderPipeline,+ RayTracingPipeline,+ Sampler,+ ShaderModule,+ Surface,+ Texture,+ TextureView+);++bitflags::bitflags! {+ /// Flags that affect internal code paths but do not+ /// change the exposed feature set.+ #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]+ struct PrivateCapabilities: u32 {+ /// Indicates support for `glBufferStorage` allocation.+ const BUFFER_ALLOCATION = 1 << 0;+ /// Support explicit layouts in shader.+ const SHADER_BINDING_LAYOUT = 1 << 1;+ /// Support extended shadow sampling instructions.+ const SHADER_TEXTURE_SHADOW_LOD = 1 << 2;+ /// Support memory barriers.+ const MEMORY_BARRIERS = 1 << 3;+ /// Vertex buffer layouts separate from the data.+ const VERTEX_BUFFER_LAYOUT = 1 << 4;+ /// Indicates that buffers used as `GL_ELEMENT_ARRAY_BUFFER` may be created / initialized / used+ /// as other targets, if not present they must not be mixed with other targets.+ const INDEX_BUFFER_ROLE_CHANGE = 1 << 5;+ /// Supports `glGetBufferSubData`+ const GET_BUFFER_SUB_DATA = 1 << 7;+ /// Supports `f16` color buffers+ const COLOR_BUFFER_HALF_FLOAT = 1 << 8;+ /// Supports `f11/f10` and `f32` color buffers+ const COLOR_BUFFER_FLOAT = 1 << 9;+ /// Supports query buffer objects.+ const QUERY_BUFFERS = 1 << 11;+ /// Supports 64 bit queries via `glGetQueryObjectui64v`+ const QUERY_64BIT = 1 << 12;+ /// Supports `glTexStorage2D`, etc.+ const TEXTURE_STORAGE = 1 << 13;+ /// Supports `push_debug_group`, `pop_debug_group` and `debug_message_insert`.+ const DEBUG_FNS = 1 << 14;+ /// Supports framebuffer invalidation.+ const INVALIDATE_FRAMEBUFFER = 1 << 15;+ /// Indicates support for `glDrawElementsInstancedBaseVertexBaseInstance` and `ARB_shader_draw_parameters`+ ///+ /// When this is true, instance offset emulation via vertex buffer rebinding and a shader uniform will be disabled.+ const FULLY_FEATURED_INSTANCING = 1 << 16;+ /// Supports direct multisampled rendering to a texture without needing a resolve texture.+ const MULTISAMPLED_RENDER_TO_TEXTURE = 1 << 17;+ /// Supports norm16 sized internal formats as filterable sampled+ /// textures, with UNORM variants also color-renderable. SNORM+ /// renderability is gated on `TEXTURE_FORMAT_SNORM16_RENDERABLE`.+ const TEXTURE_FORMAT_NORM16 = 1 << 18;+ /// Supports SNORM 16-bit formats as color attachments. Requires+ /// `GL_EXT_render_snorm` (in addition to `GL_EXT_texture_norm16`+ /// on GLES) - desktop GL alone only "optionally" renders SNORM 16.+ const TEXTURE_FORMAT_SNORM16_RENDERABLE = 1 << 19;+ /// Supports norm16 sized internal formats as image-load/store targets.+ /// Desktop GL >= 4.2 (core image-format list) or pre-4.2 with+ /// `GL_ARB_shader_image_load_store`; GLES needs `GL_NV_image_formats`+ /// (which itself depends on `GL_EXT_texture_norm16`).+ const TEXTURE_FORMAT_NORM16_STORAGE = 1 << 20;+ }+}++bitflags::bitflags! {+ /// Flags that indicate necessary workarounds for specific devices or driver bugs+ #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]+ struct Workarounds: u32 {+ // Needs workaround for Intel Mesa bug:+ // https://gitlab.freedesktop.org/mesa/mesa/-/issues/2565.+ //+ // This comment+ // (https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4972/diffs?diff_id=75888#22f5d1004713c9bbf857988c7efb81631ab88f99_323_327)+ // seems to indicate all skylake models are effected.+ const MESA_I915_SRGB_SHADER_CLEAR = 1 << 0;+ /// Buffer map must emulated because it is not supported natively+ const EMULATE_BUFFER_MAP = 1 << 1;+ }+}++type BindTarget = u32;++#[derive(Debug, Default, Clone, Copy)]+enum VertexAttribKind {+ #[default]+ Float, // glVertexAttribPointer+ Integer, // glVertexAttribIPointer+ //Double, // glVertexAttribLPointer+}++#[derive(Clone, Debug)]+pub struct TextureFormatDesc {+ pub internal: u32,+ pub external: u32,+ pub data_type: u32,+}++struct AdapterShared {+ context: AdapterContext,+ private_caps: PrivateCapabilities,+ features: wgt::Features,+ limits: wgt::Limits,+ workarounds: Workarounds,+ options: wgt::GlBackendOptions,+ shading_language_version: naga::back::glsl::Version,+ next_shader_id: AtomicU32,+ program_cache: Mutex<ProgramCache>,+ es: bool,++ /// Result of `gl.get_parameter_i32(glow::MAX_SAMPLES)`.+ /// Cached here so it doesn't need to be queried every time texture format capabilities are requested.+ /// (this has been shown to be a significant enough overhead)+ max_msaa_samples: i32,+}++impl fmt::Debug for AdapterShared {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ let Self {+ context: _, // may or may not implement Debug depending on platform+ private_caps,+ features,+ limits,+ workarounds,+ options,+ shading_language_version,+ next_shader_id,+ program_cache: _,+ es,+ max_msaa_samples,+ } = self;+ f.debug_struct("AdapterShared")+ .field("private_caps", private_caps)+ .field("features", features)+ .field("limits", limits)+ .field("workarounds", workarounds)+ .field("options", options)+ .field("shading_language_version", shading_language_version)+ .field("next_shader_id", next_shader_id)+ .field("es", es)+ .field("max_msaa_samples", max_msaa_samples)+ .finish_non_exhaustive()+ }+}++#[derive(Debug)]+pub struct Adapter {+ shared: Arc<AdapterShared>,+}++#[derive(Debug)]+pub struct Device {+ shared: Arc<AdapterShared>,+ main_vao: glow::VertexArray,+ #[cfg(all(native, feature = "renderdoc"))]+ render_doc: crate::auxil::renderdoc::RenderDoc,+ counters: Arc<wgt::HalCounters>,+}++impl Drop for Device {+ fn drop(&mut self) {+ let gl = &self.shared.context.lock();+ unsafe { gl.delete_vertex_array(self.main_vao) };+ }+}++#[derive(Debug)]+pub struct ShaderClearProgram {+ pub program: glow::Program,+ pub color_uniform_location: glow::UniformLocation,+}++#[derive(Debug)]+pub struct Queue {+ shared: Arc<AdapterShared>,+ features: wgt::Features,+ draw_fbo: glow::Framebuffer,+ copy_fbo: glow::Framebuffer,+ /// Shader program used to clear the screen for [`Workarounds::MESA_I915_SRGB_SHADER_CLEAR`]+ /// devices.+ shader_clear_program: Option<ShaderClearProgram>,+ /// Keep a reasonably large buffer filled with zeroes, so that we can implement `ClearBuffer` of+ /// zeroes by copying from it.+ zero_buffer: glow::Buffer,+ temp_query_results: Mutex<Vec<u64>>,+ draw_buffer_count: AtomicU8,+ current_index_buffer: Mutex<Option<glow::Buffer>>,+}++impl Drop for Queue {+ fn drop(&mut self) {+ let gl = &self.shared.context.lock();+ unsafe { gl.delete_framebuffer(self.draw_fbo) };+ unsafe { gl.delete_framebuffer(self.copy_fbo) };+ unsafe { gl.delete_buffer(self.zero_buffer) };+ }+}++#[derive(Clone, Debug)]+pub struct Buffer {+ raw: Option<glow::Buffer>,+ target: BindTarget,+ size: wgt::BufferAddress,+ /// Flags to use within calls to [`Device::map_buffer`](crate::Device::map_buffer).+ map_flags: u32,+ /// Buffer mapping state.+ ///+ /// If locked concurrently with the GL context, the GL context should be locked first.+ map_state: Arc<MaybeMutex<BufferMapState>>,+ /// Set when the buffer wraps an externally-owned GL name created via+ /// [`Device::buffer_from_raw`](crate::gles::Device::buffer_from_raw).+ ///+ /// `Buffer` is `Clone`, so the guard is shared via `Arc`+ /// and only fires its callback once every clone is dropped.+ drop_guard: Option<Arc<crate::DropGuard>>,+}++#[derive(Clone, Debug)]+struct BufferMapState {+ /// True if the GL buffer is actually mapped, i.e. not "fake-mapped" with+ /// an empty slice+ mapped: bool,+ data: Option<Vec<u8>>,+ offset_of_current_mapping: wgt::BufferAddress,+}++#[cfg(send_sync)]+static_assertions::assert_impl_all!(Buffer: Send, Sync);++impl crate::DynBuffer for Buffer {}++#[derive(Clone, Debug)]+pub enum TextureInner {+ Renderbuffer {+ raw: glow::Renderbuffer,+ },+ DefaultRenderbuffer,+ Texture {+ raw: glow::Texture,+ target: BindTarget,+ },+ #[cfg(webgl)]+ /// Render to a `WebGLFramebuffer`+ ///+ /// This is a web feature+ ExternalFramebuffer {+ inner: web_sys::WebGlFramebuffer,+ },+ #[cfg(native)]+ /// Render to a `glow::NativeFramebuffer`+ /// Useful when the framebuffer to draw to+ /// has a non-zero framebuffer ID+ ///+ /// This is a native feature+ ExternalNativeFramebuffer {+ inner: glow::NativeFramebuffer,+ },+}++#[cfg(send_sync)]+unsafe impl Sync for TextureInner {}+#[cfg(send_sync)]+unsafe impl Send for TextureInner {}++impl TextureInner {+ fn as_native(&self) -> (glow::Texture, BindTarget) {+ match *self {+ Self::Renderbuffer { .. } | Self::DefaultRenderbuffer => {+ panic!("Unexpected renderbuffer");+ }+ Self::Texture { raw, target } => (raw, target),+ #[cfg(webgl)]+ Self::ExternalFramebuffer { .. } => panic!("Unexpected external framebuffer"),+ #[cfg(native)]+ Self::ExternalNativeFramebuffer { .. } => panic!("unexpected external framebuffer"),+ }+ }+}++#[derive(Debug)]+pub struct Texture {+ pub inner: TextureInner,+ pub mip_level_count: u32,+ pub array_layer_count: u32,+ pub format: wgt::TextureFormat,+ pub format_desc: TextureFormatDesc,+ pub copy_size: CopyExtent,++ // The `drop_guard` field must be the last field of this struct so it is dropped last.+ // Do not add new fields after it.+ pub drop_guard: Option<crate::DropGuard>,+}++impl crate::DynTexture for Texture {}+impl crate::DynSurfaceTexture for Texture {}++impl core::borrow::Borrow<dyn crate::DynTexture> for Texture {+ fn borrow(&self) -> &dyn crate::DynTexture {+ self+ }+}++impl Texture {+ pub fn default_framebuffer(format: wgt::TextureFormat) -> Self {+ Self {+ inner: TextureInner::DefaultRenderbuffer,+ drop_guard: None,+ mip_level_count: 1,+ array_layer_count: 1,+ format,+ format_desc: TextureFormatDesc {+ internal: 0,+ external: 0,+ data_type: 0,+ },+ copy_size: CopyExtent {+ width: 0,+ height: 0,+ depth: 0,+ },+ }+ }++ /// Returns the `target`, whether the image is 3d and whether the image is a cubemap.+ fn get_info_from_desc(desc: &TextureDescriptor) -> u32 {+ match desc.dimension {+ // WebGL (1 and 2) as well as some GLES versions do not have 1D textures, so we are+ // doing `TEXTURE_2D` instead+ wgt::TextureDimension::D1 => glow::TEXTURE_2D,+ wgt::TextureDimension::D2 => {+ // HACK: detect a cube map; forces cube compatible textures to be cube textures+ match (desc.is_cube_compatible(), desc.size.depth_or_array_layers) {+ (false, 1) => glow::TEXTURE_2D,+ (false, _) => glow::TEXTURE_2D_ARRAY,+ (true, 6) => glow::TEXTURE_CUBE_MAP,+ (true, _) => glow::TEXTURE_CUBE_MAP_ARRAY,+ }+ }+ wgt::TextureDimension::D3 => glow::TEXTURE_3D,+ }+ }++ /// More information can be found in issues #1614 and #1574+ fn log_failing_target_heuristics(view_dimension: wgt::TextureViewDimension, target: u32) {+ let expected_target = match view_dimension {+ wgt::TextureViewDimension::D1 => glow::TEXTURE_2D,+ wgt::TextureViewDimension::D2 => glow::TEXTURE_2D,+ wgt::TextureViewDimension::D2Array => glow::TEXTURE_2D_ARRAY,+ wgt::TextureViewDimension::Cube => glow::TEXTURE_CUBE_MAP,+ wgt::TextureViewDimension::CubeArray => glow::TEXTURE_CUBE_MAP_ARRAY,+ wgt::TextureViewDimension::D3 => glow::TEXTURE_3D,+ };++ if expected_target == target {+ return;+ }++ let buffer;+ let got = match target {+ glow::TEXTURE_2D => "D2",+ glow::TEXTURE_2D_ARRAY => "D2Array",+ glow::TEXTURE_CUBE_MAP => "Cube",+ glow::TEXTURE_CUBE_MAP_ARRAY => "CubeArray",+ glow::TEXTURE_3D => "D3",+ target => {+ buffer = target.to_string();+ &buffer+ }+ };++ log::error!(+ concat!(+ "wgpu-hal heuristics assumed that ",+ "the view dimension will be equal to `{}` rather than `{:?}`.\n",+ "`D2` textures with ",+ "`depth_or_array_layers == 1` ",+ "are assumed to have view dimension `D2`\n",+ "`D2` textures with ",+ "`depth_or_array_layers > 1` ",+ "are assumed to have view dimension `D2Array`\n",+ "`D2` textures with ",+ "`depth_or_array_layers == 6` ",+ "are assumed to have view dimension `Cube`\n",+ "`D2` textures with ",+ "`depth_or_array_layers > 6 && depth_or_array_layers % 6 == 0` ",+ "are assumed to have view dimension `CubeArray`\n",+ ),+ got,+ view_dimension,+ );+ }+}++#[derive(Clone, Debug)]+pub struct TextureView {+ inner: TextureInner,+ aspects: crate::FormatAspects,+ mip_levels: Range<u32>,+ array_layers: Range<u32>,+ format: wgt::TextureFormat,+}++impl crate::DynTextureView for TextureView {}++#[derive(Debug)]+pub struct Sampler {+ raw: glow::Sampler,+}++impl crate::DynSampler for Sampler {}++#[derive(Debug)]+pub struct BindGroupLayout {+ entries: Arc<[wgt::BindGroupLayoutEntry]>,+}++impl crate::DynBindGroupLayout for BindGroupLayout {}++#[derive(Debug)]+struct BindGroupLayoutInfo {+ entries: Arc<[wgt::BindGroupLayoutEntry]>,+ /// Mapping of resources, indexed by `binding`, into the whole layout space.+ /// For texture resources, the value is the texture slot index.+ /// For sampler resources, the value is the index of the sampler in the whole layout.+ /// For buffers, the value is the uniform or storage slot index.+ /// For unused bindings, the value is `!0`+ binding_to_slot: Box<[u8]>,+}++#[derive(Debug)]+pub struct PipelineLayout {+ group_infos: Box<[Option<BindGroupLayoutInfo>]>,+ naga_options: naga::back::glsl::Options,+}++impl crate::DynPipelineLayout for PipelineLayout {}++impl PipelineLayout {+ /// # Panics+ /// If the pipeline layout does not contain a bind group layout used by+ /// the resource binding.+ fn get_slot(&self, br: &naga::ResourceBinding) -> u8 {+ let group_info = self.group_infos[br.group as usize].as_ref().unwrap();+ group_info.binding_to_slot[br.binding as usize]+ }+}++#[derive(Debug)]+enum BindingRegister {+ UniformBuffers,+ StorageBuffers,+ Textures,+ Images,+}++#[derive(Debug)]+enum RawBinding {+ Buffer {+ raw: glow::Buffer,+ offset: i32,+ size: i32,+ },+ Texture {+ raw: glow::Texture,+ target: BindTarget,+ aspects: crate::FormatAspects,+ mip_levels: Range<u32>,+ //TODO: array layers+ },+ Image(ImageBinding),+ Sampler(glow::Sampler),+}++#[derive(Debug)]+pub struct BindGroup {+ contents: Box<[RawBinding]>,+}++impl crate::DynBindGroup for BindGroup {}++type ShaderId = u32;++#[derive(Debug)]+pub enum ShaderModuleSource {+ Naga(crate::NagaShader),+ Passthrough { source: String },+}++#[derive(Debug)]+pub struct ShaderModule {+ source: ShaderModuleSource,+ label: Option<String>,+ id: ShaderId,+}++impl crate::DynShaderModule for ShaderModule {}++#[derive(Clone, Debug, Default)]+struct VertexFormatDesc {+ element_count: i32,+ element_format: u32,+ attrib_kind: VertexAttribKind,+}++#[derive(Clone, Debug, Default)]+struct AttributeDesc {+ location: u32,+ offset: u32,+ buffer_index: u32,+ format_desc: VertexFormatDesc,+}++#[derive(Clone, Debug)]+struct BufferBinding {+ raw: glow::Buffer,+ offset: wgt::BufferAddress,+}++#[derive(Clone, Debug)]+struct ImageBinding {+ raw: glow::Texture,+ mip_level: u32,+ array_layer: Option<u32>,+ access: u32,+ format: u32,+}++#[derive(Clone, Debug, Default, PartialEq)]+struct VertexBufferDesc {+ step: wgt::VertexStepMode,+ stride: u32,+}++#[derive(Clone, Debug)]+struct ImmediateDesc {+ location: glow::UniformLocation,+ ty: nt::glsl::GlslUniformType,+ offset: u32,+ size_bytes: u32,+}++#[cfg(send_sync)]+unsafe impl Sync for ImmediateDesc {}+#[cfg(send_sync)]+unsafe impl Send for ImmediateDesc {}++/// For each texture in the pipeline layout, store the index of the only+/// sampler (in this layout) that the texture is used with.+type SamplerBindMap = [Option<u8>; MAX_TEXTURE_SLOTS];++#[derive(Debug)]+struct PipelineInner {+ program: glow::Program,+ sampler_map: SamplerBindMap,+ first_instance_location: Option<glow::UniformLocation>,+ immediates_descs: ArrayVec<ImmediateDesc, MAX_IMMEDIATES_COMMANDS>,+ clip_distance_count: u32,+}++#[cfg(send_sync)]+unsafe impl Sync for PipelineInner {}+#[cfg(send_sync)]+unsafe impl Send for PipelineInner {}++#[derive(Clone, Debug)]+struct DepthState {+ function: u32,+ mask: bool,+}++#[derive(Clone, Debug, PartialEq)]+struct BlendComponent {+ src: u32,+ dst: u32,+ equation: u32,+}++#[derive(Clone, Debug, PartialEq)]+struct BlendDesc {+ alpha: BlendComponent,+ color: BlendComponent,+}++#[derive(Clone, Debug, Default, PartialEq)]+struct ColorTargetDesc {+ mask: wgt::ColorWrites,+ blend: Option<BlendDesc>,+}++#[derive(Debug, PartialEq, Eq, Hash)]+struct ProgramStage {+ naga_stage: naga::ShaderStage,+ shader_id: ShaderId,+ entry_point: String,+ zero_initialize_workgroup_memory: bool,+ constant_hash: Vec<u8>,+}++#[derive(Debug, PartialEq, Eq, Hash)]+struct ProgramCacheKey {+ stages: ArrayVec<ProgramStage, 3>,+ group_to_binding_to_slot: Box<[Option<Box<[u8]>>]>,+}++type ProgramCache = FastHashMap<ProgramCacheKey, Result<Arc<PipelineInner>, crate::PipelineError>>;++#[derive(Debug)]+pub struct RenderPipeline {+ inner: Arc<PipelineInner>,+ primitive: wgt::PrimitiveState,+ vertex_buffers: Box<[Option<VertexBufferDesc>]>,+ vertex_attributes: Box<[AttributeDesc]>,+ color_targets: Box<[ColorTargetDesc]>,+ depth: Option<DepthState>,+ depth_bias: wgt::DepthBiasState,+ stencil: Option<StencilState>,+ alpha_to_coverage_enabled: bool,+}++impl crate::DynRenderPipeline for RenderPipeline {}++#[cfg(send_sync)]+static_assertions::assert_impl_all!(RenderPipeline: Send, Sync);++#[derive(Debug)]+pub struct ComputePipeline {+ inner: Arc<PipelineInner>,+}++impl crate::DynComputePipeline for ComputePipeline {}++#[derive(Debug)]+pub struct RayTracingPipeline {}++impl crate::DynRayTracingPipeline for RayTracingPipeline {}++#[cfg(send_sync)]+static_assertions::assert_impl_all!(ComputePipeline: Send, Sync);++#[derive(Debug)]+pub struct QuerySet {+ queries: Box<[glow::Query]>,+ target: BindTarget,+}++impl crate::DynQuerySet for QuerySet {}++#[derive(Debug)]+pub struct AccelerationStructure;++impl crate::DynAccelerationStructure for AccelerationStructure {}++#[derive(Debug)]+pub struct PipelineCache;++impl crate::DynPipelineCache for PipelineCache {}++#[derive(Clone, Debug, PartialEq)]+struct StencilOps {+ pass: u32,+ fail: u32,+ depth_fail: u32,+}++impl Default for StencilOps {+ fn default() -> Self {+ Self {+ pass: glow::KEEP,+ fail: glow::KEEP,+ depth_fail: glow::KEEP,+ }+ }+}++#[derive(Clone, Debug, PartialEq)]+struct StencilSide {+ function: u32,+ mask_read: u32,+ mask_write: u32,+ reference: u32,+ ops: StencilOps,+}++impl Default for StencilSide {+ fn default() -> Self {+ Self {+ function: glow::ALWAYS,+ mask_read: 0xFF,+ mask_write: 0xFF,+ reference: 0,+ ops: StencilOps::default(),+ }+ }+}++#[derive(Debug, Clone, Default)]+struct StencilState {+ front: StencilSide,+ back: StencilSide,+}++#[derive(Clone, Debug, Default, PartialEq)]+struct PrimitiveState {+ front_face: u32,+ cull_face: u32,+ unclipped_depth: bool,+ polygon_mode: u32,+}++type InvalidatedAttachments = ArrayVec<u32, { crate::MAX_COLOR_ATTACHMENTS + 2 }>;++#[derive(Debug)]+enum Command {+ Draw {+ topology: u32,+ first_vertex: u32,+ vertex_count: u32,+ first_instance: u32,+ instance_count: u32,+ first_instance_location: Option<glow::UniformLocation>,+ },+ DrawIndexed {+ topology: u32,+ index_type: u32,+ index_count: u32,+ index_offset: wgt::BufferAddress,+ base_vertex: i32,+ first_instance: u32,+ instance_count: u32,+ first_instance_location: Option<glow::UniformLocation>,+ },+ DrawIndirect {+ topology: u32,+ indirect_buf: glow::Buffer,+ indirect_offset: wgt::BufferAddress,+ first_instance_location: Option<glow::UniformLocation>,+ },+ DrawIndexedIndirect {+ topology: u32,+ index_type: u32,+ indirect_buf: glow::Buffer,+ indirect_offset: wgt::BufferAddress,+ first_instance_location: Option<glow::UniformLocation>,+ },+ Dispatch([u32; 3]),+ DispatchIndirect {+ indirect_buf: glow::Buffer,+ indirect_offset: wgt::BufferAddress,+ },+ ClearBuffer {+ dst: Buffer,+ dst_target: BindTarget,+ range: crate::MemoryRange,+ },+ CopyBufferToBuffer {+ src: Buffer,+ src_target: BindTarget,+ dst: Buffer,+ dst_target: BindTarget,+ copy: crate::BufferCopy,+ },+ #[cfg(webgl)]+ CopyExternalImageToTexture {+ src: wgt::CopyExternalImageSourceInfo,+ dst: glow::Texture,+ dst_target: BindTarget,+ dst_format: wgt::TextureFormat,+ dst_premultiplication: bool,+ copy: crate::TextureCopy,+ },+ CopyTextureToTexture {+ src: glow::Texture,+ src_target: BindTarget,+ dst: glow::Texture,+ dst_target: BindTarget,+ copy: crate::TextureCopy,+ },+ CopyBufferToTexture {+ src: Buffer,+ #[allow(unused)]+ src_target: BindTarget,+ dst: glow::Texture,+ dst_target: BindTarget,+ dst_format: wgt::TextureFormat,+ copy: crate::BufferTextureCopy,+ },+ CopyTextureToBuffer {+ src: glow::Texture,+ src_target: BindTarget,+ src_format: wgt::TextureFormat,+ dst: Buffer,+ #[allow(unused)]+ dst_target: BindTarget,+ copy: crate::BufferTextureCopy,+ },+ SetIndexBuffer(glow::Buffer),+ BeginQuery(glow::Query, BindTarget),+ EndQuery(BindTarget),+ TimestampQuery(glow::Query),+ CopyQueryResults {+ query_range: Range<u32>,+ dst: Buffer,+ dst_target: BindTarget,+ dst_offset: wgt::BufferAddress,+ },+ ResetFramebuffer {+ is_default: bool,+ },+ BindAttachment {+ attachment: u32,+ view: TextureView,+ depth_slice: Option<u32>,+ sample_count: u32,+ },+ ResolveAttachment {+ attachment: u32,+ dst: TextureView,+ size: wgt::Extent3d,+ },+ InvalidateAttachments(InvalidatedAttachments),+ SetDrawColorBuffers(u8),+ ClearColorF {+ draw_buffer: u32,+ color: [f32; 4],+ is_srgb: bool,+ },+ ClearColorU(u32, [u32; 4]),+ ClearColorI(u32, [i32; 4]),+ ClearDepth(f32),+ ClearStencil(u32),+ // Clearing both the depth and stencil buffer individually appears to+ // result in the stencil buffer failing to clear, atleast in WebGL.+ // It is also more efficient to emit a single command instead of two for+ // this.+ ClearDepthAndStencil(f32, u32),+ BufferBarrier(glow::Buffer, wgt::BufferUses),+ TextureBarrier(wgt::TextureUses),+ SetViewport {+ rect: crate::Rect<i32>,+ depth: Range<f32>,+ },+ SetScissor(crate::Rect<i32>),+ SetStencilFunc {+ face: u32,+ function: u32,+ reference: u32,+ read_mask: u32,+ },+ SetStencilOps {+ face: u32,+ write_mask: u32,+ ops: StencilOps,+ },+ SetDepth(DepthState),+ SetDepthBias(wgt::DepthBiasState),+ ConfigureDepthStencil(crate::FormatAspects),+ SetAlphaToCoverage(bool),+ SetVertexAttribute {+ buffer: Option<glow::Buffer>,+ buffer_desc: VertexBufferDesc,+ attribute_desc: AttributeDesc,+ },+ UnsetVertexAttribute(u32),+ SetVertexBuffer {+ index: u32,+ buffer: BufferBinding,+ buffer_desc: VertexBufferDesc,+ },+ SetProgram(glow::Program),+ SetPrimitive(PrimitiveState),+ SetBlendConstant([f32; 4]),+ SetColorTarget {+ draw_buffer_index: Option<u32>,+ desc: ColorTargetDesc,+ },+ BindBuffer {+ target: BindTarget,+ slot: u32,+ buffer: glow::Buffer,+ offset: i32,+ size: i32,+ },+ BindSampler(u32, Option<glow::Sampler>),+ BindTexture {+ slot: u32,+ texture: glow::Texture,+ target: BindTarget,+ aspects: crate::FormatAspects,+ mip_levels: Range<u32>,+ },+ BindImage {+ slot: u32,+ binding: ImageBinding,+ },+ InsertDebugMarker(Range<u32>),+ PushDebugGroup(Range<u32>),+ PopDebugGroup,+ SetImmediates {+ uniform: ImmediateDesc,+ /// Offset from the start of the `data_bytes`+ offset: u32,+ },+ SetClipDistances {+ old_count: u32,+ new_count: u32,+ },+}++#[derive(Default)]+pub struct CommandBuffer {+ label: Option<String>,+ commands: Vec<Command>,+ data_bytes: Vec<u8>,+ queries: Vec<glow::Query>,+}++impl crate::DynCommandBuffer for CommandBuffer {}++impl fmt::Debug for CommandBuffer {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ let mut builder = f.debug_struct("CommandBuffer");+ if let Some(ref label) = self.label {+ builder.field("label", label);+ }+ builder.finish()+ }+}++#[cfg(send_sync)]+unsafe impl Sync for CommandBuffer {}+#[cfg(send_sync)]+unsafe impl Send for CommandBuffer {}++//TODO: we would have something like `Arc<typed_arena::Arena>`+// here and in the command buffers. So that everything grows+// inside the encoder and stays there until `reset_all`.++pub struct CommandEncoder {+ cmd_buffer: CommandBuffer,+ state: command::State,+ private_caps: PrivateCapabilities,+ counters: Arc<wgt::HalCounters>,+}++impl fmt::Debug for CommandEncoder {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ f.debug_struct("CommandEncoder")+ .field("cmd_buffer", &self.cmd_buffer)+ .finish()+ }+}++#[cfg(send_sync)]+unsafe impl Sync for CommandEncoder {}+#[cfg(send_sync)]+unsafe impl Send for CommandEncoder {}++#[cfg(not(webgl))]+fn gl_debug_message_callback(source: u32, gltype: u32, id: u32, severity: u32, message: &str) {+ let source_str = match source {+ glow::DEBUG_SOURCE_API => "API",+ glow::DEBUG_SOURCE_WINDOW_SYSTEM => "Window System",+ glow::DEBUG_SOURCE_SHADER_COMPILER => "ShaderCompiler",+ glow::DEBUG_SOURCE_THIRD_PARTY => "Third Party",+ glow::DEBUG_SOURCE_APPLICATION => "Application",+ glow::DEBUG_SOURCE_OTHER => "Other",+ _ => unreachable!(),+ };++ let log_severity = match severity {+ glow::DEBUG_SEVERITY_HIGH => log::Level::Error,+ glow::DEBUG_SEVERITY_MEDIUM => log::Level::Warn,+ glow::DEBUG_SEVERITY_LOW => log::Level::Debug,+ glow::DEBUG_SEVERITY_NOTIFICATION => log::Level::Trace,+ _ => unreachable!(),+ };++ let type_str = match gltype {+ glow::DEBUG_TYPE_DEPRECATED_BEHAVIOR => "Deprecated Behavior",+ glow::DEBUG_TYPE_ERROR => "Error",+ glow::DEBUG_TYPE_MARKER => "Marker",+ glow::DEBUG_TYPE_OTHER => "Other",+ glow::DEBUG_TYPE_PERFORMANCE => "Performance",+ glow::DEBUG_TYPE_POP_GROUP => "Pop Group",+ glow::DEBUG_TYPE_PORTABILITY => "Portability",+ glow::DEBUG_TYPE_PUSH_GROUP => "Push Group",+ glow::DEBUG_TYPE_UNDEFINED_BEHAVIOR => "Undefined Behavior",+ _ => unreachable!(),+ };++ let _ = std::panic::catch_unwind(|| {+ log::log!(+ log_severity,+ "GLES: [{source_str}/{type_str}] ID {id} : {message}"+ );+ });++ #[cfg(feature = "validation_canary")]+ if cfg!(debug_assertions) && log_severity == log::Level::Error {+ // Set canary and continue+ crate::VALIDATION_CANARY.add(message.to_string());+ }+}++// If we are using `std`, then use `Mutex` to provide `Send` and `Sync`+cfg_if::cfg_if! {+ if #[cfg(gles_with_std)] {+ type MaybeMutex<T> = std::sync::Mutex<T>;++ fn lock<T>(mutex: &MaybeMutex<T>) -> std::sync::MutexGuard<'_, T> {+ mutex.lock().unwrap()+ }+ } else {+ // It should be impossible for any build configuration to trigger this error+ // It is intended only as a guard against changes elsewhere causing the use of+ // `RefCell` here to become unsound.+ #[cfg(all(send_sync, not(feature = "fragile-send-sync-non-atomic-wasm")))]+ compile_error!("cannot provide non-fragile Send+Sync without std");++ type MaybeMutex<T> = core::cell::RefCell<T>;++ fn lock<T>(mutex: &MaybeMutex<T>) -> core::cell::RefMut<'_, T> {+ mutex.borrow_mut()+ }+ }+}diff --git a/vendor/wgpu-hal/src/gles/queue.rs b/vendor/wgpu-hal/src/gles/queue.rsnew file mode 100644index 00000000..9984cd55--- /dev/null+++ b/vendor/wgpu-hal/src/gles/queue.rs@@ -0,0 +1,1980 @@+use alloc::sync::Arc;+use alloc::vec;+use core::sync::atomic::Ordering;++use arrayvec::ArrayVec;+use glow::HasContext;++use super::{conv::is_layered_target, lock, Command as C, PrivateCapabilities};++const DEBUG_ID: u32 = 0;++fn extract_marker<'a>(data: &'a [u8], range: &core::ops::Range<u32>) -> &'a str {+ core::str::from_utf8(&data[range.start as usize..range.end as usize]).unwrap()+}++fn to_debug_str(s: &str) -> &str {+ // The spec mentions that if the length given to debug functions is negative,+ // the implementation will access the ptr and look for a null that terminates+ // the string but some implementations will try to access the ptr even if the+ // length is 0.+ if s.is_empty() {+ "<empty>"+ } else {+ s+ }+}++fn get_2d_target(target: u32, array_layer: u32) -> u32 {+ const CUBEMAP_FACES: [u32; 6] = [+ glow::TEXTURE_CUBE_MAP_POSITIVE_X,+ glow::TEXTURE_CUBE_MAP_NEGATIVE_X,+ glow::TEXTURE_CUBE_MAP_POSITIVE_Y,+ glow::TEXTURE_CUBE_MAP_NEGATIVE_Y,+ glow::TEXTURE_CUBE_MAP_POSITIVE_Z,+ glow::TEXTURE_CUBE_MAP_NEGATIVE_Z,+ ];++ match target {+ glow::TEXTURE_2D => target,+ glow::TEXTURE_CUBE_MAP => CUBEMAP_FACES[array_layer as usize],+ _ => unreachable!(),+ }+}++fn get_z_offset(target: u32, base: &crate::TextureCopyBase) -> u32 {+ match target {+ glow::TEXTURE_2D_ARRAY | glow::TEXTURE_CUBE_MAP_ARRAY => base.array_layer,+ glow::TEXTURE_3D => base.origin.z,+ _ => unreachable!(),+ }+}++impl super::Queue {+ /// Performs a manual shader clear, used as a workaround for a clearing bug on mesa+ unsafe fn perform_shader_clear(&self, gl: &glow::Context, draw_buffer: u32, color: [f32; 4]) {+ let shader_clear = self+ .shader_clear_program+ .as_ref()+ .expect("shader_clear_program should always be set if the workaround is enabled");+ unsafe { gl.use_program(Some(shader_clear.program)) };+ unsafe {+ gl.uniform_4_f32(+ Some(&shader_clear.color_uniform_location),+ color[0],+ color[1],+ color[2],+ color[3],+ )+ };+ unsafe { gl.disable(glow::DEPTH_TEST) };+ unsafe { gl.disable(glow::STENCIL_TEST) };+ unsafe { gl.disable(glow::SCISSOR_TEST) };+ unsafe { gl.disable(glow::BLEND) };+ unsafe { gl.disable(glow::CULL_FACE) };+ unsafe { gl.draw_buffers(&[glow::COLOR_ATTACHMENT0 + draw_buffer]) };+ unsafe { gl.draw_arrays(glow::TRIANGLES, 0, 3) };++ let draw_buffer_count = self.draw_buffer_count.load(Ordering::Relaxed);+ if draw_buffer_count != 0 {+ // Reset the draw buffers to what they were before the clear+ let indices = (0..draw_buffer_count as u32)+ .map(|i| glow::COLOR_ATTACHMENT0 + i)+ .collect::<ArrayVec<_, { crate::MAX_COLOR_ATTACHMENTS }>>();+ unsafe { gl.draw_buffers(&indices) };+ }+ }++ unsafe fn reset_state(&self, gl: &glow::Context) {+ unsafe { gl.use_program(None) };+ unsafe { gl.bind_framebuffer(glow::FRAMEBUFFER, None) };+ unsafe { gl.disable(glow::DEPTH_TEST) };+ unsafe { gl.disable(glow::STENCIL_TEST) };+ unsafe { gl.disable(glow::SCISSOR_TEST) };+ unsafe { gl.disable(glow::BLEND) };+ unsafe { gl.disable(glow::CULL_FACE) };+ unsafe { gl.disable(glow::POLYGON_OFFSET_FILL) };+ unsafe { gl.disable(glow::SAMPLE_ALPHA_TO_COVERAGE) };+ if self.features.contains(wgt::Features::DEPTH_CLIP_CONTROL) {+ unsafe { gl.disable(glow::DEPTH_CLAMP) };+ }++ unsafe { gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, None) };+ let mut current_index_buffer = self.current_index_buffer.lock();+ *current_index_buffer = None;+ }++ unsafe fn set_attachment(+ &self,+ gl: &glow::Context,+ fbo_target: u32,+ attachment: u32,+ view: &super::TextureView,+ depth_slice: Option<u32>,+ sample_count: u32,+ ) {+ match view.inner {+ super::TextureInner::Renderbuffer { raw } => {+ unsafe {+ gl.framebuffer_renderbuffer(+ fbo_target,+ attachment,+ glow::RENDERBUFFER,+ Some(raw),+ )+ };+ }+ super::TextureInner::DefaultRenderbuffer => panic!("Unexpected default RBO"),+ super::TextureInner::Texture { raw, target } => {+ let num_layers = view.array_layers.end - view.array_layers.start;+ if num_layers > 1 {+ #[cfg(webgl)]+ unsafe {+ gl.framebuffer_texture_multiview_ovr(+ fbo_target,+ attachment,+ Some(raw),+ view.mip_levels.start as i32,+ view.array_layers.start as i32,+ num_layers as i32,+ )+ };+ } else if is_layered_target(target) {+ let layer = if target == glow::TEXTURE_3D {+ depth_slice.unwrap() as i32+ } else {+ view.array_layers.start as i32+ };+ unsafe {+ gl.framebuffer_texture_layer(+ fbo_target,+ attachment,+ Some(raw),+ view.mip_levels.start as i32,+ layer,+ )+ };+ } else {+ unsafe {+ assert_eq!(view.mip_levels.len(), 1);+ if sample_count != 1 {+ gl.framebuffer_texture_2d_multisample(+ fbo_target,+ attachment,+ get_2d_target(target, view.array_layers.start),+ Some(raw),+ view.mip_levels.start as i32,+ sample_count as i32,+ )+ } else {+ gl.framebuffer_texture_2d(+ fbo_target,+ attachment,+ get_2d_target(target, view.array_layers.start),+ Some(raw),+ view.mip_levels.start as i32,+ )+ }+ };+ }+ }+ #[cfg(webgl)]+ super::TextureInner::ExternalFramebuffer { ref inner } => unsafe {+ gl.bind_external_framebuffer(glow::FRAMEBUFFER, inner);+ },+ #[cfg(native)]+ super::TextureInner::ExternalNativeFramebuffer { ref inner } => unsafe {+ gl.bind_framebuffer(glow::FRAMEBUFFER, Some(*inner));+ },+ }+ }++ unsafe fn process(+ &self,+ gl: &glow::Context,+ command: &C,+ #[cfg_attr(target_arch = "wasm32", allow(unused))] data_bytes: &[u8],+ queries: &[glow::Query],+ ) {+ match *command {+ C::Draw {+ topology,+ first_vertex,+ vertex_count,+ instance_count,+ first_instance,+ ref first_instance_location,+ } => {+ let supports_full_instancing = self+ .shared+ .private_caps+ .contains(PrivateCapabilities::FULLY_FEATURED_INSTANCING);++ if supports_full_instancing {+ unsafe {+ gl.draw_arrays_instanced_base_instance(+ topology,+ first_vertex as i32,+ vertex_count as i32,+ instance_count as i32,+ first_instance,+ )+ }+ } else {+ unsafe {+ gl.uniform_1_u32(first_instance_location.as_ref(), first_instance);+ }++ // Don't use `gl.draw_arrays` for `instance_count == 1`.+ // Angle has a bug where it doesn't consider the instance divisor when `DYNAMIC_DRAW` is used in `draw_arrays`.+ // See https://github.com/gfx-rs/wgpu/issues/3578+ unsafe {+ gl.draw_arrays_instanced(+ topology,+ first_vertex as i32,+ vertex_count as i32,+ instance_count as i32,+ )+ }+ };+ }+ C::DrawIndexed {+ topology,+ index_type,+ index_count,+ index_offset,+ base_vertex,+ first_instance,+ instance_count,+ ref first_instance_location,+ } => {+ let supports_full_instancing = self+ .shared+ .private_caps+ .contains(PrivateCapabilities::FULLY_FEATURED_INSTANCING);++ if supports_full_instancing {+ unsafe {+ gl.draw_elements_instanced_base_vertex_base_instance(+ topology,+ index_count as i32,+ index_type,+ index_offset as i32,+ instance_count as i32,+ base_vertex,+ first_instance,+ )+ }+ } else {+ unsafe { gl.uniform_1_u32(first_instance_location.as_ref(), first_instance) };++ if base_vertex == 0 {+ unsafe {+ // Don't use `gl.draw_elements`/`gl.draw_elements_base_vertex` for `instance_count == 1`.+ // Angle has a bug where it doesn't consider the instance divisor when `DYNAMIC_DRAW` is used in `gl.draw_elements`/`gl.draw_elements_base_vertex`.+ // See https://github.com/gfx-rs/wgpu/issues/3578+ gl.draw_elements_instanced(+ topology,+ index_count as i32,+ index_type,+ index_offset as i32,+ instance_count as i32,+ )+ }+ } else {+ // If we've gotten here, wgpu-core has already validated that this function exists via the DownlevelFlags::BASE_VERTEX feature.+ unsafe {+ gl.draw_elements_instanced_base_vertex(+ topology,+ index_count as _,+ index_type,+ index_offset as i32,+ instance_count as i32,+ base_vertex,+ )+ }+ }+ }+ }+ C::DrawIndirect {+ topology,+ indirect_buf,+ indirect_offset,+ ref first_instance_location,+ } => {+ unsafe { gl.uniform_1_u32(first_instance_location.as_ref(), 0) };++ unsafe { gl.bind_buffer(glow::DRAW_INDIRECT_BUFFER, Some(indirect_buf)) };+ unsafe { gl.draw_arrays_indirect_offset(topology, indirect_offset as i32) };+ }+ C::DrawIndexedIndirect {+ topology,+ index_type,+ indirect_buf,+ indirect_offset,+ ref first_instance_location,+ } => {+ unsafe { gl.uniform_1_u32(first_instance_location.as_ref(), 0) };++ unsafe { gl.bind_buffer(glow::DRAW_INDIRECT_BUFFER, Some(indirect_buf)) };+ unsafe {+ gl.draw_elements_indirect_offset(topology, index_type, indirect_offset as i32)+ };+ }+ C::Dispatch(group_counts) => {+ unsafe { gl.dispatch_compute(group_counts[0], group_counts[1], group_counts[2]) };+ }+ C::DispatchIndirect {+ indirect_buf,+ indirect_offset,+ } => {+ unsafe { gl.bind_buffer(glow::DISPATCH_INDIRECT_BUFFER, Some(indirect_buf)) };+ unsafe { gl.dispatch_compute_indirect(indirect_offset as i32) };+ }+ C::ClearBuffer {+ ref dst,+ dst_target,+ ref range,+ } => match dst.raw {+ Some(buffer) => {+ // When `INDEX_BUFFER_ROLE_CHANGE` isn't available, we can't copy into the+ // index buffer from the zero buffer. This would fail in Chrome with the+ // following message:+ //+ // > Cannot copy into an element buffer destination from a non-element buffer+ // > source+ //+ // Instead, we'll upload zeroes into the buffer.+ let can_use_zero_buffer = self+ .shared+ .private_caps+ .contains(PrivateCapabilities::INDEX_BUFFER_ROLE_CHANGE)+ || dst_target != glow::ELEMENT_ARRAY_BUFFER;++ if can_use_zero_buffer {+ unsafe { gl.bind_buffer(glow::COPY_READ_BUFFER, Some(self.zero_buffer)) };+ unsafe { gl.bind_buffer(dst_target, Some(buffer)) };+ let mut dst_offset = range.start;+ while dst_offset < range.end {+ let size = (range.end - dst_offset).min(super::ZERO_BUFFER_SIZE as u64);+ unsafe {+ gl.copy_buffer_sub_data(+ glow::COPY_READ_BUFFER,+ dst_target,+ 0,+ dst_offset as i32,+ size as i32,+ )+ };+ dst_offset += size;+ }+ } else {+ unsafe { gl.bind_buffer(dst_target, Some(buffer)) };+ let zeroes = vec![0u8; (range.end - range.start) as usize];+ unsafe {+ gl.buffer_sub_data_u8_slice(dst_target, range.start as i32, &zeroes)+ };+ }+ }+ None => {+ let mut map_state = lock(&dst.map_state);+ map_state.data.as_mut().unwrap().as_mut_slice()+ [range.start as usize..range.end as usize]+ .fill(0);+ }+ },+ C::CopyBufferToBuffer {+ ref src,+ src_target,+ ref dst,+ dst_target,+ copy,+ } => {+ let copy_src_target = glow::COPY_READ_BUFFER;+ let is_index_buffer_only_element_dst = !self+ .shared+ .private_caps+ .contains(PrivateCapabilities::INDEX_BUFFER_ROLE_CHANGE)+ && dst_target == glow::ELEMENT_ARRAY_BUFFER+ || src_target == glow::ELEMENT_ARRAY_BUFFER;++ // WebGL not allowed to copy data from other targets to element buffer and can't copy element data to other buffers+ let copy_dst_target = if is_index_buffer_only_element_dst {+ glow::ELEMENT_ARRAY_BUFFER+ } else {+ glow::COPY_WRITE_BUFFER+ };+ let size = copy.size.get() as usize;+ match (src.raw, dst.raw) {+ (Some(ref src), Some(ref dst)) => {+ unsafe { gl.bind_buffer(copy_src_target, Some(*src)) };+ unsafe { gl.bind_buffer(copy_dst_target, Some(*dst)) };+ unsafe {+ gl.copy_buffer_sub_data(+ copy_src_target,+ copy_dst_target,+ copy.src_offset as _,+ copy.dst_offset as _,+ copy.size.get() as _,+ )+ };+ }+ (Some(src), None) => {+ let mut map_state = lock(&dst.map_state);+ let dst_data = &mut map_state.data.as_mut().unwrap().as_mut_slice()+ [copy.dst_offset as usize..copy.dst_offset as usize + size];++ unsafe { gl.bind_buffer(copy_src_target, Some(src)) };+ unsafe {+ self.shared.get_buffer_sub_data(+ gl,+ copy_src_target,+ copy.src_offset as i32,+ dst_data,+ )+ };+ }+ (None, Some(dst)) => {+ let map_state = lock(&src.map_state);+ let src_data = &map_state.data.as_ref().unwrap().as_slice()+ [copy.src_offset as usize..copy.src_offset as usize + size];+ unsafe { gl.bind_buffer(copy_dst_target, Some(dst)) };+ unsafe {+ gl.buffer_sub_data_u8_slice(+ copy_dst_target,+ copy.dst_offset as i32,+ src_data,+ )+ };+ }+ (None, None) => {+ todo!()+ }+ }+ unsafe { gl.bind_buffer(copy_src_target, None) };+ if is_index_buffer_only_element_dst {+ unsafe {+ gl.bind_buffer(+ glow::ELEMENT_ARRAY_BUFFER,+ *self.current_index_buffer.lock(),+ )+ };+ } else {+ unsafe { gl.bind_buffer(copy_dst_target, None) };+ }+ }+ #[cfg(webgl)]+ C::CopyExternalImageToTexture {+ ref src,+ dst,+ dst_target,+ dst_format,+ dst_premultiplication,+ ref copy,+ } => {+ const UNPACK_FLIP_Y_WEBGL: u32 =+ web_sys::WebGl2RenderingContext::UNPACK_FLIP_Y_WEBGL;+ const UNPACK_PREMULTIPLY_ALPHA_WEBGL: u32 =+ web_sys::WebGl2RenderingContext::UNPACK_PREMULTIPLY_ALPHA_WEBGL;++ unsafe {+ if src.flip_y {+ gl.pixel_store_bool(UNPACK_FLIP_Y_WEBGL, true);+ }+ if dst_premultiplication {+ gl.pixel_store_bool(UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);+ }+ }++ unsafe { gl.bind_texture(dst_target, Some(dst)) };+ let format_desc = self.shared.describe_texture_format(dst_format);+ if is_layered_target(dst_target) {+ let z_offset = get_z_offset(dst_target, ©.dst_base);++ match src.source {+ wgt::ExternalImageSource::ImageBitmap(ref b) => unsafe {+ gl.tex_sub_image_3d_with_image_bitmap(+ dst_target,+ copy.dst_base.mip_level as i32,+ copy.dst_base.origin.x as i32,+ copy.dst_base.origin.y as i32,+ z_offset as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ copy.size.depth as i32,+ format_desc.external,+ format_desc.data_type,+ b,+ );+ },+ wgt::ExternalImageSource::HTMLImageElement(ref i) => unsafe {+ gl.tex_sub_image_3d_with_html_image_element(+ dst_target,+ copy.dst_base.mip_level as i32,+ copy.dst_base.origin.x as i32,+ copy.dst_base.origin.y as i32,+ z_offset as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ copy.size.depth as i32,+ format_desc.external,+ format_desc.data_type,+ i,+ );+ },+ wgt::ExternalImageSource::HTMLVideoElement(ref v) => unsafe {+ gl.tex_sub_image_3d_with_html_video_element(+ dst_target,+ copy.dst_base.mip_level as i32,+ copy.dst_base.origin.x as i32,+ copy.dst_base.origin.y as i32,+ z_offset as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ copy.size.depth as i32,+ format_desc.external,+ format_desc.data_type,+ v,+ );+ },+ #[cfg(not(web_sys_unstable_apis))]+ wgt::ExternalImageSource::VideoFrame(_) => {+ unimplemented!("web_sys_unstable_apis is needed for glow")+ }+ #[cfg(web_sys_unstable_apis)]+ wgt::ExternalImageSource::VideoFrame(ref v) => unsafe {+ gl.tex_sub_image_3d_with_video_frame(+ dst_target,+ copy.dst_base.mip_level as i32,+ copy.dst_base.origin.x as i32,+ copy.dst_base.origin.y as i32,+ z_offset as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ copy.size.depth as i32,+ format_desc.external,+ format_desc.data_type,+ v,+ )+ },+ wgt::ExternalImageSource::ImageData(ref i) => unsafe {+ gl.tex_sub_image_3d_with_image_data(+ dst_target,+ copy.dst_base.mip_level as i32,+ copy.dst_base.origin.x as i32,+ copy.dst_base.origin.y as i32,+ z_offset as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ copy.size.depth as i32,+ format_desc.external,+ format_desc.data_type,+ i,+ );+ },+ wgt::ExternalImageSource::HTMLCanvasElement(ref c) => unsafe {+ gl.tex_sub_image_3d_with_html_canvas_element(+ dst_target,+ copy.dst_base.mip_level as i32,+ copy.dst_base.origin.x as i32,+ copy.dst_base.origin.y as i32,+ z_offset as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ copy.size.depth as i32,+ format_desc.external,+ format_desc.data_type,+ c,+ );+ },+ wgt::ExternalImageSource::OffscreenCanvas(_) => unreachable!(),+ }+ } else {+ let dst_target = get_2d_target(dst_target, copy.dst_base.array_layer);++ match src.source {+ wgt::ExternalImageSource::ImageBitmap(ref b) => unsafe {+ gl.tex_sub_image_2d_with_image_bitmap_and_width_and_height(+ dst_target,+ copy.dst_base.mip_level as i32,+ copy.dst_base.origin.x as i32,+ copy.dst_base.origin.y as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ format_desc.external,+ format_desc.data_type,+ b,+ );+ },+ wgt::ExternalImageSource::HTMLImageElement(ref i) => unsafe {+ gl.tex_sub_image_2d_with_html_image_and_width_and_height(+ dst_target,+ copy.dst_base.mip_level as i32,+ copy.dst_base.origin.x as i32,+ copy.dst_base.origin.y as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ format_desc.external,+ format_desc.data_type,+ i,+ )+ },+ wgt::ExternalImageSource::HTMLVideoElement(ref v) => unsafe {+ gl.tex_sub_image_2d_with_html_video_and_width_and_height(+ dst_target,+ copy.dst_base.mip_level as i32,+ copy.dst_base.origin.x as i32,+ copy.dst_base.origin.y as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ format_desc.external,+ format_desc.data_type,+ v,+ )+ },+ #[cfg(not(web_sys_unstable_apis))]+ wgt::ExternalImageSource::VideoFrame(_) => {+ unimplemented!("web_sys_unstable_apis is needed for glow")+ }+ #[cfg(web_sys_unstable_apis)]+ wgt::ExternalImageSource::VideoFrame(ref v) => unsafe {+ gl.tex_sub_image_2d_with_video_frame_and_width_and_height(+ dst_target,+ copy.dst_base.mip_level as i32,+ copy.dst_base.origin.x as i32,+ copy.dst_base.origin.y as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ format_desc.external,+ format_desc.data_type,+ v,+ )+ },+ wgt::ExternalImageSource::ImageData(ref i) => unsafe {+ gl.tex_sub_image_2d_with_image_data_and_width_and_height(+ dst_target,+ copy.dst_base.mip_level as i32,+ copy.dst_base.origin.x as i32,+ copy.dst_base.origin.y as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ format_desc.external,+ format_desc.data_type,+ i,+ );+ },+ wgt::ExternalImageSource::HTMLCanvasElement(ref c) => unsafe {+ gl.tex_sub_image_2d_with_html_canvas_and_width_and_height(+ dst_target,+ copy.dst_base.mip_level as i32,+ copy.dst_base.origin.x as i32,+ copy.dst_base.origin.y as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ format_desc.external,+ format_desc.data_type,+ c,+ )+ },+ wgt::ExternalImageSource::OffscreenCanvas(_) => unreachable!(),+ }+ }++ unsafe {+ if src.flip_y {+ gl.pixel_store_bool(UNPACK_FLIP_Y_WEBGL, false);+ }+ if dst_premultiplication {+ gl.pixel_store_bool(UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);+ }+ }+ }+ C::CopyTextureToTexture {+ src,+ src_target,+ dst,+ dst_target,+ ref copy,+ } => {+ //TODO: handle 3D copies+ unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(self.copy_fbo)) };+ if is_layered_target(src_target) {+ //TODO: handle GLES without framebuffer_texture_3d+ unsafe {+ gl.framebuffer_texture_layer(+ glow::READ_FRAMEBUFFER,+ glow::COLOR_ATTACHMENT0,+ Some(src),+ copy.src_base.mip_level as i32,+ copy.src_base.array_layer as i32,+ )+ };+ } else {+ unsafe {+ gl.framebuffer_texture_2d(+ glow::READ_FRAMEBUFFER,+ glow::COLOR_ATTACHMENT0,+ src_target,+ Some(src),+ copy.src_base.mip_level as i32,+ )+ };+ }++ unsafe { gl.bind_texture(dst_target, Some(dst)) };+ if is_layered_target(dst_target) {+ unsafe {+ gl.copy_tex_sub_image_3d(+ dst_target,+ copy.dst_base.mip_level as i32,+ copy.dst_base.origin.x as i32,+ copy.dst_base.origin.y as i32,+ get_z_offset(dst_target, ©.dst_base) as i32,+ copy.src_base.origin.x as i32,+ copy.src_base.origin.y as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ )+ };+ } else {+ unsafe {+ gl.copy_tex_sub_image_2d(+ get_2d_target(dst_target, copy.dst_base.array_layer),+ copy.dst_base.mip_level as i32,+ copy.dst_base.origin.x as i32,+ copy.dst_base.origin.y as i32,+ copy.src_base.origin.x as i32,+ copy.src_base.origin.y as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ )+ };+ }+ }+ C::CopyBufferToTexture {+ ref src,+ src_target: _,+ dst,+ dst_target,+ dst_format,+ ref copy,+ } => {+ let (block_width, block_height) = dst_format.block_dimensions();+ let block_size = dst_format.block_copy_size(None).unwrap();+ let format_desc = self.shared.describe_texture_format(dst_format);+ let row_texels = copy+ .buffer_layout+ .bytes_per_row+ .map_or(0, |bpr| block_width * bpr / block_size);+ let column_texels = copy+ .buffer_layout+ .rows_per_image+ .map_or(0, |rpi| block_height * rpi);++ unsafe { gl.bind_texture(dst_target, Some(dst)) };+ unsafe { gl.pixel_store_i32(glow::UNPACK_ROW_LENGTH, row_texels as i32) };+ unsafe { gl.pixel_store_i32(glow::UNPACK_IMAGE_HEIGHT, column_texels as i32) };+ let mut unbind_unpack_buffer = false;+ if !dst_format.is_compressed() {+ let map_state;+ let unpack_data = match src.raw {+ Some(buffer) => {+ unsafe { gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, Some(buffer)) };+ unbind_unpack_buffer = true;+ glow::PixelUnpackData::BufferOffset(copy.buffer_layout.offset as u32)+ }+ None => {+ map_state = lock(&src.map_state);+ let src_data = &map_state.data.as_ref().unwrap().as_slice()+ [copy.buffer_layout.offset as usize..];+ glow::PixelUnpackData::Slice(Some(src_data))+ }+ };+ if is_layered_target(dst_target) {+ unsafe {+ gl.tex_sub_image_3d(+ dst_target,+ copy.texture_base.mip_level as i32,+ copy.texture_base.origin.x as i32,+ copy.texture_base.origin.y as i32,+ get_z_offset(dst_target, ©.texture_base) as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ copy.size.depth as i32,+ format_desc.external,+ format_desc.data_type,+ unpack_data,+ )+ };+ } else {+ unsafe {+ gl.tex_sub_image_2d(+ get_2d_target(dst_target, copy.texture_base.array_layer),+ copy.texture_base.mip_level as i32,+ copy.texture_base.origin.x as i32,+ copy.texture_base.origin.y as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ format_desc.external,+ format_desc.data_type,+ unpack_data,+ )+ };+ }+ } else {+ let bytes_per_row = copy+ .buffer_layout+ .bytes_per_row+ .unwrap_or(copy.size.width * block_size);+ let minimum_rows_per_image = copy.size.height.div_ceil(block_height);+ let rows_per_image = copy+ .buffer_layout+ .rows_per_image+ .unwrap_or(minimum_rows_per_image);++ let bytes_per_image = bytes_per_row * rows_per_image;+ let minimum_bytes_per_image = bytes_per_row * minimum_rows_per_image;+ let bytes_in_upload =+ (bytes_per_image * (copy.size.depth - 1)) + minimum_bytes_per_image;+ let offset = copy.buffer_layout.offset as u32;++ let map_state;+ let unpack_data = match src.raw {+ Some(buffer) => {+ unsafe { gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, Some(buffer)) };+ unbind_unpack_buffer = true;+ glow::CompressedPixelUnpackData::BufferRange(+ offset..offset + bytes_in_upload,+ )+ }+ None => {+ map_state = lock(&src.map_state);+ let src_data = &map_state.data.as_ref().unwrap().as_slice()+ [(offset as usize)..(offset + bytes_in_upload) as usize];+ glow::CompressedPixelUnpackData::Slice(src_data)+ }+ };++ if is_layered_target(dst_target) {+ unsafe {+ gl.compressed_tex_sub_image_3d(+ dst_target,+ copy.texture_base.mip_level as i32,+ copy.texture_base.origin.x as i32,+ copy.texture_base.origin.y as i32,+ get_z_offset(dst_target, ©.texture_base) as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ copy.size.depth as i32,+ format_desc.internal,+ unpack_data,+ )+ };+ } else {+ unsafe {+ gl.compressed_tex_sub_image_2d(+ get_2d_target(dst_target, copy.texture_base.array_layer),+ copy.texture_base.mip_level as i32,+ copy.texture_base.origin.x as i32,+ copy.texture_base.origin.y as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ format_desc.internal,+ unpack_data,+ )+ };+ }+ }+ if unbind_unpack_buffer {+ unsafe { gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, None) };+ }+ }+ C::CopyTextureToBuffer {+ src,+ src_target,+ src_format,+ ref dst,+ dst_target: _,+ ref copy,+ } => {+ let block_size = src_format.block_copy_size(None).unwrap();+ if src_format.is_compressed() {+ log::error!("Not implemented yet: compressed texture copy to buffer");+ return;+ }+ if src_target == glow::TEXTURE_CUBE_MAP+ || src_target == glow::TEXTURE_CUBE_MAP_ARRAY+ {+ log::error!("Not implemented yet: cubemap texture copy to buffer");+ return;+ }+ let format_desc = self.shared.describe_texture_format(src_format);+ let row_texels = copy+ .buffer_layout+ .bytes_per_row+ .map_or(copy.size.width, |bpr| bpr / block_size);+ let column_texels = copy+ .buffer_layout+ .rows_per_image+ .unwrap_or(copy.size.height);++ unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(self.copy_fbo)) };++ let read_pixels = |offset| {+ let mut map_state;+ let unpack_data = match dst.raw {+ Some(buffer) => {+ unsafe { gl.pixel_store_i32(glow::PACK_ROW_LENGTH, row_texels as i32) };+ unsafe { gl.bind_buffer(glow::PIXEL_PACK_BUFFER, Some(buffer)) };+ glow::PixelPackData::BufferOffset(offset as u32)+ }+ None => {+ map_state = lock(&dst.map_state);+ let dst_data = &mut map_state.data.as_mut().unwrap().as_mut_slice()+ [offset as usize..];+ glow::PixelPackData::Slice(Some(dst_data))+ }+ };+ unsafe {+ gl.read_pixels(+ copy.texture_base.origin.x as i32,+ copy.texture_base.origin.y as i32,+ copy.size.width as i32,+ copy.size.height as i32,+ format_desc.external,+ format_desc.data_type,+ unpack_data,+ )+ };+ };++ match src_target {+ glow::TEXTURE_2D => {+ unsafe {+ gl.framebuffer_texture_2d(+ glow::READ_FRAMEBUFFER,+ glow::COLOR_ATTACHMENT0,+ src_target,+ Some(src),+ copy.texture_base.mip_level as i32,+ )+ };+ read_pixels(copy.buffer_layout.offset);+ }+ glow::TEXTURE_2D_ARRAY => {+ unsafe {+ gl.framebuffer_texture_layer(+ glow::READ_FRAMEBUFFER,+ glow::COLOR_ATTACHMENT0,+ Some(src),+ copy.texture_base.mip_level as i32,+ copy.texture_base.array_layer as i32,+ )+ };+ read_pixels(copy.buffer_layout.offset);+ }+ glow::TEXTURE_3D => {+ for z in copy.texture_base.origin.z..copy.size.depth {+ unsafe {+ gl.framebuffer_texture_layer(+ glow::READ_FRAMEBUFFER,+ glow::COLOR_ATTACHMENT0,+ Some(src),+ copy.texture_base.mip_level as i32,+ z as i32,+ )+ };+ let offset = copy.buffer_layout.offset+ + (z * block_size * row_texels * column_texels) as u64;+ read_pixels(offset);+ }+ }+ glow::TEXTURE_CUBE_MAP | glow::TEXTURE_CUBE_MAP_ARRAY => unimplemented!(),+ _ => unreachable!(),+ }+ }+ C::SetIndexBuffer(buffer) => {+ unsafe { gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(buffer)) };+ let mut current_index_buffer = self.current_index_buffer.lock();+ *current_index_buffer = Some(buffer);+ }+ C::BeginQuery(query, target) => {+ unsafe { gl.begin_query(target, query) };+ }+ C::EndQuery(target) => {+ unsafe { gl.end_query(target) };+ }+ C::TimestampQuery(query) => {+ unsafe { gl.query_counter(query, glow::TIMESTAMP) };+ }+ C::CopyQueryResults {+ ref query_range,+ ref dst,+ dst_target,+ dst_offset,+ } => {+ if self+ .shared+ .private_caps+ .contains(PrivateCapabilities::QUERY_BUFFERS)+ && dst.raw.is_some()+ {+ unsafe {+ // We're assuming that the only relevant queries are 8 byte timestamps or+ // occlusion tests.+ let query_size = 8;++ let query_range_size = query_size * query_range.len();++ let buffer = gl.create_buffer().ok();+ gl.bind_buffer(glow::QUERY_BUFFER, buffer);+ gl.buffer_data_size(+ glow::QUERY_BUFFER,+ query_range_size as _,+ glow::STREAM_COPY,+ );++ for (i, &query) in queries+ [query_range.start as usize..query_range.end as usize]+ .iter()+ .enumerate()+ {+ gl.get_query_parameter_u64_with_offset(+ query,+ glow::QUERY_RESULT,+ query_size * i,+ )+ }+ gl.bind_buffer(dst_target, dst.raw);+ gl.copy_buffer_sub_data(+ glow::QUERY_BUFFER,+ dst_target,+ 0,+ dst_offset as _,+ query_range_size as _,+ );+ if let Some(buffer) = buffer {+ gl.delete_buffer(buffer)+ }+ }+ } else {+ let mut temp_query_results = self.temp_query_results.lock();+ temp_query_results.clear();+ for &query in+ queries[query_range.start as usize..query_range.end as usize].iter()+ {+ let mut result: u64 = 0;+ unsafe {+ if self+ .shared+ .private_caps+ .contains(PrivateCapabilities::QUERY_64BIT)+ {+ let result: *mut u64 = &mut result;+ gl.get_query_parameter_u64_with_offset(+ query,+ glow::QUERY_RESULT,+ result as usize,+ )+ } else {+ result =+ gl.get_query_parameter_u32(query, glow::QUERY_RESULT) as u64;+ }+ };+ temp_query_results.push(result);+ }+ let query_data = bytemuck::cast_slice(&temp_query_results);+ match dst.raw {+ Some(buffer) => {+ unsafe { gl.bind_buffer(dst_target, Some(buffer)) };+ unsafe {+ gl.buffer_sub_data_u8_slice(+ dst_target,+ dst_offset as i32,+ query_data,+ )+ };+ }+ None => {+ let mut map_state = lock(&dst.map_state);+ let data = map_state.data.as_mut().unwrap();+ let len = query_data.len().min(data.len());+ data[..len].copy_from_slice(&query_data[..len]);+ }+ }+ }+ }+ C::ResetFramebuffer { is_default } => {+ if is_default {+ unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, None) };+ } else {+ unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, Some(self.draw_fbo)) };+ unsafe {+ gl.framebuffer_texture_2d(+ glow::DRAW_FRAMEBUFFER,+ glow::DEPTH_STENCIL_ATTACHMENT,+ glow::TEXTURE_2D,+ None,+ 0,+ )+ };+ for i in 0..self.shared.limits.max_color_attachments {+ let target = glow::COLOR_ATTACHMENT0 + i;+ unsafe {+ gl.framebuffer_texture_2d(+ glow::DRAW_FRAMEBUFFER,+ target,+ glow::TEXTURE_2D,+ None,+ 0,+ )+ };+ }+ }+ unsafe { gl.color_mask(true, true, true, true) };+ unsafe { gl.depth_mask(true) };+ unsafe { gl.stencil_mask(!0) };+ unsafe { gl.disable(glow::DEPTH_TEST) };+ unsafe { gl.disable(glow::STENCIL_TEST) };+ unsafe { gl.disable(glow::SCISSOR_TEST) };+ }+ C::BindAttachment {+ attachment,+ ref view,+ depth_slice,+ sample_count,+ } => {+ unsafe {+ self.set_attachment(+ gl,+ glow::DRAW_FRAMEBUFFER,+ attachment,+ view,+ depth_slice,+ sample_count,+ )+ };+ }+ C::ResolveAttachment {+ attachment,+ ref dst,+ ref size,+ } => {+ unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(self.draw_fbo)) };+ unsafe { gl.read_buffer(attachment) };+ unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, Some(self.copy_fbo)) };+ unsafe {+ self.set_attachment(+ gl,+ glow::DRAW_FRAMEBUFFER,+ glow::COLOR_ATTACHMENT0,+ dst,+ None,+ 1,+ )+ };+ unsafe {+ gl.blit_framebuffer(+ 0,+ 0,+ size.width as i32,+ size.height as i32,+ 0,+ 0,+ size.width as i32,+ size.height as i32,+ glow::COLOR_BUFFER_BIT,+ glow::NEAREST,+ )+ };+ unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, None) };+ unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, Some(self.draw_fbo)) };+ }+ C::InvalidateAttachments(ref list) => {+ if self+ .shared+ .private_caps+ .contains(PrivateCapabilities::INVALIDATE_FRAMEBUFFER)+ {+ unsafe { gl.invalidate_framebuffer(glow::DRAW_FRAMEBUFFER, list) };+ }+ }+ C::SetDrawColorBuffers(count) => {+ self.draw_buffer_count.store(count, Ordering::Relaxed);+ let indices = (0..count as u32)+ .map(|i| glow::COLOR_ATTACHMENT0 + i)+ .collect::<ArrayVec<_, { crate::MAX_COLOR_ATTACHMENTS }>>();+ unsafe { gl.draw_buffers(&indices) };+ }+ C::ClearColorF {+ draw_buffer,+ ref color,+ is_srgb,+ } => {+ if self+ .shared+ .workarounds+ .contains(super::Workarounds::MESA_I915_SRGB_SHADER_CLEAR)+ && is_srgb+ {+ unsafe { self.perform_shader_clear(gl, draw_buffer, *color) };+ } else {+ unsafe { gl.clear_buffer_f32_slice(glow::COLOR, draw_buffer, color) };+ }+ }+ C::ClearColorU(draw_buffer, ref color) => {+ unsafe { gl.clear_buffer_u32_slice(glow::COLOR, draw_buffer, color) };+ }+ C::ClearColorI(draw_buffer, ref color) => {+ unsafe { gl.clear_buffer_i32_slice(glow::COLOR, draw_buffer, color) };+ }+ C::ClearDepth(depth) => {+ // Prefer `clear` as `clear_buffer` functions have issues on Sandy Bridge+ // on Windows.+ unsafe {+ gl.clear_depth_f32(depth);+ gl.clear(glow::DEPTH_BUFFER_BIT);+ }+ }+ C::ClearStencil(value) => {+ // Prefer `clear` as `clear_buffer` functions have issues on Sandy Bridge+ // on Windows.+ unsafe {+ gl.clear_stencil(value as i32);+ gl.clear(glow::STENCIL_BUFFER_BIT);+ }+ }+ C::ClearDepthAndStencil(depth, stencil_value) => {+ // Prefer `clear` as `clear_buffer` functions have issues on Sandy Bridge+ // on Windows.+ unsafe {+ gl.clear_depth_f32(depth);+ gl.clear_stencil(stencil_value as i32);+ gl.clear(glow::DEPTH_BUFFER_BIT | glow::STENCIL_BUFFER_BIT);+ }+ }+ C::BufferBarrier(raw, usage) => {+ let mut flags = 0;+ if usage.contains(wgt::BufferUses::VERTEX) {+ flags |= glow::VERTEX_ATTRIB_ARRAY_BARRIER_BIT;+ unsafe { gl.bind_buffer(glow::ARRAY_BUFFER, Some(raw)) };+ unsafe { gl.vertex_attrib_pointer_f32(0, 1, glow::BYTE, true, 0, 0) };+ }+ if usage.contains(wgt::BufferUses::INDEX) {+ flags |= glow::ELEMENT_ARRAY_BARRIER_BIT;+ unsafe { gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(raw)) };+ }+ if usage.contains(wgt::BufferUses::UNIFORM) {+ flags |= glow::UNIFORM_BARRIER_BIT;+ }+ if usage.contains(wgt::BufferUses::INDIRECT) {+ flags |= glow::COMMAND_BARRIER_BIT;+ unsafe { gl.bind_buffer(glow::DRAW_INDIRECT_BUFFER, Some(raw)) };+ }+ if usage.contains(wgt::BufferUses::COPY_SRC) {+ flags |= glow::PIXEL_BUFFER_BARRIER_BIT;+ unsafe { gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, Some(raw)) };+ }+ if usage.contains(wgt::BufferUses::COPY_DST) {+ flags |= glow::PIXEL_BUFFER_BARRIER_BIT;+ unsafe { gl.bind_buffer(glow::PIXEL_PACK_BUFFER, Some(raw)) };+ }+ if usage.intersects(wgt::BufferUses::MAP_READ | wgt::BufferUses::MAP_WRITE) {+ flags |= glow::BUFFER_UPDATE_BARRIER_BIT;+ }+ if usage.intersects(+ wgt::BufferUses::STORAGE_READ_ONLY | wgt::BufferUses::STORAGE_READ_WRITE,+ ) {+ flags |= glow::SHADER_STORAGE_BARRIER_BIT;+ }+ unsafe { gl.memory_barrier(flags) };+ }+ // because `STORAGE_WRITE_ONLY` and `STORAGE_READ_WRITE` are only states+ // we can transit from due OpenGL memory barriers are used to make _subsequent_+ // operations see changes from the _shader_ side. We filter out usage changes that are+ // does not comes from the shader side in `transition_textures`+ C::TextureBarrier(usage) => {+ let mut flags = 0;+ if usage.contains(wgt::TextureUses::RESOURCE) {+ flags |= glow::TEXTURE_FETCH_BARRIER_BIT;+ }+ if usage.intersects(+ wgt::TextureUses::STORAGE_READ_ONLY+ | wgt::TextureUses::STORAGE_WRITE_ONLY+ | wgt::TextureUses::STORAGE_READ_WRITE,+ ) {+ flags |= glow::SHADER_IMAGE_ACCESS_BARRIER_BIT;+ }+ if usage.intersects(wgt::TextureUses::COPY_SRC) {+ flags |= glow::PIXEL_BUFFER_BARRIER_BIT;+ }+ if usage.contains(wgt::TextureUses::COPY_DST) {+ flags |= glow::TEXTURE_UPDATE_BARRIER_BIT;+ }+ if usage.intersects(+ wgt::TextureUses::COLOR_TARGET+ | wgt::TextureUses::DEPTH_STENCIL_READ+ | wgt::TextureUses::DEPTH_STENCIL_WRITE,+ ) {+ flags |= glow::FRAMEBUFFER_BARRIER_BIT;+ }+ unsafe { gl.memory_barrier(flags) };+ }+ C::SetViewport {+ ref rect,+ ref depth,+ } => {+ unsafe { gl.viewport(rect.x, rect.y, rect.w, rect.h) };+ unsafe { gl.depth_range_f32(depth.start, depth.end) };+ }+ C::SetScissor(ref rect) => {+ unsafe { gl.scissor(rect.x, rect.y, rect.w, rect.h) };+ unsafe { gl.enable(glow::SCISSOR_TEST) };+ }+ C::SetStencilFunc {+ face,+ function,+ reference,+ read_mask,+ } => {+ unsafe { gl.stencil_func_separate(face, function, reference as i32, read_mask) };+ }+ C::SetStencilOps {+ face,+ write_mask,+ ref ops,+ } => {+ unsafe { gl.stencil_mask_separate(face, write_mask) };+ unsafe { gl.stencil_op_separate(face, ops.fail, ops.depth_fail, ops.pass) };+ }+ C::SetVertexAttribute {+ buffer,+ ref buffer_desc,+ attribute_desc: ref vat,+ } => {+ unsafe { gl.bind_buffer(glow::ARRAY_BUFFER, buffer) };+ unsafe { gl.enable_vertex_attrib_array(vat.location) };++ if buffer.is_none() {+ match vat.format_desc.attrib_kind {+ super::VertexAttribKind::Float => unsafe {+ gl.vertex_attrib_format_f32(+ vat.location,+ vat.format_desc.element_count,+ vat.format_desc.element_format,+ true, // always normalized+ vat.offset,+ )+ },+ super::VertexAttribKind::Integer => unsafe {+ gl.vertex_attrib_format_i32(+ vat.location,+ vat.format_desc.element_count,+ vat.format_desc.element_format,+ vat.offset,+ )+ },+ }++ //Note: there is apparently a bug on AMD 3500U:+ // this call is ignored if the current array is disabled.+ unsafe { gl.vertex_attrib_binding(vat.location, vat.buffer_index) };+ } else {+ match vat.format_desc.attrib_kind {+ super::VertexAttribKind::Float => unsafe {+ gl.vertex_attrib_pointer_f32(+ vat.location,+ vat.format_desc.element_count,+ vat.format_desc.element_format,+ true, // always normalized+ buffer_desc.stride as i32,+ vat.offset as i32,+ )+ },+ super::VertexAttribKind::Integer => unsafe {+ gl.vertex_attrib_pointer_i32(+ vat.location,+ vat.format_desc.element_count,+ vat.format_desc.element_format,+ buffer_desc.stride as i32,+ vat.offset as i32,+ )+ },+ }+ unsafe { gl.vertex_attrib_divisor(vat.location, buffer_desc.step as u32) };+ }+ }+ C::UnsetVertexAttribute(location) => {+ unsafe { gl.disable_vertex_attrib_array(location) };+ }+ C::SetVertexBuffer {+ index,+ ref buffer,+ ref buffer_desc,+ } => {+ unsafe { gl.vertex_binding_divisor(index, buffer_desc.step as u32) };+ unsafe {+ gl.bind_vertex_buffer(+ index,+ Some(buffer.raw),+ buffer.offset as i32,+ buffer_desc.stride as i32,+ )+ };+ }+ C::SetDepth(ref depth) => {+ unsafe { gl.depth_func(depth.function) };+ unsafe { gl.depth_mask(depth.mask) };+ }+ C::SetDepthBias(bias) => {+ if bias.is_enabled() {+ unsafe { gl.enable(glow::POLYGON_OFFSET_FILL) };+ unsafe { gl.polygon_offset(bias.slope_scale, bias.constant as f32) };+ } else {+ unsafe { gl.disable(glow::POLYGON_OFFSET_FILL) };+ }+ }+ C::ConfigureDepthStencil(aspects) => {+ if aspects.contains(crate::FormatAspects::DEPTH) {+ unsafe { gl.enable(glow::DEPTH_TEST) };+ } else {+ unsafe { gl.disable(glow::DEPTH_TEST) };+ }+ if aspects.contains(crate::FormatAspects::STENCIL) {+ unsafe { gl.enable(glow::STENCIL_TEST) };+ } else {+ unsafe { gl.disable(glow::STENCIL_TEST) };+ }+ }+ C::SetAlphaToCoverage(enabled) => {+ if enabled {+ unsafe { gl.enable(glow::SAMPLE_ALPHA_TO_COVERAGE) };+ } else {+ unsafe { gl.disable(glow::SAMPLE_ALPHA_TO_COVERAGE) };+ }+ }+ C::SetProgram(program) => {+ unsafe { gl.use_program(Some(program)) };+ }+ C::SetPrimitive(ref state) => {+ unsafe { gl.front_face(state.front_face) };+ if state.cull_face != 0 {+ unsafe { gl.enable(glow::CULL_FACE) };+ unsafe { gl.cull_face(state.cull_face) };+ } else {+ unsafe { gl.disable(glow::CULL_FACE) };+ }+ if self.features.contains(wgt::Features::DEPTH_CLIP_CONTROL) {+ //Note: this is a bit tricky, since we are controlling the clip, not the clamp.+ if state.unclipped_depth {+ unsafe { gl.enable(glow::DEPTH_CLAMP) };+ } else {+ unsafe { gl.disable(glow::DEPTH_CLAMP) };+ }+ }+ // POLYGON_MODE_LINE also implies POLYGON_MODE_POINT+ if self.features.contains(wgt::Features::POLYGON_MODE_LINE) {+ unsafe { gl.polygon_mode(glow::FRONT_AND_BACK, state.polygon_mode) };+ }+ }+ C::SetBlendConstant(c) => {+ unsafe { gl.blend_color(c[0], c[1], c[2], c[3]) };+ }+ C::SetColorTarget {+ draw_buffer_index,+ desc: super::ColorTargetDesc { mask, ref blend },+ } => {+ use wgt::ColorWrites as Cw;+ if let Some(index) = draw_buffer_index {+ unsafe {+ gl.color_mask_draw_buffer(+ index,+ mask.contains(Cw::RED),+ mask.contains(Cw::GREEN),+ mask.contains(Cw::BLUE),+ mask.contains(Cw::ALPHA),+ )+ };+ if let Some(ref blend) = *blend {+ unsafe { gl.enable_draw_buffer(glow::BLEND, index) };+ if blend.color != blend.alpha {+ unsafe {+ gl.blend_equation_separate_draw_buffer(+ index,+ blend.color.equation,+ blend.alpha.equation,+ )+ };+ unsafe {+ gl.blend_func_separate_draw_buffer(+ index,+ blend.color.src,+ blend.color.dst,+ blend.alpha.src,+ blend.alpha.dst,+ )+ };+ } else {+ unsafe { gl.blend_equation_draw_buffer(index, blend.color.equation) };+ unsafe {+ gl.blend_func_draw_buffer(index, blend.color.src, blend.color.dst)+ };+ }+ } else {+ unsafe { gl.disable_draw_buffer(glow::BLEND, index) };+ }+ } else {+ unsafe {+ gl.color_mask(+ mask.contains(Cw::RED),+ mask.contains(Cw::GREEN),+ mask.contains(Cw::BLUE),+ mask.contains(Cw::ALPHA),+ )+ };+ if let Some(ref blend) = *blend {+ unsafe { gl.enable(glow::BLEND) };+ if blend.color != blend.alpha {+ unsafe {+ gl.blend_equation_separate(+ blend.color.equation,+ blend.alpha.equation,+ )+ };+ unsafe {+ gl.blend_func_separate(+ blend.color.src,+ blend.color.dst,+ blend.alpha.src,+ blend.alpha.dst,+ )+ };+ } else {+ unsafe { gl.blend_equation(blend.color.equation) };+ unsafe { gl.blend_func(blend.color.src, blend.color.dst) };+ }+ } else {+ unsafe { gl.disable(glow::BLEND) };+ }+ }+ }+ C::BindBuffer {+ target,+ slot,+ buffer,+ offset,+ size,+ } => {+ unsafe { gl.bind_buffer_range(target, slot, Some(buffer), offset, size) };+ }+ C::BindSampler(texture_index, sampler) => {+ unsafe { gl.bind_sampler(texture_index, sampler) };+ }+ C::BindTexture {+ slot,+ texture,+ target,+ aspects,+ ref mip_levels,+ } => {+ unsafe { gl.active_texture(glow::TEXTURE0 + slot) };+ unsafe { gl.bind_texture(target, Some(texture)) };++ unsafe {+ gl.tex_parameter_i32(target, glow::TEXTURE_BASE_LEVEL, mip_levels.start as i32)+ };+ unsafe {+ gl.tex_parameter_i32(+ target,+ glow::TEXTURE_MAX_LEVEL,+ (mip_levels.end - 1) as i32,+ )+ };++ let version = gl.version();+ let is_min_es_3_1 = version.is_embedded && (version.major, version.minor) >= (3, 1);+ let is_min_4_3 = !version.is_embedded && (version.major, version.minor) >= (4, 3);+ if is_min_es_3_1 || is_min_4_3 {+ let mode = match aspects {+ crate::FormatAspects::DEPTH => Some(glow::DEPTH_COMPONENT),+ crate::FormatAspects::STENCIL => Some(glow::STENCIL_INDEX),+ _ => None,+ };+ if let Some(mode) = mode {+ unsafe {+ gl.tex_parameter_i32(+ target,+ glow::DEPTH_STENCIL_TEXTURE_MODE,+ mode as _,+ )+ };+ }+ }+ }+ C::BindImage { slot, ref binding } => {+ unsafe {+ gl.bind_image_texture(+ slot,+ Some(binding.raw),+ binding.mip_level as i32,+ binding.array_layer.is_none(),+ binding.array_layer.unwrap_or_default() as i32,+ binding.access,+ binding.format,+ )+ };+ }+ C::InsertDebugMarker(ref range) => {+ let marker = extract_marker(data_bytes, range);+ unsafe {+ if self+ .shared+ .private_caps+ .contains(PrivateCapabilities::DEBUG_FNS)+ {+ gl.debug_message_insert(+ glow::DEBUG_SOURCE_APPLICATION,+ glow::DEBUG_TYPE_MARKER,+ DEBUG_ID,+ glow::DEBUG_SEVERITY_NOTIFICATION,+ to_debug_str(marker),+ )+ }+ };+ }+ C::PushDebugGroup(ref range) => {+ let marker = extract_marker(data_bytes, range);+ unsafe {+ if self+ .shared+ .private_caps+ .contains(PrivateCapabilities::DEBUG_FNS)+ {+ gl.push_debug_group(+ glow::DEBUG_SOURCE_APPLICATION,+ DEBUG_ID,+ to_debug_str(marker),+ )+ }+ };+ }+ C::PopDebugGroup => {+ unsafe {+ if self+ .shared+ .private_caps+ .contains(PrivateCapabilities::DEBUG_FNS)+ {+ gl.pop_debug_group()+ }+ };+ }+ C::SetImmediates {+ ref uniform,+ offset,+ } => {+ fn get_data<T, const COUNT: usize>(data: &[u8], offset: u32) -> [T; COUNT]+ where+ [T; COUNT]: bytemuck::AnyBitPattern,+ {+ let data_required = size_of::<T>() * COUNT;+ let raw = &data[(offset as usize)..][..data_required];+ bytemuck::pod_read_unaligned(raw)+ }++ let location = Some(&uniform.location);+ use nt::glsl::{GlslScalar, GlslUniformType, GlslVectorSize};+ match uniform.ty {+ //+ // --- Float 1-4 Component ---+ //+ GlslUniformType::Scalar(GlslScalar::F32) => {+ let data = get_data::<f32, 1>(data_bytes, offset)[0];+ unsafe { gl.uniform_1_f32(location, data) };+ }+ GlslUniformType::Vector {+ size: GlslVectorSize::Bi,+ scalar: GlslScalar::F32,+ } => {+ let data = &get_data::<f32, 2>(data_bytes, offset);+ unsafe { gl.uniform_2_f32_slice(location, data) };+ }+ GlslUniformType::Vector {+ size: GlslVectorSize::Tri,+ scalar: GlslScalar::F32,+ } => {+ let data = &get_data::<f32, 3>(data_bytes, offset);+ unsafe { gl.uniform_3_f32_slice(location, data) };+ }+ GlslUniformType::Vector {+ size: GlslVectorSize::Quad,+ scalar: GlslScalar::F32,+ } => {+ let data = &get_data::<f32, 4>(data_bytes, offset);+ unsafe { gl.uniform_4_f32_slice(location, data) };+ }++ //+ // --- Int 1-4 Component ---+ //+ GlslUniformType::Scalar(GlslScalar::I32) => {+ let data = get_data::<i32, 1>(data_bytes, offset)[0];+ unsafe { gl.uniform_1_i32(location, data) };+ }+ GlslUniformType::Vector {+ size: GlslVectorSize::Bi,+ scalar: GlslScalar::I32,+ } => {+ let data = &get_data::<i32, 2>(data_bytes, offset);+ unsafe { gl.uniform_2_i32_slice(location, data) };+ }+ GlslUniformType::Vector {+ size: GlslVectorSize::Tri,+ scalar: GlslScalar::I32,+ } => {+ let data = &get_data::<i32, 3>(data_bytes, offset);+ unsafe { gl.uniform_3_i32_slice(location, data) };+ }+ GlslUniformType::Vector {+ size: GlslVectorSize::Quad,+ scalar: GlslScalar::I32,+ } => {+ let data = &get_data::<i32, 4>(data_bytes, offset);+ unsafe { gl.uniform_4_i32_slice(location, data) };+ }++ //+ // --- Uint 1-4 Component ---+ //+ GlslUniformType::Scalar(GlslScalar::U32) => {+ let data = get_data::<u32, 1>(data_bytes, offset)[0];+ unsafe { gl.uniform_1_u32(location, data) };+ }+ GlslUniformType::Vector {+ size: GlslVectorSize::Bi,+ scalar: GlslScalar::U32,+ } => {+ let data = &get_data::<u32, 2>(data_bytes, offset);+ unsafe { gl.uniform_2_u32_slice(location, data) };+ }+ GlslUniformType::Vector {+ size: GlslVectorSize::Tri,+ scalar: GlslScalar::U32,+ } => {+ let data = &get_data::<u32, 3>(data_bytes, offset);+ unsafe { gl.uniform_3_u32_slice(location, data) };+ }+ GlslUniformType::Vector {+ size: GlslVectorSize::Quad,+ scalar: GlslScalar::U32,+ } => {+ let data = &get_data::<u32, 4>(data_bytes, offset);+ unsafe { gl.uniform_4_u32_slice(location, data) };+ }++ //+ // --- Matrix 2xR ---+ //+ GlslUniformType::Matrix {+ columns: GlslVectorSize::Bi,+ rows: GlslVectorSize::Bi,+ scalar: GlslScalar::F32,+ } => {+ let data = &get_data::<f32, 4>(data_bytes, offset);+ unsafe { gl.uniform_matrix_2_f32_slice(location, false, data) };+ }+ GlslUniformType::Matrix {+ columns: GlslVectorSize::Bi,+ rows: GlslVectorSize::Tri,+ scalar: GlslScalar::F32,+ } => {+ // repack 2 vec3s into 6 values.+ let unpacked_data = &get_data::<f32, 8>(data_bytes, offset);+ #[rustfmt::skip]+ let packed_data = [+ unpacked_data[0], unpacked_data[1], unpacked_data[2],+ unpacked_data[4], unpacked_data[5], unpacked_data[6],+ ];+ unsafe { gl.uniform_matrix_2x3_f32_slice(location, false, &packed_data) };+ }+ GlslUniformType::Matrix {+ columns: GlslVectorSize::Bi,+ rows: GlslVectorSize::Quad,+ scalar: GlslScalar::F32,+ } => {+ let data = &get_data::<f32, 8>(data_bytes, offset);+ unsafe { gl.uniform_matrix_2x4_f32_slice(location, false, data) };+ }++ //+ // --- Matrix 3xR ---+ //+ GlslUniformType::Matrix {+ columns: GlslVectorSize::Tri,+ rows: GlslVectorSize::Bi,+ scalar: GlslScalar::F32,+ } => {+ let data = &get_data::<f32, 6>(data_bytes, offset);+ unsafe { gl.uniform_matrix_3x2_f32_slice(location, false, data) };+ }+ GlslUniformType::Matrix {+ columns: GlslVectorSize::Tri,+ rows: GlslVectorSize::Tri,+ scalar: GlslScalar::F32,+ } => {+ // repack 3 vec3s into 9 values.+ let unpacked_data = &get_data::<f32, 12>(data_bytes, offset);+ #[rustfmt::skip]+ let packed_data = [+ unpacked_data[0], unpacked_data[1], unpacked_data[2],+ unpacked_data[4], unpacked_data[5], unpacked_data[6],+ unpacked_data[8], unpacked_data[9], unpacked_data[10],+ ];+ unsafe { gl.uniform_matrix_3_f32_slice(location, false, &packed_data) };+ }+ GlslUniformType::Matrix {+ columns: GlslVectorSize::Tri,+ rows: GlslVectorSize::Quad,+ scalar: GlslScalar::F32,+ } => {+ let data = &get_data::<f32, 12>(data_bytes, offset);+ unsafe { gl.uniform_matrix_3x4_f32_slice(location, false, data) };+ }++ //+ // --- Matrix 4xR ---+ //+ GlslUniformType::Matrix {+ columns: GlslVectorSize::Quad,+ rows: GlslVectorSize::Bi,+ scalar: GlslScalar::F32,+ } => {+ let data = &get_data::<f32, 8>(data_bytes, offset);+ unsafe { gl.uniform_matrix_4x2_f32_slice(location, false, data) };+ }+ GlslUniformType::Matrix {+ columns: GlslVectorSize::Quad,+ rows: GlslVectorSize::Tri,+ scalar: GlslScalar::F32,+ } => {+ // repack 4 vec3s into 12 values.+ let unpacked_data = &get_data::<f32, 16>(data_bytes, offset);+ #[rustfmt::skip]+ let packed_data = [+ unpacked_data[0], unpacked_data[1], unpacked_data[2],+ unpacked_data[4], unpacked_data[5], unpacked_data[6],+ unpacked_data[8], unpacked_data[9], unpacked_data[10],+ unpacked_data[12], unpacked_data[13], unpacked_data[14],+ ];+ unsafe { gl.uniform_matrix_4x3_f32_slice(location, false, &packed_data) };+ }+ GlslUniformType::Matrix {+ columns: GlslVectorSize::Quad,+ rows: GlslVectorSize::Quad,+ scalar: GlslScalar::F32,+ } => {+ let data = &get_data::<f32, 16>(data_bytes, offset);+ unsafe { gl.uniform_matrix_4_f32_slice(location, false, data) };+ }+ _ => panic!("Unsupported uniform datatype: {:?}!", uniform.ty),+ }+ }+ C::SetClipDistances {+ old_count,+ new_count,+ } => {+ // Disable clip planes that are no longer active+ for i in new_count..old_count {+ unsafe { gl.disable(glow::CLIP_DISTANCE0 + i) };+ }++ // Enable clip planes that are now active+ for i in old_count..new_count {+ unsafe { gl.enable(glow::CLIP_DISTANCE0 + i) };+ }+ }+ }+ }+}++impl crate::Queue for super::Queue {+ type A = super::Api;++ unsafe fn submit(+ &self,+ command_buffers: &[&super::CommandBuffer],+ _surface_textures: &[&super::Texture],+ (signal_fence, signal_value): (&super::Fence, crate::FenceValue),+ ) -> Result<(), crate::DeviceError> {+ let shared = Arc::clone(&self.shared);+ let gl = &shared.context.lock();+ for cmd_buf in command_buffers.iter() {+ // The command encoder assumes a default state when encoding the command buffer.+ // Always reset the state between command_buffers to reflect this assumption. Do+ // this at the beginning of the loop in case something outside of wgpu modified+ // this state prior to commit.+ unsafe { self.reset_state(gl) };+ if let Some(ref label) = cmd_buf.label {+ if self+ .shared+ .private_caps+ .contains(PrivateCapabilities::DEBUG_FNS)+ {+ unsafe {+ gl.push_debug_group(+ glow::DEBUG_SOURCE_APPLICATION,+ DEBUG_ID,+ to_debug_str(label),+ )+ };+ }+ }++ for command in cmd_buf.commands.iter() {+ unsafe { self.process(gl, command, &cmd_buf.data_bytes, &cmd_buf.queries) };+ }++ if cmd_buf.label.is_some()+ && self+ .shared+ .private_caps+ .contains(PrivateCapabilities::DEBUG_FNS)+ {+ unsafe { gl.pop_debug_group() };+ }+ }++ signal_fence.maintain(gl);+ signal_fence.signal(gl, signal_value)?;++ // This is extremely important. If we don't flush, the above fences may never+ // be signaled, particularly in headless contexts. Headed contexts will+ // often flush every so often, but headless contexts may not.+ unsafe { gl.flush() };++ Ok(())+ }++ unsafe fn present(+ &self,+ surface: &super::Surface,+ texture: super::Texture,+ ) -> Result<(), crate::SurfaceError> {+ unsafe { surface.present(texture, &self.shared.context) }+ }++ unsafe fn get_timestamp_period(&self) -> f32 {+ 1.0+ }++ unsafe fn wait_for_idle(&self) -> Result<(), crate::DeviceError> {+ let gl = &self.shared.context.lock();+ unsafe { gl.finish() };+ Ok(())+ }+}++#[cfg(send_sync)]+unsafe impl Sync for super::Queue {}+#[cfg(send_sync)]+unsafe impl Send for super::Queue {}diff --git a/vendor/wgpu-hal/src/gles/shaders/clear.frag b/vendor/wgpu-hal/src/gles/shaders/clear.fragnew file mode 100644index 00000000..1d0e414b--- /dev/null+++ b/vendor/wgpu-hal/src/gles/shaders/clear.frag@@ -0,0 +1,7 @@+uniform vec4 color;+//Hack: Some WebGL implementations don't find "color" otherwise.+uniform vec4 color_workaround;+out vec4 frag;+void main() {+ frag = color + color_workaround;+}diff --git a/vendor/wgpu-hal/src/gles/shaders/clear.vert b/vendor/wgpu-hal/src/gles/shaders/clear.vertnew file mode 100644index 00000000..341b4e5f--- /dev/null+++ b/vendor/wgpu-hal/src/gles/shaders/clear.vert@@ -0,0 +1,9 @@+// A triangle that fills the whole screen+vec2[3] TRIANGLE_POS = vec2[](+ vec2( 0.0, -3.0),+ vec2(-3.0, 1.0),+ vec2( 3.0, 1.0)+);+void main() {+ gl_Position = vec4(TRIANGLE_POS[gl_VertexID], 0.0, 1.0);+}\ No newline at end of filediff --git a/vendor/wgpu-hal/src/gles/shaders/srgb_present.frag b/vendor/wgpu-hal/src/gles/shaders/srgb_present.fragnew file mode 100644index 00000000..853f82a6--- /dev/null+++ b/vendor/wgpu-hal/src/gles/shaders/srgb_present.frag@@ -0,0 +1,16 @@+#version 300 es+precision mediump float;+in vec2 uv;+uniform sampler2D present_texture;+out vec4 frag;+vec4 linear_to_srgb(vec4 linear) {+ vec3 color_linear = linear.rgb;+ vec3 selector = ceil(color_linear - 0.0031308); // 0 if under value, 1 if over+ vec3 under = 12.92 * color_linear;+ vec3 over = 1.055 * pow(color_linear, vec3(0.41666)) - 0.055;+ vec3 result = mix(under, over, selector);+ return vec4(result, linear.a);+}+void main() {+ frag = linear_to_srgb(texture(present_texture, uv));+}\ No newline at end of filediff --git a/vendor/wgpu-hal/src/gles/shaders/srgb_present.vert b/vendor/wgpu-hal/src/gles/shaders/srgb_present.vertnew file mode 100644index 00000000..922f2a18--- /dev/null+++ b/vendor/wgpu-hal/src/gles/shaders/srgb_present.vert@@ -0,0 +1,18 @@+#version 300 es+precision mediump float;+// A triangle that fills the whole screen+const vec2[3] TRIANGLE_POS = vec2[](+ vec2( 0.0, -3.0),+ vec2(-3.0, 1.0),+ vec2( 3.0, 1.0)+);+const vec2[3] TRIANGLE_UV = vec2[](+ vec2( 0.5, 1.),+ vec2( -1.0, -1.0),+ vec2( 2.0, -1.0)+);+out vec2 uv;+void main() {+ uv = TRIANGLE_UV[gl_VertexID];+ gl_Position = vec4(TRIANGLE_POS[gl_VertexID], 0.0, 1.0);+}\ No newline at end of filediff --git a/vendor/wgpu-hal/src/gles/web.rs b/vendor/wgpu-hal/src/gles/web.rsnew file mode 100644index 00000000..326b4f35--- /dev/null+++ b/vendor/wgpu-hal/src/gles/web.rs@@ -0,0 +1,490 @@+use alloc::{format, string::String, vec::Vec};++use glow::HasContext;+use parking_lot::{Mutex, RwLock};+use wasm_bindgen::{JsCast, JsValue};++use super::TextureFormatDesc;++/// A wrapper around a [`glow::Context`] to provide a fake `lock()` api that makes it compatible+/// with the `AdapterContext` API from the EGL implementation.+#[derive(Debug)]+pub struct AdapterContext {+ pub glow_context: glow::Context,+ pub webgl2_context: web_sys::WebGl2RenderingContext,+}++impl AdapterContext {+ pub fn is_owned(&self) -> bool {+ false+ }++ /// Obtain a lock to the EGL context and get handle to the [`glow::Context`] that can be used to+ /// do rendering.+ #[track_caller]+ pub fn lock(&self) -> &glow::Context {+ &self.glow_context+ }+}++#[derive(Debug)]+pub struct Instance {+ options: wgt::GlBackendOptions,+}++impl Instance {+ pub fn create_surface_from_canvas(+ &self,+ canvas: web_sys::HtmlCanvasElement,+ ) -> Result<Surface, crate::InstanceError> {+ let result =+ canvas.get_context_with_context_options("webgl2", &Self::create_context_options());+ self.create_surface_from_context(Canvas::Canvas(canvas), result)+ }++ pub fn create_surface_from_offscreen_canvas(+ &self,+ canvas: web_sys::OffscreenCanvas,+ ) -> Result<Surface, crate::InstanceError> {+ let result =+ canvas.get_context_with_context_options("webgl2", &Self::create_context_options());+ self.create_surface_from_context(Canvas::Offscreen(canvas), result)+ }++ /// Common portion of public `create_surface_from_*` functions.+ ///+ /// Note: Analogous code also exists in the WebGPU backend at+ /// `wgpu::backend::web::Context`.+ fn create_surface_from_context(+ &self,+ canvas: Canvas,+ context_result: Result<Option<js_sys::Object>, JsValue>,+ ) -> Result<Surface, crate::InstanceError> {+ let context_object: js_sys::Object = match context_result {+ Ok(Some(context)) => context,+ Ok(None) => {+ // <https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-getcontext-dev>+ // A getContext() call “returns null if contextId is not supported, or if the+ // canvas has already been initialized with another context type”. Additionally,+ // “not supported” could include “insufficient GPU resources” or “the GPU process+ // previously crashed”. So, we must return it as an `Err` since it could occur+ // for circumstances outside the application author's control.+ return Err(crate::InstanceError::new(String::from(concat!(+ "canvas.getContext() returned null; ",+ "webgl2 not available or canvas already in use"+ ))));+ }+ Err(js_error) => {+ // <https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-getcontext>+ // A thrown exception indicates misuse of the canvas state.+ return Err(crate::InstanceError::new(format!(+ "canvas.getContext() threw exception {js_error:?}",+ )));+ }+ };++ // Not returning this error because it is a type error that shouldn't happen unless+ // the browser, JS builtin objects, or wasm bindings are misbehaving somehow.+ let webgl2_context: web_sys::WebGl2RenderingContext = context_object+ .dyn_into()+ .expect("canvas context is not a WebGl2RenderingContext");++ Ok(Surface {+ canvas,+ webgl2_context,+ srgb_present_program: Mutex::new(None),+ swapchain: RwLock::new(None),+ texture: Mutex::new(None),+ presentable: true,+ })+ }++ fn create_context_options() -> js_sys::Object {+ let context_options = js_sys::Object::new();+ js_sys::Reflect::set(&context_options, &"antialias".into(), &JsValue::FALSE)+ .expect("Cannot create context options");+ context_options+ }+}++#[cfg(send_sync)]+static_assertions::assert_impl_all!(Instance: Send, Sync);++impl crate::Instance for Instance {+ type A = super::Api;++ unsafe fn init(desc: &crate::InstanceDescriptor<'_>) -> Result<Self, crate::InstanceError> {+ profiling::scope!("Init OpenGL (WebGL) Backend");+ Ok(Instance {+ options: desc.backend_options.gl.clone(),+ })+ }++ unsafe fn enumerate_adapters(+ &self,+ surface_hint: Option<&Surface>,+ ) -> Vec<crate::ExposedAdapter<super::Api>> {+ if let Some(surface_hint) = surface_hint {+ let gl = glow::Context::from_webgl2_context(surface_hint.webgl2_context.clone());++ unsafe {+ super::Adapter::expose(+ AdapterContext {+ glow_context: gl,+ webgl2_context: surface_hint.webgl2_context.clone(),+ },+ self.options.clone(),+ )+ }+ .into_iter()+ .collect()+ } else {+ Vec::new()+ }+ }++ unsafe fn create_surface(+ &self,+ _display_handle: raw_window_handle::RawDisplayHandle,+ window_handle: raw_window_handle::RawWindowHandle,+ ) -> Result<Surface, crate::InstanceError> {+ let canvas: web_sys::HtmlCanvasElement = match window_handle {+ raw_window_handle::RawWindowHandle::Web(handle) => web_sys::window()+ .and_then(|win| win.document())+ .expect("Cannot get document")+ .query_selector(&format!("canvas[data-raw-handle=\"{}\"]", handle.id))+ .expect("Cannot query for canvas")+ .expect("Canvas is not found")+ .dyn_into()+ .expect("Failed to downcast to canvas type"),+ raw_window_handle::RawWindowHandle::WebCanvas(handle) => {+ let value: &JsValue = unsafe { handle.obj.cast().as_ref() };+ value.clone().unchecked_into()+ }+ raw_window_handle::RawWindowHandle::WebOffscreenCanvas(handle) => {+ let value: &JsValue = unsafe { handle.obj.cast().as_ref() };+ let canvas: web_sys::OffscreenCanvas = value.clone().unchecked_into();++ return self.create_surface_from_offscreen_canvas(canvas);+ }+ _ => {+ return Err(crate::InstanceError::new(format!(+ "window handle {window_handle:?} is not a web handle"+ )))+ }+ };++ self.create_surface_from_canvas(canvas)+ }+}++impl super::Adapter {+ /// Creates a new external adapter from an existing WebGL2 rendering context.+ ///+ /// # Safety+ ///+ /// - The underlying WebGL2 context must be valid (not lost).+ /// - The underlying WebGL2 context must be valid when interfacing with any objects returned by+ /// wgpu-hal from this adapter.+ /// - The underlying WebGL2 context must be valid when dropping this adapter and when+ /// dropping any objects returned from this adapter.+ pub unsafe fn new_external(+ webgl2_context: web_sys::WebGl2RenderingContext,+ options: wgt::GlBackendOptions,+ ) -> Option<crate::ExposedAdapter<super::Api>> {+ let glow_context = glow::Context::from_webgl2_context(webgl2_context.clone());+ unsafe {+ Self::expose(+ AdapterContext {+ glow_context,+ webgl2_context,+ },+ options,+ )+ }+ }++ pub fn adapter_context(&self) -> &AdapterContext {+ &self.shared.context+ }+}++impl super::Device {+ /// Returns the underlying WebGL2 `AdapterContext`.+ pub fn context(&self) -> &AdapterContext {+ &self.shared.context+ }+}++#[derive(Debug)]+pub struct Surface {+ canvas: Canvas,+ pub(super) webgl2_context: web_sys::WebGl2RenderingContext,+ pub(super) swapchain: RwLock<Option<Swapchain>>,+ texture: Mutex<Option<glow::Texture>>,+ pub(super) presentable: bool,+ srgb_present_program: Mutex<Option<glow::Program>>,+}++impl Clone for Surface {+ fn clone(&self) -> Self {+ Self {+ canvas: self.canvas.clone(),+ webgl2_context: self.webgl2_context.clone(),+ swapchain: RwLock::new(self.swapchain.read().clone()),+ texture: Mutex::new(*self.texture.lock()),+ presentable: self.presentable,+ srgb_present_program: Mutex::new(*self.srgb_present_program.lock()),+ }+ }+}++#[cfg(send_sync)]+unsafe impl Sync for Surface {}+#[cfg(send_sync)]+unsafe impl Send for Surface {}++#[derive(Clone, Debug)]+enum Canvas {+ Canvas(web_sys::HtmlCanvasElement),+ Offscreen(web_sys::OffscreenCanvas),+}++#[derive(Clone, Debug)]+pub struct Swapchain {+ pub(crate) extent: wgt::Extent3d,+ // pub(crate) channel: f::ChannelType,+ pub(super) format: wgt::TextureFormat,+ pub(super) framebuffer: glow::Framebuffer,+ pub(super) format_desc: TextureFormatDesc,+}++impl Surface {+ pub(super) unsafe fn present(+ &self,+ _suf_texture: super::Texture,+ context: &AdapterContext,+ ) -> Result<(), crate::SurfaceError> {+ let gl = &context.glow_context;+ let swapchain = self.swapchain.read();+ let swapchain = swapchain.as_ref().ok_or(crate::SurfaceError::Other(+ "need to configure surface before presenting",+ ))?;++ if swapchain.format.is_srgb() {+ // Important to set the viewport since we don't know in what state the user left it.+ unsafe {+ gl.viewport(+ 0,+ 0,+ swapchain.extent.width as _,+ swapchain.extent.height as _,+ )+ };+ unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, None) };+ unsafe { gl.bind_sampler(0, None) };+ unsafe { gl.active_texture(glow::TEXTURE0) };+ unsafe { gl.bind_texture(glow::TEXTURE_2D, *self.texture.lock()) };+ unsafe { gl.use_program(*self.srgb_present_program.lock()) };+ unsafe { gl.disable(glow::DEPTH_TEST) };+ unsafe { gl.disable(glow::STENCIL_TEST) };+ unsafe { gl.disable(glow::SCISSOR_TEST) };+ unsafe { gl.disable(glow::BLEND) };+ unsafe { gl.disable(glow::CULL_FACE) };+ unsafe { gl.draw_buffers(&[glow::BACK]) };+ unsafe { gl.draw_arrays(glow::TRIANGLES, 0, 3) };+ } else {+ unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(swapchain.framebuffer)) };+ unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, None) };+ // Note the Y-flipping here. GL's presentation is not flipped,+ // but main rendering is. Therefore, we Y-flip the output positions+ // in the shader, and also this blit.+ unsafe {+ gl.blit_framebuffer(+ 0,+ swapchain.extent.height as i32,+ swapchain.extent.width as i32,+ 0,+ 0,+ 0,+ swapchain.extent.width as i32,+ swapchain.extent.height as i32,+ glow::COLOR_BUFFER_BIT,+ glow::NEAREST,+ )+ };+ }++ Ok(())+ }++ unsafe fn create_srgb_present_program(gl: &glow::Context) -> glow::Program {+ let program = unsafe { gl.create_program() }.expect("Could not create shader program");+ let vertex =+ unsafe { gl.create_shader(glow::VERTEX_SHADER) }.expect("Could not create shader");+ unsafe { gl.shader_source(vertex, include_str!("./shaders/srgb_present.vert")) };+ unsafe { gl.compile_shader(vertex) };+ let fragment =+ unsafe { gl.create_shader(glow::FRAGMENT_SHADER) }.expect("Could not create shader");+ unsafe { gl.shader_source(fragment, include_str!("./shaders/srgb_present.frag")) };+ unsafe { gl.compile_shader(fragment) };+ unsafe { gl.attach_shader(program, vertex) };+ unsafe { gl.attach_shader(program, fragment) };+ unsafe { gl.link_program(program) };+ unsafe { gl.delete_shader(vertex) };+ unsafe { gl.delete_shader(fragment) };+ unsafe { gl.bind_texture(glow::TEXTURE_2D, None) };++ program+ }++ pub fn supports_srgb(&self) -> bool {+ // present.frag takes care of handling srgb conversion+ true+ }+}++impl crate::Surface for Surface {+ type A = super::Api;++ unsafe fn configure(+ &self,+ device: &super::Device,+ config: &crate::SurfaceConfiguration,+ ) -> Result<(), crate::SurfaceError> {+ match self.canvas {+ Canvas::Canvas(ref canvas) => {+ canvas.set_width(config.extent.width);+ canvas.set_height(config.extent.height);+ }+ Canvas::Offscreen(ref canvas) => {+ canvas.set_width(config.extent.width);+ canvas.set_height(config.extent.height);+ }+ }++ let gl = &device.shared.context.lock();++ {+ let mut swapchain = self.swapchain.write();+ if let Some(swapchain) = swapchain.take() {+ // delete all frame buffers already allocated+ unsafe { gl.delete_framebuffer(swapchain.framebuffer) };+ }+ }+ {+ let mut srgb_present_program = self.srgb_present_program.lock();+ if srgb_present_program.is_none() && config.format.is_srgb() {+ *srgb_present_program = Some(unsafe { Self::create_srgb_present_program(gl) });+ }+ }+ {+ let mut texture = self.texture.lock();+ if let Some(texture) = texture.take() {+ unsafe { gl.delete_texture(texture) };+ }++ *texture = Some(unsafe { gl.create_texture() }.map_err(|error| {+ log::error!("Internal swapchain texture creation failed: {error}");+ crate::DeviceError::OutOfMemory+ })?);++ let desc = device.shared.describe_texture_format(config.format);+ unsafe { gl.bind_texture(glow::TEXTURE_2D, *texture) };+ unsafe {+ gl.tex_parameter_i32(+ glow::TEXTURE_2D,+ glow::TEXTURE_MIN_FILTER,+ glow::NEAREST as _,+ )+ };+ unsafe {+ gl.tex_parameter_i32(+ glow::TEXTURE_2D,+ glow::TEXTURE_MAG_FILTER,+ glow::NEAREST as _,+ )+ };+ unsafe {+ gl.tex_storage_2d(+ glow::TEXTURE_2D,+ 1,+ desc.internal,+ config.extent.width as i32,+ config.extent.height as i32,+ )+ };++ let framebuffer = unsafe { gl.create_framebuffer() }.map_err(|error| {+ log::error!("Internal swapchain framebuffer creation failed: {error}");+ crate::DeviceError::OutOfMemory+ })?;+ unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(framebuffer)) };+ unsafe {+ gl.framebuffer_texture_2d(+ glow::READ_FRAMEBUFFER,+ glow::COLOR_ATTACHMENT0,+ glow::TEXTURE_2D,+ *texture,+ 0,+ )+ };+ unsafe { gl.bind_texture(glow::TEXTURE_2D, None) };++ let mut swapchain = self.swapchain.write();+ *swapchain = Some(Swapchain {+ extent: config.extent,+ // channel: config.format.base_format().1,+ format: config.format,+ format_desc: desc,+ framebuffer,+ });+ }++ Ok(())+ }++ unsafe fn unconfigure(&self, device: &super::Device) {+ let gl = device.shared.context.lock();+ {+ let mut swapchain = self.swapchain.write();+ if let Some(swapchain) = swapchain.take() {+ unsafe { gl.delete_framebuffer(swapchain.framebuffer) };+ }+ }+ if let Some(renderbuffer) = self.texture.lock().take() {+ unsafe { gl.delete_texture(renderbuffer) };+ }+ }++ unsafe fn acquire_texture(+ &self,+ _timeout_ms: Option<core::time::Duration>, //TODO+ _fence: &super::Fence,+ ) -> Result<crate::AcquiredSurfaceTexture<super::Api>, crate::SurfaceError> {+ let swapchain = self.swapchain.read();+ let sc = swapchain.as_ref().unwrap();+ let texture = super::Texture {+ inner: super::TextureInner::Texture {+ raw: self.texture.lock().unwrap(),+ target: glow::TEXTURE_2D,+ },+ drop_guard: None,+ array_layer_count: 1,+ mip_level_count: 1,+ format: sc.format,+ format_desc: sc.format_desc.clone(),+ copy_size: crate::CopyExtent {+ width: sc.extent.width,+ height: sc.extent.height,+ depth: 1,+ },+ };+ Ok(crate::AcquiredSurfaceTexture {+ texture,+ suboptimal: false,+ })+ }++ unsafe fn discard_texture(&self, _texture: super::Texture) {}+}diff --git a/vendor/wgpu-hal/src/gles/wgl.rs b/vendor/wgpu-hal/src/gles/wgl.rsnew file mode 100644index 00000000..09ee3e95--- /dev/null+++ b/vendor/wgpu-hal/src/gles/wgl.rs@@ -0,0 +1,905 @@+#![expect(+ missing_debug_implementations,+ reason = "TODO: someone developing on Windows add Debug impls where possible"+)]++use alloc::{borrow::ToOwned as _, ffi::CString, string::String, sync::Arc, vec::Vec};+use core::{+ ffi::{c_int, c_void, CStr},+ mem::{self, ManuallyDrop},+ ptr,+ time::Duration,+};+use std::{+ sync::{+ mpsc::{sync_channel, SyncSender},+ LazyLock,+ },+ thread,+};++use glow::HasContext;+use glutin_wgl_sys::wgl_extra::{+ Wgl, CONTEXT_CORE_PROFILE_BIT_ARB, CONTEXT_DEBUG_BIT_ARB, CONTEXT_FLAGS_ARB,+ CONTEXT_PROFILE_MASK_ARB,+};+use hashbrown::HashSet;+use parking_lot::{Mutex, MutexGuard, RwLock};+use raw_window_handle::{RawDisplayHandle, RawWindowHandle};+use wgt::InstanceFlags;+use windows::{+ core::{Error, PCSTR},+ Win32::{+ Foundation,+ Graphics::{Gdi, OpenGL},+ System::LibraryLoader,+ UI::WindowsAndMessaging,+ },+};++/// The amount of time to wait while trying to obtain a lock to the adapter context+const CONTEXT_LOCK_TIMEOUT_SECS: u64 = 1;++/// A wrapper around a `[`glow::Context`]` and the required WGL context that uses locking to+/// guarantee exclusive access when shared with multiple threads.+pub struct AdapterContext {+ inner: Arc<Mutex<Inner>>,+}++#[cfg(send_sync)]+static_assertions::assert_impl_all!(AdapterContext: Send, Sync);++impl AdapterContext {+ pub fn is_owned(&self) -> bool {+ true+ }++ pub fn raw_context(&self) -> *mut c_void {+ match self.inner.lock().context {+ Some(ref wgl) => wgl.context.0,+ None => ptr::null_mut(),+ }+ }++ /// Obtain a lock to the WGL context and get handle to the [`glow::Context`] that can be used to+ /// do rendering.+ #[track_caller]+ pub fn lock(&self) -> AdapterContextLock<'_> {+ let inner = self+ .inner+ // Don't lock forever. If it takes longer than 1 second to get the lock we've got a+ // deadlock and should panic to show where we got stuck+ .try_lock_for(Duration::from_secs(CONTEXT_LOCK_TIMEOUT_SECS))+ .expect("Could not lock adapter context. This is most-likely a deadlock.");++ if let Some(wgl) = &inner.context {+ wgl.make_current(inner.device.dc).unwrap()+ };++ AdapterContextLock { inner }+ }++ /// Obtain a lock to the WGL context and get handle to the [`glow::Context`] that can be used to+ /// do rendering.+ ///+ /// Unlike [`lock`](Self::lock), this accepts a device to pass to `make_current` and exposes the error+ /// when `make_current` fails.+ #[track_caller]+ fn lock_with_dc(&self, device: Gdi::HDC) -> windows::core::Result<AdapterContextLock<'_>> {+ let inner = self+ .inner+ .try_lock_for(Duration::from_secs(CONTEXT_LOCK_TIMEOUT_SECS))+ .expect("Could not lock adapter context. This is most-likely a deadlock.");++ if let Some(wgl) = &inner.context {+ wgl.make_current(device)?;+ }++ Ok(AdapterContextLock { inner })+ }+}++/// A guard containing a lock to an [`AdapterContext`], while the GL context is kept current.+pub struct AdapterContextLock<'a> {+ inner: MutexGuard<'a, Inner>,+}++impl<'a> core::ops::Deref for AdapterContextLock<'a> {+ type Target = glow::Context;++ fn deref(&self) -> &Self::Target {+ &self.inner.gl+ }+}++impl<'a> Drop for AdapterContextLock<'a> {+ fn drop(&mut self) {+ if let Some(wgl) = &self.inner.context {+ wgl.unmake_current().unwrap()+ }+ }+}++struct WglContext {+ context: OpenGL::HGLRC,+}++impl WglContext {+ fn make_current(&self, device: Gdi::HDC) -> windows::core::Result<()> {+ unsafe { OpenGL::wglMakeCurrent(device, self.context) }+ }++ fn unmake_current(&self) -> windows::core::Result<()> {+ if unsafe { OpenGL::wglGetCurrentContext() }.is_invalid() {+ return Ok(());+ }+ unsafe { OpenGL::wglMakeCurrent(Default::default(), Default::default()) }+ }+}++impl Drop for WglContext {+ fn drop(&mut self) {+ if let Err(e) = unsafe { OpenGL::wglDeleteContext(self.context) } {+ log::error!("failed to delete WGL context: {e}");+ }+ }+}++unsafe impl Send for WglContext {}+unsafe impl Sync for WglContext {}++struct Inner {+ gl: ManuallyDrop<glow::Context>,+ device: InstanceDevice,+ context: Option<WglContext>,+}++impl Drop for Inner {+ fn drop(&mut self) {+ struct CurrentGuard<'a>(&'a WglContext);+ impl Drop for CurrentGuard<'_> {+ fn drop(&mut self) {+ self.0.unmake_current().unwrap();+ }+ }++ // Context must be current when dropped. See safety docs on+ // `glow::HasContext`.+ //+ // NOTE: This is only set to `None` by `Adapter::new_external` which+ // requires the context to be current when anything that may be holding+ // the `Arc<AdapterShared>` is dropped.+ let _guard = self.context.as_ref().map(|wgl| {+ wgl.make_current(self.device.dc).unwrap();+ CurrentGuard(wgl)+ });+ // SAFETY: Field not used after this.+ unsafe { ManuallyDrop::drop(&mut self.gl) };+ }+}++unsafe impl Send for Inner {}+unsafe impl Sync for Inner {}++pub struct Instance {+ srgb_capable: bool,+ options: wgt::GlBackendOptions,+ inner: Arc<Mutex<Inner>>,+}++#[cfg(send_sync)]+static_assertions::assert_impl_all!(Instance: Send, Sync);++fn load_gl_func(name: &str, module: Option<Foundation::HMODULE>) -> *const c_void {+ let addr = CString::new(name.as_bytes()).unwrap();+ let mut ptr = unsafe { OpenGL::wglGetProcAddress(PCSTR(addr.as_ptr().cast())) };+ if ptr.is_none() {+ if let Some(module) = module {+ ptr = unsafe { LibraryLoader::GetProcAddress(module, PCSTR(addr.as_ptr().cast())) };+ }+ }+ ptr.map_or_else(ptr::null_mut, |p| p as *mut c_void)+}++fn get_extensions(extra: &Wgl, dc: Gdi::HDC) -> HashSet<String> {+ if extra.GetExtensionsStringARB.is_loaded() {+ unsafe { CStr::from_ptr(extra.GetExtensionsStringARB(dc.0)) }+ .to_str()+ .unwrap_or("")+ } else {+ ""+ }+ .split(' ')+ .map(|s| s.to_owned())+ .collect()+}++unsafe fn setup_pixel_format(dc: Gdi::HDC) -> Result<(), crate::InstanceError> {+ {+ let format = OpenGL::PIXELFORMATDESCRIPTOR {+ nVersion: 1,+ nSize: size_of::<OpenGL::PIXELFORMATDESCRIPTOR>() as u16,+ dwFlags: OpenGL::PFD_DRAW_TO_WINDOW+ | OpenGL::PFD_SUPPORT_OPENGL+ | OpenGL::PFD_DOUBLEBUFFER,+ iPixelType: OpenGL::PFD_TYPE_RGBA,+ cColorBits: 8,+ ..unsafe { mem::zeroed() }+ };++ let index = unsafe { OpenGL::ChoosePixelFormat(dc, &format) };+ if index == 0 {+ return Err(crate::InstanceError::with_source(+ String::from("unable to choose pixel format"),+ Error::from_thread(),+ ));+ }++ let current = unsafe { OpenGL::GetPixelFormat(dc) };++ if index != current {+ unsafe { OpenGL::SetPixelFormat(dc, index, &format) }.map_err(|e| {+ crate::InstanceError::with_source(String::from("unable to set pixel format"), e)+ })?;+ }+ }++ {+ let index = unsafe { OpenGL::GetPixelFormat(dc) };+ if index == 0 {+ return Err(crate::InstanceError::with_source(+ String::from("unable to get pixel format index"),+ Error::from_thread(),+ ));+ }+ let mut format = Default::default();+ if unsafe {+ OpenGL::DescribePixelFormat(dc, index, size_of_val(&format) as u32, Some(&mut format))+ } == 0+ {+ return Err(crate::InstanceError::with_source(+ String::from("unable to read pixel format"),+ Error::from_thread(),+ ));+ }++ if !format.dwFlags.contains(OpenGL::PFD_SUPPORT_OPENGL)+ || format.iPixelType != OpenGL::PFD_TYPE_RGBA+ {+ return Err(crate::InstanceError::new(String::from(+ "unsuitable pixel format",+ )));+ }+ }+ Ok(())+}++fn create_global_window_class() -> Result<CString, crate::InstanceError> {+ let instance = unsafe { LibraryLoader::GetModuleHandleA(None) }.map_err(|e| {+ crate::InstanceError::with_source(String::from("unable to get executable instance"), e)+ })?;++ // Use the address of `UNIQUE` as part of the window class name to ensure different+ // `wgpu` versions use different names.+ static UNIQUE: Mutex<u8> = Mutex::new(0);+ let class_addr: *const _ = &UNIQUE;+ let name = format!("wgpu Device Class {:x}\0", class_addr as usize);+ let name = CString::from_vec_with_nul(name.into_bytes()).unwrap();++ // The window class may already be registered if we are a dynamic library that got+ // unloaded & loaded back into the same process. If so, just skip creation.+ let already_exists = unsafe {+ let mut wc = mem::zeroed::<WindowsAndMessaging::WNDCLASSEXA>();+ WindowsAndMessaging::GetClassInfoExA(+ Some(instance.into()),+ PCSTR(name.as_ptr().cast()),+ &mut wc,+ )+ .is_ok()+ };+ if already_exists {+ return Ok(name);+ }++ // Use a wrapper function for compatibility with `windows-rs`.+ unsafe extern "system" fn wnd_proc(+ window: Foundation::HWND,+ msg: u32,+ wparam: Foundation::WPARAM,+ lparam: Foundation::LPARAM,+ ) -> Foundation::LRESULT {+ unsafe { WindowsAndMessaging::DefWindowProcA(window, msg, wparam, lparam) }+ }++ let window_class = WindowsAndMessaging::WNDCLASSEXA {+ cbSize: size_of::<WindowsAndMessaging::WNDCLASSEXA>() as u32,+ style: WindowsAndMessaging::CS_OWNDC,+ lpfnWndProc: Some(wnd_proc),+ cbClsExtra: 0,+ cbWndExtra: 0,+ hInstance: instance.into(),+ hIcon: WindowsAndMessaging::HICON::default(),+ hCursor: WindowsAndMessaging::HCURSOR::default(),+ hbrBackground: Gdi::HBRUSH::default(),+ lpszMenuName: PCSTR::null(),+ lpszClassName: PCSTR(name.as_ptr().cast()),+ hIconSm: WindowsAndMessaging::HICON::default(),+ };++ let atom = unsafe { WindowsAndMessaging::RegisterClassExA(&window_class) };++ if atom == 0 {+ return Err(crate::InstanceError::with_source(+ String::from("unable to register window class"),+ Error::from_thread(),+ ));+ }++ // We intentionally leak the window class as we only need one per process.++ Ok(name)+}++fn get_global_window_class() -> Result<CString, crate::InstanceError> {+ static GLOBAL: LazyLock<Result<CString, crate::InstanceError>> =+ LazyLock::new(create_global_window_class);+ GLOBAL.clone()+}++struct InstanceDevice {+ dc: Gdi::HDC,++ /// This is used to keep the thread owning `dc` alive until this struct is dropped.+ _tx: SyncSender<()>,+}++fn create_instance_device() -> Result<InstanceDevice, crate::InstanceError> {+ #[derive(Clone, Copy)]+ // TODO: We can get these SendSync definitions in the upstream metadata if this is the case+ struct SendDc(Gdi::HDC);+ unsafe impl Sync for SendDc {}+ unsafe impl Send for SendDc {}++ struct Window {+ window: Foundation::HWND,+ }+ impl Drop for Window {+ fn drop(&mut self) {+ if let Err(e) = unsafe { WindowsAndMessaging::DestroyWindow(self.window) } {+ log::error!("failed to destroy window: {e}");+ }+ }+ }++ let window_class = get_global_window_class()?;++ let (drop_tx, drop_rx) = sync_channel(0);+ let (setup_tx, setup_rx) = sync_channel(0);++ // We spawn a thread which owns the hidden window for this instance.+ thread::Builder::new()+ .stack_size(256 * 1024)+ .name("wgpu-hal WGL Instance Thread".to_owned())+ .spawn(move || {+ let setup = (|| {+ let instance = unsafe { LibraryLoader::GetModuleHandleA(None) }.map_err(|e| {+ crate::InstanceError::with_source(+ String::from("unable to get executable instance"),+ e,+ )+ })?;++ // Create a hidden window since we don't pass `WS_VISIBLE`.+ let window = unsafe {+ WindowsAndMessaging::CreateWindowExA(+ WindowsAndMessaging::WINDOW_EX_STYLE::default(),+ PCSTR(window_class.as_ptr().cast()),+ PCSTR(window_class.as_ptr().cast()),+ WindowsAndMessaging::WINDOW_STYLE::default(),+ 0,+ 0,+ 1,+ 1,+ None,+ None,+ Some(instance.into()),+ None,+ )+ }+ .map_err(|e| {+ crate::InstanceError::with_source(+ String::from("unable to create hidden instance window"),+ e,+ )+ })?;+ let window = Window { window };++ let dc = unsafe { Gdi::GetDC(Some(window.window)) };+ if dc.is_invalid() {+ return Err(crate::InstanceError::with_source(+ String::from("unable to create memory device"),+ Error::from_thread(),+ ));+ }+ let dc = DeviceContextHandle {+ device: dc,+ window: window.window,+ };+ unsafe { setup_pixel_format(dc.device)? };++ Ok((window, dc))+ })();++ match setup {+ Ok((_window, dc)) => {+ setup_tx.send(Ok(SendDc(dc.device))).unwrap();+ // Wait for the shutdown event to free the window and device context handle.+ drop_rx.recv().ok();+ }+ Err(err) => {+ setup_tx.send(Err(err)).unwrap();+ }+ }+ })+ .map_err(|e| {+ crate::InstanceError::with_source(String::from("unable to create instance thread"), e)+ })?;++ let dc = setup_rx.recv().unwrap()?.0;++ Ok(InstanceDevice { dc, _tx: drop_tx })+}++impl crate::Instance for Instance {+ type A = super::Api;++ unsafe fn init(desc: &crate::InstanceDescriptor<'_>) -> Result<Self, crate::InstanceError> {+ profiling::scope!("Init OpenGL (WGL) Backend");+ let opengl_module =+ unsafe { LibraryLoader::LoadLibraryA(PCSTR(c"opengl32.dll".as_ptr().cast())) }+ .map_err(|e| {+ crate::InstanceError::with_source(+ String::from("unable to load the OpenGL library"),+ e,+ )+ })?;++ let device = create_instance_device()?;+ let dc = device.dc;++ let context = unsafe { OpenGL::wglCreateContext(dc) }.map_err(|e| {+ crate::InstanceError::with_source(+ String::from("unable to create initial OpenGL context"),+ e,+ )+ })?;+ let context = WglContext { context };+ context.make_current(dc).map_err(|e| {+ crate::InstanceError::with_source(+ String::from("unable to set initial OpenGL context as current"),+ e,+ )+ })?;++ let extra = Wgl::load_with(|name| load_gl_func(name, None));+ let extensions = get_extensions(&extra, dc);++ let can_use_profile = extensions.contains("WGL_ARB_create_context_profile")+ && extra.CreateContextAttribsARB.is_loaded();++ let context = if can_use_profile {+ let attributes = [+ CONTEXT_PROFILE_MASK_ARB as c_int,+ CONTEXT_CORE_PROFILE_BIT_ARB as c_int,+ CONTEXT_FLAGS_ARB as c_int,+ if desc.flags.contains(InstanceFlags::DEBUG) {+ CONTEXT_DEBUG_BIT_ARB as c_int+ } else {+ 0+ },+ 0, // End of list+ ];+ let context =+ unsafe { extra.CreateContextAttribsARB(dc.0, ptr::null(), attributes.as_ptr()) };+ if context.is_null() {+ return Err(crate::InstanceError::with_source(+ String::from("unable to create OpenGL context"),+ Error::from_thread(),+ ));+ }+ WglContext {+ context: OpenGL::HGLRC(context.cast_mut()),+ }+ } else {+ context+ };++ context.make_current(dc).map_err(|e| {+ crate::InstanceError::with_source(+ String::from("unable to set OpenGL context as current"),+ e,+ )+ })?;++ let mut gl = unsafe {+ glow::Context::from_loader_function(|name| load_gl_func(name, Some(opengl_module)))+ };++ let extra = Wgl::load_with(|name| load_gl_func(name, None));+ let extensions = get_extensions(&extra, dc);++ let srgb_capable = extensions.contains("WGL_EXT_framebuffer_sRGB")+ || extensions.contains("WGL_ARB_framebuffer_sRGB")+ || gl+ .supported_extensions()+ .contains("GL_ARB_framebuffer_sRGB");++ // In contrast to OpenGL ES, OpenGL requires explicitly enabling sRGB conversions,+ // as otherwise the user has to do the sRGB conversion.+ if srgb_capable {+ unsafe { gl.enable(glow::FRAMEBUFFER_SRGB) };+ }++ if desc.flags.contains(InstanceFlags::VALIDATION) && gl.supports_debug() {+ log::debug!("Enabling GL debug output");+ unsafe { gl.enable(glow::DEBUG_OUTPUT) };+ unsafe { gl.debug_message_callback(super::gl_debug_message_callback) };+ }++ // Wrap in ManuallyDrop to make it easier to "current" the GL context before dropping this+ // GLOW context, which could also happen if a panic occurs after we uncurrent the context+ // below but before Inner is constructed.+ let gl = ManuallyDrop::new(gl);+ context.unmake_current().map_err(|e| {+ crate::InstanceError::with_source(+ String::from("unable to unset the current WGL context"),+ e,+ )+ })?;++ Ok(Instance {+ inner: Arc::new(Mutex::new(Inner {+ device,+ gl,+ context: Some(context),+ })),+ options: desc.backend_options.gl.clone(),+ srgb_capable,+ })+ }++ unsafe fn create_surface(+ &self,+ display_handle: RawDisplayHandle,+ window_handle: RawWindowHandle,+ ) -> Result<Surface, crate::InstanceError> {+ assert!(matches!(display_handle, RawDisplayHandle::Windows(_)));+ let window = if let RawWindowHandle::Win32(handle) = window_handle {+ handle+ } else {+ return Err(crate::InstanceError::new(format!(+ "unsupported window: {window_handle:?}"+ )));+ };+ Ok(Surface {+ // This cast exists because of https://github.com/rust-windowing/raw-window-handle/issues/171+ window: Foundation::HWND(window.hwnd.get() as *mut _),+ presentable: true,+ swapchain: RwLock::new(None),+ srgb_capable: self.srgb_capable,+ })+ }++ unsafe fn enumerate_adapters(+ &self,+ _surface_hint: Option<&Surface>,+ ) -> Vec<crate::ExposedAdapter<super::Api>> {+ unsafe {+ super::Adapter::expose(+ AdapterContext {+ inner: self.inner.clone(),+ },+ self.options.clone(),+ )+ }+ .into_iter()+ .collect()+ }+}++impl super::Adapter {+ /// Creates a new external adapter using the specified loader function.+ ///+ /// # Safety+ ///+ /// - The underlying OpenGL ES context must be current.+ /// - The underlying OpenGL ES context must be current when interfacing with any objects returned by+ /// wgpu-hal from this adapter.+ /// - The underlying OpenGL ES context must be current when dropping this adapter and when+ /// dropping any objects returned from this adapter.+ pub unsafe fn new_external(+ fun: impl FnMut(&str) -> *const c_void,+ options: wgt::GlBackendOptions,+ ) -> Option<crate::ExposedAdapter<super::Api>> {+ let context = unsafe { glow::Context::from_loader_function(fun) };+ unsafe {+ Self::expose(+ AdapterContext {+ inner: Arc::new(Mutex::new(Inner {+ gl: ManuallyDrop::new(context),+ device: create_instance_device().ok()?,+ context: None,+ })),+ },+ options,+ )+ }+ }++ pub fn adapter_context(&self) -> &AdapterContext {+ &self.shared.context+ }+}++impl super::Device {+ /// Returns the underlying WGL context.+ pub fn context(&self) -> &AdapterContext {+ &self.shared.context+ }+}++struct DeviceContextHandle {+ device: Gdi::HDC,+ window: Foundation::HWND,+}++impl Drop for DeviceContextHandle {+ fn drop(&mut self) {+ unsafe {+ Gdi::ReleaseDC(Some(self.window), self.device);+ };+ }+}++pub struct Swapchain {+ framebuffer: glow::Framebuffer,+ renderbuffer: glow::Renderbuffer,++ /// Extent because the window lies+ extent: wgt::Extent3d,++ format: wgt::TextureFormat,+ format_desc: super::TextureFormatDesc,+ #[allow(unused)]+ sample_type: wgt::TextureSampleType,+}++pub struct Surface {+ window: Foundation::HWND,+ pub(super) presentable: bool,+ swapchain: RwLock<Option<Swapchain>>,+ srgb_capable: bool,+}++unsafe impl Send for Surface {}+unsafe impl Sync for Surface {}++impl Surface {+ pub(super) unsafe fn present(+ &self,+ _suf_texture: super::Texture,+ context: &AdapterContext,+ ) -> Result<(), crate::SurfaceError> {+ let swapchain = self.swapchain.read();+ let sc = swapchain.as_ref().unwrap();+ let dc = unsafe { Gdi::GetDC(Some(self.window)) };+ if dc.is_invalid() {+ log::error!(+ "unable to get the device context from window: {}",+ Error::from_thread()+ );+ return Err(crate::SurfaceError::Other(+ "unable to get the device context from window",+ ));+ }+ let dc = DeviceContextHandle {+ device: dc,+ window: self.window,+ };++ let gl = context.lock_with_dc(dc.device).map_err(|e| {+ log::error!("unable to make the OpenGL context current for surface: {e}",);+ crate::SurfaceError::Other("unable to make the OpenGL context current for surface")+ })?;++ unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, None) };+ unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(sc.framebuffer)) };++ if self.srgb_capable {+ // Disable sRGB conversions for `glBlitFramebuffer` as behavior does diverge between+ // drivers and formats otherwise and we want to ensure no sRGB conversions happen.+ unsafe { gl.disable(glow::FRAMEBUFFER_SRGB) };+ }++ // Note the Y-flipping here. GL's presentation is not flipped,+ // but main rendering is. Therefore, we Y-flip the output positions+ // in the shader, and also this blit.+ unsafe {+ gl.blit_framebuffer(+ 0,+ sc.extent.height as i32,+ sc.extent.width as i32,+ 0,+ 0,+ 0,+ sc.extent.width as i32,+ sc.extent.height as i32,+ glow::COLOR_BUFFER_BIT,+ glow::NEAREST,+ )+ };++ if self.srgb_capable {+ unsafe { gl.enable(glow::FRAMEBUFFER_SRGB) };+ }++ unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, None) };+ unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, None) };++ if let Err(e) = unsafe { OpenGL::SwapBuffers(dc.device) } {+ log::error!("unable to swap buffers: {e}");+ return Err(crate::SurfaceError::Other("unable to swap buffers"));+ }++ Ok(())+ }++ pub fn supports_srgb(&self) -> bool {+ self.srgb_capable+ }+}++impl crate::Surface for Surface {+ type A = super::Api;++ unsafe fn configure(+ &self,+ device: &super::Device,+ config: &crate::SurfaceConfiguration,+ ) -> Result<(), crate::SurfaceError> {+ // Remove the old configuration.+ unsafe { self.unconfigure(device) };++ let dc = unsafe { Gdi::GetDC(Some(self.window)) };+ if dc.is_invalid() {+ log::error!(+ "unable to get the device context from window: {}",+ Error::from_thread()+ );+ return Err(crate::SurfaceError::Other(+ "unable to get the device context from window",+ ));+ }+ let dc = DeviceContextHandle {+ device: dc,+ window: self.window,+ };++ if let Err(e) = unsafe { setup_pixel_format(dc.device) } {+ log::error!("unable to setup surface pixel format: {e}",);+ return Err(crate::SurfaceError::Other(+ "unable to setup surface pixel format",+ ));+ }++ let format_desc = device.shared.describe_texture_format(config.format);+ let gl = &device.shared.context.lock_with_dc(dc.device).map_err(|e| {+ log::error!("unable to make the OpenGL context current for surface: {e}",);+ crate::SurfaceError::Other("unable to make the OpenGL context current for surface")+ })?;++ let renderbuffer = unsafe { gl.create_renderbuffer() }.map_err(|error| {+ log::error!("Internal swapchain renderbuffer creation failed: {error}");+ crate::DeviceError::OutOfMemory+ })?;+ unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, Some(renderbuffer)) };+ unsafe {+ gl.renderbuffer_storage(+ glow::RENDERBUFFER,+ format_desc.internal,+ config.extent.width as _,+ config.extent.height as _,+ )+ };++ let framebuffer = unsafe { gl.create_framebuffer() }.map_err(|error| {+ log::error!("Internal swapchain framebuffer creation failed: {error}");+ crate::DeviceError::OutOfMemory+ })?;+ unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(framebuffer)) };+ unsafe {+ gl.framebuffer_renderbuffer(+ glow::READ_FRAMEBUFFER,+ glow::COLOR_ATTACHMENT0,+ glow::RENDERBUFFER,+ Some(renderbuffer),+ )+ };+ unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, None) };+ unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, None) };++ // Setup presentation mode+ let extra = Wgl::load_with(|name| load_gl_func(name, None));+ let extensions = get_extensions(&extra, dc.device);+ if !(extensions.contains("WGL_EXT_swap_control") && extra.SwapIntervalEXT.is_loaded()) {+ log::error!("WGL_EXT_swap_control is unsupported");+ return Err(crate::SurfaceError::Other(+ "WGL_EXT_swap_control is unsupported",+ ));+ }++ let vsync = match config.present_mode {+ wgt::PresentMode::Immediate => false,+ wgt::PresentMode::Fifo => true,+ _ => {+ log::error!("unsupported present mode: {:?}", config.present_mode);+ return Err(crate::SurfaceError::Other("unsupported present mode"));+ }+ };++ if unsafe { extra.SwapIntervalEXT(if vsync { 1 } else { 0 }) } == Foundation::FALSE.0 {+ log::error!("unable to set swap interval: {}", Error::from_thread());+ return Err(crate::SurfaceError::Other("unable to set swap interval"));+ }++ self.swapchain.write().replace(Swapchain {+ renderbuffer,+ framebuffer,+ extent: config.extent,+ format: config.format,+ format_desc,+ sample_type: wgt::TextureSampleType::Float { filterable: false },+ });++ Ok(())+ }++ unsafe fn unconfigure(&self, device: &super::Device) {+ let gl = &device.shared.context.lock();+ if let Some(sc) = self.swapchain.write().take() {+ unsafe {+ gl.delete_renderbuffer(sc.renderbuffer);+ gl.delete_framebuffer(sc.framebuffer)+ };+ }+ }++ unsafe fn acquire_texture(+ &self,+ _timeout_ms: Option<Duration>,+ _fence: &super::Fence,+ ) -> Result<crate::AcquiredSurfaceTexture<super::Api>, crate::SurfaceError> {+ let swapchain = self.swapchain.read();+ let sc = swapchain.as_ref().unwrap();+ let texture = super::Texture {+ inner: super::TextureInner::Renderbuffer {+ raw: sc.renderbuffer,+ },+ drop_guard: None,+ array_layer_count: 1,+ mip_level_count: 1,+ format: sc.format,+ format_desc: sc.format_desc.clone(),+ copy_size: crate::CopyExtent {+ width: sc.extent.width,+ height: sc.extent.height,+ depth: 1,+ },+ };+ Ok(crate::AcquiredSurfaceTexture {+ texture,+ suboptimal: false,+ })+ }+ unsafe fn discard_texture(&self, _texture: super::Texture) {}+}diff --git a/vendor/wgpu-hal/src/lib.rs b/vendor/wgpu-hal/src/lib.rsnew file mode 100644index 00000000..ca626f27--- /dev/null+++ b/vendor/wgpu-hal/src/lib.rs@@ -0,0 +1,3086 @@+//! A cross-platform unsafe graphics abstraction.+//!+//! This crate defines a set of traits abstracting over modern graphics APIs,+//! with implementations ("backends") for Vulkan, Metal, Direct3D, and GL.+//!+//! `wgpu-hal` is a spiritual successor to+//! [gfx-hal](https://github.com/gfx-rs/gfx), but with reduced scope, and+//! oriented towards WebGPU implementation goals. It has no overhead for+//! validation or tracking, and the API translation overhead is kept to the bare+//! minimum by the design of WebGPU. This API can be used for resource-demanding+//! applications and engines.+//!+//! The `wgpu-hal` crate's main design choices:+//!+//! - Our traits are meant to be *portable*: proper use+//! should get equivalent results regardless of the backend.+//!+//! - Our traits' contracts are *unsafe*: implementations perform minimal+//! validation, if any, and incorrect use will often cause undefined behavior.+//! This allows us to minimize the overhead we impose over the underlying+//! graphics system. If you need safety, the [`wgpu-core`] crate provides a+//! safe API for driving `wgpu-hal`, implementing all necessary validation,+//! resource state tracking, and so on. (Note that `wgpu-core` is designed for+//! use via FFI; the [`wgpu`] crate provides more idiomatic Rust bindings for+//! `wgpu-core`.) Or, you can do your own validation.+//!+//! - In the same vein, returned errors *only cover cases the user can't+//! anticipate*, like running out of memory or losing the device. Any errors+//! that the user could reasonably anticipate are their responsibility to+//! avoid. For example, `wgpu-hal` returns no error for mapping a buffer that's+//! not mappable: as the buffer creator, the user should already know if they+//! can map it.+//!+//! - We use *static dispatch*. The traits are not+//! generally object-safe. You must select a specific backend type+//! like [`vulkan::Api`] or [`metal::Api`], and then use that+//! according to the main traits, or call backend-specific methods.+//!+//! - We use *idiomatic Rust parameter passing*,+//! taking objects by reference, returning them by value, and so on,+//! unlike `wgpu-core`, which refers to objects by ID.+//!+//! - We map buffer contents *persistently*. This means that the buffer can+//! remain mapped on the CPU while the GPU reads or writes to it. You must+//! explicitly indicate when data might need to be transferred between CPU and+//! GPU, if [`Device::map_buffer`] indicates that this is necessary.+//!+//! - You must record *explicit barriers* between different usages of a+//! resource. For example, if a buffer is written to by a compute+//! shader, and then used as and index buffer to a draw call, you+//! must use [`CommandEncoder::transition_buffers`] between those two+//! operations.+//!+//! - Pipeline layouts are *explicitly specified* when setting bind groups.+//! Incompatible layouts disturb groups bound at higher indices.+//!+//! - The API *accepts collections as iterators*, to avoid forcing the user to+//! store data in particular containers. The implementation doesn't guarantee+//! that any of the iterators are drained, unless stated otherwise by the+//! function documentation. For this reason, we recommend that iterators don't+//! do any mutating work.+//!+//! Unfortunately, `wgpu-hal`'s safety requirements are not fully documented.+//! Ideally, all trait methods would have doc comments setting out the+//! requirements users must meet to ensure correct and portable behavior. If you+//! are aware of a specific requirement that a backend imposes that is not+//! ensured by the traits' documented rules, please file an issue. Or, if you are+//! a capable technical writer, please file a pull request!+//!+//! [`wgpu-core`]: https://crates.io/crates/wgpu-core+//! [`wgpu`]: https://crates.io/crates/wgpu+//! [`vulkan::Api`]: vulkan/struct.Api.html+//! [`metal::Api`]: metal/struct.Api.html+//!+//! ## Primary backends+//!+//! The `wgpu-hal` crate has full-featured backends implemented on the following+//! platform graphics APIs:+//!+//! - Vulkan, available on Linux, Android, and Windows, using the [`ash`] crate's+//! Vulkan bindings. It's also available on macOS, if you install [MoltenVK].+//!+//! - Metal on macOS, using the [`metal`] crate's bindings.+//!+//! - Direct3D 12 on Windows, using the [`windows`] crate's bindings.+//!+//! [`ash`]: https://crates.io/crates/ash+//! [MoltenVK]: https://github.com/KhronosGroup/MoltenVK+//! [`metal`]: https://crates.io/crates/metal+//! [`windows`]: https://crates.io/crates/windows+//!+//! ## Secondary backends+//!+//! The `wgpu-hal` crate has a partial implementation based on the following+//! platform graphics API:+//!+//! - The GL backend is available anywhere OpenGL, OpenGL ES, or WebGL are+//! available. See the [`gles`] module documentation for details.+//!+//! [`gles`]: gles/index.html+//!+//! You can see what capabilities an adapter is missing by checking the+//! [`DownlevelCapabilities`][tdc] in [`ExposedAdapter::capabilities`], available+//! from [`Instance::enumerate_adapters`].+//!+//! The API is generally designed to fit the primary backends better than the+//! secondary backends, so the latter may impose more overhead.+//!+//! [tdc]: wgt::DownlevelCapabilities+//!+//! ## Traits+//!+//! The `wgpu-hal` crate defines a handful of traits that together+//! represent a cross-platform abstraction for modern GPU APIs.+//!+//! - The [`Api`] trait represents a `wgpu-hal` backend. It has no methods of its+//! own, only a collection of associated types.+//!+//! - [`Api::Instance`] implements the [`Instance`] trait. [`Instance::init`]+//! creates an instance value, which you can use to enumerate the adapters+//! available on the system. For example, [`vulkan::Api::Instance::init`][Ii]+//! returns an instance that can enumerate the Vulkan physical devices on your+//! system.+//!+//! - [`Api::Adapter`] implements the [`Adapter`] trait, representing a+//! particular device from a particular backend. For example, a Vulkan instance+//! might have a Lavapipe software adapter and a GPU-based adapter.+//!+//! - [`Api::Device`] implements the [`Device`] trait, representing an active+//! link to a device. You get a device value by calling [`Adapter::open`], and+//! then use it to create buffers, textures, shader modules, and so on.+//!+//! - [`Api::Queue`] implements the [`Queue`] trait, which you use to submit+//! command buffers to a given device.+//!+//! - [`Api::CommandEncoder`] implements the [`CommandEncoder`] trait, which you+//! use to build buffers of commands to submit to a queue. This has all the+//! methods for drawing and running compute shaders, which is presumably what+//! you're here for.+//!+//! - [`Api::Surface`] implements the [`Surface`] trait, which represents a+//! swapchain for presenting images on the screen, via interaction with the+//! system's window manager.+//!+//! The [`Api`] trait has various other associated types like [`Api::Buffer`] and+//! [`Api::Texture`] that represent resources the rest of the interface can+//! operate on, but these generally do not have their own traits.+//!+//! [Ii]: Instance::init+//!+//! ## Validation is the calling code's responsibility, not `wgpu-hal`'s+//!+//! As much as possible, `wgpu-hal` traits place the burden of validation,+//! resource tracking, and state tracking on the caller, not on the trait+//! implementations themselves. Anything which can reasonably be handled in+//! backend-independent code should be. A `wgpu_hal` backend's sole obligation is+//! to provide portable behavior, and report conditions that the calling code+//! can't reasonably anticipate, like device loss or running out of memory.+//!+//! The `wgpu` crate collection is intended for use in security-sensitive+//! applications, like web browsers, where the API is available to untrusted+//! code. This means that `wgpu-core`'s validation is not simply a service to+//! developers, to be provided opportunistically when the performance costs are+//! acceptable and the necessary data is ready at hand. Rather, `wgpu-core`'s+//! validation must be exhaustive, to ensure that even malicious content cannot+//! provoke and exploit undefined behavior in the platform's graphics API.+//!+//! Because graphics APIs' requirements are complex, the only practical way for+//! `wgpu` to provide exhaustive validation is to comprehensively track the+//! lifetime and state of all the resources in the system. Implementing this+//! separately for each backend is infeasible; effort would be better spent+//! making the cross-platform validation in `wgpu-core` legible and trustworthy.+//! Fortunately, the requirements are largely similar across the various+//! platforms, so cross-platform validation is practical.+//!+//! Some backends have specific requirements that aren't practical to foist off+//! on the `wgpu-hal` user. For example, properly managing macOS Objective-C or+//! Microsoft COM reference counts is best handled by using appropriate pointer+//! types within the backend.+//!+//! A desire for "defense in depth" may suggest performing additional validation+//! in `wgpu-hal` when the opportunity arises, but this must be done with+//! caution. Even experienced contributors infer the expectations their changes+//! must meet by considering not just requirements made explicit in types, tests,+//! assertions, and comments, but also those implicit in the surrounding code.+//! When one sees validation or state-tracking code in `wgpu-hal`, it is tempting+//! to conclude, "Oh, `wgpu-hal` checks for this, so `wgpu-core` needn't worry+//! about it - that would be redundant!" The responsibility for exhaustive+//! validation always rests with `wgpu-core`, regardless of what may or may not+//! be checked in `wgpu-hal`.+//!+//! To this end, any "defense in depth" validation that does appear in `wgpu-hal`+//! for requirements that `wgpu-core` should have enforced should report failure+//! via the `unreachable!` macro, because problems detected at this stage always+//! indicate a bug in `wgpu-core`.+//!+//! ## Debugging+//!+//! Most of the information on the wiki [Debugging wgpu Applications][wiki-debug]+//! page still applies to this API, with the exception of API tracing/replay+//! functionality, which is only available in `wgpu-core`.+//!+//! [wiki-debug]: https://github.com/gfx-rs/wgpu/wiki/Debugging-wgpu-Applications++#![no_std]+#![cfg_attr(docsrs, feature(doc_cfg))]+#![allow(+ // this happens on the GL backend, where it is both thread safe and non-thread safe in the same code.+ clippy::arc_with_non_send_sync,+ // We don't use syntax sugar where it's not necessary.+ clippy::match_like_matches_macro,+ // Redundant matching is more explicit.+ clippy::redundant_pattern_matching,+ // Explicit lifetimes are often easier to reason about.+ clippy::needless_lifetimes,+ // No need for defaults in the internal types.+ clippy::new_without_default,+ // Matches are good and extendable, no need to make an exception here.+ clippy::single_match,+ // Push commands are more regular than macros.+ clippy::vec_init_then_push,+ // TODO!+ clippy::missing_safety_doc,+ // It gets in the way a lot and does not prevent bugs in practice.+ clippy::pattern_type_mismatch,+ // We should investigate these.+ clippy::large_enum_variant+)]+#![warn(+ clippy::alloc_instead_of_core,+ clippy::ptr_as_ptr,+ clippy::std_instead_of_alloc,+ clippy::std_instead_of_core,+ trivial_casts,+ trivial_numeric_casts,+ unsafe_op_in_unsafe_fn,+ unused_extern_crates,+ unused_qualifications+)]++extern crate alloc;+#[allow(unused_extern_crates)]+extern crate naga_types as nt;+extern crate wgpu_types as wgt;+// Each of these backends needs `std` in some fashion; usually `std::thread` functions.+#[cfg(any(dx12, gles_with_std, metal, vulkan))]+#[macro_use]+extern crate std;++/// DirectX12 API internals.+#[cfg(dx12)]+pub mod dx12;+/// GLES API internals.+#[cfg(gles)]+pub mod gles;+/// Metal API internals.+#[cfg(metal)]+pub mod metal;+/// A dummy API implementation.+// TODO(https://github.com/gfx-rs/wgpu/issues/7120): this should have a cfg+pub mod noop;+/// Vulkan API internals.+#[cfg(vulkan)]+pub mod vulkan;++pub mod auxil;+pub mod api {+ #[cfg(dx12)]+ pub use super::dx12::Api as Dx12;+ #[cfg(gles)]+ pub use super::gles::Api as Gles;+ #[cfg(metal)]+ pub use super::metal::Api as Metal;+ pub use super::noop::Api as Noop;+ #[cfg(vulkan)]+ pub use super::vulkan::Api as Vulkan;+}++mod dynamic;+#[cfg(feature = "validation_canary")]+mod validation_canary;++#[cfg(feature = "validation_canary")]+pub use validation_canary::{ValidationCanary, VALIDATION_CANARY};++pub(crate) use dynamic::impl_dyn_resource;+pub use dynamic::{+ DynAccelerationStructure, DynAcquiredSurfaceTexture, DynAdapter, DynBindGroup,+ DynBindGroupLayout, DynBuffer, DynCommandBuffer, DynCommandEncoder, DynComputePipeline,+ DynDevice, DynExposedAdapter, DynFence, DynInstance, DynOpenDevice, DynPipelineCache,+ DynPipelineLayout, DynQuerySet, DynQueue, DynRayTracingPipeline, DynRenderPipeline,+ DynResource, DynSampler, DynShaderModule, DynSurface, DynSurfaceTexture, DynTexture,+ DynTextureView,+};++#[allow(unused)]+use alloc::boxed::Box;+use alloc::{borrow::Cow, string::String, vec::Vec};+use core::{+ borrow::Borrow,+ error::Error,+ fmt,+ num::{NonZeroU32, NonZeroU64},+ ops::{Range, RangeInclusive},+ ptr::NonNull,+};++use bitflags::bitflags;+use raw_window_handle::DisplayHandle;+use thiserror::Error;+use wgt::WasmNotSendSync;++cfg_if::cfg_if! {+ if #[cfg(supports_ptr_atomics)] {+ use alloc::sync::Arc;+ } else if #[cfg(feature = "portable-atomic")] {+ use portable_atomic_util::Arc;+ }+}++// - Vertex + Fragment+// - Compute+// Task + Mesh + Fragment+pub const MAX_CONCURRENT_SHADER_STAGES: usize = 3;+pub const MAX_ANISOTROPY: u8 = 16;+pub const MAX_BIND_GROUPS: usize = 8;+pub const MAX_VERTEX_BUFFERS: usize = 16;+pub const MAX_COLOR_ATTACHMENTS: usize = 8;+pub const MAX_MIP_LEVELS: u32 = 16;+/// Size of a single occlusion/timestamp query, when copied into a buffer, in bytes.+/// cbindgen:ignore+pub const QUERY_SIZE: wgt::BufferAddress = 8;++pub type Label<'a> = Option<&'a str>;+pub type MemoryRange = Range<wgt::BufferAddress>;+pub type FenceValue = u64;+#[cfg(supports_64bit_atomics)]+pub type AtomicFenceValue = core::sync::atomic::AtomicU64;+#[cfg(not(supports_64bit_atomics))]+pub type AtomicFenceValue = portable_atomic::AtomicU64;++/// A callback to signal that wgpu is no longer using a resource.+#[cfg(any(gles, vulkan, metal))]+pub type DropCallback = Box<dyn FnOnce() + Send + Sync + 'static>;++#[cfg(any(gles, vulkan, metal))]+pub struct DropGuard {+ callback: Option<DropCallback>,+}++#[cfg(all(any(gles, vulkan, metal), any(native, Emscripten)))]+impl DropGuard {+ fn from_option(callback: Option<DropCallback>) -> Option<Self> {+ callback.map(Self::new)+ }++ fn new(callback: DropCallback) -> Self {+ Self {+ callback: Some(callback),+ }+ }+}++#[cfg(any(gles, vulkan, metal))]+impl Drop for DropGuard {+ fn drop(&mut self) {+ if let Some(cb) = self.callback.take() {+ (cb)();+ }+ }+}++#[cfg(any(gles, vulkan, metal))]+impl fmt::Debug for DropGuard {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ f.debug_struct("DropGuard").finish()+ }+}++#[derive(Clone, Debug, PartialEq, Eq, Error)]+pub enum DeviceError {+ #[error("Out of memory")]+ OutOfMemory,+ #[error("Device is lost")]+ Lost,+ #[error("Unexpected error variant (driver implementation is at fault)")]+ Unexpected,+}++#[cfg(any(dx12, vulkan))]+impl From<gpu_allocator::AllocationError> for DeviceError {+ fn from(result: gpu_allocator::AllocationError) -> Self {+ match result {+ gpu_allocator::AllocationError::OutOfMemory => Self::OutOfMemory,+ gpu_allocator::AllocationError::FailedToMap(e) => {+ log::error!("gpu-allocator: Failed to map: {e}");+ Self::Lost+ }+ gpu_allocator::AllocationError::NoCompatibleMemoryTypeFound => {+ log::error!("gpu-allocator: No Compatible Memory Type Found");+ Self::Lost+ }+ gpu_allocator::AllocationError::InvalidAllocationCreateDesc => {+ log::error!("gpu-allocator: Invalid Allocation Creation Description");+ Self::Lost+ }+ gpu_allocator::AllocationError::InvalidAllocatorCreateDesc(e) => {+ log::error!("gpu-allocator: Invalid Allocator Creation Description: {e}");+ Self::Lost+ }++ gpu_allocator::AllocationError::Internal(e) => {+ log::error!("gpu-allocator: Internal Error: {e}");+ Self::Lost+ }+ gpu_allocator::AllocationError::BarrierLayoutNeedsDevice10+ | gpu_allocator::AllocationError::CastableFormatsRequiresEnhancedBarriers+ | gpu_allocator::AllocationError::CastableFormatsRequiresAtLeastDevice12 => {+ unreachable!()+ }+ }+ }+}++// A copy of gpu_allocator::AllocationSizes, allowing to read the configured value for+// the dx12 backend, we should instead add getters to gpu_allocator::AllocationSizes+// and remove this type.+// https://github.com/Traverse-Research/gpu-allocator/issues/295+#[cfg_attr(not(any(dx12, vulkan)), expect(dead_code))]+pub(crate) struct AllocationSizes {+ pub(crate) min_device_memblock_size: u64,+ pub(crate) max_device_memblock_size: u64,+ pub(crate) min_host_memblock_size: u64,+ pub(crate) max_host_memblock_size: u64,+}++impl AllocationSizes {+ #[allow(dead_code, reason = "may be unused on some platforms")]+ pub(crate) fn from_memory_hints(memory_hints: &wgt::MemoryHints) -> Self {+ // TODO: the allocator's configuration should take hardware capability into+ // account.+ const MB: u64 = 1024 * 1024;++ match memory_hints {+ wgt::MemoryHints::Performance => Self {+ min_device_memblock_size: 128 * MB,+ max_device_memblock_size: 256 * MB,+ min_host_memblock_size: 64 * MB,+ max_host_memblock_size: 128 * MB,+ },+ wgt::MemoryHints::MemoryUsage => Self {+ min_device_memblock_size: 8 * MB,+ max_device_memblock_size: 64 * MB,+ min_host_memblock_size: 4 * MB,+ max_host_memblock_size: 32 * MB,+ },+ wgt::MemoryHints::Manual {+ suballocated_device_memory_block_size,+ } => {+ // TODO: https://github.com/gfx-rs/wgpu/issues/8625+ // Would it be useful to expose the host size in memory hints+ // instead of always using half of the device size?+ let device_size = suballocated_device_memory_block_size;+ let host_size = device_size.start / 2..device_size.end / 2;++ // gpu_allocator clamps the sizes between 4MiB and 256MiB, but we clamp them ourselves since we use+ // the sizes when detecting high memory pressure and there is no way to query the values otherwise.+ Self {+ min_device_memblock_size: device_size.start.clamp(4 * MB, 256 * MB),+ max_device_memblock_size: device_size.end.clamp(4 * MB, 256 * MB),+ min_host_memblock_size: host_size.start.clamp(4 * MB, 256 * MB),+ max_host_memblock_size: host_size.end.clamp(4 * MB, 256 * MB),+ }+ }+ }+ }+}++#[cfg(any(dx12, vulkan))]+impl From<AllocationSizes> for gpu_allocator::AllocationSizes {+ fn from(value: AllocationSizes) -> gpu_allocator::AllocationSizes {+ gpu_allocator::AllocationSizes::new(+ value.min_device_memblock_size,+ value.min_host_memblock_size,+ )+ .with_max_device_memblock_size(value.max_device_memblock_size)+ .with_max_host_memblock_size(value.max_host_memblock_size)+ }+}++#[allow(dead_code, reason = "may be unused on some platforms")]+#[cold]+fn hal_usage_error<T: fmt::Display>(txt: T) -> ! {+ panic!("wgpu-hal invariant was violated (usage error): {txt}")+}++#[allow(dead_code, reason = "may be unused on some platforms")]+#[cold]+fn hal_internal_error<T: fmt::Display>(txt: T) -> ! {+ panic!("wgpu-hal ran into a preventable internal error: {txt}")+}++#[derive(Clone, Debug, Eq, PartialEq, Error)]+pub enum ShaderError {+ #[error("Compilation failed: {0:?}")]+ Compilation(String),+ #[error(transparent)]+ Device(#[from] DeviceError),+}++#[derive(Clone, Debug, Eq, PartialEq, Error)]+pub enum PipelineError {+ #[error("Linkage failed for stage {0:?}: {1}")]+ Linkage(wgt::ShaderStages, String),+ #[error("Entry point for stage {0:?} is invalid")]+ EntryPoint(naga::ShaderStage),+ #[error(transparent)]+ Device(#[from] DeviceError),+ #[error("Pipeline constant error for stage {0:?}: {1}")]+ PipelineConstants(wgt::ShaderStages, String),+}++#[derive(Clone, Debug, Eq, PartialEq, Error)]+pub enum PipelineCacheError {+ #[error(transparent)]+ Device(#[from] DeviceError),+}++#[derive(Clone, Debug, Eq, PartialEq, Error)]+pub enum SurfaceError {+ #[error("Surface is lost")]+ Lost,+ #[error("Surface is outdated, needs to be re-created")]+ Outdated,+ #[error("Timed out waiting for a surface texture")]+ Timeout,+ #[error("The window is occluded (e.g. minimized or behind another window). Try again once the window is no longer occluded.")]+ Occluded,+ #[error(transparent)]+ Device(#[from] DeviceError),+ #[error("Other reason: {0}")]+ Other(&'static str),+}++/// Error occurring while trying to create an instance, or create a surface from an instance;+/// typically relating to the state of the underlying graphics API or hardware.+#[derive(Clone, Debug, Error)]+#[error("{message}")]+pub struct InstanceError {+ /// These errors are very platform specific, so do not attempt to encode them as an enum.+ ///+ /// This message should describe the problem in sufficient detail to be useful for a+ /// user-to-developer “why won't this work on my machine” bug report, and otherwise follow+ /// <https://rust-lang.github.io/api-guidelines/interoperability.html#error-types-are-meaningful-and-well-behaved-c-good-err>.+ message: String,++ /// Underlying error value, if any is available.+ #[source]+ source: Option<Arc<dyn Error + Send + Sync + 'static>>,+}++impl InstanceError {+ #[allow(dead_code, reason = "may be unused on some platforms")]+ pub(crate) fn new(message: String) -> Self {+ Self {+ message,+ source: None,+ }+ }+ #[allow(dead_code, reason = "may be unused on some platforms")]+ pub(crate) fn with_source(message: String, source: impl Error + Send + Sync + 'static) -> Self {+ cfg_if::cfg_if! {+ if #[cfg(supports_ptr_atomics)] {+ let source = Arc::new(source);+ } else {+ // TODO(https://github.com/rust-lang/rust/issues/18598): avoid indirection via Box once arbitrary types support unsized coercion+ let source: Box<dyn Error + Send + Sync + 'static> = Box::new(source);+ let source = Arc::from(source);+ }+ }+ Self {+ message,+ source: Some(source),+ }+ }+}++/// All the types and methods that make up a implementation on top of a backend.+///+/// Only the types that have non-dyn trait bounds have methods on them. Most methods+/// are either on [`CommandEncoder`] or [`Device`].+///+/// The api can either be used through generics (through use of this trait and associated+/// types) or dynamically through using the `Dyn*` traits.+pub trait Api: Clone + fmt::Debug + Sized + WasmNotSendSync + 'static {+ const VARIANT: wgt::Backend;++ type Instance: DynInstance + Instance<A = Self>;+ type Surface: DynSurface + Surface<A = Self>;+ type Adapter: DynAdapter + Adapter<A = Self>;+ type Device: DynDevice + Device<A = Self>;++ type Queue: DynQueue + Queue<A = Self>;+ type CommandEncoder: DynCommandEncoder + CommandEncoder<A = Self>;++ /// This API's command buffer type.+ ///+ /// The only thing you can do with `CommandBuffer`s is build them+ /// with a [`CommandEncoder`] and then pass them to+ /// [`Queue::submit`] for execution, or destroy them by passing+ /// them to [`CommandEncoder::reset_all`].+ ///+ /// [`CommandEncoder`]: Api::CommandEncoder+ type CommandBuffer: DynCommandBuffer;++ type Buffer: DynBuffer;+ type Texture: DynTexture;+ type SurfaceTexture: DynSurfaceTexture + Borrow<Self::Texture>;+ type TextureView: DynTextureView;+ type Sampler: DynSampler;+ type QuerySet: DynQuerySet;++ /// A value you can block on to wait for something to finish.+ ///+ /// A `Fence` holds a monotonically increasing [`FenceValue`]. You can call+ /// [`Device::wait`] to block until a fence reaches or passes a value you+ /// choose. [`Queue::submit`] can take a `Fence` and a [`FenceValue`] to+ /// store in it when the submitted work is complete.+ ///+ /// Attempting to set a fence to a value less than its current value has no+ /// effect.+ ///+ /// Waiting on a fence returns as soon as the fence reaches *or passes* the+ /// requested value. This implies that, in order to reliably determine when+ /// an operation has completed, operations must finish in order of+ /// increasing fence values: if a higher-valued operation were to finish+ /// before a lower-valued operation, then waiting for the fence to reach the+ /// lower value could return before the lower-valued operation has actually+ /// finished.+ ///+ /// Fences are internally synchronised by the hal, and so should not need to be+ /// contained in external synchronisation primitives.+ type Fence: DynFence;++ type BindGroupLayout: DynBindGroupLayout;+ type BindGroup: DynBindGroup;+ type PipelineLayout: DynPipelineLayout;+ type ShaderModule: DynShaderModule;+ type RenderPipeline: DynRenderPipeline;+ type ComputePipeline: DynComputePipeline;+ type RayTracingPipeline: DynRayTracingPipeline;+ type PipelineCache: DynPipelineCache;++ type AccelerationStructure: DynAccelerationStructure + 'static;+}++pub trait Instance: Sized + WasmNotSendSync {+ type A: Api;++ unsafe fn init(desc: &InstanceDescriptor<'_>) -> Result<Self, InstanceError>;+ unsafe fn create_surface(+ &self,+ display_handle: raw_window_handle::RawDisplayHandle,+ window_handle: raw_window_handle::RawWindowHandle,+ ) -> Result<<Self::A as Api>::Surface, InstanceError>;+ /// `surface_hint` is only used by the GLES backend targeting WebGL2+ unsafe fn enumerate_adapters(+ &self,+ surface_hint: Option<&<Self::A as Api>::Surface>,+ ) -> Vec<ExposedAdapter<Self::A>>;+}++pub trait Surface: WasmNotSendSync {+ type A: Api;++ /// Configure `self` to use `device`.+ ///+ /// # Safety+ ///+ /// - All GPU work using `self` must have been completed.+ /// - All [`AcquiredSurfaceTexture`]s must have been destroyed.+ /// - All [`Api::TextureView`]s derived from the [`AcquiredSurfaceTexture`]s must have been destroyed.+ /// - The surface `self` must not currently be configured to use any other [`Device`].+ unsafe fn configure(+ &self,+ device: &<Self::A as Api>::Device,+ config: &SurfaceConfiguration,+ ) -> Result<(), SurfaceError>;++ /// Unconfigure `self` on `device`.+ ///+ /// # Safety+ ///+ /// - All GPU work that uses `surface` must have been completed.+ /// - All [`AcquiredSurfaceTexture`]s must have been destroyed.+ /// - All [`Api::TextureView`]s derived from the [`AcquiredSurfaceTexture`]s must have been destroyed.+ /// - The surface `self` must have been configured on `device`.+ unsafe fn unconfigure(&self, device: &<Self::A as Api>::Device);++ /// Return the next texture to be presented by `self`, for the caller to draw on.+ ///+ /// On success, return an [`AcquiredSurfaceTexture`] representing the+ /// texture into which the caller should draw the image to be displayed on+ /// `self`.+ ///+ /// If `timeout` elapses before `self` has a texture ready to be acquired,+ /// return `Err(SurfaceError::Timeout)`. If `timeout` is `None`, wait+ /// indefinitely, with no timeout.+ ///+ /// # Using an [`AcquiredSurfaceTexture`]+ ///+ /// On success, this function returns an [`AcquiredSurfaceTexture`] whose+ /// [`texture`] field is a [`SurfaceTexture`] from which the caller can+ /// [`borrow`] a [`Texture`] to draw on. The [`AcquiredSurfaceTexture`] also+ /// carries some metadata about that [`SurfaceTexture`].+ ///+ /// All calls to [`Queue::submit`] that draw on that [`Texture`] must also+ /// include the [`SurfaceTexture`] in the `surface_textures` argument.+ ///+ /// When you are done drawing on the texture, you can display it on `self`+ /// by passing the [`SurfaceTexture`] and `self` to [`Queue::present`].+ ///+ /// If you do not wish to display the texture, you must pass the+ /// [`SurfaceTexture`] to [`self.discard_texture`], so that it can be reused+ /// by future acquisitions.+ ///+ /// The fence is internally synchronised by the hal.+ ///+ /// # Portability+ ///+ /// Some backends can't support a timeout when acquiring a texture. On these+ /// backends, `timeout` is ignored.+ ///+ /// On macOS, this returns `Err(SurfaceError::Timeout)` when the window is+ /// not visible (minimized, fully occluded, or on another virtual desktop)+ /// to avoid blocking in `CAMetalLayer.nextDrawable()`.+ ///+ /// # Safety+ ///+ /// - The surface `self` must currently be configured on some [`Device`].+ ///+ /// - The `fence` argument must be the same [`Fence`] passed to all calls to+ /// [`Queue::submit`] that used [`Texture`]s acquired from this surface.+ ///+ /// - You may only have one texture acquired from `self` at a time. When+ /// `acquire_texture` returns `Ok(ast)`, you must pass the returned+ /// [`SurfaceTexture`] `ast.texture` to either [`Queue::present`] or+ /// [`Surface::discard_texture`] before calling `acquire_texture` again.+ ///+ /// [`texture`]: AcquiredSurfaceTexture::texture+ /// [`SurfaceTexture`]: Api::SurfaceTexture+ /// [`borrow`]: alloc::borrow::Borrow::borrow+ /// [`Texture`]: Api::Texture+ /// [`Fence`]: Api::Fence+ /// [`self.discard_texture`]: Surface::discard_texture+ unsafe fn acquire_texture(+ &self,+ timeout: Option<core::time::Duration>,+ fence: &<Self::A as Api>::Fence,+ ) -> Result<AcquiredSurfaceTexture<Self::A>, SurfaceError>;++ /// Relinquish an acquired texture without presenting it.+ ///+ /// After this call, the texture underlying [`SurfaceTexture`] may be+ /// returned by subsequent calls to [`self.acquire_texture`].+ ///+ /// # Safety+ ///+ /// - The surface `self` must currently be configured on some [`Device`].+ ///+ /// - `texture` must be a [`SurfaceTexture`] returned by a call to+ /// [`self.acquire_texture`] that has not yet been passed to+ /// [`Queue::present`].+ ///+ /// [`SurfaceTexture`]: Api::SurfaceTexture+ /// [`self.acquire_texture`]: Surface::acquire_texture+ unsafe fn discard_texture(&self, texture: <Self::A as Api>::SurfaceTexture);+}++pub trait Adapter: WasmNotSendSync {+ type A: Api;++ unsafe fn open(+ &self,+ features: wgt::Features,+ limits: &wgt::Limits,+ memory_hints: &wgt::MemoryHints,+ ) -> Result<OpenDevice<Self::A>, DeviceError>;++ /// Return the set of supported capabilities for a texture format.+ unsafe fn texture_format_capabilities(+ &self,+ format: wgt::TextureFormat,+ ) -> TextureFormatCapabilities;++ /// Returns the capabilities of working with a specified surface.+ ///+ /// `None` means presentation is not supported for it.+ unsafe fn surface_capabilities(+ &self,+ surface: &<Self::A as Api>::Surface,+ ) -> Option<SurfaceCapabilities>;++ /// Returns the HDR / luminance characteristics of the display backing+ /// `surface`, queried from the OS on each call.+ ///+ /// `None` means no information is available; wgpu-core maps it to+ /// [`wgt::DisplayHdrInfo::default`]. Implementors must not panic; degrade any+ /// OS-query failure to `None`. The default implementation returns `None`.+ ///+ /// Implemented by Metal (macOS only, and only from the main thread), DX12, and+ /// Vulkan (Win32 `HWND` surfaces only); GLES and noop keep the default `None`.+ unsafe fn surface_display_hdr_info(+ &self,+ surface: &<Self::A as Api>::Surface,+ ) -> Option<wgt::DisplayHdrInfo> {+ let _ = surface;+ None+ }++ /// Creates a [`PresentationTimestamp`] using the adapter's WSI.+ ///+ /// [`PresentationTimestamp`]: wgt::PresentationTimestamp+ unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp;++ /// The combination of all usages that the are guaranteed to be be ordered by the hardware.+ /// If a usage is ordered, then if the buffer state doesn't change between draw calls,+ /// there are no barriers needed for synchronization.+ fn get_ordered_buffer_usages(&self) -> wgt::BufferUses;++ /// The combination of all usages that the are guaranteed to be be ordered by the hardware.+ /// If a usage is ordered, then if the buffer state doesn't change between draw calls,+ /// there are no barriers needed for synchronization.+ fn get_ordered_texture_usages(&self) -> wgt::TextureUses;+}++/// A connection to a GPU and a pool of resources to use with it.+///+/// A `wgpu-hal` `Device` represents an open connection to a specific graphics+/// processor, controlled via the backend [`Device::A`]. A `Device` is mostly+/// used for creating resources. Each `Device` has an associated [`Queue`] used+/// for command submission.+///+/// On Vulkan a `Device` corresponds to a logical device ([`VkDevice`]). Other+/// backends don't have an exact analog: for example, [`ID3D12Device`]s and+/// [`MTLDevice`]s are owned by the backends' [`wgpu_hal::Adapter`]+/// implementations, and shared by all [`wgpu_hal::Device`]s created from that+/// `Adapter`.+///+/// A `Device`'s life cycle is generally:+///+/// 1) Obtain a `Device` and its associated [`Queue`] by calling+/// [`Adapter::open`].+///+/// Alternatively, the backend-specific types that implement [`Adapter`] often+/// have methods for creating a `wgpu-hal` `Device` from a platform-specific+/// handle. For example, [`vulkan::Adapter::device_from_raw`] can create a+/// [`vulkan::Device`] from an [`ash::Device`].+///+/// 1) Create resources to use on the device by calling methods like+/// [`Device::create_texture`] or [`Device::create_shader_module`].+///+/// 1) Call [`Device::create_command_encoder`] to obtain a [`CommandEncoder`],+/// which you can use to build [`CommandBuffer`]s holding commands to be+/// executed on the GPU.+///+/// 1) Call [`Queue::submit`] on the `Device`'s associated [`Queue`] to submit+/// [`CommandBuffer`]s for execution on the GPU. If needed, call+/// [`Device::wait`] to wait for them to finish execution.+///+/// 1) Free resources with methods like [`Device::destroy_texture`] or+/// [`Device::destroy_shader_module`].+///+/// 1) Drop the device.+///+/// [`vkDevice`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#VkDevice+/// [`ID3D12Device`]: https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nn-d3d12-id3d12device+/// [`MTLDevice`]: https://developer.apple.com/documentation/metal/mtldevice+/// [`wgpu_hal::Adapter`]: Adapter+/// [`wgpu_hal::Device`]: Device+/// [`vulkan::Adapter::device_from_raw`]: vulkan/struct.Adapter.html#method.device_from_raw+/// [`vulkan::Device`]: vulkan/struct.Device.html+/// [`ash::Device`]: https://docs.rs/ash/latest/ash/struct.Device.html+/// [`CommandBuffer`]: Api::CommandBuffer+///+/// # Safety+///+/// As with other `wgpu-hal` APIs, [validation] is the caller's+/// responsibility. Here are the general requirements for all `Device`+/// methods:+///+/// - Any resource passed to a `Device` method must have been created by that+/// `Device`. For example, a [`Texture`] passed to [`Device::destroy_texture`] must+/// have been created with the `Device` passed as `self`.+///+/// - Resources may not be destroyed if they are used by any submitted command+/// buffers that have not yet finished execution.+///+/// [validation]: index.html#validation-is-the-calling-codes-responsibility-not-wgpu-hals+/// [`Texture`]: Api::Texture+pub trait Device: WasmNotSendSync {+ type A: Api;++ /// Creates a new buffer.+ ///+ /// The initial usage is `wgt::BufferUses::empty()`.+ unsafe fn create_buffer(+ &self,+ desc: &BufferDescriptor,+ ) -> Result<<Self::A as Api>::Buffer, DeviceError>;++ /// Free `buffer` and any GPU resources it owns.+ ///+ /// Note that backends are allowed to allocate GPU memory for buffers from+ /// allocation pools, and this call is permitted to simply return `buffer`'s+ /// storage to that pool, without making it available to other applications.+ ///+ /// # Safety+ ///+ /// - The given `buffer` must not currently be mapped.+ unsafe fn destroy_buffer(&self, buffer: <Self::A as Api>::Buffer);++ /// A hook for when a wgpu-core buffer is created from a raw wgpu-hal buffer.+ unsafe fn add_raw_buffer(&self, buffer: &<Self::A as Api>::Buffer);++ /// Return a pointer to CPU memory mapping the contents of `buffer`.+ ///+ /// Buffer mappings are persistent: the buffer may remain mapped on the CPU+ /// while the GPU reads or writes to it. (Note that `wgpu_core` does not use+ /// this feature: when a `wgpu_core::Buffer` is unmapped, the underlying+ /// `wgpu_hal` buffer is also unmapped.)+ ///+ /// If this function returns `Ok(mapping)`, then:+ ///+ /// - `mapping.ptr` is the CPU address of the start of the mapped memory.+ ///+ /// - If `mapping.is_coherent` is `true`, then CPU writes to the mapped+ /// memory are immediately visible on the GPU, and vice versa.+ ///+ /// # Safety+ ///+ /// - The given `buffer` must have been created with the [`MAP_READ`] or+ /// [`MAP_WRITE`] flags set in [`BufferDescriptor::usage`].+ ///+ /// - The given `range` must fall within the size of `buffer`.+ ///+ /// - The caller must avoid data races between the CPU and the GPU. A data+ /// race is any pair of accesses to a particular byte, one of which is a+ /// write, that are not ordered with respect to each other by some sort of+ /// synchronization operation.+ ///+ /// - If this function returns `Ok(mapping)` and `mapping.is_coherent` is+ /// `false`, then:+ ///+ /// - Every CPU write to a mapped byte followed by a GPU read of that byte+ /// must have at least one call to [`Device::flush_mapped_ranges`]+ /// covering that byte that occurs between those two accesses.+ ///+ /// - Every GPU write to a mapped byte followed by a CPU read of that byte+ /// must have at least one call to [`Device::invalidate_mapped_ranges`]+ /// covering that byte that occurs between those two accesses.+ ///+ /// Note that the data race rule above requires that all such access pairs+ /// be ordered, so it is meaningful to talk about what must occur+ /// "between" them.+ ///+ /// - Zero-sized mappings are not allowed.+ ///+ /// - The returned [`BufferMapping::ptr`] must not be used after a call to+ /// [`Device::unmap_buffer`].+ ///+ /// [`MAP_READ`]: wgt::BufferUses::MAP_READ+ /// [`MAP_WRITE`]: wgt::BufferUses::MAP_WRITE+ unsafe fn map_buffer(+ &self,+ buffer: &<Self::A as Api>::Buffer,+ range: MemoryRange,+ ) -> Result<BufferMapping, DeviceError>;++ /// Remove the mapping established by the last call to [`Device::map_buffer`].+ ///+ /// # Safety+ ///+ /// - The given `buffer` must be currently mapped.+ unsafe fn unmap_buffer(&self, buffer: &<Self::A as Api>::Buffer);++ /// Indicate that CPU writes to mapped buffer memory should be made visible to the GPU.+ ///+ /// # Safety+ ///+ /// - The given `buffer` must be currently mapped.+ ///+ /// - All ranges produced by `ranges` must fall within `buffer`'s size.+ unsafe fn flush_mapped_ranges<I>(&self, buffer: &<Self::A as Api>::Buffer, ranges: I)+ where+ I: Iterator<Item = MemoryRange>;++ /// Indicate that GPU writes to mapped buffer memory should be made visible to the CPU.+ ///+ /// # Safety+ ///+ /// - The given `buffer` must be currently mapped.+ ///+ /// - All ranges produced by `ranges` must fall within `buffer`'s size.+ unsafe fn invalidate_mapped_ranges<I>(&self, buffer: &<Self::A as Api>::Buffer, ranges: I)+ where+ I: Iterator<Item = MemoryRange>;++ /// Creates a new texture.+ ///+ /// The initial usage for all subresources is `wgt::TextureUses::UNINITIALIZED`.+ unsafe fn create_texture(+ &self,+ desc: &TextureDescriptor,+ ) -> Result<<Self::A as Api>::Texture, DeviceError>;+ unsafe fn destroy_texture(&self, texture: <Self::A as Api>::Texture);++ /// A hook for when a wgpu-core texture is created from a raw wgpu-hal texture.+ unsafe fn add_raw_texture(&self, texture: &<Self::A as Api>::Texture);++ unsafe fn create_texture_view(+ &self,+ texture: &<Self::A as Api>::Texture,+ desc: &TextureViewDescriptor,+ ) -> Result<<Self::A as Api>::TextureView, DeviceError>;+ unsafe fn destroy_texture_view(&self, view: <Self::A as Api>::TextureView);+ unsafe fn create_sampler(+ &self,+ desc: &SamplerDescriptor,+ ) -> Result<<Self::A as Api>::Sampler, DeviceError>;+ unsafe fn destroy_sampler(&self, sampler: <Self::A as Api>::Sampler);++ /// Create a fresh [`CommandEncoder`].+ ///+ /// The new `CommandEncoder` is in the "closed" state.+ unsafe fn create_command_encoder(+ &self,+ desc: &CommandEncoderDescriptor<<Self::A as Api>::Queue>,+ ) -> Result<<Self::A as Api>::CommandEncoder, DeviceError>;++ /// Creates a bind group layout.+ unsafe fn create_bind_group_layout(+ &self,+ desc: &BindGroupLayoutDescriptor,+ ) -> Result<<Self::A as Api>::BindGroupLayout, DeviceError>;+ unsafe fn destroy_bind_group_layout(&self, bg_layout: <Self::A as Api>::BindGroupLayout);+ unsafe fn create_pipeline_layout(+ &self,+ desc: &PipelineLayoutDescriptor<<Self::A as Api>::BindGroupLayout>,+ ) -> Result<<Self::A as Api>::PipelineLayout, DeviceError>;+ unsafe fn destroy_pipeline_layout(&self, pipeline_layout: <Self::A as Api>::PipelineLayout);++ #[allow(clippy::type_complexity)]+ unsafe fn create_bind_group(+ &self,+ desc: &BindGroupDescriptor<+ <Self::A as Api>::BindGroupLayout,+ <Self::A as Api>::Buffer,+ <Self::A as Api>::Sampler,+ <Self::A as Api>::TextureView,+ <Self::A as Api>::AccelerationStructure,+ >,+ ) -> Result<<Self::A as Api>::BindGroup, DeviceError>;+ unsafe fn destroy_bind_group(&self, group: <Self::A as Api>::BindGroup);++ unsafe fn create_shader_module(+ &self,+ desc: &ShaderModuleDescriptor,+ shader: ShaderInput,+ ) -> Result<<Self::A as Api>::ShaderModule, ShaderError>;+ unsafe fn destroy_shader_module(&self, module: <Self::A as Api>::ShaderModule);++ #[allow(clippy::type_complexity)]+ unsafe fn create_render_pipeline(+ &self,+ desc: &RenderPipelineDescriptor<+ <Self::A as Api>::PipelineLayout,+ <Self::A as Api>::ShaderModule,+ <Self::A as Api>::PipelineCache,+ >,+ ) -> Result<<Self::A as Api>::RenderPipeline, PipelineError>;+ unsafe fn destroy_render_pipeline(&self, pipeline: <Self::A as Api>::RenderPipeline);++ #[allow(clippy::type_complexity)]+ unsafe fn create_compute_pipeline(+ &self,+ desc: &ComputePipelineDescriptor<+ <Self::A as Api>::PipelineLayout,+ <Self::A as Api>::ShaderModule,+ <Self::A as Api>::PipelineCache,+ >,+ ) -> Result<<Self::A as Api>::ComputePipeline, PipelineError>;+ unsafe fn destroy_compute_pipeline(&self, pipeline: <Self::A as Api>::ComputePipeline);++ #[allow(clippy::type_complexity)]+ unsafe fn create_ray_tracing_pipeline(+ &self,+ desc: &RayTracingPipelineDescriptor<+ <Self::A as Api>::PipelineLayout,+ <Self::A as Api>::ShaderModule,+ <Self::A as Api>::PipelineCache,+ >,+ ) -> Result<<Self::A as Api>::RayTracingPipeline, PipelineError>;+ unsafe fn destroy_ray_tracing_pipeline(&self, pipeline: <Self::A as Api>::RayTracingPipeline);+ /// Obtain the opaque data from each group, behaves as if group 0 is the ray generation, group 1+ /// is the miss shader, and group 2.. are the intersection groups.+ unsafe fn get_raytracing_pipeline_group_data(+ &self,+ pipeline: &<Self::A as Api>::RayTracingPipeline,+ groups: Range<u32>,+ ) -> Result<Vec<u8>, DeviceError>;++ unsafe fn create_pipeline_cache(+ &self,+ desc: &PipelineCacheDescriptor<'_>,+ ) -> Result<<Self::A as Api>::PipelineCache, PipelineCacheError>;+ fn pipeline_cache_validation_key(&self) -> Option<[u8; 16]> {+ None+ }+ unsafe fn destroy_pipeline_cache(&self, cache: <Self::A as Api>::PipelineCache);++ unsafe fn create_query_set(+ &self,+ desc: &wgt::QuerySetDescriptor<Label>,+ ) -> Result<<Self::A as Api>::QuerySet, DeviceError>;+ unsafe fn destroy_query_set(&self, set: <Self::A as Api>::QuerySet);+ unsafe fn create_fence(&self) -> Result<<Self::A as Api>::Fence, DeviceError>;+ unsafe fn destroy_fence(&self, fence: <Self::A as Api>::Fence);+ unsafe fn get_fence_value(+ &self,+ fence: &<Self::A as Api>::Fence,+ ) -> Result<FenceValue, DeviceError>;++ /// Wait for `fence` to reach `value`.+ ///+ /// Operations like [`Queue::submit`] can accept a [`Fence`] and a+ /// [`FenceValue`] to store in it, so you can use this `wait` function+ /// to wait for a given queue submission to finish execution.+ ///+ /// The `value` argument must not exceed the highest value that an actual+ /// operation you have already presented to the device is going to store in+ /// `fence`. You cannot wait for values yet to be submitted. (This+ /// restriction accommodates implementations like the `vulkan` backend's+ /// [`FencePool`] that must allocate a distinct synchronization object for+ /// each fence value one is able to wait for.)+ ///+ /// Calling `wait` with a lower [`FenceValue`] than `fence`'s current value+ /// returns immediately.+ ///+ /// If `timeout` is not provided, the function will block indefinitely or until+ /// an error is encountered.+ ///+ /// Returns `Ok(true)` on success and `Ok(false)` on timeout.+ ///+ /// [`Fence`]: Api::Fence+ /// [`FencePool`]: vulkan/enum.Fence.html#variant.FencePool+ unsafe fn wait(+ &self,+ fence: &<Self::A as Api>::Fence,+ value: FenceValue,+ timeout: Option<core::time::Duration>,+ ) -> Result<bool, DeviceError>;++ /// Start a graphics debugger capture.+ ///+ /// # Safety+ ///+ /// See [`wgpu::Device::start_graphics_debugger_capture`][api] for more details.+ ///+ /// [api]: ../wgpu/struct.Device.html#method.start_graphics_debugger_capture+ unsafe fn start_graphics_debugger_capture(&self) -> bool;++ /// Stop a graphics debugger capture.+ ///+ /// # Safety+ ///+ /// See [`wgpu::Device::stop_graphics_debugger_capture`][api] for more details.+ ///+ /// [api]: ../wgpu/struct.Device.html#method.stop_graphics_debugger_capture+ unsafe fn stop_graphics_debugger_capture(&self);++ #[allow(unused_variables)]+ unsafe fn pipeline_cache_get_data(+ &self,+ cache: &<Self::A as Api>::PipelineCache,+ ) -> Option<Vec<u8>> {+ None+ }++ unsafe fn create_acceleration_structure(+ &self,+ desc: &AccelerationStructureDescriptor,+ ) -> Result<<Self::A as Api>::AccelerationStructure, DeviceError>;+ unsafe fn get_acceleration_structure_build_sizes(+ &self,+ desc: &GetAccelerationStructureBuildSizesDescriptor<<Self::A as Api>::Buffer>,+ ) -> AccelerationStructureBuildSizes;+ unsafe fn get_acceleration_structure_device_address(+ &self,+ acceleration_structure: &<Self::A as Api>::AccelerationStructure,+ ) -> wgt::BufferAddress;+ unsafe fn destroy_acceleration_structure(+ &self,+ acceleration_structure: <Self::A as Api>::AccelerationStructure,+ );+ fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8>;++ fn get_internal_counters(&self) -> wgt::HalCounters;++ fn generate_allocator_report(&self) -> Option<wgt::AllocatorReport> {+ None+ }++ fn check_if_oom(&self) -> Result<(), DeviceError>;+}++pub trait Queue: WasmNotSendSync {+ type A: Api;++ /// Submit `command_buffers` for execution on GPU.+ ///+ /// Update `fence` to `value` when the operation is complete. See+ /// [`Fence`] for details.+ ///+ /// All command buffers submitted to a `wgpu_hal` queue are executed in the+ /// order they're submitted, with each buffer able to observe the effects of+ /// previous buffers' execution. Specifically:+ ///+ /// - If two calls to `submit` on a single `Queue` occur in a particular+ /// order (that is, they happen on the same thread, or on two threads that+ /// have synchronized to establish an ordering), then the first+ /// submission's commands all complete execution before any of the second+ /// submission's commands begin. All results produced by one submission+ /// are visible to the next.+ ///+ /// - Within a submission, command buffers execute in the order in which they+ /// appear in `command_buffers`. All results produced by one buffer are+ /// visible to the next.+ ///+ /// If two calls to `submit` on a single `Queue` from different threads are+ /// not synchronized to occur in a particular order, they must pass distinct+ /// [`Fence`]s. As explained in the [`Fence`] documentation, waiting for+ /// operations to complete is only trustworthy when operations finish in+ /// order of increasing fence value, but submissions from different threads+ /// cannot determine how to order the fence values if the submissions+ /// themselves are unordered. If each thread uses a separate [`Fence`], this+ /// problem does not arise.+ ///+ /// # Safety+ ///+ /// - Each [`CommandBuffer`][cb] in `command_buffers` must have been created+ /// from a [`CommandEncoder`][ce] that was constructed from the+ /// [`Device`][d] associated with this [`Queue`].+ ///+ /// - Each [`CommandBuffer`][cb] must remain alive until the submitted+ /// commands have finished execution. Since command buffers must not+ /// outlive their encoders, this implies that the encoders must remain+ /// alive as well.+ ///+ /// - All resources used by a submitted [`CommandBuffer`][cb]+ /// ([`Texture`][t]s, [`BindGroup`][bg]s, [`RenderPipeline`][rp]s, and so+ /// on) must remain alive until the command buffer finishes execution.+ ///+ /// - Every [`SurfaceTexture`][st] that any command in `command_buffers`+ /// writes to must appear in the `surface_textures` argument.+ ///+ /// - No [`SurfaceTexture`][st] may appear in the `surface_textures`+ /// argument more than once.+ ///+ /// - Each [`SurfaceTexture`][st] in `surface_textures` must be configured+ /// for use with the [`Device`][d] associated with this [`Queue`],+ /// typically by calling [`Surface::configure`].+ ///+ /// - All calls to this function that include a given [`SurfaceTexture`][st]+ /// in `surface_textures` must use the same [`Fence`].+ ///+ /// - The [`Fence`] passed as `signal_fence.0` must remain alive until+ /// all submissions that will signal it have completed.+ ///+ /// [`Fence`]: Api::Fence+ /// [cb]: Api::CommandBuffer+ /// [ce]: Api::CommandEncoder+ /// [d]: Api::Device+ /// [t]: Api::Texture+ /// [bg]: Api::BindGroup+ /// [rp]: Api::RenderPipeline+ /// [st]: Api::SurfaceTexture+ unsafe fn submit(+ &self,+ command_buffers: &[&<Self::A as Api>::CommandBuffer],+ surface_textures: &[&<Self::A as Api>::SurfaceTexture],+ signal_fence: (&<Self::A as Api>::Fence, FenceValue),+ ) -> Result<(), DeviceError>;+ /// Present a surface texture to the screen.+ ///+ /// This consumes the surface texture, returning it to the swapchain.+ ///+ /// # Safety+ ///+ /// - `texture` must have been acquired from `surface` via+ /// [`Surface::acquire_texture`] and not yet presented or discarded.+ /// - `surface` must be configured for use with the [`Device`][d] associated+ /// with this [`Queue`].+ /// - `texture` must be in the "present" state. Either:+ /// - It was passed in [`submit`][s]'s `surface_textures` argument+ /// (which transitions it to the present state), or+ /// - The caller has otherwise transitioned it (e.g. via a clear ++ /// barrier to `PRESENT` for textures that were never rendered to).+ /// - Any command buffers that write to `texture` must have been submitted+ /// via [`submit`][s] before this call. The submissions do not need to+ /// have completed on the GPU; platform-level synchronization handles the+ /// ordering between rendering and display.+ /// - Must be externally synchronized with all other queue operations+ /// ([`submit`][s], [`present`][Queue::present],+ /// [`wait_for_idle`][Queue::wait_for_idle]) on the same queue.+ ///+ /// [d]: Api::Device+ /// [s]: Queue::submit+ unsafe fn present(+ &self,+ surface: &<Self::A as Api>::Surface,+ texture: <Self::A as Api>::SurfaceTexture,+ ) -> Result<(), SurfaceError>;+ /// Block until all previously submitted work on this queue has completed,+ /// including any pending presentations.+ ///+ /// # Safety+ ///+ /// - Must be externally synchronized with all other queue operations+ /// ([`submit`][Queue::submit], [`present`][Queue::present],+ /// [`wait_for_idle`][Queue::wait_for_idle]) on the same queue.+ unsafe fn wait_for_idle(&self) -> Result<(), DeviceError>;+ unsafe fn get_timestamp_period(&self) -> f32;+}++/// Encoder and allocation pool for `CommandBuffer`s.+///+/// A `CommandEncoder` not only constructs `CommandBuffer`s but also+/// acts as the allocation pool that owns the buffers' underlying+/// storage. Thus, `CommandBuffer`s must not outlive the+/// `CommandEncoder` that created them.+///+/// The life cycle of a `CommandBuffer` is as follows:+///+/// - Call [`Device::create_command_encoder`] to create a new+/// `CommandEncoder`, in the "closed" state.+///+/// - Call `begin_encoding` on a closed `CommandEncoder` to begin+/// recording commands. This puts the `CommandEncoder` in the+/// "recording" state.+///+/// - Call methods like `copy_buffer_to_buffer`, `begin_render_pass`,+/// etc. on a "recording" `CommandEncoder` to add commands to the+/// list. (If an error occurs, you must call `discard_encoding`; see+/// below.)+///+/// - Call `end_encoding` on a recording `CommandEncoder` to close the+/// encoder and construct a fresh `CommandBuffer` consisting of the+/// list of commands recorded up to that point.+///+/// - Call `discard_encoding` on a recording `CommandEncoder` to drop+/// the commands recorded thus far and close the encoder. This is+/// the only safe thing to do on a `CommandEncoder` if an error has+/// occurred while recording commands.+///+/// - Call `reset_all` on a closed `CommandEncoder`, passing all the+/// live `CommandBuffers` built from it. All the `CommandBuffer`s+/// are destroyed, and their resources are freed.+///+/// # Safety+///+/// - The `CommandEncoder` must be in the states described above to+/// make the given calls.+///+/// - A `CommandBuffer` that has been submitted for execution on the+/// GPU must live until its execution is complete.+///+/// - A `CommandBuffer` must not outlive the `CommandEncoder` that+/// built it.+///+/// It is the user's responsibility to meet this requirements. This+/// allows `CommandEncoder` implementations to keep their state+/// tracking to a minimum.+pub trait CommandEncoder: WasmNotSendSync + fmt::Debug {+ type A: Api;++ /// Begin encoding a new command buffer.+ ///+ /// This puts this `CommandEncoder` in the "recording" state.+ ///+ /// # Safety+ ///+ /// This `CommandEncoder` must be in the "closed" state.+ unsafe fn begin_encoding(&mut self, label: Label) -> Result<(), DeviceError>;++ /// Discard the command list under construction.+ ///+ /// If an error has occurred while recording commands, this+ /// is the only safe thing to do with the encoder.+ ///+ /// This puts this `CommandEncoder` in the "closed" state.+ ///+ /// # Safety+ ///+ /// This `CommandEncoder` must be in the "recording" state.+ ///+ /// Callers must not assume that implementations of this+ /// function are idempotent, and thus should not call it+ /// multiple times in a row.+ unsafe fn discard_encoding(&mut self);++ /// Return a fresh [`CommandBuffer`] holding the recorded commands.+ ///+ /// The returned [`CommandBuffer`] holds all the commands recorded+ /// on this `CommandEncoder` since the last call to+ /// [`begin_encoding`].+ ///+ /// This puts this `CommandEncoder` in the "closed" state.+ ///+ /// # Safety+ ///+ /// This `CommandEncoder` must be in the "recording" state.+ ///+ /// The returned [`CommandBuffer`] must not outlive this+ /// `CommandEncoder`. Implementations are allowed to build+ /// `CommandBuffer`s that depend on storage owned by this+ /// `CommandEncoder`.+ ///+ /// [`CommandBuffer`]: Api::CommandBuffer+ /// [`begin_encoding`]: CommandEncoder::begin_encoding+ unsafe fn end_encoding(&mut self) -> Result<<Self::A as Api>::CommandBuffer, DeviceError>;++ /// Reclaim all resources belonging to this `CommandEncoder`.+ ///+ /// # Safety+ ///+ /// This `CommandEncoder` must be in the "closed" state.+ ///+ /// The `command_buffers` iterator must produce all the live+ /// [`CommandBuffer`]s built using this `CommandEncoder` --- that+ /// is, every extant `CommandBuffer` returned from `end_encoding`.+ ///+ /// [`CommandBuffer`]: Api::CommandBuffer+ unsafe fn reset_all<I>(&mut self, command_buffers: I)+ where+ I: Iterator<Item = <Self::A as Api>::CommandBuffer>;++ unsafe fn transition_buffers<'a, T>(&mut self, barriers: T)+ where+ T: Iterator<Item = BufferBarrier<'a, <Self::A as Api>::Buffer>>;++ unsafe fn transition_textures<'a, T>(&mut self, barriers: T)+ where+ T: Iterator<Item = TextureBarrier<'a, <Self::A as Api>::Texture>>;++ // copy operations++ unsafe fn clear_buffer(&mut self, buffer: &<Self::A as Api>::Buffer, range: MemoryRange);++ unsafe fn copy_buffer_to_buffer<T>(+ &mut self,+ src: &<Self::A as Api>::Buffer,+ dst: &<Self::A as Api>::Buffer,+ regions: T,+ ) where+ T: Iterator<Item = BufferCopy>;++ /// Copy from an external image to an internal texture.+ /// Works with a single array layer.+ /// Note: `dst` current usage has to be `wgt::TextureUses::COPY_DST`.+ /// Note: the copy extent is in physical size (rounded to the block size)+ #[cfg(webgl)]+ unsafe fn copy_external_image_to_texture<T>(+ &mut self,+ src: &wgt::CopyExternalImageSourceInfo,+ dst: &<Self::A as Api>::Texture,+ dst_premultiplication: bool,+ regions: T,+ ) where+ T: Iterator<Item = TextureCopy>;++ /// Copy from one texture to another.+ /// Works with a single array layer.+ /// Note: `dst` current usage has to be `wgt::TextureUses::COPY_DST`.+ /// Note: the copy extent is in physical size (rounded to the block size)+ unsafe fn copy_texture_to_texture<T>(+ &mut self,+ src: &<Self::A as Api>::Texture,+ src_usage: wgt::TextureUses,+ dst: &<Self::A as Api>::Texture,+ regions: T,+ ) where+ T: Iterator<Item = TextureCopy>;++ /// Copy from buffer to texture.+ /// Works with a single array layer.+ /// Note: `dst` current usage has to be `wgt::TextureUses::COPY_DST`.+ /// Note: the copy extent is in physical size (rounded to the block size)+ unsafe fn copy_buffer_to_texture<T>(+ &mut self,+ src: &<Self::A as Api>::Buffer,+ dst: &<Self::A as Api>::Texture,+ regions: T,+ ) where+ T: Iterator<Item = BufferTextureCopy>;++ /// Copy from texture to buffer.+ /// Works with a single array layer.+ /// Note: the copy extent is in physical size (rounded to the block size)+ unsafe fn copy_texture_to_buffer<T>(+ &mut self,+ src: &<Self::A as Api>::Texture,+ src_usage: wgt::TextureUses,+ dst: &<Self::A as Api>::Buffer,+ regions: T,+ ) where+ T: Iterator<Item = BufferTextureCopy>;++ unsafe fn copy_acceleration_structure_to_acceleration_structure(+ &mut self,+ src: &<Self::A as Api>::AccelerationStructure,+ dst: &<Self::A as Api>::AccelerationStructure,+ copy: wgt::AccelerationStructureCopy,+ );+ // pass common++ /// Sets the bind group at `index` to `group`.+ ///+ /// If this is not the first call to `set_bind_group` within the current+ /// render or compute pass:+ ///+ /// - If `layout` contains `n` bind group layouts, then any previously set+ /// bind groups at indices `n` or higher are cleared.+ ///+ /// - If the first `m` bind group layouts of `layout` are equal to those of+ /// the previously passed layout, but no more, then any previously set+ /// bind groups at indices `m` or higher are cleared.+ ///+ /// It follows from the above that passing the same layout as before doesn't+ /// clear any bind groups.+ ///+ /// # Safety+ ///+ /// - This [`CommandEncoder`] must be within a render or compute pass.+ ///+ /// - `index` must be the valid index of some bind group layout in `layout`.+ /// Call this the "relevant bind group layout".+ ///+ /// - The layout of `group` must be equal to the relevant bind group layout.+ ///+ /// - The length of `dynamic_offsets` must match the number of buffer+ /// bindings [with dynamic offsets][hdo] in the relevant bind group+ /// layout.+ ///+ /// - If those buffer bindings are ordered by increasing [`binding` number]+ /// and paired with elements from `dynamic_offsets`, then each offset must+ /// be a valid offset for the binding's corresponding buffer in `group`.+ ///+ /// [hdo]: wgt::BindingType::Buffer::has_dynamic_offset+ /// [`binding` number]: wgt::BindGroupLayoutEntry::binding+ unsafe fn set_bind_group(+ &mut self,+ layout: &<Self::A as Api>::PipelineLayout,+ index: u32,+ group: &<Self::A as Api>::BindGroup,+ dynamic_offsets: &[wgt::DynamicOffset],+ );++ /// Sets a range in immediate data.+ ///+ /// IMPORTANT: while the data is passed as words, the offset is in bytes!+ ///+ /// # Safety+ ///+ /// - `offset_bytes` must be a multiple of 4.+ /// - The range of immediates written must be valid for the pipeline layout at draw time.+ unsafe fn set_immediates(+ &mut self,+ layout: &<Self::A as Api>::PipelineLayout,+ offset_bytes: u32,+ data: &[u32],+ );++ unsafe fn insert_debug_marker(&mut self, label: &str);+ unsafe fn begin_debug_marker(&mut self, group_label: &str);+ unsafe fn end_debug_marker(&mut self);++ // queries++ /// # Safety:+ ///+ /// - If `set` is an occlusion query set, it must be the same one as used in the [`RenderPassDescriptor::occlusion_query_set`] parameter.+ unsafe fn begin_query(&mut self, set: &<Self::A as Api>::QuerySet, index: u32);+ /// # Safety:+ ///+ /// - If `set` is an occlusion query set, it must be the same one as used in the [`RenderPassDescriptor::occlusion_query_set`] parameter.+ unsafe fn end_query(&mut self, set: &<Self::A as Api>::QuerySet, index: u32);+ unsafe fn write_timestamp(&mut self, set: &<Self::A as Api>::QuerySet, index: u32);+ unsafe fn reset_queries(&mut self, set: &<Self::A as Api>::QuerySet, range: Range<u32>);+ unsafe fn copy_query_results(+ &mut self,+ set: &<Self::A as Api>::QuerySet,+ range: Range<u32>,+ buffer: &<Self::A as Api>::Buffer,+ offset: wgt::BufferAddress,+ stride: wgt::BufferSize,+ );++ // render passes++ /// Begin a new render pass, clearing all active bindings.+ ///+ /// This clears any bindings established by the following calls:+ ///+ /// - [`set_bind_group`](CommandEncoder::set_bind_group)+ /// - [`set_immediates`](CommandEncoder::set_immediates)+ /// - [`begin_query`](CommandEncoder::begin_query)+ /// - [`set_render_pipeline`](CommandEncoder::set_render_pipeline)+ /// - [`set_index_buffer`](CommandEncoder::set_index_buffer)+ /// - [`set_vertex_buffer`](CommandEncoder::set_vertex_buffer)+ ///+ /// # Safety+ ///+ /// - All prior calls to [`begin_render_pass`] on this [`CommandEncoder`] must have been followed+ /// by a call to [`end_render_pass`].+ ///+ /// - All prior calls to [`begin_compute_pass`] on this [`CommandEncoder`] must have been followed+ /// by a call to [`end_compute_pass`].+ ///+ /// - All prior calls to [`begin_ray_tracing_pass`] on this [`CommandEncoder`] must have been followed+ /// by a call to [`end_ray_tracing_pass`].+ ///+ /// [`begin_render_pass`]: CommandEncoder::begin_render_pass+ /// [`begin_compute_pass`]: CommandEncoder::begin_compute_pass+ /// [`begin_ray_tracing_pass`]: CommandEncoder::begin_ray_tracing_pass+ /// [`end_render_pass`]: CommandEncoder::end_render_pass+ /// [`end_compute_pass`]: CommandEncoder::end_compute_pass+ /// [`end_ray_tracing_pass`]: CommandEncoder::end_ray_tracing_pass+ unsafe fn begin_render_pass(+ &mut self,+ desc: &RenderPassDescriptor<<Self::A as Api>::QuerySet, <Self::A as Api>::TextureView>,+ ) -> Result<(), DeviceError>;++ /// End the current render pass.+ ///+ /// # Safety+ ///+ /// - There must have been a prior call to [`begin_render_pass`] on this [`CommandEncoder`]+ /// that has not been followed by a call to [`end_render_pass`].+ ///+ /// [`begin_render_pass`]: CommandEncoder::begin_render_pass+ /// [`end_render_pass`]: CommandEncoder::end_render_pass+ unsafe fn end_render_pass(&mut self);++ unsafe fn set_render_pipeline(&mut self, pipeline: &<Self::A as Api>::RenderPipeline);++ unsafe fn set_index_buffer<'a>(+ &mut self,+ binding: BufferBinding<'a, <Self::A as Api>::Buffer>,+ format: wgt::IndexFormat,+ );+ unsafe fn set_vertex_buffer<'a>(+ &mut self,+ index: u32,+ binding: BufferBinding<'a, <Self::A as Api>::Buffer>,+ );+ unsafe fn set_viewport(&mut self, rect: &Rect<f32>, depth_range: Range<f32>);+ unsafe fn set_scissor_rect(&mut self, rect: &Rect<u32>);+ unsafe fn set_stencil_reference(&mut self, value: u32);+ unsafe fn set_blend_constants(&mut self, color: &[f32; 4]);++ unsafe fn draw(+ &mut self,+ first_vertex: u32,+ vertex_count: u32,+ first_instance: u32,+ instance_count: u32,+ );+ unsafe fn draw_indexed(+ &mut self,+ first_index: u32,+ index_count: u32,+ base_vertex: i32,+ first_instance: u32,+ instance_count: u32,+ );+ unsafe fn draw_indirect(+ &mut self,+ buffer: &<Self::A as Api>::Buffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ );+ unsafe fn draw_indexed_indirect(+ &mut self,+ buffer: &<Self::A as Api>::Buffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ );+ unsafe fn draw_indirect_count(+ &mut self,+ buffer: &<Self::A as Api>::Buffer,+ offset: wgt::BufferAddress,+ count_buffer: &<Self::A as Api>::Buffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ );+ unsafe fn draw_indexed_indirect_count(+ &mut self,+ buffer: &<Self::A as Api>::Buffer,+ offset: wgt::BufferAddress,+ count_buffer: &<Self::A as Api>::Buffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ );+ unsafe fn draw_mesh_tasks(+ &mut self,+ group_count_x: u32,+ group_count_y: u32,+ group_count_z: u32,+ );+ unsafe fn draw_mesh_tasks_indirect(+ &mut self,+ buffer: &<Self::A as Api>::Buffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ );+ unsafe fn draw_mesh_tasks_indirect_count(+ &mut self,+ buffer: &<Self::A as Api>::Buffer,+ offset: wgt::BufferAddress,+ count_buffer: &<Self::A as Api>::Buffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ );++ // compute passes++ /// Begin a new compute pass, clearing all active bindings.+ ///+ /// This clears any bindings established by the following calls:+ ///+ /// - [`set_bind_group`](CommandEncoder::set_bind_group)+ /// - [`set_immediates`](CommandEncoder::set_immediates)+ /// - [`begin_query`](CommandEncoder::begin_query)+ /// - [`set_compute_pipeline`](CommandEncoder::set_compute_pipeline)+ ///+ /// # Safety+ ///+ /// - All prior calls to [`begin_render_pass`] on this [`CommandEncoder`] must have been followed+ /// by a call to [`end_render_pass`].+ ///+ /// - All prior calls to [`begin_compute_pass`] on this [`CommandEncoder`] must have been followed+ /// by a call to [`end_compute_pass`].+ ///+ /// - All prior calls to [`begin_ray_tracing_pass`] on this [`CommandEncoder`] must have been followed+ /// by a call to [`end_ray_tracing_pass`].+ ///+ /// [`begin_render_pass`]: CommandEncoder::begin_render_pass+ /// [`begin_compute_pass`]: CommandEncoder::begin_compute_pass+ /// [`begin_ray_tracing_pass`]: CommandEncoder::begin_ray_tracing_pass+ /// [`end_render_pass`]: CommandEncoder::end_render_pass+ /// [`end_compute_pass`]: CommandEncoder::end_compute_pass+ /// [`end_ray_tracing_pass`]: CommandEncoder::end_ray_tracing_pass+ unsafe fn begin_compute_pass(+ &mut self,+ desc: &ComputePassDescriptor<<Self::A as Api>::QuerySet>,+ );++ /// End the current compute pass.+ ///+ /// # Safety+ ///+ /// - There must have been a prior call to [`begin_compute_pass`] on this [`CommandEncoder`]+ /// that has not been followed by a call to [`end_compute_pass`].+ ///+ /// [`begin_compute_pass`]: CommandEncoder::begin_compute_pass+ /// [`end_compute_pass`]: CommandEncoder::end_compute_pass+ unsafe fn end_compute_pass(&mut self);++ unsafe fn set_compute_pipeline(&mut self, pipeline: &<Self::A as Api>::ComputePipeline);++ unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]);+ unsafe fn dispatch_workgroups_indirect(+ &mut self,+ buffer: &<Self::A as Api>::Buffer,+ offset: wgt::BufferAddress,+ );++ /// Begin a new ray tracing pass, clearing all active bindings.+ ///+ /// This clears any bindings established by the following calls:+ ///+ /// - [`set_bind_group`](CommandEncoder::set_bind_group)+ /// - [`set_immediates`](CommandEncoder::set_immediates)+ /// - [`begin_query`](CommandEncoder::begin_query)+ /// - [`set_ray_tracing_pipeline`](CommandEncoder::set_compute_pipeline)+ ///+ /// # Safety+ ///+ /// - All prior calls to [`begin_render_pass`] on this [`CommandEncoder`] must have been followed+ /// by a call to [`end_render_pass`].+ ///+ /// - All prior calls to [`begin_compute_pass`] on this [`CommandEncoder`] must have been followed+ /// by a call to [`end_compute_pass`].+ ///+ /// - All prior calls to [`begin_ray_tracing_pass`] on this [`CommandEncoder`] must have been followed+ /// by a call to [`end_ray_tracing_pass`].+ ///+ /// [`begin_render_pass`]: CommandEncoder::begin_render_pass+ /// [`begin_compute_pass`]: CommandEncoder::begin_compute_pass+ /// [`begin_ray_tracing_pass`]: CommandEncoder::begin_ray_tracing_pass+ /// [`end_render_pass`]: CommandEncoder::end_render_pass+ /// [`end_compute_pass`]: CommandEncoder::end_compute_pass+ /// [`end_ray_tracing_pass`]: CommandEncoder::end_ray_tracing_pass+ unsafe fn begin_ray_tracing_pass(&mut self, desc: &RayTracingPassDescriptor);++ /// End the current compute pass.+ ///+ /// # Safety+ ///+ /// - There must have been a prior call to [`begin_ray_tracing_pass`] on this [`CommandEncoder`]+ /// that has not been followed by a call to [`end_ray_tracing_pass`].+ ///+ /// [`begin_ray_tracing_pass`]: CommandEncoder::begin_ray_tracing_pass+ /// [`end_ray_tracing_pass`]: CommandEncoder::end_ray_tracing_pass+ unsafe fn end_ray_tracing_pass(&mut self);++ /// # Safety+ ///+ /// - Pipeline must not be destroyed+ unsafe fn set_ray_tracing_pipeline(&mut self, pipeline: &<Self::A as Api>::RayTracingPipeline);++ unsafe fn trace_rays<'a>(+ &mut self,+ count: [u32; 3],+ ray_generation_group_data: PipelineGroupData<'a, <Self::A as Api>::Buffer>,+ miss_group_data: PipelineGroupData<'a, <Self::A as Api>::Buffer>,+ intersection_group_data: PipelineGroupData<'a, <Self::A as Api>::Buffer>,+ );++ /// To get the required sizes for the buffer allocations use `get_acceleration_structure_build_sizes` per descriptor+ /// All buffers must be synchronized externally+ /// All buffer regions, which are written to may only be passed once per function call,+ /// with the exception of updates in the same descriptor.+ /// Consequences of this limitation:+ /// - scratch buffers need to be unique+ /// - a tlas can't be build in the same call with a blas it contains+ unsafe fn build_acceleration_structures<'a, T>(+ &mut self,+ descriptor_count: u32,+ descriptors: T,+ ) where+ Self::A: 'a,+ T: IntoIterator<+ Item = BuildAccelerationStructureDescriptor<+ 'a,+ <Self::A as Api>::Buffer,+ <Self::A as Api>::AccelerationStructure,+ >,+ >;+ unsafe fn place_acceleration_structure_barrier(+ &mut self,+ barrier: AccelerationStructureBarrier,+ );+ // modeled off dx12, because this is able to be polyfilled in vulkan as opposed to the other way round+ unsafe fn read_acceleration_structure_compact_size(+ &mut self,+ acceleration_structure: &<Self::A as Api>::AccelerationStructure,+ buf: &<Self::A as Api>::Buffer,+ );+ unsafe fn set_acceleration_structure_dependencies(+ command_buffers: &[&<Self::A as Api>::CommandBuffer],+ dependencies: &[&<Self::A as Api>::AccelerationStructure],+ );+}++bitflags!(+ /// Pipeline layout creation flags.+ #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]+ pub struct PipelineLayoutFlags: u32 {+ /// D3D12: Add support for `first_vertex` and `first_instance` builtins+ /// via immediates for direct execution.+ const FIRST_VERTEX_INSTANCE = 1 << 0;+ /// D3D12: Add support for `num_workgroups` builtins via immediates+ /// for direct execution.+ const NUM_WORK_GROUPS = 1 << 1;+ /// D3D12: Add support for the builtins that the other flags enable for+ /// indirect execution.+ const INDIRECT_BUILTIN_UPDATE = 1 << 2;+ }+);++bitflags!(+ /// Pipeline layout creation flags.+ #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]+ pub struct BindGroupLayoutFlags: u32 {+ /// Allows for bind group binding arrays to be shorter than the array in the BGL.+ const PARTIALLY_BOUND = 1 << 0;+ }+);++bitflags!(+ /// Texture format capability flags.+ #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]+ pub struct TextureFormatCapabilities: u32 {+ /// Format can be sampled.+ const SAMPLED = 1 << 0;+ /// Format can be sampled with a linear sampler.+ const SAMPLED_LINEAR = 1 << 1;+ /// Format can be sampled with a min/max reduction sampler.+ const SAMPLED_MINMAX = 1 << 2;++ /// Format can be used as storage with read-only access.+ const STORAGE_READ_ONLY = 1 << 3;+ /// Format can be used as storage with write-only access.+ const STORAGE_WRITE_ONLY = 1 << 4;+ /// Format can be used as storage with both read and write access.+ const STORAGE_READ_WRITE = 1 << 5;+ /// Format can be used as storage with atomics.+ const STORAGE_ATOMIC = 1 << 6;++ /// Format can be used as color and input attachment.+ const COLOR_ATTACHMENT = 1 << 7;+ /// Format can be used as color (with blending) and input attachment.+ const COLOR_ATTACHMENT_BLEND = 1 << 8;+ /// Format can be used as depth-stencil and input attachment.+ const DEPTH_STENCIL_ATTACHMENT = 1 << 9;++ /// Format can be multisampled by x2.+ const MULTISAMPLE_X2 = 1 << 10;+ /// Format can be multisampled by x4.+ const MULTISAMPLE_X4 = 1 << 11;+ /// Format can be multisampled by x8.+ const MULTISAMPLE_X8 = 1 << 12;+ /// Format can be multisampled by x16.+ const MULTISAMPLE_X16 = 1 << 13;++ /// Format can be used for render pass resolve targets.+ const MULTISAMPLE_RESOLVE = 1 << 14;++ /// Format can be copied from.+ const COPY_SRC = 1 << 15;+ /// Format can be copied to.+ const COPY_DST = 1 << 16;+ }+);++bitflags!(+ /// Texture format capability flags.+ #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]+ pub struct FormatAspects: u8 {+ const COLOR = 1 << 0;+ const DEPTH = 1 << 1;+ const STENCIL = 1 << 2;+ const PLANE_0 = 1 << 3;+ const PLANE_1 = 1 << 4;+ const PLANE_2 = 1 << 5;++ const DEPTH_STENCIL = Self::DEPTH.bits() | Self::STENCIL.bits();+ }+);++impl FormatAspects {+ pub fn new(format: wgt::TextureFormat, aspect: wgt::TextureAspect) -> Self {+ let aspect_mask = match aspect {+ wgt::TextureAspect::All => Self::all(),+ wgt::TextureAspect::DepthOnly => Self::DEPTH,+ wgt::TextureAspect::StencilOnly => Self::STENCIL,+ wgt::TextureAspect::Plane0 => Self::PLANE_0,+ wgt::TextureAspect::Plane1 => Self::PLANE_1,+ wgt::TextureAspect::Plane2 => Self::PLANE_2,+ };+ Self::from(format) & aspect_mask+ }++ /// Returns `true` if only one flag is set+ pub fn is_one(&self) -> bool {+ self.bits().is_power_of_two()+ }++ pub fn map(&self) -> wgt::TextureAspect {+ match *self {+ Self::COLOR => wgt::TextureAspect::All,+ Self::DEPTH => wgt::TextureAspect::DepthOnly,+ Self::STENCIL => wgt::TextureAspect::StencilOnly,+ Self::PLANE_0 => wgt::TextureAspect::Plane0,+ Self::PLANE_1 => wgt::TextureAspect::Plane1,+ Self::PLANE_2 => wgt::TextureAspect::Plane2,+ _ => unreachable!(),+ }+ }+}++impl From<wgt::TextureFormat> for FormatAspects {+ fn from(format: wgt::TextureFormat) -> Self {+ match format {+ wgt::TextureFormat::Stencil8 => Self::STENCIL,+ wgt::TextureFormat::Depth16Unorm+ | wgt::TextureFormat::Depth32Float+ | wgt::TextureFormat::Depth24Plus => Self::DEPTH,+ wgt::TextureFormat::Depth32FloatStencil8 | wgt::TextureFormat::Depth24PlusStencil8 => {+ Self::DEPTH_STENCIL+ }+ wgt::TextureFormat::NV12 | wgt::TextureFormat::P010 => Self::PLANE_0 | Self::PLANE_1,+ _ => Self::COLOR,+ }+ }+}++bitflags!(+ #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]+ pub struct MemoryFlags: u32 {+ const TRANSIENT = 1 << 0;+ const PREFER_COHERENT = 1 << 1;+ }+);++bitflags!(+ /// Attachment load and store operations.+ ///+ /// There must be at least one flag from the LOAD group and one from the STORE group set.+ #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]+ pub struct AttachmentOps: u8 {+ /// Load the existing contents of the attachment.+ const LOAD = 1 << 0;+ /// Clear the attachment to a specified value.+ const LOAD_CLEAR = 1 << 1;+ /// The contents of the attachment are undefined.+ const LOAD_DONT_CARE = 1 << 2;+ /// Store the contents of the attachment.+ const STORE = 1 << 3;+ /// The contents of the attachment are undefined after the pass.+ const STORE_DISCARD = 1 << 4;+ }+);++#[derive(Debug)]+pub struct InstanceDescriptor<'a> {+ pub name: &'a str,+ pub flags: wgt::InstanceFlags,+ pub memory_budget_thresholds: wgt::MemoryBudgetThresholds,+ pub backend_options: wgt::BackendOptions,+ pub telemetry: Option<Telemetry>,+ /// This is a borrow because the surrounding `core::Instance` keeps the owned display handle+ /// alive already.+ pub display: Option<DisplayHandle<'a>>,+}++#[derive(Clone, Debug)]+pub struct Alignments {+ /// The alignment of the start of the buffer used as a GPU copy source.+ pub buffer_copy_offset: wgt::BufferSize,++ /// The alignment of the row pitch of the texture data stored in a buffer that is+ /// used in a GPU copy operation.+ pub buffer_copy_pitch: wgt::BufferSize,++ /// The finest alignment of bound range checking for uniform buffers.+ ///+ /// When `wgpu_hal` restricts shader references to the [accessible+ /// region][ar] of a [`Uniform`] buffer, the size of the accessible region+ /// is the bind group binding's stated [size], rounded up to the next+ /// multiple of this value.+ ///+ /// We don't need an analogous field for storage buffer bindings, because+ /// all our backends promise to enforce the size at least to a four-byte+ /// alignment, and `wgpu_hal` requires bound range lengths to be a multiple+ /// of four anyway.+ ///+ /// [ar]: struct.BufferBinding.html#accessible-region+ /// [`Uniform`]: wgt::BufferBindingType::Uniform+ /// [size]: BufferBinding::size+ pub uniform_bounds_check_alignment: wgt::BufferSize,++ /// The size of the raw TLAS instance+ pub raw_tlas_instance_size: u32,++ /// What the scratch buffer for building an acceleration structure must be aligned to+ pub ray_tracing_scratch_buffer_alignment: u32,++ /// How large a single piece of group data is. That is, how large the vector returned+ /// from `device.get_raytracing_pipeline_group_data(&pipeline, n..(n+1))` is.+ ///+ /// If ray tracing pipelines are implemented, this must be non zero.+ pub ray_tracing_pipeline_group_data_size: u32,++ /// If ray tracing pipelines are implemented, this must be a power of two (and non zero).+ pub ray_tracing_pipeline_group_data_alignment: u32,++ /// If ray tracing pipelines are implemented, this must be a power of two (and non zero).+ ///+ /// The offset within `PipelineGroupData` must be a multiple of this+ pub ray_tracing_pipeline_data_offset_alignment: u32,+}++#[derive(Clone, Debug)]+pub struct Capabilities {+ pub limits: wgt::Limits,+ pub alignments: Alignments,+ pub downlevel: wgt::DownlevelCapabilities,+ /// Supported cooperative matrix configurations.+ ///+ /// Empty if cooperative matrices are not supported.+ pub cooperative_matrix_properties: Vec<wgt::CooperativeMatrixProperties>,+}++/// An adapter with all the information needed to reason about its capabilities.+///+/// These are either made by [`Instance::enumerate_adapters`] or by backend specific+/// methods on the backend [`Instance`] or [`Adapter`].+#[derive(Debug)]+pub struct ExposedAdapter<A: Api> {+ pub adapter: A::Adapter,+ pub info: wgt::AdapterInfo,+ pub features: wgt::Features,+ pub capabilities: Capabilities,+}++/// Describes information about what a `Surface`'s presentation capabilities are.+/// Fetch this with [Adapter::surface_capabilities].+#[derive(Debug, Clone)]+pub struct SurfaceCapabilities {+ /// List of supported texture formats together with the color spaces+ /// supported for each format.+ ///+ /// Must be at least one. At most one entry per format, each with a+ /// non-empty set of color spaces.+ pub formats: Vec<wgt::SurfaceFormatCapabilities>,++ /// Range for the number of queued frames.+ ///+ /// This adjusts either the swapchain frame count to value + 1 - or sets SetMaximumFrameLatency to the value given,+ /// or uses a wait-for-present in the acquire method to limit rendering such that it acts like it's a value + 1 swapchain frame set.+ ///+ /// - `maximum_frame_latency.start` must be at least 1.+ /// - `maximum_frame_latency.end` must be larger or equal to `maximum_frame_latency.start`.+ pub maximum_frame_latency: RangeInclusive<u32>,++ /// Current extent of the surface, if known.+ pub current_extent: Option<wgt::Extent3d>,++ /// Supported texture usage flags.+ ///+ /// Must have at least `wgt::TextureUses::COLOR_TARGET`+ pub usage: wgt::TextureUses,++ /// List of supported V-sync modes.+ ///+ /// Must be at least one.+ pub present_modes: Vec<wgt::PresentMode>,++ /// List of supported alpha composition modes.+ ///+ /// Must be at least one.+ pub composite_alpha_modes: Vec<wgt::CompositeAlphaMode>,+}++impl SurfaceCapabilities {+ /// Returns the supported texture formats, dropping the per-format color-space+ /// information carried in [`Self::formats`].+ pub fn texture_formats(&self) -> impl Iterator<Item = wgt::TextureFormat> + '_ {+ self.formats.iter().map(|fc| fc.format)+ }+}++#[derive(Debug)]+pub struct AcquiredSurfaceTexture<A: Api> {+ pub texture: A::SurfaceTexture,+ /// The presentation configuration no longer matches+ /// the surface properties exactly, but can still be used to present+ /// to the surface successfully.+ pub suboptimal: bool,+}++/// An open connection to a device and a queue.+///+/// This can be created from [`Adapter::open`] or backend+/// specific methods on the backend's [`Instance`] or [`Adapter`].+#[derive(Debug)]+pub struct OpenDevice<A: Api> {+ pub device: A::Device,+ pub queue: A::Queue,+}++#[derive(Clone, Debug)]+pub struct BufferMapping {+ pub ptr: NonNull<u8>,+ pub is_coherent: bool,+}++#[derive(Clone, Debug)]+pub struct BufferDescriptor<'a> {+ pub label: Label<'a>,+ pub size: wgt::BufferAddress,+ pub usage: wgt::BufferUses,+ pub memory_flags: MemoryFlags,+}++#[derive(Clone, Debug)]+pub struct TextureDescriptor<'a> {+ pub label: Label<'a>,+ pub size: wgt::Extent3d,+ pub mip_level_count: u32,+ pub sample_count: u32,+ pub dimension: wgt::TextureDimension,+ pub format: wgt::TextureFormat,+ pub usage: wgt::TextureUses,+ pub memory_flags: MemoryFlags,+ /// Allows views of this texture to have a different format+ /// than the texture does.+ pub view_formats: Vec<wgt::TextureFormat>,+}++impl TextureDescriptor<'_> {+ pub fn copy_extent(&self) -> CopyExtent {+ CopyExtent::map_extent_to_copy_size(&self.size, self.dimension)+ }++ pub fn is_cube_compatible(&self) -> bool {+ self.dimension == wgt::TextureDimension::D2+ && self.size.depth_or_array_layers.is_multiple_of(6)+ && self.sample_count == 1+ && self.size.width == self.size.height+ }++ pub fn array_layer_count(&self) -> u32 {+ match self.dimension {+ wgt::TextureDimension::D1 | wgt::TextureDimension::D3 => 1,+ wgt::TextureDimension::D2 => self.size.depth_or_array_layers,+ }+ }+}++/// TextureView descriptor.+///+/// Valid usage:+///. - `format` has to be the same as `TextureDescriptor::format`+///. - `dimension` has to be compatible with `TextureDescriptor::dimension`+///. - `usage` has to be a subset of `TextureDescriptor::usage`+///. - `range` has to be a subset of parent texture+#[derive(Clone, Debug)]+pub struct TextureViewDescriptor<'a> {+ pub label: Label<'a>,+ pub format: wgt::TextureFormat,+ pub dimension: wgt::TextureViewDimension,+ pub usage: wgt::TextureUses,+ pub range: wgt::ImageSubresourceRange,+}++#[derive(Clone, Debug)]+pub struct SamplerDescriptor<'a> {+ pub label: Label<'a>,+ pub address_modes: [wgt::AddressMode; 3],+ pub mag_filter: wgt::FilterMode,+ pub min_filter: wgt::FilterMode,+ pub mipmap_filter: wgt::MipmapFilterMode,+ pub lod_clamp: Range<f32>,+ pub compare: Option<wgt::CompareFunction>,+ // Must in the range [1, 16].+ //+ // Anisotropic filtering must be supported if this is not 1.+ pub anisotropy_clamp: u16,+ pub border_color: Option<wgt::SamplerBorderColor>,+}++/// BindGroupLayout descriptor.+///+/// Valid usage:+/// - `entries` are sorted by ascending `wgt::BindGroupLayoutEntry::binding`+#[derive(Clone, Debug)]+pub struct BindGroupLayoutDescriptor<'a> {+ pub label: Label<'a>,+ pub flags: BindGroupLayoutFlags,+ pub entries: &'a [wgt::BindGroupLayoutEntry],+}++#[derive(Clone, Debug)]+pub struct PipelineLayoutDescriptor<'a, B: DynBindGroupLayout + ?Sized> {+ pub label: Label<'a>,+ pub flags: PipelineLayoutFlags,+ pub bind_group_layouts: &'a [Option<&'a B>],+ pub immediate_size: u32,+}++/// A region of a buffer made visible to shaders via a [`BindGroup`].+///+/// [`BindGroup`]: Api::BindGroup+///+/// ## Construction+///+/// The recommended way to construct a `BufferBinding` is using the `binding`+/// method on a wgpu-core `Buffer`, which will validate the binding size+/// against the buffer size. A `new_unchecked` constructor is also provided for+/// cases where direct construction is necessary.+///+/// ## Accessible region+///+/// `wgpu_hal` guarantees that shaders compiled with+/// [`ShaderModuleDescriptor::runtime_checks`] set to `true` cannot read or+/// write data via this binding outside the *accessible region* of a buffer:+///+/// - The accessible region starts at [`offset`].+///+/// - For [`Storage`] bindings, the size of the accessible region is [`size`],+/// which must be a multiple of 4.+///+/// - For [`Uniform`] bindings, the size of the accessible region is [`size`]+/// rounded up to the next multiple of+/// [`Alignments::uniform_bounds_check_alignment`].+///+/// Note that this guarantee is stricter than WGSL's requirements for+/// [out-of-bounds accesses][woob], as WGSL allows them to return values from+/// elsewhere in the buffer. But this guarantee is necessary anyway, to permit+/// `wgpu-core` to avoid clearing uninitialized regions of buffers that will+/// never be read by the application before they are overwritten. This+/// optimization consults bind group buffer binding regions to determine which+/// parts of which buffers shaders might observe. This optimization is only+/// sound if shader access is bounds-checked.+///+/// ## Zero-length bindings+///+/// Some back ends cannot tolerate zero-length regions; for example, see+/// [VUID-VkDescriptorBufferInfo-offset-00340][340] and+/// [VUID-VkDescriptorBufferInfo-range-00341][341], or the+/// documentation for GLES's [glBindBufferRange][bbr]. This documentation+/// previously stated that a `BufferBinding` must have `offset` strictly less+/// than the size of the buffer, but this restriction was not honored elsewhere+/// in the code, so has been removed. However, it remains the case that+/// some backends do not support zero-length bindings, so additional+/// logic is needed somewhere to handle this properly. See+/// [#3170](https://github.com/gfx-rs/wgpu/issues/3170).+///+/// [`offset`]: BufferBinding::offset+/// [`size`]: BufferBinding::size+/// [`Storage`]: wgt::BufferBindingType::Storage+/// [`Uniform`]: wgt::BufferBindingType::Uniform+/// [340]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#VUID-VkDescriptorBufferInfo-offset-00340+/// [341]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#VUID-VkDescriptorBufferInfo-range-00341+/// [bbr]: https://registry.khronos.org/OpenGL-Refpages/es3.0/html/glBindBufferRange.xhtml+/// [woob]: https://gpuweb.github.io/gpuweb/wgsl/#out-of-bounds-access-sec+#[derive(Debug)]+pub struct BufferBinding<'a, B: DynBuffer + ?Sized> {+ /// The buffer being bound.+ ///+ /// This is not fully `pub` to prevent direct construction of+ /// `BufferBinding`s, while still allowing public read access to the `offset`+ /// and `size` properties.+ pub(crate) buffer: &'a B,++ /// The offset at which the bound region starts.+ ///+ /// This must be less or equal to the size of the buffer.+ pub offset: wgt::BufferAddress,++ /// The size of the region bound, in bytes.+ ///+ /// If `None`, the region extends from `offset` to the end of the+ /// buffer. Given the restrictions on `offset`, this means that+ /// the size is always greater than zero.+ pub size: Option<wgt::BufferSize>,+}++// We must implement this manually because `B` is not necessarily `Clone`.+impl<B: DynBuffer + ?Sized> Clone for BufferBinding<'_, B> {+ fn clone(&self) -> Self {+ BufferBinding {+ buffer: self.buffer,+ offset: self.offset,+ size: self.size,+ }+ }+}++/// Temporary convenience trait to let us call `.get()` on `u64`s in code that+/// really wants to be using `NonZeroU64`.+/// TODO(<https://github.com/gfx-rs/wgpu/issues/3170>): remove this+pub trait ShouldBeNonZeroExt {+ fn get(&self) -> u64;+}++impl ShouldBeNonZeroExt for NonZeroU64 {+ fn get(&self) -> u64 {+ NonZeroU64::get(*self)+ }+}++impl ShouldBeNonZeroExt for u64 {+ fn get(&self) -> u64 {+ *self+ }+}++impl ShouldBeNonZeroExt for Option<NonZeroU64> {+ fn get(&self) -> u64 {+ match *self {+ Some(non_zero) => non_zero.get(),+ None => 0,+ }+ }+}++impl<'a, B: DynBuffer + ?Sized> BufferBinding<'a, B> {+ /// Construct a `BufferBinding` with the given contents.+ ///+ /// When possible, use the `binding` method on a wgpu-core `Buffer` instead+ /// of this method. `Buffer::binding` validates the size of the binding+ /// against the size of the buffer.+ ///+ /// It is more difficult to provide a validating constructor here, due to+ /// not having direct access to the size of a `DynBuffer`.+ ///+ /// SAFETY: The caller is responsible for ensuring that a binding of `size`+ /// bytes starting at `offset` is contained within the buffer.+ ///+ /// The `S` type parameter is a temporary convenience to allow callers to+ /// pass a zero size. When the zero-size binding issue is resolved, the+ /// argument should just match the type of the member.+ /// TODO(<https://github.com/gfx-rs/wgpu/issues/3170>): remove the parameter+ pub fn new_unchecked<S: Into<Option<NonZeroU64>>>(+ buffer: &'a B,+ offset: wgt::BufferAddress,+ size: S,+ ) -> Self {+ Self {+ buffer,+ offset,+ size: size.into(),+ }+ }+}++#[derive(Debug)]+pub struct TextureBinding<'a, T: DynTextureView + ?Sized> {+ pub view: &'a T,+ pub usage: wgt::TextureUses,+}++impl<'a, T: DynTextureView + ?Sized> Clone for TextureBinding<'a, T> {+ fn clone(&self) -> Self {+ TextureBinding {+ view: self.view,+ usage: self.usage,+ }+ }+}++#[derive(Debug)]+pub struct ExternalTextureBinding<'a, B: DynBuffer + ?Sized, T: DynTextureView + ?Sized> {+ pub planes: [TextureBinding<'a, T>; 3],+ pub params: BufferBinding<'a, B>,+}++impl<'a, B: DynBuffer + ?Sized, T: DynTextureView + ?Sized> Clone+ for ExternalTextureBinding<'a, B, T>+{+ fn clone(&self) -> Self {+ ExternalTextureBinding {+ planes: self.planes.clone(),+ params: self.params.clone(),+ }+ }+}++/// cbindgen:ignore+#[derive(Clone, Debug)]+pub struct BindGroupEntry {+ pub binding: u32,+ pub resource_index: u32,+ pub count: u32,+}++/// BindGroup descriptor.+///+/// Valid usage:+///. - `entries` has to be sorted by ascending `BindGroupEntry::binding`+///. - `entries` has to have the same set of `BindGroupEntry::binding` as `layout`+///. - each entry has to be compatible with the `layout`+///. - each entry's `BindGroupEntry::resource_index` is within range+/// of the corresponding resource array, selected by the relevant+/// `BindGroupLayoutEntry`.+#[derive(Clone, Debug)]+pub struct BindGroupDescriptor<+ 'a,+ Bgl: DynBindGroupLayout + ?Sized,+ B: DynBuffer + ?Sized,+ S: DynSampler + ?Sized,+ T: DynTextureView + ?Sized,+ A: DynAccelerationStructure + ?Sized,+> {+ pub label: Label<'a>,+ pub layout: &'a Bgl,+ pub buffers: &'a [BufferBinding<'a, B>],+ pub samplers: &'a [&'a S],+ pub textures: &'a [TextureBinding<'a, T>],+ pub entries: &'a [BindGroupEntry],+ pub acceleration_structures: &'a [&'a A],+ pub external_textures: &'a [ExternalTextureBinding<'a, B, T>],+}++#[derive(Clone, Debug)]+pub struct CommandEncoderDescriptor<'a, Q: DynQueue + ?Sized> {+ pub label: Label<'a>,+ pub queue: &'a Q,+}++/// Naga shader module.+#[derive(Default)]+pub struct NagaShader {+ /// Shader module IR.+ pub module: Cow<'static, naga::Module>,+ /// Analysis information of the module.+ pub info: naga::valid::ModuleInfo,+ /// Source codes for debug+ pub debug_source: Option<DebugSource>,+}++// Custom implementation avoids the need to generate Debug impl code+// for the whole Naga module and info.+impl fmt::Debug for NagaShader {+ fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {+ write!(formatter, "Naga shader")+ }+}++/// Shader input.+pub enum ShaderInput<'a> {+ Naga(NagaShader),+ MetalLib {+ file: &'a [u8],+ num_workgroups: hashbrown::HashMap<String, (u32, u32, u32)>,+ },+ Msl {+ shader: &'a str,+ num_workgroups: hashbrown::HashMap<String, (u32, u32, u32)>,+ },+ SpirV(&'a [u32]),+ Dxil {+ shader: &'a [u8],+ },+ Hlsl {+ shader: &'a str,+ },+ Glsl {+ shader: &'a str,+ },+}++impl fmt::Debug for ShaderInput<'_> {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ match self {+ // Don't include the entire shader source, especially for binary formats, because it+ // would be spammy.+ Self::Naga { .. } => f.debug_tuple("Naga").finish_non_exhaustive(),+ Self::MetalLib { .. } => f.debug_tuple("MetalLib").finish_non_exhaustive(),+ Self::Msl { .. } => f.debug_tuple("Msl").finish_non_exhaustive(),+ Self::SpirV { .. } => f.debug_tuple("SpirV").finish_non_exhaustive(),+ Self::Dxil { .. } => f.debug_tuple("Dxil").finish_non_exhaustive(),+ Self::Hlsl { .. } => f.debug_tuple("Hlsl").finish_non_exhaustive(),+ Self::Glsl { .. } => f.debug_tuple("Glsl").finish_non_exhaustive(),+ }+ }+}++#[derive(Debug)]+pub struct ShaderModuleDescriptor<'a> {+ pub label: Label<'a>,++ /// # Safety+ ///+ /// See the documentation for each flag in [`ShaderRuntimeChecks`][src].+ ///+ /// [src]: wgt::ShaderRuntimeChecks+ pub runtime_checks: wgt::ShaderRuntimeChecks,+}++#[derive(Debug, Clone)]+pub struct DebugSource {+ pub file_name: Cow<'static, str>,+ pub source_code: Cow<'static, str>,+}++/// Describes a programmable pipeline stage.+#[derive(Debug)]+pub struct ProgrammableStage<'a, M: DynShaderModule + ?Sized> {+ /// The compiled shader module for this stage.+ pub module: &'a M,+ /// The name of the entry point in the compiled shader. There must be a function with this name+ /// in the shader.+ pub entry_point: &'a str,+ /// Pipeline constants+ pub constants: &'a naga::back::PipelineConstants,+ /// Whether workgroup scoped memory will be initialized with zero values for this stage.+ ///+ /// This is required by the WebGPU spec, but may have overhead which can be avoided+ /// for cross-platform applications+ pub zero_initialize_workgroup_memory: bool,+}++impl<M: DynShaderModule + ?Sized> Clone for ProgrammableStage<'_, M> {+ fn clone(&self) -> Self {+ Self {+ module: self.module,+ entry_point: self.entry_point,+ constants: self.constants,+ zero_initialize_workgroup_memory: self.zero_initialize_workgroup_memory,+ }+ }+}++/// Describes a compute pipeline.+#[derive(Clone, Debug)]+pub struct ComputePipelineDescriptor<+ 'a,+ Pl: DynPipelineLayout + ?Sized,+ M: DynShaderModule + ?Sized,+ Pc: DynPipelineCache + ?Sized,+> {+ pub label: Label<'a>,+ /// The layout of bind groups for this pipeline.+ pub layout: &'a Pl,+ /// The compiled compute stage and its entry point.+ pub stage: ProgrammableStage<'a, M>,+ /// The cache which will be used and filled when compiling this pipeline+ pub cache: Option<&'a Pc>,+}++#[derive(Debug)]+pub struct PipelineCacheDescriptor<'a> {+ pub label: Label<'a>,+ pub data: Option<&'a [u8]>,+}++/// Describes how the vertex buffer is interpreted.+#[derive(Clone, Debug)]+pub struct VertexBufferLayout<'a> {+ /// The stride, in bytes, between elements of this buffer.+ pub array_stride: wgt::BufferAddress,+ /// How often this vertex buffer is "stepped" forward.+ pub step_mode: wgt::VertexStepMode,+ /// The list of attributes which comprise a single vertex.+ pub attributes: &'a [wgt::VertexAttribute],+}++#[derive(Clone, Debug)]+pub enum VertexProcessor<'a, M: DynShaderModule + ?Sized> {+ Standard {+ /// The format of any vertex buffers used with this pipeline.+ vertex_buffers: &'a [Option<VertexBufferLayout<'a>>],+ /// The vertex stage for this pipeline.+ vertex_stage: ProgrammableStage<'a, M>,+ },+ Mesh {+ task_stage: Option<ProgrammableStage<'a, M>>,+ mesh_stage: ProgrammableStage<'a, M>,+ },+}++/// Describes a render (graphics) pipeline.+#[derive(Clone, Debug)]+pub struct RenderPipelineDescriptor<+ 'a,+ Pl: DynPipelineLayout + ?Sized,+ M: DynShaderModule + ?Sized,+ Pc: DynPipelineCache + ?Sized,+> {+ pub label: Label<'a>,+ /// The layout of bind groups for this pipeline.+ pub layout: &'a Pl,+ /// The vertex processing state(vertex shader + buffers or task + mesh shaders)+ pub vertex_processor: VertexProcessor<'a, M>,+ /// The properties of the pipeline at the primitive assembly and rasterization level.+ pub primitive: wgt::PrimitiveState,+ /// The effect of draw calls on the depth and stencil aspects of the output target, if any.+ pub depth_stencil: Option<wgt::DepthStencilState>,+ /// The multi-sampling properties of the pipeline.+ pub multisample: wgt::MultisampleState,+ /// The fragment stage for this pipeline.+ pub fragment_stage: Option<ProgrammableStage<'a, M>>,+ /// The effect of draw calls on the color aspect of the output target.+ pub color_targets: &'a [Option<wgt::ColorTargetState>],+ /// If the pipeline will be used with a multiview render pass, this indicates how many array+ /// layers the attachments will have.+ pub multiview_mask: Option<NonZeroU32>,+ /// The cache which will be used and filled when compiling this pipeline+ pub cache: Option<&'a Pc>,+}++#[derive(Clone, Debug)]+pub struct RayObjectIntersectionState<'a, M: DynShaderModule + ?Sized> {+ pub closest_hit: ProgrammableStage<'a, M>,+ pub any_hit: Option<ProgrammableStage<'a, M>>,+}++/// Describes a ray tracing pipeline.+#[derive(Clone, Debug)]+pub struct RayTracingPipelineDescriptor<+ 'a,+ Pl: DynPipelineLayout + ?Sized,+ M: DynShaderModule + ?Sized,+ Pc: DynPipelineCache + ?Sized,+> {+ pub label: Label<'a>,+ /// The layout of bind groups for this pipeline.+ pub layout: &'a Pl,+ /// The ray generation stage.+ pub ray_generation: ProgrammableStage<'a, M>,+ /// The miss stage.+ pub miss: ProgrammableStage<'a, M>,+ /// All the object intersection stages.+ pub intersection: &'a [RayObjectIntersectionState<'a, M>],+ /// The maximum recursion depth allowed for the ray tracing (ray_generation shader counts as depth 0).+ pub max_recursion_depth: u32,+ /// The cache which will be used and filled when compiling this pipeline+ pub cache: Option<&'a Pc>,+}++#[derive(Debug, Clone)]+pub struct SurfaceConfiguration {+ /// Maximum number of queued frames. Must be in+ /// `SurfaceCapabilities::maximum_frame_latency` range.+ pub maximum_frame_latency: u32,+ /// Vertical synchronization mode.+ pub present_mode: wgt::PresentMode,+ /// Alpha composition mode.+ pub composite_alpha_mode: wgt::CompositeAlphaMode,+ /// Format of the surface textures.+ pub format: wgt::TextureFormat,+ /// Color space in which the presentation engine interprets the surface+ /// textures. Never [`wgt::SurfaceColorSpace::Auto`]; `wgpu-core` resolves+ /// `Auto` to a concrete color space before configuring the surface, and+ /// the (format, color space) pair must be listed in+ /// `SurfaceCapabilities::formats`.+ pub color_space: wgt::SurfaceColorSpace,+ /// Requested texture extent. Must be in+ /// `SurfaceCapabilities::extents` range.+ pub extent: wgt::Extent3d,+ /// Allowed usage of surface textures,+ pub usage: wgt::TextureUses,+ /// Allows views of swapchain texture to have a different format+ /// than the texture does.+ pub view_formats: Vec<wgt::TextureFormat>,+}++#[derive(Debug, Clone)]+pub struct Rect<T> {+ pub x: T,+ pub y: T,+ pub w: T,+ pub h: T,+}++#[derive(Debug, Clone, PartialEq)]+pub struct StateTransition<T> {+ pub from: T,+ pub to: T,+}++#[derive(Debug, Clone)]+pub struct BufferBarrier<'a, B: DynBuffer + ?Sized> {+ pub buffer: &'a B,+ pub usage: StateTransition<wgt::BufferUses>,+}++#[derive(Debug, Clone)]+pub struct TextureBarrier<'a, T: DynTexture + ?Sized> {+ pub texture: &'a T,+ pub range: wgt::ImageSubresourceRange,+ pub usage: StateTransition<wgt::TextureUses>,+}++#[derive(Clone, Copy, Debug)]+pub struct BufferCopy {+ pub src_offset: wgt::BufferAddress,+ pub dst_offset: wgt::BufferAddress,+ pub size: wgt::BufferSize,+}++#[derive(Clone, Debug)]+pub struct TextureCopyBase {+ pub mip_level: u32,+ pub array_layer: u32,+ /// Origin within a texture.+ /// Note: for 1D and 2D textures, Z must be 0.+ pub origin: wgt::Origin3d,+ pub aspect: FormatAspects,+}++#[derive(Clone, Copy, Debug)]+pub struct CopyExtent {+ pub width: u32,+ pub height: u32,+ pub depth: u32,+}++impl From<wgt::Extent3d> for CopyExtent {+ fn from(value: wgt::Extent3d) -> Self {+ let wgt::Extent3d {+ width,+ height,+ depth_or_array_layers,+ } = value;+ Self {+ width,+ height,+ depth: depth_or_array_layers,+ }+ }+}++impl From<CopyExtent> for wgt::Extent3d {+ fn from(value: CopyExtent) -> Self {+ let CopyExtent {+ width,+ height,+ depth,+ } = value;+ Self {+ width,+ height,+ depth_or_array_layers: depth,+ }+ }+}++#[derive(Clone, Debug)]+pub struct TextureCopy {+ pub src_base: TextureCopyBase,+ pub dst_base: TextureCopyBase,+ pub size: CopyExtent,+}++#[derive(Clone, Debug)]+pub struct BufferTextureCopy {+ pub buffer_layout: wgt::TexelCopyBufferLayout,+ pub texture_base: TextureCopyBase,+ pub size: CopyExtent,+}++#[derive(Clone, Debug)]+pub struct Attachment<'a, T: DynTextureView + ?Sized> {+ pub view: &'a T,+ /// Contains either a single mutating usage as a target,+ /// or a valid combination of read-only usages.+ pub usage: wgt::TextureUses,+}++#[derive(Clone, Debug)]+pub struct ColorAttachment<'a, T: DynTextureView + ?Sized> {+ pub target: Attachment<'a, T>,+ pub depth_slice: Option<u32>,+ pub resolve_target: Option<Attachment<'a, T>>,+ pub ops: AttachmentOps,+ pub clear_value: wgt::Color,+}++#[derive(Clone, Debug)]+pub struct DepthStencilAttachment<'a, T: DynTextureView + ?Sized> {+ pub target: Attachment<'a, T>,+ pub depth_ops: AttachmentOps,+ pub stencil_ops: AttachmentOps,+ pub clear_value: (f32, u32),+}++#[derive(Clone, Debug)]+pub struct PassTimestampWrites<'a, Q: DynQuerySet + ?Sized> {+ pub query_set: &'a Q,+ pub beginning_of_pass_write_index: Option<u32>,+ pub end_of_pass_write_index: Option<u32>,+}++#[derive(Clone, Debug)]+pub struct RenderPassDescriptor<'a, Q: DynQuerySet + ?Sized, T: DynTextureView + ?Sized> {+ pub label: Label<'a>,+ pub extent: wgt::Extent3d,+ pub sample_count: u32,+ pub color_attachments: &'a [Option<ColorAttachment<'a, T>>],+ pub depth_stencil_attachment: Option<DepthStencilAttachment<'a, T>>,+ pub multiview_mask: Option<NonZeroU32>,+ pub timestamp_writes: Option<PassTimestampWrites<'a, Q>>,+ pub occlusion_query_set: Option<&'a Q>,+}++#[derive(Clone, Debug)]+pub struct ComputePassDescriptor<'a, Q: DynQuerySet + ?Sized> {+ pub label: Label<'a>,+ pub timestamp_writes: Option<PassTimestampWrites<'a, Q>>,+}++#[derive(Clone, Debug)]+pub struct RayTracingPassDescriptor<'a> {+ pub label: Label<'a>,+}++#[test]+fn test_default_limits() {+ let limits = wgt::Limits::default();+ assert!(limits.max_bind_groups <= MAX_BIND_GROUPS as u32);+}++#[derive(Clone, Debug)]+pub struct AccelerationStructureDescriptor<'a> {+ pub label: Label<'a>,+ pub size: wgt::BufferAddress,+ pub format: AccelerationStructureFormat,+ pub allow_compaction: bool,+}++#[derive(Debug, Clone, Copy, Eq, PartialEq)]+pub enum AccelerationStructureFormat {+ TopLevel,+ BottomLevel,+}++#[derive(Debug, Clone, Copy, Eq, PartialEq)]+pub enum AccelerationStructureBuildMode {+ Build,+ Update,+}++/// Information of the required size for a corresponding entries struct (+ flags)+#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]+pub struct AccelerationStructureBuildSizes {+ pub acceleration_structure_size: wgt::BufferAddress,+ pub update_scratch_size: wgt::BufferAddress,+ pub build_scratch_size: wgt::BufferAddress,+}++/// Updates use source_acceleration_structure if present, else the update will be performed in place.+/// For updates, only the data is allowed to change (not the meta data or sizes).+#[derive(Clone, Debug)]+pub struct BuildAccelerationStructureDescriptor<+ 'a,+ B: DynBuffer + ?Sized,+ A: DynAccelerationStructure + ?Sized,+> {+ pub entries: &'a AccelerationStructureEntries<'a, B>,+ pub mode: AccelerationStructureBuildMode,+ pub flags: AccelerationStructureBuildFlags,+ pub source_acceleration_structure: Option<&'a A>,+ pub destination_acceleration_structure: &'a A,+ pub scratch_buffer: &'a B,+ pub scratch_buffer_offset: wgt::BufferAddress,+}++/// - All buffers, buffer addresses and offsets will be ignored.+/// - The build mode will be ignored.+/// - Reducing the amount of Instances, Triangle groups or AABB groups (or the number of Triangles/AABBs in corresponding groups),+/// may result in reduced size requirements.+/// - Any other change may result in a bigger or smaller size requirement.+#[derive(Clone, Debug)]+pub struct GetAccelerationStructureBuildSizesDescriptor<'a, B: DynBuffer + ?Sized> {+ pub entries: &'a AccelerationStructureEntries<'a, B>,+ pub flags: AccelerationStructureBuildFlags,+}++/// Entries for a single descriptor+/// * `Instances` - Multiple instances for a top level acceleration structure+/// * `Triangles` - Multiple triangle meshes for a bottom level acceleration structure+/// * `AABBs` - List of list of axis aligned bounding boxes for a bottom level acceleration structure+#[derive(Debug)]+pub enum AccelerationStructureEntries<'a, B: DynBuffer + ?Sized> {+ Instances(AccelerationStructureInstances<'a, B>),+ Triangles(Vec<AccelerationStructureTriangles<'a, B>>),+ AABBs(Vec<AccelerationStructureAABBs<'a, B>>),+}++/// * `first_vertex` - offset in the vertex buffer (as number of vertices)+/// * `indices` - optional index buffer with attributes+/// * `transform` - optional transform+#[derive(Clone, Debug)]+pub struct AccelerationStructureTriangles<'a, B: DynBuffer + ?Sized> {+ pub vertex_buffer: Option<&'a B>,+ pub vertex_format: wgt::VertexFormat,+ pub first_vertex: u32,+ pub vertex_count: u32,+ pub vertex_stride: wgt::BufferAddress,+ pub indices: Option<AccelerationStructureTriangleIndices<'a, B>>,+ pub transform: Option<AccelerationStructureTriangleTransform<'a, B>>,+ pub flags: AccelerationStructureGeometryFlags,+}++/// * `offset` - offset in bytes+#[derive(Clone, Debug)]+pub struct AccelerationStructureAABBs<'a, B: DynBuffer + ?Sized> {+ pub buffer: Option<&'a B>,+ pub offset: u32,+ pub count: u32,+ pub stride: wgt::BufferAddress,+ pub flags: AccelerationStructureGeometryFlags,+}++#[derive(Clone, Debug)]+pub struct AccelerationStructureCopy {+ pub copy_flags: wgt::AccelerationStructureCopy,+ pub type_flags: wgt::AccelerationStructureType,+}++/// * `offset` - offset in bytes+#[derive(Clone, Debug)]+pub struct AccelerationStructureInstances<'a, B: DynBuffer + ?Sized> {+ pub buffer: Option<&'a B>,+ pub offset: u32,+ pub count: u32,+}++/// * `offset` - offset in bytes+#[derive(Clone, Debug)]+pub struct AccelerationStructureTriangleIndices<'a, B: DynBuffer + ?Sized> {+ pub format: wgt::IndexFormat,+ pub buffer: Option<&'a B>,+ pub offset: u32,+ pub count: u32,+}++/// * `offset` - offset in bytes+#[derive(Clone, Debug)]+pub struct AccelerationStructureTriangleTransform<'a, B: DynBuffer + ?Sized> {+ pub buffer: &'a B,+ pub offset: u32,+}++pub use wgt::AccelerationStructureFlags as AccelerationStructureBuildFlags;+pub use wgt::AccelerationStructureGeometryFlags;++bitflags::bitflags! {+ #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]+ pub struct AccelerationStructureUses: u8 {+ // For blas used as input for tlas+ const BUILD_INPUT = 1 << 0;+ // Target for acceleration structure build+ const BUILD_OUTPUT = 1 << 1;+ // Tlas used in a shader+ const SHADER_INPUT = 1 << 2;+ // Blas used to query compacted size+ const QUERY_INPUT = 1 << 3;+ // BLAS used as a src for a copy operation+ const COPY_SRC = 1 << 4;+ // BLAS used as a dst for a copy operation+ const COPY_DST = 1 << 5;+ }+}++#[derive(Debug, Clone)]+pub struct AccelerationStructureBarrier {+ pub usage: StateTransition<AccelerationStructureUses>,+}++#[derive(Debug, Copy, Clone)]+pub struct TlasInstance {+ pub transform: [f32; 12],+ pub custom_data: u32,+ pub mask: u8,+ pub blas_address: u64,+ /// The offset for the index into the intersection hit+ /// group calculation. Number is in hit groups.+ pub pipeline_intersection_data_offset: u32,+}++#[cfg(dx12)]+#[derive(Debug)]+pub enum D3D12ExposeAdapterResult {+ CreateDeviceError(dx12::CreateDeviceError),+ UnknownFeatureLevel(i32),+ ResourceBindingTier2Requirement,+ ShaderModel6Requirement,+ Success(dx12::FeatureLevel, dx12::ShaderModel),+}++/// Pluggable telemetry, mainly to be used by Firefox.+#[derive(Debug, Clone, Copy)]+pub struct Telemetry {+ #[cfg(dx12)]+ pub d3d12_expose_adapter: fn(+ desc: &windows::Win32::Graphics::Dxgi::DXGI_ADAPTER_DESC2,+ driver_version: Result<[u16; 4], windows_core::HRESULT>,+ result: D3D12ExposeAdapterResult,+ ),+}++#[derive(Debug)]+pub struct PipelineGroupData<'a, B: DynBuffer + ?Sized> {+ pub buffer: &'a B,+ pub offset: wgt::BufferAddress,+ pub stride: u64,+ pub count: u64,+}diff --git a/vendor/wgpu-hal/src/metal/adapter.rs b/vendor/wgpu-hal/src/metal/adapter.rsnew file mode 100644index 00000000..f0bab75c--- /dev/null+++ b/vendor/wgpu-hal/src/metal/adapter.rs@@ -0,0 +1,1851 @@+use objc2::rc::autoreleasepool;+use objc2::runtime::{AnyObject, ProtocolObject, Sel};+use objc2::{available, sel};+use objc2_foundation::{NSOperatingSystemVersion, NSProcessInfo, NSString};+use objc2_metal::{+ MTLArgumentBuffersTier, MTLCompileOptions, MTLCounterSamplingPoint, MTLDevice, MTLFeatureSet, MTLGPUFamily,+ MTLIndirectAccelerationStructureInstanceDescriptor, MTLLanguageVersion, MTLPixelFormat,+ MTLReadWriteTextureTier,+};+use wgt::{AstcBlock, AstcChannel};++use alloc::{string::ToString as _, sync::Arc, vec::Vec};+use core::sync::atomic;+use parking_lot::Mutex;+use std::sync::OnceLock;++use crate::metal::QueueShared;++use super::{OsFeatures, TimestampQuerySupport};++/// The Metal language version to compile shaders with, as Craft Anything needs it.+///+/// wgpu takes the version from the macOS version alone, but the system's Metal compiler may not+/// accept it: on some Macs with older GPUs (OpenCore Legacy Patcher) macOS 15 expects Metal 3.2+/// and the compiler stops at 3.1, so every shader fails and the device is lost (wgpu #10687,+/// Craft Anything #742). This compiles a tiny kernel with each version up to the OS's, highest+/// first, and keeps the first one the compiler accepts. If none is accepted, the OS's version is+/// kept, as before.+fn accepted_msl_version(+ device: &ProtocolObject<dyn MTLDevice>,+ os_version: MTLLanguageVersion,+) -> MTLLanguageVersion {+ const CANDIDATES: [MTLLanguageVersion; 9] = [+ MTLLanguageVersion::Version4_0,+ MTLLanguageVersion::Version3_2,+ MTLLanguageVersion::Version3_1,+ MTLLanguageVersion::Version3_0,+ MTLLanguageVersion::Version2_4,+ MTLLanguageVersion::Version2_3,+ MTLLanguageVersion::Version2_2,+ MTLLanguageVersion::Version2_1,+ MTLLanguageVersion::Version2_0,+ ];+ let probe = NSString::from_str(+ "#include <metal_stdlib>\nusing namespace metal;\nkernel void wgpu_msl_probe() {}\n",+ );+ for candidate in CANDIDATES.into_iter().filter(|v| *v <= os_version) {+ let options = MTLCompileOptions::new();+ options.setLanguageVersion(candidate);+ if device.newLibraryWithSource_options_error(&probe, Some(&options)).is_ok() {+ if candidate != os_version {+ log::warn!(+ "The Metal compiler does not accept the language version expected from this OS ({os_version:?}); using {candidate:?} instead"+ );+ }+ return candidate;+ }+ }+ log::warn!("The Metal compiler accepted none of the language versions up to {os_version:?}; using it anyway");+ os_version+}++/// Check if a device's class has a given method in its method table.+///+/// This mirrors the check that `objc2` performs internally (in debug builds)+/// before sending a message. We use it to skip method calls that would panic+/// on proxy objects like Apple's `CaptureMTLDevice`, which forwards messages+/// at runtime but doesn't declare the methods in its class.+fn device_class_responds_to(device: &ProtocolObject<dyn MTLDevice>, sel: Sel) -> bool {+ AnyObject::class(device.as_ref()).responds_to(sel)+}++/// Maximum number of command buffers for `MTLCommandQueue`s that we create.+///+/// If a [new command buffer] is requested when Metal has run out of command+/// buffers, it waits indefinitely for one to become available. If the+/// outstanding command buffers are actively executing on the GPU, this will+/// happen relatively quickly. But if the outstanding command buffers will only+/// be recovered upon GC, and attempting to get a new command buffer prevents+/// forward progress towards that GC, there is a deadlock.+///+/// This is mostly a problem for the CTS, which frequently creates command+/// buffers that it does not submit. It is unclear how likely command buffer+/// exhaustion is in real applications.+///+/// This limit was increased from a previous value of 2048 for+/// <https://bugzilla.mozilla.org/show_bug.cgi?id=1971452>.+///+/// [new command buffer]: https://developer.apple.com/documentation/metal/mtlcommandqueue/makecommandbuffer()?language=objc+pub(super) const MAX_COMMAND_BUFFERS: usize = 4096;++/// "Maximum number of entries in the buffer argument table, per graphics or kernel function"+///+/// Vertex buffers are placed at the end of the table (highest indices),+/// counting down from MAX_BUFFERS - 1.+pub const MAX_BUFFERS: u32 = 31;++impl super::Adapter {+ pub(super) fn new(shared: Arc<super::AdapterShared>) -> Self {+ Self { shared }+ }+ pub fn raw_device(&self) -> &ProtocolObject<dyn MTLDevice> {+ &self.shared.device+ }+}++impl crate::Adapter for super::Adapter {+ type A = super::Api;++ unsafe fn open(+ &self,+ features: wgt::Features,+ limits: &wgt::Limits,+ _memory_hints: &wgt::MemoryHints,+ ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> {+ autoreleasepool(|_| {+ let queue = self+ .shared+ .device+ .newCommandQueueWithMaxCommandBufferCount(MAX_COMMAND_BUFFERS)+ .unwrap();++ // Acquiring the meaning of timestamp ticks is hard with Metal!+ // The only thing there is a method correlating cpu & gpu timestamps (`device.sample_timestamps`).+ // Users are supposed to call this method twice and calculate the difference,+ // see "Converting GPU Timestamps into CPU Time":+ // https://developer.apple.com/documentation/metal/gpu_counters_and_counter_sample_buffers/converting_gpu_timestamps_into_cpu_time+ // Not only does this mean we get an approximate value, this is as also *very slow*!+ // Chromium opted to solve this using a linear regression that they stop at some point+ // https://source.chromium.org/chromium/chromium/src/+/refs/heads/main:third_party/dawn/src/dawn/native/metal/DeviceMTL.mm;drc=76be2f9f117654f3fe4faa477b0445114fccedda;bpv=0;bpt=1;l=46+ // Generally, the assumption is that timestamp values aren't changing over time, after all all other APIs provide stable values.+ //+ // We should do as Chromium does for the general case, but this requires quite some state tracking+ // and doesn't even provide perfectly accurate values, especially at the start of the application when+ // we didn't have the chance to sample a lot of values just yet.+ //+ // So instead, we're doing the dangerous but easy thing and use our "knowledge" of timestamps+ // conversions on different devices, after all Metal isn't supported on that many ;)+ // Based on:+ // * https://github.com/gfx-rs/wgpu/pull/2528+ // * https://github.com/gpuweb/gpuweb/issues/1325#issuecomment-761041326+ let timestamp_period = if self.shared.device.name().to_string().starts_with("Intel") {+ 83.333+ } else {+ // Known for Apple Silicon (at least M1 & M2, iPad Pro 2018) and AMD GPUs.+ 1.0+ };++ Ok(crate::OpenDevice {+ device: super::Device {+ shared: Arc::clone(&self.shared),+ features,+ counters: Default::default(),+ limits: limits.clone(),+ },+ queue: super::Queue {+ shared: Arc::new(QueueShared {+ raw: queue,+ command_buffer_created_not_submitted: atomic::AtomicUsize::new(0),+ pending_waits: Mutex::new(Vec::new()),+ pending_signals: Mutex::new(Vec::new()),+ relay: OnceLock::new(),+ }),+ timestamp_period,+ },+ })+ })+ }++ unsafe fn texture_format_capabilities(+ &self,+ format: wgt::TextureFormat,+ ) -> crate::TextureFormatCapabilities {+ use crate::TextureFormatCapabilities as Tfc;+ use wgt::TextureFormat as Tf;++ let msl_version = self.shared.private_caps.msl_version;+ let pc = &self.shared.private_texture_format_caps;+ // Affected formats documented at:+ // https://developer.apple.com/documentation/metal/mtlreadwritetexturetier/mtlreadwritetexturetier1?language=objc+ // https://developer.apple.com/documentation/metal/mtlreadwritetexturetier/mtlreadwritetexturetier2?language=objc+ let (read_write_tier1_if, read_write_tier2_if) = match pc.read_write_texture_tier {+ MTLReadWriteTextureTier::TierNone => (Tfc::empty(), Tfc::empty()),+ MTLReadWriteTextureTier::Tier1 => (Tfc::STORAGE_READ_WRITE, Tfc::empty()),+ MTLReadWriteTextureTier::Tier2 => (Tfc::STORAGE_READ_WRITE, Tfc::STORAGE_READ_WRITE),+ // Unknown levels of support are likely higher than Tier 2.+ _ => (Tfc::STORAGE_READ_WRITE, Tfc::STORAGE_READ_WRITE),+ };+ let msaa_count = pc.sample_count_mask;++ let msaa_resolve_desktop_if = if pc.msaa_desktop {+ Tfc::MULTISAMPLE_RESOLVE+ } else {+ Tfc::empty()+ };+ let msaa_resolve_apple3x_if = if pc.msaa_desktop | pc.msaa_apple3 {+ Tfc::MULTISAMPLE_RESOLVE+ } else {+ Tfc::empty()+ };+ let is_not_apple1x = super::CapabilitiesQuery::supports_any(+ self.shared.device.as_ref(),+ &[+ MTLFeatureSet::iOS_GPUFamily2_v1,+ MTLFeatureSet::macOS_GPUFamily1_v1,+ MTLFeatureSet::tvOS_GPUFamily1_v1,+ ],+ );++ let image_atomic_if = if msl_version >= MTLLanguageVersion::Version3_1 {+ Tfc::STORAGE_ATOMIC+ } else {+ Tfc::empty()+ };++ let image_64_atomic_if = if pc.int64_atomics {+ Tfc::STORAGE_ATOMIC+ } else {+ Tfc::empty()+ };++ // Metal defined pixel format capabilities+ let all_caps = Tfc::SAMPLED_LINEAR+ | Tfc::STORAGE_WRITE_ONLY+ | Tfc::COLOR_ATTACHMENT+ | Tfc::COLOR_ATTACHMENT_BLEND+ | msaa_count+ | Tfc::MULTISAMPLE_RESOLVE;++ let extra = match format {+ Tf::R8Unorm | Tf::R16Float | Tf::Rgba8Unorm | Tf::Rgba16Float => {+ read_write_tier2_if | all_caps+ }+ Tf::R8Snorm | Tf::Rg8Snorm | Tf::Rgba8Snorm => {+ let mut flags = all_caps;+ flags.set(Tfc::MULTISAMPLE_RESOLVE, is_not_apple1x);+ flags+ }+ Tf::R8Uint+ | Tf::R8Sint+ | Tf::R16Uint+ | Tf::R16Sint+ | Tf::Rgba8Uint+ | Tf::Rgba8Sint+ | Tf::Rgba16Uint+ | Tf::Rgba16Sint => {+ read_write_tier2_if | Tfc::STORAGE_WRITE_ONLY | Tfc::COLOR_ATTACHMENT | msaa_count+ }+ Tf::R16Unorm+ | Tf::R16Snorm+ | Tf::Rg16Unorm+ | Tf::Rg16Snorm+ | Tf::Rgba16Unorm+ | Tf::Rgba16Snorm => {+ Tfc::SAMPLED_LINEAR+ | Tfc::STORAGE_WRITE_ONLY+ | Tfc::COLOR_ATTACHMENT+ | Tfc::COLOR_ATTACHMENT_BLEND+ | msaa_count+ | msaa_resolve_desktop_if+ }+ Tf::Rg8Unorm | Tf::Rg16Float | Tf::Bgra8Unorm => all_caps,+ Tf::Rg8Uint | Tf::Rg8Sint => {+ Tfc::STORAGE_WRITE_ONLY | Tfc::COLOR_ATTACHMENT | msaa_count+ }+ Tf::R32Uint | Tf::R32Sint => {+ read_write_tier1_if+ | Tfc::STORAGE_WRITE_ONLY+ | Tfc::COLOR_ATTACHMENT+ | msaa_count+ | image_atomic_if+ }+ Tf::R32Float => {+ let flags = if pc.format_r32float_all {+ all_caps+ } else {+ Tfc::STORAGE_WRITE_ONLY+ | Tfc::COLOR_ATTACHMENT+ | Tfc::COLOR_ATTACHMENT_BLEND+ | msaa_count+ };+ read_write_tier1_if | flags+ }+ Tf::Rg16Uint | Tf::Rg16Sint => {+ Tfc::STORAGE_WRITE_ONLY | Tfc::COLOR_ATTACHMENT | msaa_count+ }+ Tf::Rgba8UnormSrgb | Tf::Bgra8UnormSrgb => {+ let mut flags = all_caps;+ flags.set(Tfc::STORAGE_WRITE_ONLY, pc.format_rgba8_srgb_all);+ flags+ }+ Tf::Rgb10a2Uint => {+ let mut flags = Tfc::COLOR_ATTACHMENT | msaa_count;+ flags.set(Tfc::STORAGE_WRITE_ONLY, pc.format_rgb10a2_uint_write);+ flags+ }+ Tf::Rgb10a2Unorm => {+ let mut flags = all_caps;+ flags.set(Tfc::STORAGE_WRITE_ONLY, pc.format_rgb10a2_unorm_all);+ flags+ }+ Tf::Rg11b10Ufloat => {+ let mut flags = all_caps;+ flags.set(Tfc::STORAGE_WRITE_ONLY, pc.format_rg11b10_all);+ flags+ }+ Tf::R64Uint => {+ Tfc::COLOR_ATTACHMENT+ | Tfc::STORAGE_WRITE_ONLY+ | image_64_atomic_if+ | read_write_tier1_if+ }+ Tf::Rg32Uint | Tf::Rg32Sint => {+ Tfc::COLOR_ATTACHMENT | Tfc::STORAGE_WRITE_ONLY | msaa_count+ }+ Tf::Rg32Float => {+ if pc.format_rg32float_all {+ all_caps+ } else {+ Tfc::STORAGE_WRITE_ONLY+ | Tfc::COLOR_ATTACHMENT+ | Tfc::COLOR_ATTACHMENT_BLEND+ | msaa_count+ }+ }+ Tf::Rgba32Uint | Tf::Rgba32Sint => {+ read_write_tier2_if | Tfc::STORAGE_WRITE_ONLY | Tfc::COLOR_ATTACHMENT | msaa_count+ }+ Tf::Rgba32Float => {+ let mut flags =+ read_write_tier2_if | Tfc::STORAGE_WRITE_ONLY | Tfc::COLOR_ATTACHMENT;+ if pc.format_rgba32float_all {+ flags |= all_caps+ } else if pc.msaa_apple7 {+ flags |= msaa_count+ };+ flags+ }+ Tf::Stencil8 => {+ all_caps | Tfc::DEPTH_STENCIL_ATTACHMENT | msaa_count | msaa_resolve_apple3x_if+ }+ Tf::Depth16Unorm => {+ if pc.format_depth16unorm {+ let mut flags =+ Tfc::DEPTH_STENCIL_ATTACHMENT | msaa_count | msaa_resolve_apple3x_if;+ if pc.format_depth16unorm_filter {+ flags |= Tfc::SAMPLED_LINEAR;+ }+ flags+ } else {+ return Tfc::empty();+ }+ }+ Tf::Depth32Float | Tf::Depth32FloatStencil8 => {+ let mut flags =+ Tfc::DEPTH_STENCIL_ATTACHMENT | msaa_count | msaa_resolve_apple3x_if;+ if pc.format_depth32float_filter {+ flags |= Tfc::SAMPLED_LINEAR+ }+ flags+ }+ Tf::Depth24Plus | Tf::Depth24PlusStencil8 => {+ let mut flags = Tfc::DEPTH_STENCIL_ATTACHMENT | msaa_count;+ if pc.format_depth24_stencil8 {+ flags |= Tfc::SAMPLED_LINEAR | Tfc::MULTISAMPLE_RESOLVE+ } else {+ flags |= msaa_resolve_apple3x_if;+ if pc.format_depth32float_filter {+ flags |= Tfc::SAMPLED_LINEAR+ }+ }+ flags+ }+ Tf::NV12 => return Tfc::empty(),+ Tf::P010 => return Tfc::empty(),+ Tf::Rgb9e5Ufloat => {+ if pc.msaa_apple3 {+ all_caps+ } else if pc.msaa_desktop {+ Tfc::SAMPLED_LINEAR+ } else {+ Tfc::SAMPLED_LINEAR+ | Tfc::COLOR_ATTACHMENT+ | Tfc::COLOR_ATTACHMENT_BLEND+ | msaa_count+ | Tfc::MULTISAMPLE_RESOLVE+ }+ }+ Tf::Bc1RgbaUnorm+ | Tf::Bc1RgbaUnormSrgb+ | Tf::Bc2RgbaUnorm+ | Tf::Bc2RgbaUnormSrgb+ | Tf::Bc3RgbaUnorm+ | Tf::Bc3RgbaUnormSrgb+ | Tf::Bc4RUnorm+ | Tf::Bc4RSnorm+ | Tf::Bc5RgUnorm+ | Tf::Bc5RgSnorm+ | Tf::Bc6hRgbUfloat+ | Tf::Bc6hRgbFloat+ | Tf::Bc7RgbaUnorm+ | Tf::Bc7RgbaUnormSrgb => {+ if pc.format_bc {+ Tfc::SAMPLED_LINEAR+ } else {+ Tfc::empty()+ }+ }+ Tf::Etc2Rgb8Unorm+ | Tf::Etc2Rgb8UnormSrgb+ | Tf::Etc2Rgb8A1Unorm+ | Tf::Etc2Rgb8A1UnormSrgb+ | Tf::Etc2Rgba8Unorm+ | Tf::Etc2Rgba8UnormSrgb+ | Tf::EacR11Unorm+ | Tf::EacR11Snorm+ | Tf::EacRg11Unorm+ | Tf::EacRg11Snorm => {+ if pc.format_eac_etc {+ Tfc::SAMPLED_LINEAR+ } else {+ Tfc::empty()+ }+ }+ Tf::Astc {+ block: _,+ channel: _,+ } => {+ if pc.format_astc || pc.format_astc_hdr {+ Tfc::SAMPLED_LINEAR+ } else {+ Tfc::empty()+ }+ }+ };++ Tfc::COPY_SRC | Tfc::COPY_DST | Tfc::SAMPLED | Tfc::STORAGE_READ_ONLY | extra+ }++ unsafe fn surface_capabilities(+ &self,+ surface: &super::Surface,+ ) -> Option<crate::SurfaceCapabilities> {+ // `CAMetalLayer` color-matches layer contents to whatever display the+ // window is on, with the compositor tone-mapping where needed, so+ // Display-P3 and HDR (PQ/HLG) color spaces work regardless of the+ // physical display's gamut and are not gated on it here.+ let format_caps = |format: wgt::TextureFormat| {+ let mut color_spaces =+ wgt::SurfaceColorSpaces::SRGB | wgt::SurfaceColorSpaces::DISPLAY_P3;+ if format == wgt::TextureFormat::Rgba16Float {+ // `Rgba16Float` enables Metal's extended dynamic range, in both+ // linear (scRGB) and encoded (extended nonlinear sRGB) form,+ // for the BT.709 and Display-P3 gamuts.+ color_spaces |= wgt::SurfaceColorSpaces::EXTENDED_SRGB_LINEAR+ | wgt::SurfaceColorSpaces::EXTENDED_SRGB+ | wgt::SurfaceColorSpaces::EXTENDED_DISPLAY_P3;+ }+ // PQ/HLG only on the >=10-bit formats: 8-bit PQ would result in unusable+ // banding. The ITUR_2100 color space constants require+ // macOS 11.0/iOS 14.0.+ if matches!(+ format,+ wgt::TextureFormat::Rgba16Float | wgt::TextureFormat::Rgb10a2Unorm+ ) && available!(macos = 11.0, ios = 14.0, tvos = 14.0, visionos = 1.0)+ {+ color_spaces |=+ wgt::SurfaceColorSpaces::BT2100_PQ | wgt::SurfaceColorSpaces::BT2100_HLG;+ }+ wgt::SurfaceFormatCapabilities {+ format,+ color_spaces,+ }+ };+ let mut formats = vec![+ format_caps(wgt::TextureFormat::Bgra8Unorm),+ format_caps(wgt::TextureFormat::Bgra8UnormSrgb),+ format_caps(wgt::TextureFormat::Rgba16Float),+ ];+ if self+ .shared+ .private_texture_format_caps+ .format_rgb10a2_unorm_all+ {+ formats.push(format_caps(wgt::TextureFormat::Rgb10a2Unorm));+ }++ Some(crate::SurfaceCapabilities {+ formats,+ // We use this here to govern the maximum number of drawables + 1.+ // See https://developer.apple.com/documentation/quartzcore/cametallayer/maximumdrawablecount+ maximum_frame_latency: if available!(+ macos = 10.13.2,+ ios = 11.2,+ tvos = 11.2,+ visionos = 1.0+ ) {+ 1..=2+ } else {+ // 3 is the default value for maximum drawables in `CAMetalLayer` documentation+ // iOS 10.3 was tested to use 3 on iphone5s+ 2..=2+ },+ // We enable Immediate mode using `-[CAMetalLayer setDisplaySyncEnabled: false]`.+ present_modes: if OsFeatures::display_sync() {+ vec![wgt::PresentMode::Fifo, wgt::PresentMode::Immediate]+ } else {+ vec![wgt::PresentMode::Fifo]+ },+ composite_alpha_modes: vec![+ wgt::CompositeAlphaMode::Opaque,+ wgt::CompositeAlphaMode::PostMultiplied,+ ],++ current_extent: Some(surface.dimensions()),+ usage: wgt::TextureUses::COLOR_TARGET+ | wgt::TextureUses::COPY_SRC+ | wgt::TextureUses::COPY_DST+ | wgt::TextureUses::STORAGE_READ_ONLY+ | wgt::TextureUses::STORAGE_WRITE_ONLY+ | wgt::TextureUses::STORAGE_READ_WRITE,+ })+ }++ unsafe fn surface_display_hdr_info(+ &self,+ surface: &super::Surface,+ ) -> Option<wgt::DisplayHdrInfo> {+ surface.display_hdr_info()+ }++ unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {+ let timestamp = self.shared.presentation_timer.get_timestamp_ns();++ wgt::PresentationTimestamp(timestamp)+ }++ fn get_ordered_buffer_usages(&self) -> wgt::BufferUses {+ wgt::BufferUses::INCLUSIVE | wgt::BufferUses::MAP_WRITE+ }++ // Don't put barriers between inclusive uses+ fn get_ordered_texture_usages(&self) -> wgt::TextureUses {+ wgt::TextureUses::INCLUSIVE+ | wgt::TextureUses::COLOR_TARGET+ | wgt::TextureUses::DEPTH_STENCIL_WRITE+ }+}++const RESOURCE_HEAP_SUPPORT: &[MTLFeatureSet] = &[+ MTLFeatureSet::iOS_GPUFamily1_v3,+ MTLFeatureSet::tvOS_GPUFamily1_v2,+ MTLFeatureSet::macOS_GPUFamily1_v3,+];++const MUTABLE_COMPARISON_SAMPLER_SUPPORT: &[MTLFeatureSet] = &[+ MTLFeatureSet::iOS_GPUFamily3_v1,+ MTLFeatureSet::macOS_GPUFamily1_v1,+];++const SAMPLER_CLAMP_TO_BORDER_SUPPORT: &[MTLFeatureSet] = &[MTLFeatureSet::macOS_GPUFamily1_v2];++const ASTC_PIXEL_FORMAT_FEATURES: &[MTLFeatureSet] = &[+ MTLFeatureSet::iOS_GPUFamily2_v1,+ MTLFeatureSet::tvOS_GPUFamily1_v1,+];++const ANY8_UNORM_SRGB_ALL: &[MTLFeatureSet] = &[+ MTLFeatureSet::iOS_GPUFamily2_v3,+ MTLFeatureSet::tvOS_GPUFamily1_v2,+];++const ANY8_SNORM_RESOLVE: &[MTLFeatureSet] = &[+ MTLFeatureSet::iOS_GPUFamily2_v1,+ MTLFeatureSet::tvOS_GPUFamily1_v1,+ MTLFeatureSet::macOS_GPUFamily1_v1,+];++const RGBA8_SRGB: &[MTLFeatureSet] = &[+ MTLFeatureSet::iOS_GPUFamily2_v3,+ MTLFeatureSet::tvOS_GPUFamily1_v2,+];++const RGB10A2UNORM_ALL: &[MTLFeatureSet] = &[+ MTLFeatureSet::iOS_GPUFamily3_v1,+ MTLFeatureSet::tvOS_GPUFamily2_v1,+ MTLFeatureSet::macOS_GPUFamily1_v1,+];++const RGB10A2UINT_WRITE: &[MTLFeatureSet] = &[+ MTLFeatureSet::iOS_GPUFamily3_v1,+ MTLFeatureSet::tvOS_GPUFamily2_v1,+ MTLFeatureSet::macOS_GPUFamily1_v1,+];++const RG11B10FLOAT_ALL: &[MTLFeatureSet] = &[+ MTLFeatureSet::iOS_GPUFamily3_v1,+ MTLFeatureSet::tvOS_GPUFamily2_v1,+ MTLFeatureSet::macOS_GPUFamily1_v1,+];++const RGB9E5FLOAT_ALL: &[MTLFeatureSet] = &[+ MTLFeatureSet::iOS_GPUFamily3_v1,+ MTLFeatureSet::tvOS_GPUFamily2_v1,+];++const BGR10A2_ALL: &[MTLFeatureSet] = &[+ MTLFeatureSet::iOS_GPUFamily1_v4,+ MTLFeatureSet::tvOS_GPUFamily1_v3,+ MTLFeatureSet::macOS_GPUFamily2_v1,+];++/// "Indirect draw & dispatch arguments" in the Metal feature set tables+const INDIRECT_DRAW_DISPATCH_SUPPORT: &[MTLFeatureSet] = &[+ MTLFeatureSet::iOS_GPUFamily3_v1,+ MTLFeatureSet::tvOS_GPUFamily2_v1,+ MTLFeatureSet::macOS_GPUFamily1_v1,+];++/// "Base vertex/instance drawing" in the Metal feature set tables+///+/// in our terms, `base_vertex` and `first_instance` must be 0+const BASE_VERTEX_FIRST_INSTANCE_SUPPORT: &[MTLFeatureSet] = INDIRECT_DRAW_DISPATCH_SUPPORT;++const TEXTURE_CUBE_ARRAY_SUPPORT: &[MTLFeatureSet] = &[+ MTLFeatureSet::iOS_GPUFamily4_v1,+ MTLFeatureSet::tvOS_GPUFamily1_v2,+ MTLFeatureSet::macOS_GPUFamily1_v1,+];++const DUAL_SOURCE_BLEND_SUPPORT: &[MTLFeatureSet] = &[+ MTLFeatureSet::iOS_GPUFamily1_v4,+ MTLFeatureSet::tvOS_GPUFamily1_v3,+ MTLFeatureSet::macOS_GPUFamily1_v2,+];++const LAYERED_RENDERING_SUPPORT: &[MTLFeatureSet] = &[+ MTLFeatureSet::iOS_GPUFamily5_v1,+ MTLFeatureSet::macOS_GPUFamily1_v1,+ MTLFeatureSet::macOS_GPUFamily2_v1,+];++const FUNCTION_SPECIALIZATION_SUPPORT: &[MTLFeatureSet] = &[+ MTLFeatureSet::iOS_GPUFamily1_v3,+ MTLFeatureSet::tvOS_GPUFamily1_v2,+ MTLFeatureSet::macOS_GPUFamily1_v2,+];++const DEPTH_CLIP_MODE: &[MTLFeatureSet] = &[+ MTLFeatureSet::iOS_GPUFamily4_v1,+ MTLFeatureSet::tvOS_GPUFamily1_v3,+ MTLFeatureSet::macOS_GPUFamily1_v1,+];++impl super::CapabilitiesQuery {+ fn supports_any(raw: &ProtocolObject<dyn MTLDevice>, features_sets: &[MTLFeatureSet]) -> bool {+ features_sets+ .iter()+ .cloned()+ .any(|x| raw.supportsFeatureSet(x))+ }++ /// Query the capabilities of the device.+ pub fn new(device: &ProtocolObject<dyn MTLDevice>) -> Self {+ // There are four different OSes we can target: macOS, iOS, tvOS and+ // visionOS. This can be detected using `cfg!(target_os = "ios")`, or+ // more conveniently using the `available!(...)` macro, which also+ // checks that the OS version that the binary is currently running on+ // is higher than or equal to the specified version.+ //+ // Along with the different OSes, there is also two other modes that+ // applications can run in: the Simulator, and Mac Catalyst. This can+ // be detected using `cfg!(target_env = "sim")` or+ // `cfg!(target_env = "macabi")`.+ //+ // Finally, iOS applications can be run on macOS and visionOS directly+ // using the "Designed for iPad" mode. This cannot be detected at+ // compile-time.+ //+ // All of this means that it only makes sense to use `cfg!(...)` and+ // `available!(...)` in here to check which Metal APIs are available;+ // we cannot rely on it for knowing properties of the device. For+ // that, we'll want to use `supportsFeatureSet` or `supportsFamily`.+ //+ // See the following link for further details:+ // https://developer.apple.com/documentation/metal/developing-metal-apps-that-run-in-simulator++ let version = NSProcessInfo::processInfo().operatingSystemVersion();+ let os_type = super::OsType::new(version, device);++ let family_check = available!(macos = 10.15, ios = 13.0, tvos = 13.0, visionos = 1.0);+ let metal3 = family_check && device.supportsFamily(MTLGPUFamily::Metal3);+ let metal4 = family_check && device.supportsFamily(MTLGPUFamily::Metal4);+ let mut sample_count_mask = crate::TextureFormatCapabilities::MULTISAMPLE_X4; // 1 and 4 samples are supported on all devices+ if device.supportsTextureSampleCount(2) {+ sample_count_mask |= crate::TextureFormatCapabilities::MULTISAMPLE_X2;+ }+ if device.supportsTextureSampleCount(8) {+ sample_count_mask |= crate::TextureFormatCapabilities::MULTISAMPLE_X8;+ }+ if device.supportsTextureSampleCount(16) {+ sample_count_mask |= crate::TextureFormatCapabilities::MULTISAMPLE_X16;+ }++ let rw_texture_tier = if available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0)+ {+ device.readWriteTextureSupport()+ } else if available!(macos = 10.12) {+ if Self::supports_any(device, &[MTLFeatureSet::macOS_ReadWriteTextureTier2]) {+ MTLReadWriteTextureTier::Tier2+ } else {+ MTLReadWriteTextureTier::Tier1+ }+ } else {+ MTLReadWriteTextureTier::TierNone+ };++ let mut timestamp_query_support = TimestampQuerySupport::empty();+ if available!(macos = 11.0, ios = 14.0, tvos = 14.0, visionos = 1.0)+ && device.supportsCounterSampling(MTLCounterSamplingPoint::AtStageBoundary)+ {+ // If we don't support at stage boundary, don't support anything else.+ timestamp_query_support.insert(TimestampQuerySupport::STAGE_BOUNDARIES);++ if device.supportsCounterSampling(MTLCounterSamplingPoint::AtDrawBoundary) {+ timestamp_query_support.insert(TimestampQuerySupport::ON_RENDER_ENCODER);+ }+ if device.supportsCounterSampling(MTLCounterSamplingPoint::AtDispatchBoundary) {+ timestamp_query_support.insert(TimestampQuerySupport::ON_COMPUTE_ENCODER);+ }+ if device.supportsCounterSampling(MTLCounterSamplingPoint::AtBlitBoundary) {+ timestamp_query_support.insert(TimestampQuerySupport::ON_BLIT_ENCODER);+ }+ // `TimestampQuerySupport::INSIDE_WGPU_PASSES` emerges from the other flags.+ }++ let argument_buffers = available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0)+ .then(|| device.argumentBuffersSupport());++ let is_virtual = device.name().to_string().to_lowercase().contains("virtual");++ let mesh_shaders = family_check+ && (device.supportsFamily(MTLGPUFamily::Metal3)+ || device.supportsFamily(MTLGPUFamily::Apple7)+ || device.supportsFamily(MTLGPUFamily::Mac2))+ // Mesh shaders don't work on virtual devices even if they should be supported. CI thing+ && !is_virtual;++ let os_msl_version = if available!(macos = 26.0, ios = 26.0, tvos = 26.0, visionos = 26.0) {+ MTLLanguageVersion::Version4_0+ } else if available!(macos = 15.0, ios = 18.0, tvos = 18.0, visionos = 2.0) {+ MTLLanguageVersion::Version3_2+ } else if available!(macos = 14.0, ios = 17.0, tvos = 17.0, visionos = 1.0) {+ MTLLanguageVersion::Version3_1+ } else if available!(macos = 13.0, ios = 16.0, tvos = 16.0, visionos = 1.0) {+ MTLLanguageVersion::Version3_0+ } else if available!(macos = 12.0, ios = 15.0, tvos = 15.0, visionos = 1.0) {+ MTLLanguageVersion::Version2_4+ } else if available!(macos = 11.0, ios = 14.0, tvos = 14.0, visionos = 1.0) {+ MTLLanguageVersion::Version2_3+ } else if available!(macos = 10.15, ios = 13.0, tvos = 13.0, visionos = 1.0) {+ MTLLanguageVersion::Version2_2+ } else if available!(macos = 10.14, ios = 12.0, tvos = 12.0, visionos = 1.0) {+ MTLLanguageVersion::Version2_1+ } else if available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0) {+ MTLLanguageVersion::Version2_0+ } else if available!(macos = 10.12, ios = 10.0, tvos = 10.0, visionos = 1.0) {+ MTLLanguageVersion::Version1_2+ } else if available!(macos = 10.11, ios = 9.0, tvos = 9.0, visionos = 1.0) {+ MTLLanguageVersion::Version1_1+ } else {+ MTLLanguageVersion::Version1_0+ };+ let msl_version = accepted_msl_version(device, os_msl_version);++ Self {+ msl_version,+ // macOS 10.11 doesn't support read-write resources+ fragment_rw_storage: available!(macos = 10.12, ios = 8.0, tvos = 8.0, visionos = 1.0),+ read_write_texture_tier: rw_texture_tier,+ msaa_desktop: os_type == super::OsType::Macos,+ msaa_apple3: (family_check && device.supportsFamily(MTLGPUFamily::Apple3))+ || device.supportsFeatureSet(MTLFeatureSet::iOS_GPUFamily3_v4),+ msaa_apple7: family_check && device.supportsFamily(MTLGPUFamily::Apple7),+ resource_heaps: Self::supports_any(device, RESOURCE_HEAP_SUPPORT),+ argument_buffers,+ mutable_comparison_samplers: Self::supports_any(+ device,+ MUTABLE_COMPARISON_SAMPLER_SUPPORT,+ ),+ sampler_clamp_to_border: Self::supports_any(device, SAMPLER_CLAMP_TO_BORDER_SUPPORT),+ indirect_draw_dispatch: Self::supports_any(device, INDIRECT_DRAW_DISPATCH_SUPPORT),+ base_vertex_first_instance_drawing: Self::supports_any(+ device,+ BASE_VERTEX_FIRST_INSTANCE_SUPPORT,+ ),+ dual_source_blending: Self::supports_any(device, DUAL_SOURCE_BLEND_SUPPORT),+ low_power: os_type != super::OsType::Macos || device.isLowPower(),+ headless: os_type == super::OsType::Macos && device.isHeadless(),+ layered_rendering: Self::supports_any(device, LAYERED_RENDERING_SUPPORT),+ function_specialization: Self::supports_any(device, FUNCTION_SPECIALIZATION_SUPPORT),+ depth_clip_mode: Self::supports_any(device, DEPTH_CLIP_MODE),+ texture_cube_array: Self::supports_any(device, TEXTURE_CUBE_ARRAY_SUPPORT),+ supports_float_filtering: os_type == super::OsType::Macos+ || (available!(macos = 11.0, ios = 14.0, tvos = 16.0, visionos = 1.0)+ && device_class_responds_to(device, sel!(supports32BitFloatFiltering))+ && device.supports32BitFloatFiltering()),+ format_depth24_stencil8: os_type == super::OsType::Macos+ && device.isDepth24Stencil8PixelFormatSupported(),+ format_depth32_stencil8_filter: os_type == super::OsType::Macos,+ format_depth32_stencil8_none: os_type != super::OsType::Macos,+ format_min_srgb_channels: if os_type == super::OsType::Macos {+ 4+ } else {+ 1+ },+ format_b5: os_type != super::OsType::Macos,+ format_bc: os_type == super::OsType::Macos+ || (available!(macos = 11.0, ios = 16.4, tvos = 16.4, visionos = 1.0)+ && device_class_responds_to(device, sel!(supportsBCTextureCompression))+ && device.supportsBCTextureCompression()),+ format_eac_etc: os_type != super::OsType::Macos+ // M1 in macOS supports EAC/ETC2+ || (family_check && device.supportsFamily(MTLGPUFamily::Apple7)),+ // A8(Apple2) and later always support ASTC pixel formats+ format_astc: (family_check && device.supportsFamily(MTLGPUFamily::Apple2))+ || Self::supports_any(device, ASTC_PIXEL_FORMAT_FEATURES),+ // A13(Apple6) M1(Apple7) and later always support HDR ASTC pixel formats+ format_astc_hdr: family_check && device.supportsFamily(MTLGPUFamily::Apple6),+ // Apple3 and later supports compressed volume texture formats including ASTC Sliced 3D+ format_astc_3d: family_check && device.supportsFamily(MTLGPUFamily::Apple3),+ format_any8_unorm_srgb_all: Self::supports_any(device, ANY8_UNORM_SRGB_ALL),+ format_any8_unorm_srgb_no_write: !Self::supports_any(device, ANY8_UNORM_SRGB_ALL)+ && os_type != super::OsType::Macos,+ format_any8_snorm_all: Self::supports_any(device, ANY8_SNORM_RESOLVE),+ format_r16_norm_all: os_type == super::OsType::Macos,+ // No devices support r32's all capabilities+ format_r32_all: false,+ // All devices support r32's write capability+ format_r32_no_write: false,+ // iOS support r32float's write capability, macOS support r32float's all capabilities+ format_r32float_no_write_no_filter: false,+ // Only iOS doesn't support r32float's filter capability+ format_r32float_no_filter: os_type != super::OsType::Macos,+ format_r32float_all: os_type == super::OsType::Macos,+ format_rgba8_srgb_all: Self::supports_any(device, RGBA8_SRGB),+ format_rgba8_srgb_no_write: !Self::supports_any(device, RGBA8_SRGB),+ format_rgb10a2_unorm_all: Self::supports_any(device, RGB10A2UNORM_ALL),+ format_rgb10a2_unorm_no_write: !Self::supports_any(device, RGB10A2UNORM_ALL),+ format_rgb10a2_uint_write: Self::supports_any(device, RGB10A2UINT_WRITE),+ format_rg11b10_all: Self::supports_any(device, RG11B10FLOAT_ALL),+ format_rg11b10_no_write: !Self::supports_any(device, RG11B10FLOAT_ALL),+ format_rgb9e5_all: Self::supports_any(device, RGB9E5FLOAT_ALL),+ format_rgb9e5_no_write: !Self::supports_any(device, RGB9E5FLOAT_ALL)+ && os_type != super::OsType::Macos,+ format_rgb9e5_filter_only: os_type == super::OsType::Macos,+ format_rg32_color: true,+ format_rg32_color_write: true,+ // Only macOS support rg32float's all capabilities+ format_rg32float_all: os_type == super::OsType::Macos,+ // All devices support rg32float's color + blend capabilities+ format_rg32float_color_blend: true,+ // Only iOS doesn't support rg32float's filter+ format_rg32float_no_filter: os_type != super::OsType::Macos,+ format_rgba32int_color: true,+ // All devices support rgba32uint and rgba32sint's color + write capabilities+ format_rgba32int_color_write: true,+ format_rgba32float_color: true,+ // All devices support rgba32float's color + write capabilities+ format_rgba32float_color_write: true,+ // Only macOS support rgba32float's all capabilities+ format_rgba32float_all: os_type == super::OsType::Macos,+ // https://developer.apple.com/documentation/metal/mtlpixelformat/depth16unorm+ format_depth16unorm: available!(macos = 10.12, ios = 13.0, tvos = 13.0, visionos = 1.0),+ // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=12+ format_depth16unorm_filter: family_check+ && (metal3+ || device.supportsFamily(MTLGPUFamily::Apple3)+ || device.supportsFamily(MTLGPUFamily::Mac2)),+ format_depth32float_filter: os_type == super::OsType::Macos,+ format_depth32float_none: os_type != super::OsType::Macos,+ format_bgr10a2_all: Self::supports_any(device, BGR10A2_ALL),+ format_bgr10a2_no_write: !Self::supports_any(device, BGR10A2_ALL),+ // "Maximum number of entries in the texture argument table, per graphics or kernel function"+ // The tuple is (sampled, storage).+ // The default limit split in WebGPU is 80%-20%.+ // - The default maxSampledTexturesPerShaderStage in WebGPU is 16.+ // - The default maxStorageTexturesPerShaderStage in WebGPU is 4.+ // Use a split of 75%-25% which can exactly split 128 and 96.+ max_textures_per_stage: if os_type == super::OsType::Macos+ || (family_check && device.supportsFamily(MTLGPUFamily::Apple6))+ {+ (96, 32) // 128+ } else if family_check && device.supportsFamily(MTLGPUFamily::Apple4) {+ (72, 24) // 96+ } else {+ (23, 8) // 31+ },+ max_binding_array_elements: if argument_buffers == Some(MTLArgumentBuffersTier::Tier2) {+ 1_000_000+ } else if family_check && device.supportsFamily(MTLGPUFamily::Apple4) {+ 96+ } else {+ 31+ },+ // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=7+ max_sampler_binding_array_elements: if family_check+ && device.supportsFamily(MTLGPUFamily::Apple9)+ {+ 500_000+ } else if family_check+ && (device.supportsFamily(MTLGPUFamily::Apple7)+ || device.supportsFamily(MTLGPUFamily::Mac2))+ {+ 1024+ } else if family_check && device.supportsFamily(MTLGPUFamily::Apple6) {+ 128+ } else {+ 16+ },+ // "Buffer alignment for copying an existing texture to a buffer"+ buffer_alignment: if matches!(os_type, super::OsType::Macos | super::OsType::VisionOs) {+ 256+ } else if family_check && device.supportsFamily(MTLGPUFamily::Apple3) {+ 16+ } else {+ 64+ },+ // "Minimum constant buffer offset alignment"+ constant_buffer_offset_alignment: if matches!(+ os_type,+ super::OsType::Macos | super::OsType::VisionOs+ ) {+ 256+ } else if device.supportsFeatureSet(MTLFeatureSet::macOS_GPUFamily2_v1) {+ 32+ } else {+ 4+ },+ max_buffer_size: if available!(macos = 10.14, ios = 12.0, tvos = 12.0, visionos = 1.0) {+ device.maxBufferLength() as u64+ } else if os_type == super::OsType::Macos {+ 1 << 30 // 1GB on macOS 10.11 and up+ } else {+ 1 << 28 // 256MB on iOS 8.0++ },+ // "Maximum 1D texture width" &+ // "Maximum 2D texture width and height" &+ // "Maximum cube map texture width and height"+ max_texture_size: if family_check && device.supportsFamily(MTLGPUFamily::Apple10) {+ 32768+ } else if Self::supports_any(+ device,+ &[+ MTLFeatureSet::iOS_GPUFamily3_v1,+ MTLFeatureSet::tvOS_GPUFamily2_v1,+ MTLFeatureSet::macOS_GPUFamily1_v1,+ ],+ ) {+ 16384+ } else {+ 8192+ },+ // "Maximum 3D texture width, height, and depth"+ max_texture_3d_size: 2048,+ // "Maximum number of layers per 1D texture array, 2D texture array, or 3D texture"+ max_texture_layers: 2048,+ max_fragment_input_components: if os_type == super::OsType::Macos+ || device.supportsFeatureSet(MTLFeatureSet::iOS_GPUFamily4_v1)+ {+ 124+ } else {+ 60+ },+ // "Maximum number of color render targets per render pass descriptor"+ // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=7+ max_color_render_targets: if Self::supports_any(+ device,+ &[+ MTLFeatureSet::iOS_GPUFamily2_v1,+ MTLFeatureSet::tvOS_GPUFamily1_v1,+ MTLFeatureSet::macOS_GPUFamily1_v1,+ ],+ ) {+ 8+ } else {+ 4+ },+ // "Maximum total render target size, per pixel, when using multiple color render targets"+ // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=7+ max_color_attachment_bytes_per_sample: if family_check+ && device.supportsFamily(MTLGPUFamily::Apple4)+ {+ 64 // 512 bits+ } else if family_check && device.supportsFamily(MTLGPUFamily::Apple2) {+ 32 // 256 bits+ } else if device.supportsFeatureSet(MTLFeatureSet::macOS_GPUFamily1_v1) {+ // No Limit, use max_color_render_targets * MAX_TARGET_PIXEL_BYTE_COST+ 8 * wgt::TextureFormat::MAX_TARGET_PIXEL_BYTE_COST as u8 // 1024 bits+ } else {+ 16 // 128 bits+ },+ // This limit is the minimum of:+ // - "Maximum scalar or vector inputs to a fragment function"+ // - "Maximum number of input components to a fragment function" / 4+ //+ // On non-Apple GPUs only, the Metal validation layers will error with:+ //+ // "number of shader varying components (125) exceeds limit (124).+ // Note that on macOS the following attributes count towards the+ // limit: [[position]], [[clip_distance]], [[point_size]],+ // [[point_coord]], and, when read in the fragment shader,+ // [[viewport_array_index]] & [[render_target_array_index]]."+ //+ // if the limit in the feature tables is crossed while also using+ // one of the built-ins mentioned above.+ //+ // Note that the error says "on macOS" but it actually only applies+ // to non-Apple GPUs.+ //+ // We only need to account for the position built-in since the others+ // are either not used or they already count towards the limit+ // calculation as specified by WebGPU.+ max_inter_stage_shader_variables: if family_check+ && device.supportsFamily(MTLGPUFamily::Apple4)+ {+ 31 // min(124, 124 / 4)+ } else if device.supportsFeatureSet(MTLFeatureSet::macOS_GPUFamily1_v1) {+ 30 // min(32, 124 / 4) - 1+ } else {+ 15 // min(60, 60 / 4)+ },+ // "Maximum threads per threadgroup"+ // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=6+ // These are older checks but still hold true; no entry in this table supports+ // more than 1024 threads.+ // **Note:** this is used to emit the `[[max_total_threads_per_threadgroup]]`+ // attribute. This is how we guarantee that compute dispatches will always run+ // and not silently fail due to hardware register pressure or occupancy limits.+ max_threads_per_group: if Self::supports_any(+ device,+ &[+ MTLFeatureSet::iOS_GPUFamily4_v1,+ MTLFeatureSet::macOS_GPUFamily1_v1,+ ],+ ) {+ 1024+ } else {+ 512+ },+ // "Maximum total threadgroup memory allocation"+ // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=6+ // These are older checks but still hold true; no entry in this table supports+ // more than 32kb.+ max_total_threadgroup_memory: if Self::supports_any(+ device,+ &[+ MTLFeatureSet::iOS_GPUFamily4_v1,+ MTLFeatureSet::macOS_GPUFamily1_v1,+ ],+ ) {+ 32 << 10+ } else {+ 16 << 10+ },+ sample_count_mask,+ supports_debug_markers: Self::supports_any(+ device,+ &[+ MTLFeatureSet::macOS_GPUFamily1_v2,+ MTLFeatureSet::iOS_GPUFamily1_v3,+ MTLFeatureSet::tvOS_GPUFamily1_v2,+ ],+ ),+ // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=4+ supports_binary_archives: family_check+ && (metal3+ || device.supportsFamily(MTLGPUFamily::Apple3)+ || device.supportsFamily(MTLGPUFamily::Mac2)),+ // This is just trusted blindly since docs referencing supports_any have been removed+ // but we don't want to remove feature support.+ supports_arrays_of_textures: Self::supports_any(+ device,+ &[+ MTLFeatureSet::iOS_GPUFamily3_v2,+ MTLFeatureSet::tvOS_GPUFamily2_v1,+ MTLFeatureSet::macOS_GPUFamily1_v3,+ ],+ ),+ // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=3+ supports_arrays_of_textures_write: family_check+ && (metal3+ || device.supportsFamily(MTLGPUFamily::Apple6)+ || device.supportsFamily(MTLGPUFamily::Mac2)),+ // Depth clipping is supported on all macOS GPU families and iOS family 4 and later+ supports_depth_clip_control: os_type == super::OsType::Macos+ || device.supportsFeatureSet(MTLFeatureSet::iOS_GPUFamily4_v1),+ // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=4+ supports_shader_primitive_index: family_check+ && (metal3+ || device.supportsFamily(MTLGPUFamily::Apple7)+ || device.supportsFamily(MTLGPUFamily::Mac2)),+ // https://developer.apple.com/documentation/metal/mtldevice/hasunifiedmemory+ has_unified_memory: if available!(+ macos = 10.15,+ ios = 13.0,+ tvos = 13.0,+ visionos = 1.0+ ) && device_class_responds_to(device, sel!(hasUnifiedMemory))+ {+ Some(device.hasUnifiedMemory())+ } else {+ None+ },+ timestamp_query_support,+ // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=4+ supports_simd_scoped_operations: family_check+ && (metal3+ || device.supportsFamily(MTLGPUFamily::Mac2)+ || device.supportsFamily(MTLGPUFamily::Apple7)),+ // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=4+ int64: family_check && (metal3 || device.supportsFamily(MTLGPUFamily::Apple3)),+ // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=4+ // There is also a footnote that says+ // "Some GPU devices in the Apple8 family support 64-bit atomic minimum and maximum..."+ int64_atomics_min_max: family_check+ && (device.supportsFamily(MTLGPUFamily::Apple9)+ || (device.supportsFamily(MTLGPUFamily::Apple8)+ && device.supportsFamily(MTLGPUFamily::Mac2))),+ int64_atomics: family_check && device.supportsFamily(MTLGPUFamily::Apple9),+ // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=4+ float_atomics: family_check+ && (metal3+ || device.supportsFamily(MTLGPUFamily::Apple7)+ || device.supportsFamily(MTLGPUFamily::Mac2)),+ // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=5 (footnote)+ // Supported on some Metal4, Apple7, Mac2, and some other platforms can be queried with device.supportsShaderBarycentricCoordinates().+ shader_barycentrics: metal4+ || (family_check+ && (device.supportsFamily(MTLGPUFamily::Apple7)+ || device.supportsFamily(MTLGPUFamily::Mac2)))+ || (available!(macos = 10.15, ios = 14.0, tvos = 16.0, visionos = 1.0)+ && device_class_responds_to(+ device,+ sel!(supportsShaderBarycentricCoordinates),+ )+ && device.supportsShaderBarycentricCoordinates()),+ // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=3+ // See https://github.com/gfx-rs/wgpu/pull/8725 for more details+ supports_memoryless_storage: metal4+ || if family_check {+ // Apple A7 (MTLGPUFamily::Apple1) has been tested to have support.+ device.supportsFamily(MTLGPUFamily::Apple1)+ } else {+ // macOS: Always rely on family check+ // iOS/tvOS: API added in 10.0+ // visionOS: Always rely on family check+ available!(ios = 10.0, tvos = 10.0)+ },+ supported_vertex_amplification_factor: {+ let mut factor = 1;+ // https://developer.apple.com/documentation/metal/mtldevice/supportsvertexamplificationcount(_:)+ if available!(macos = 10.15.4, ios = 13.0, tvos = 16.0, visionos = 1.0) {+ // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=8+ // The table specifies either none, 2, 8, or unsupported, implying it is a relatively small power of 2+ // The bitmask only uses 32 bits, so it can't be higher even if the device for some reason claims to support that.+ while factor < 32 && device.supportsVertexAmplificationCount(factor * 2) {+ factor *= 2+ }+ }+ factor as u32+ },+ // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=4+ mesh_shaders,+ max_task_workgroup_count: if mesh_shaders+ && (metal4 || device.supportsFamily(MTLGPUFamily::Apple2))+ {+ u32::MAX+ } else if mesh_shaders {+ 1024+ } else {+ 0+ },+ max_mesh_workgroup_count: if mesh_shaders+ && device.supportsFamily(MTLGPUFamily::Apple10)+ {+ 2u32.pow(22)+ } else if mesh_shaders && device.supportsFamily(MTLGPUFamily::Apple9) {+ 2u32.pow(20)+ } else if mesh_shaders {+ 1024+ } else {+ 0+ },+ max_task_payload_size: if mesh_shaders { 16384 - 32 } else { 0 },+ supports_cooperative_matrix: family_check+ && (device.supportsFamily(MTLGPUFamily::Apple7)+ || device.supportsFamily(MTLGPUFamily::Mac2)),+ // https://developer.apple.com/documentation/metal/mtlresidencyset+ supports_raytracing: if available!(+ macos = 15.0,+ ios = 18.0,+ tvos = 18.0,+ visionos = 2.0,+ ) {+ device_class_responds_to(device, sel!(supportsRaytracing))+ && device.supportsRaytracing()+ && device_class_responds_to(device, sel!(supportsRaytracingFromRender))+ && device.supportsRaytracingFromRender()+ } else {+ false+ },+ shader_per_vertex: family_check && device.supportsFamily(MTLGPUFamily::Apple10),++ //https://developer.apple.com/documentation/metal/mtltexturetype/type2dmultisamplearray+ supports_multisample_array: available!(+ macos = 10.14,+ ios = 14.0,+ tvos = 16.0,+ visionos = 1.0+ ),+ }+ }++ pub fn features(&self) -> wgt::Features {+ use wgt::Features as F;++ let mut features = F::empty()+ | F::MAPPABLE_PRIMARY_BUFFERS+ | F::VERTEX_WRITABLE_STORAGE+ | F::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES+ | F::IMMEDIATES+ | F::POLYGON_MODE_LINE+ | F::CLEAR_TEXTURE+ | F::TEXTURE_FORMAT_16BIT_NORM+ | F::SHADER_F16+ | F::SHADER_I16+ | F::DEPTH32FLOAT_STENCIL8+ | F::BGRA8UNORM_STORAGE+ | F::PASSTHROUGH_SHADERS+ | F::EXTERNAL_TEXTURE;++ features.set(F::FLOAT32_FILTERABLE, self.supports_float_filtering);+ features.set(F::FLOAT32_BLENDABLE, true);+ features.set(F::INDIRECT_FIRST_INSTANCE, self.indirect_draw_dispatch);+ features.set(+ F::TIMESTAMP_QUERY,+ self.timestamp_query_support+ .contains(TimestampQuerySupport::STAGE_BOUNDARIES),+ );+ features.set(+ F::TIMESTAMP_QUERY_INSIDE_ENCODERS,+ self.timestamp_query_support+ .contains(TimestampQuerySupport::ON_BLIT_ENCODER),+ );+ features.set(+ F::TIMESTAMP_QUERY_INSIDE_PASSES,+ self.timestamp_query_support+ .contains(TimestampQuerySupport::INSIDE_WGPU_PASSES),+ );+ features.set(F::CLIP_DISTANCES, true);+ features.set(+ F::DUAL_SOURCE_BLENDING,+ self.msl_version >= MTLLanguageVersion::Version1_2 && self.dual_source_blending,+ );+ features.set(F::TEXTURE_COMPRESSION_ASTC, self.format_astc);+ features.set(F::TEXTURE_COMPRESSION_ASTC_HDR, self.format_astc_hdr);+ features.set(F::TEXTURE_COMPRESSION_ASTC_SLICED_3D, self.format_astc_3d);+ features.set(F::TEXTURE_COMPRESSION_BC, self.format_bc);+ features.set(F::TEXTURE_COMPRESSION_BC_SLICED_3D, self.format_bc); // BC guarantees Sliced 3D+ features.set(F::TEXTURE_COMPRESSION_ETC2, self.format_eac_etc);++ features.set(F::DEPTH_CLIP_CONTROL, self.supports_depth_clip_control);+ features.set(F::PRIMITIVE_INDEX, self.supports_shader_primitive_index);++ features.set(+ F::TEXTURE_BINDING_ARRAY+ | F::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING+ | F::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING+ | F::PARTIALLY_BOUND_BINDING_ARRAY,+ self.msl_version >= MTLLanguageVersion::Version3_0+ && self.supports_arrays_of_textures+ && self+ .argument_buffers+ .unwrap_or(MTLArgumentBuffersTier::Tier1)+ >= MTLArgumentBuffersTier::Tier2,+ );+ features.set(+ F::BUFFER_BINDING_ARRAY,+ self.msl_version >= MTLLanguageVersion::Version2_0+ && self+ .argument_buffers+ .unwrap_or(MTLArgumentBuffersTier::Tier1)+ >= MTLArgumentBuffersTier::Tier2,+ );+ features.set(+ F::STORAGE_RESOURCE_BINDING_ARRAY,+ self.msl_version >= MTLLanguageVersion::Version3_0+ && self.supports_arrays_of_textures+ && self.supports_arrays_of_textures_write+ && self+ .argument_buffers+ .unwrap_or(MTLArgumentBuffersTier::Tier1)+ >= MTLArgumentBuffersTier::Tier2,+ );+ features.set(+ F::SHADER_INT64,+ self.int64 && self.msl_version >= MTLLanguageVersion::Version2_3,+ );+ features.set(+ F::SHADER_INT64_ATOMIC_MIN_MAX,+ self.int64_atomics_min_max && self.msl_version >= MTLLanguageVersion::Version2_4,+ );+ features.set(+ F::TEXTURE_INT64_ATOMIC,+ self.int64_atomics && self.msl_version >= MTLLanguageVersion::Version3_1,+ );+ features.set(+ F::TEXTURE_ATOMIC,+ self.msl_version >= MTLLanguageVersion::Version3_1,+ );+ features.set(+ F::SHADER_FLOAT32_ATOMIC,+ self.float_atomics && self.msl_version >= MTLLanguageVersion::Version3_0,+ );+ features.set(+ F::MEMORY_DECORATION_COHERENT,+ self.msl_version >= MTLLanguageVersion::Version3_2,+ );++ features.set(+ F::ADDRESS_MODE_CLAMP_TO_BORDER,+ self.sampler_clamp_to_border,+ );+ features.set(F::ADDRESS_MODE_CLAMP_TO_ZERO, true);++ features.set(F::RG11B10UFLOAT_RENDERABLE, self.format_rg11b10_all);++ features.set(+ F::SHADER_BARYCENTRICS,+ self.shader_barycentrics && self.msl_version >= MTLLanguageVersion::Version2_2,+ );++ features.set(F::SHADER_PER_VERTEX, self.shader_per_vertex);++ if self.supports_simd_scoped_operations {+ features.insert(F::SUBGROUP | F::SUBGROUP_BARRIER);+ }++ features.set(+ F::EXPERIMENTAL_MESH_SHADER | F::EXPERIMENTAL_MESH_SHADER_POINTS,+ self.mesh_shaders,+ );+ features.set(+ F::EXPERIMENTAL_MESH_SHADER_MULTIVIEW,+ self.supported_vertex_amplification_factor > 1 && self.mesh_shaders,+ );++ // Cooperative matrix (simdgroup matrix) requires MSL 2.3++ features.set(+ F::EXPERIMENTAL_COOPERATIVE_MATRIX,+ self.supports_cooperative_matrix && self.msl_version >= MTLLanguageVersion::Version2_3,+ );++ if self.supported_vertex_amplification_factor > 1 {+ features.insert(F::MULTIVIEW);+ }++ features.set(F::EXPERIMENTAL_RAY_QUERY, self.supports_raytracing);++ features.set(F::MULTISAMPLE_ARRAY, self.supports_multisample_array);++ features+ }++ pub fn capabilities(&self, instance_flags: wgt::InstanceFlags) -> crate::Capabilities {+ let mut downlevel = wgt::DownlevelCapabilities::default();+ downlevel.flags.set(+ wgt::DownlevelFlags::FRAGMENT_WRITABLE_STORAGE,+ self.fragment_rw_storage,+ );+ downlevel.flags.set(+ wgt::DownlevelFlags::CUBE_ARRAY_TEXTURES,+ self.texture_cube_array,+ );+ // TODO: separate the mutable comparisons from immutable ones+ downlevel.flags.set(+ wgt::DownlevelFlags::COMPARISON_SAMPLERS,+ self.mutable_comparison_samplers,+ );+ downlevel.flags.set(+ wgt::DownlevelFlags::INDIRECT_EXECUTION,+ self.indirect_draw_dispatch,+ );+ // TODO: add another flag for `first_instance`+ downlevel.flags.set(+ wgt::DownlevelFlags::BASE_VERTEX,+ self.base_vertex_first_instance_drawing,+ );+ downlevel+ .flags+ .set(wgt::DownlevelFlags::ANISOTROPIC_FILTERING, true);++ downlevel.flags.set(+ wgt::DownlevelFlags::MSL2_1,+ self.msl_version >= MTLLanguageVersion::Version2_1,+ );+ downlevel.flags.set(+ wgt::DownlevelFlags::TEXTURE_COMPRESSION,+ self.format_bc || (self.format_eac_etc && self.format_astc),+ );++ let max_storage_buffers_per_shader_stage;+ let max_uniform_buffers_per_shader_stage;+ let max_vertex_buffers;+ let max_acceleration_structures_per_shader_stage;+ let max_buffers_and_acceleration_structures_per_shader_stage;++ // Metal has a single buffer limit that we must split across 3 WebGPU limits:+ // - maxStorageBuffersPerShaderStage; must be at least 8+ // - maxUniformBuffersPerShaderStage; must be at least 12+ // - maxVertexBuffers; must be at least 8+ // We also have to reserve 2 additional internal buffers:+ // - one for immediate data+ // - one for sizes of other buffers+ if instance_flags.contains(wgt::InstanceFlags::STRICT_WEBGPU_COMPLIANCE) {+ max_storage_buffers_per_shader_stage = 9;+ max_uniform_buffers_per_shader_stage = 12;+ max_vertex_buffers = 8;+ max_acceleration_structures_per_shader_stage = 0;+ max_buffers_and_acceleration_structures_per_shader_stage = u32::MAX;+ } else {+ const MAX_USABLE_BUFFERS: u32 = MAX_BUFFERS - 2;+ max_storage_buffers_per_shader_stage = MAX_USABLE_BUFFERS;+ max_uniform_buffers_per_shader_stage = MAX_USABLE_BUFFERS;+ max_vertex_buffers = MAX_USABLE_BUFFERS;+ max_acceleration_structures_per_shader_stage = MAX_USABLE_BUFFERS;+ max_buffers_and_acceleration_structures_per_shader_stage = MAX_USABLE_BUFFERS;+ }++ let limits = crate::auxil::adjust_raw_limits(wgt::Limits {+ //+ // WebGPU LIMITS:+ // Based on https://gpuweb.github.io/gpuweb/correspondence/#limits+ //+ max_texture_dimension_1d: self.max_texture_size as u32,+ max_texture_dimension_2d: self.max_texture_size as u32,+ max_texture_dimension_3d: self.max_texture_3d_size as u32,+ max_texture_array_layers: self.max_texture_layers as u32,+ // No limit.+ max_bind_groups: u32::MAX,+ // No limit. Once we start using argument buffers we should set this appropriately.+ max_bind_groups_plus_vertex_buffers: u32::MAX,+ // No limit.+ max_bindings_per_bind_group: u32::MAX,+ // No limit, use maxUniformBuffersPerShaderStage.+ max_dynamic_uniform_buffers_per_pipeline_layout: max_uniform_buffers_per_shader_stage,+ // No limit, use maxStorageBuffersPerShaderStage.+ max_dynamic_storage_buffers_per_pipeline_layout: max_storage_buffers_per_shader_stage,+ // "Maximum number of entries in the sampler state argument table, per graphics or kernel function"+ max_samplers_per_shader_stage: 16,+ max_sampled_textures_per_shader_stage: self.max_textures_per_stage.0,+ max_storage_textures_per_shader_stage: self.max_textures_per_stage.1,+ max_storage_buffers_per_shader_stage,+ max_uniform_buffers_per_shader_stage,+ max_vertex_buffers,+ max_buffer_size: self.max_buffer_size,+ // No limit, use maxBufferSize.+ max_uniform_buffer_binding_size: self.max_buffer_size,+ // No limit, use maxBufferSize.+ max_storage_buffer_binding_size: self.max_buffer_size,+ min_uniform_buffer_offset_alignment: self.constant_buffer_offset_alignment,+ // No documented limit. Use 32, which is the lowest allowed value.+ min_storage_buffer_offset_alignment: 32,+ // "Maximum number of vertex attributes, per vertex descriptor"+ max_vertex_attributes: 31,+ // No documented limit, matches Vulkan's minimum limit and D3D12's static limit.+ max_vertex_buffer_array_stride: 2048,+ max_inter_stage_shader_variables: self.max_inter_stage_shader_variables,+ max_color_attachments: self.max_color_render_targets as u32,+ max_color_attachment_bytes_per_sample: self.max_color_attachment_bytes_per_sample+ as u32,+ max_compute_workgroup_storage_size: self.max_total_threadgroup_memory,+ max_compute_invocations_per_workgroup: self.max_threads_per_group,+ max_compute_workgroup_size_x: self.max_threads_per_group,+ max_compute_workgroup_size_y: self.max_threads_per_group,+ max_compute_workgroup_size_z: self.max_threads_per_group,+ // No documented limit, matches Vulkan's minimum limit and D3D12's static limit.+ max_compute_workgroups_per_dimension: 0xFFFF,+ max_immediate_size: 0x1000,+ //+ // NATIVE (Non-WebGPU) LIMITS:+ //+ max_non_sampler_bindings: u32::MAX,++ max_binding_array_elements_per_shader_stage: self.max_binding_array_elements,+ max_binding_array_sampler_elements_per_shader_stage: self+ .max_sampler_binding_array_elements,+ max_binding_array_acceleration_structure_elements_per_shader_stage: 0,++ // from https://developer.apple.com/documentation/metal/mtlaccelerationstructureusage/extendedlimits+ max_blas_primitive_count: 1 << 28,+ max_blas_geometry_count: 1 << 24,+ max_tlas_instance_count: 1 << 24,+ // From 2.17.7 in https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf+ // > [Acceleration structures] are opaque objects that can be bound directly using+ // buffer binding points or via argument buffers+ max_acceleration_structures_per_shader_stage,+ max_buffers_and_acceleration_structures_per_shader_stage,++ max_multiview_view_count: if self.supported_vertex_amplification_factor > 1 {+ self.supported_vertex_amplification_factor+ } else {+ 0+ },++ // Should be not too large+ max_task_workgroup_total_count: self.max_task_workgroup_count,+ max_task_workgroups_per_dimension: self.max_task_workgroup_count,+ max_mesh_workgroup_total_count: self.max_mesh_workgroup_count,+ max_mesh_workgroups_per_dimension: self.max_mesh_workgroup_count,+ max_task_invocations_per_workgroup: if self.mesh_shaders { 1024 } else { 0 },+ max_task_invocations_per_dimension: if self.mesh_shaders { 1024 } else { 0 },+ max_mesh_invocations_per_workgroup: if self.mesh_shaders { 1024 } else { 0 },+ max_mesh_invocations_per_dimension: if self.mesh_shaders { 1024 } else { 0 },+ // Using certain variables or debuggers can reduce the size by 32 bytes+ max_task_payload_size: self.max_task_payload_size,+ max_mesh_output_vertices: 256,+ max_mesh_output_primitives: 256,+ max_mesh_output_layers: self.max_texture_layers as u32,+ max_mesh_multiview_view_count: 0,+ // unimplemented+ max_ray_dispatch_count: 0,+ max_ray_recursion_depth: 0,+ });++ crate::Capabilities {+ limits,+ alignments: crate::Alignments {+ buffer_copy_offset: wgt::BufferSize::new(self.buffer_alignment).unwrap(),+ buffer_copy_pitch: wgt::BufferSize::new(4).unwrap(),+ // This backend has Naga incorporate bounds checks into the+ // Metal Shading Language it generates, so from `wgpu_hal`'s+ // users' point of view, references are tightly checked.+ uniform_bounds_check_alignment: wgt::BufferSize::new(1).unwrap(),+ raw_tlas_instance_size: u32::try_from(size_of::<+ MTLIndirectAccelerationStructureInstanceDescriptor,+ >())+ .unwrap(),+ ray_tracing_scratch_buffer_alignment: 1,+ // Not yet supported+ ray_tracing_pipeline_group_data_size: 0,+ ray_tracing_pipeline_group_data_alignment: 0,+ ray_tracing_pipeline_data_offset_alignment: 0,+ },+ downlevel,+ cooperative_matrix_properties: self.cooperative_matrix_properties(),+ }+ }++ /// Returns the supported cooperative matrix configurations for Metal.+ ///+ /// Metal's simdgroup_matrix supports 8x8 tiles with f16 and f32 element types.+ fn cooperative_matrix_properties(&self) -> Vec<wgt::CooperativeMatrixProperties> {+ if !self.supports_cooperative_matrix || self.msl_version < MTLLanguageVersion::Version2_3 {+ return Vec::new();+ }++ vec![+ // 8x8 f32 configuration+ wgt::CooperativeMatrixProperties {+ m_size: 8,+ n_size: 8,+ k_size: 8,+ ab_type: wgt::CooperativeScalarType::F32,+ cr_type: wgt::CooperativeScalarType::F32,+ saturating_accumulation: false,+ },+ // 8x8 f16 configuration+ wgt::CooperativeMatrixProperties {+ m_size: 8,+ n_size: 8,+ k_size: 8,+ ab_type: wgt::CooperativeScalarType::F16,+ cr_type: wgt::CooperativeScalarType::F16,+ saturating_accumulation: false,+ },+ // Mixed precision: f16 inputs, f32 accumulator+ wgt::CooperativeMatrixProperties {+ m_size: 8,+ n_size: 8,+ k_size: 8,+ ab_type: wgt::CooperativeScalarType::F16,+ cr_type: wgt::CooperativeScalarType::F32,+ saturating_accumulation: false,+ },+ ]+ }++ pub fn private_capabilities(&self) -> super::PrivateCapabilities {+ super::PrivateCapabilities {+ msl_version: self.msl_version,+ low_power: self.low_power,+ headless: self.headless,+ has_unified_memory: self.has_unified_memory,+ timestamp_query_support: self.timestamp_query_support,+ supports_memoryless_storage: self.supports_memoryless_storage,+ mesh_shaders: self.mesh_shaders,+ }+ }++ pub fn private_texture_format_capabilities(&self) -> super::PrivateTextureFormatCapabilities {+ super::PrivateTextureFormatCapabilities {+ read_write_texture_tier: self.read_write_texture_tier,+ sample_count_mask: self.sample_count_mask,+ int64_atomics: self.int64_atomics,+ msaa_desktop: self.msaa_desktop,+ msaa_apple3: self.msaa_apple3,+ msaa_apple7: self.msaa_apple7,+ format_r32float_all: self.format_r32float_all,+ format_rgba8_srgb_all: self.format_rgba8_srgb_all,+ format_rgb10a2_uint_write: self.format_rgb10a2_uint_write,+ format_rgb10a2_unorm_all: self.format_rgb10a2_unorm_all,+ format_rg11b10_all: self.format_rg11b10_all,+ format_rg32float_all: self.format_rg32float_all,+ format_rgba32float_all: self.format_rgba32float_all,+ format_depth16unorm: self.format_depth16unorm,+ format_depth16unorm_filter: self.format_depth16unorm_filter,+ format_depth32float_filter: self.format_depth32float_filter,+ format_depth24_stencil8: self.format_depth24_stencil8,+ format_bc: self.format_bc,+ format_eac_etc: self.format_eac_etc,+ format_astc: self.format_astc,+ format_astc_hdr: self.format_astc_hdr,+ }+ }+}++impl super::PrivateCapabilities {+ pub fn device_type(&self) -> wgt::DeviceType {+ if self.has_unified_memory.unwrap_or(self.low_power) {+ wgt::DeviceType::IntegratedGpu+ } else {+ wgt::DeviceType::DiscreteGpu+ }+ }+}++impl super::PrivateTextureFormatCapabilities {+ pub fn map_format(&self, format: wgt::TextureFormat) -> MTLPixelFormat {+ use wgt::TextureFormat as Tf;+ use MTLPixelFormat as MTL;+ match format {+ Tf::R8Unorm => MTL::R8Unorm,+ Tf::R8Snorm => MTL::R8Snorm,+ Tf::R8Uint => MTL::R8Uint,+ Tf::R8Sint => MTL::R8Sint,+ Tf::R16Uint => MTL::R16Uint,+ Tf::R16Sint => MTL::R16Sint,+ Tf::R16Unorm => MTL::R16Unorm,+ Tf::R16Snorm => MTL::R16Snorm,+ Tf::R16Float => MTL::R16Float,+ Tf::Rg8Unorm => MTL::RG8Unorm,+ Tf::Rg8Snorm => MTL::RG8Snorm,+ Tf::Rg8Uint => MTL::RG8Uint,+ Tf::Rg8Sint => MTL::RG8Sint,+ Tf::Rg16Unorm => MTL::RG16Unorm,+ Tf::Rg16Snorm => MTL::RG16Snorm,+ Tf::R32Uint => MTL::R32Uint,+ Tf::R32Sint => MTL::R32Sint,+ Tf::R32Float => MTL::R32Float,+ Tf::Rg16Uint => MTL::RG16Uint,+ Tf::Rg16Sint => MTL::RG16Sint,+ Tf::Rg16Float => MTL::RG16Float,+ Tf::Rgba8Unorm => MTL::RGBA8Unorm,+ Tf::Rgba8UnormSrgb => MTL::RGBA8Unorm_sRGB,+ Tf::Bgra8UnormSrgb => MTL::BGRA8Unorm_sRGB,+ Tf::Rgba8Snorm => MTL::RGBA8Snorm,+ Tf::Bgra8Unorm => MTL::BGRA8Unorm,+ Tf::Rgba8Uint => MTL::RGBA8Uint,+ Tf::Rgba8Sint => MTL::RGBA8Sint,+ Tf::Rgb10a2Uint => MTL::RGB10A2Uint,+ Tf::Rgb10a2Unorm => MTL::RGB10A2Unorm,+ Tf::Rg11b10Ufloat => MTL::RG11B10Float,+ // Ruint64 textures are emulated on metal+ Tf::R64Uint => MTL::RG32Uint,+ Tf::Rg32Uint => MTL::RG32Uint,+ Tf::Rg32Sint => MTL::RG32Sint,+ Tf::Rg32Float => MTL::RG32Float,+ Tf::Rgba16Uint => MTL::RGBA16Uint,+ Tf::Rgba16Sint => MTL::RGBA16Sint,+ Tf::Rgba16Unorm => MTL::RGBA16Unorm,+ Tf::Rgba16Snorm => MTL::RGBA16Snorm,+ Tf::Rgba16Float => MTL::RGBA16Float,+ Tf::Rgba32Uint => MTL::RGBA32Uint,+ Tf::Rgba32Sint => MTL::RGBA32Sint,+ Tf::Rgba32Float => MTL::RGBA32Float,+ Tf::Stencil8 => MTL::Stencil8,+ Tf::Depth16Unorm => MTL::Depth16Unorm,+ Tf::Depth32Float => MTL::Depth32Float,+ Tf::Depth32FloatStencil8 => MTL::Depth32Float_Stencil8,+ Tf::Depth24Plus => {+ if self.format_depth24_stencil8 {+ MTL::Depth24Unorm_Stencil8+ } else {+ MTL::Depth32Float+ }+ }+ Tf::Depth24PlusStencil8 => {+ if self.format_depth24_stencil8 {+ MTL::Depth24Unorm_Stencil8+ } else {+ MTL::Depth32Float_Stencil8+ }+ }+ Tf::NV12 => unreachable!(),+ Tf::P010 => unreachable!(),+ Tf::Rgb9e5Ufloat => MTL::RGB9E5Float,+ Tf::Bc1RgbaUnorm => MTL::BC1_RGBA,+ Tf::Bc1RgbaUnormSrgb => MTL::BC1_RGBA_sRGB,+ Tf::Bc2RgbaUnorm => MTL::BC2_RGBA,+ Tf::Bc2RgbaUnormSrgb => MTL::BC2_RGBA_sRGB,+ Tf::Bc3RgbaUnorm => MTL::BC3_RGBA,+ Tf::Bc3RgbaUnormSrgb => MTL::BC3_RGBA_sRGB,+ Tf::Bc4RUnorm => MTL::BC4_RUnorm,+ Tf::Bc4RSnorm => MTL::BC4_RSnorm,+ Tf::Bc5RgUnorm => MTL::BC5_RGUnorm,+ Tf::Bc5RgSnorm => MTL::BC5_RGSnorm,+ Tf::Bc6hRgbFloat => MTL::BC6H_RGBFloat,+ Tf::Bc6hRgbUfloat => MTL::BC6H_RGBUfloat,+ Tf::Bc7RgbaUnorm => MTL::BC7_RGBAUnorm,+ Tf::Bc7RgbaUnormSrgb => MTL::BC7_RGBAUnorm_sRGB,+ Tf::Etc2Rgb8Unorm => MTL::ETC2_RGB8,+ Tf::Etc2Rgb8UnormSrgb => MTL::ETC2_RGB8_sRGB,+ Tf::Etc2Rgb8A1Unorm => MTL::ETC2_RGB8A1,+ Tf::Etc2Rgb8A1UnormSrgb => MTL::ETC2_RGB8A1_sRGB,+ Tf::Etc2Rgba8Unorm => MTL::EAC_RGBA8,+ Tf::Etc2Rgba8UnormSrgb => MTL::EAC_RGBA8_sRGB,+ Tf::EacR11Unorm => MTL::EAC_R11Unorm,+ Tf::EacR11Snorm => MTL::EAC_R11Snorm,+ Tf::EacRg11Unorm => MTL::EAC_RG11Unorm,+ Tf::EacRg11Snorm => MTL::EAC_RG11Snorm,+ Tf::Astc { block, channel } => match channel {+ AstcChannel::Unorm => match block {+ AstcBlock::B4x4 => MTL::ASTC_4x4_LDR,+ AstcBlock::B5x4 => MTL::ASTC_5x4_LDR,+ AstcBlock::B5x5 => MTL::ASTC_5x5_LDR,+ AstcBlock::B6x5 => MTL::ASTC_6x5_LDR,+ AstcBlock::B6x6 => MTL::ASTC_6x6_LDR,+ AstcBlock::B8x5 => MTL::ASTC_8x5_LDR,+ AstcBlock::B8x6 => MTL::ASTC_8x6_LDR,+ AstcBlock::B8x8 => MTL::ASTC_8x8_LDR,+ AstcBlock::B10x5 => MTL::ASTC_10x5_LDR,+ AstcBlock::B10x6 => MTL::ASTC_10x6_LDR,+ AstcBlock::B10x8 => MTL::ASTC_10x8_LDR,+ AstcBlock::B10x10 => MTL::ASTC_10x10_LDR,+ AstcBlock::B12x10 => MTL::ASTC_12x10_LDR,+ AstcBlock::B12x12 => MTL::ASTC_12x12_LDR,+ },+ AstcChannel::UnormSrgb => match block {+ AstcBlock::B4x4 => MTL::ASTC_4x4_sRGB,+ AstcBlock::B5x4 => MTL::ASTC_5x4_sRGB,+ AstcBlock::B5x5 => MTL::ASTC_5x5_sRGB,+ AstcBlock::B6x5 => MTL::ASTC_6x5_sRGB,+ AstcBlock::B6x6 => MTL::ASTC_6x6_sRGB,+ AstcBlock::B8x5 => MTL::ASTC_8x5_sRGB,+ AstcBlock::B8x6 => MTL::ASTC_8x6_sRGB,+ AstcBlock::B8x8 => MTL::ASTC_8x8_sRGB,+ AstcBlock::B10x5 => MTL::ASTC_10x5_sRGB,+ AstcBlock::B10x6 => MTL::ASTC_10x6_sRGB,+ AstcBlock::B10x8 => MTL::ASTC_10x8_sRGB,+ AstcBlock::B10x10 => MTL::ASTC_10x10_sRGB,+ AstcBlock::B12x10 => MTL::ASTC_12x10_sRGB,+ AstcBlock::B12x12 => MTL::ASTC_12x12_sRGB,+ },+ AstcChannel::Hdr => match block {+ AstcBlock::B4x4 => MTL::ASTC_4x4_HDR,+ AstcBlock::B5x4 => MTL::ASTC_5x4_HDR,+ AstcBlock::B5x5 => MTL::ASTC_5x5_HDR,+ AstcBlock::B6x5 => MTL::ASTC_6x5_HDR,+ AstcBlock::B6x6 => MTL::ASTC_6x6_HDR,+ AstcBlock::B8x5 => MTL::ASTC_8x5_HDR,+ AstcBlock::B8x6 => MTL::ASTC_8x6_HDR,+ AstcBlock::B8x8 => MTL::ASTC_8x8_HDR,+ AstcBlock::B10x5 => MTL::ASTC_10x5_HDR,+ AstcBlock::B10x6 => MTL::ASTC_10x6_HDR,+ AstcBlock::B10x8 => MTL::ASTC_10x8_HDR,+ AstcBlock::B10x10 => MTL::ASTC_10x10_HDR,+ AstcBlock::B12x10 => MTL::ASTC_12x10_HDR,+ AstcBlock::B12x12 => MTL::ASTC_12x12_HDR,+ },+ },+ }+ }++ pub fn map_view_format(+ &self,+ format: wgt::TextureFormat,+ aspects: crate::FormatAspects,+ ) -> MTLPixelFormat {+ use crate::FormatAspects as Fa;+ use wgt::TextureFormat as Tf;+ use MTLPixelFormat as MTL;+ match (format, aspects) {+ // map combined depth-stencil format to their stencil-only format+ // see https://developer.apple.com/library/archive/documentation/Miscellaneous/Conceptual/MetalProgrammingGuide/WhatsNewiniOS10tvOS10andOSX1012/WhatsNewiniOS10tvOS10andOSX1012.html#//apple_ref/doc/uid/TP40014221-CH14-DontLinkElementID_77+ (Tf::Depth24PlusStencil8, Fa::STENCIL) => {+ if self.format_depth24_stencil8 {+ MTL::X24_Stencil8+ } else {+ MTL::X32_Stencil8+ }+ }+ (Tf::Depth32FloatStencil8, Fa::STENCIL) => MTL::X32_Stencil8,++ _ => self.map_format(format),+ }+ }+}++impl super::PrivateDisabilities {+ pub fn new(device: &ProtocolObject<dyn MTLDevice>) -> Self {+ let is_intel = device.name().to_string().starts_with("Intel");+ Self {+ broken_viewport_near_depth: is_intel+ && !device.supportsFeatureSet(MTLFeatureSet::macOS_GPUFamily1_v4),+ broken_layered_clear_image: is_intel,+ }+ }+}++impl super::OsType {+ fn new(version: NSOperatingSystemVersion, device: &ProtocolObject<dyn MTLDevice>) -> Self {+ // Metal was first introduced in OS X 10.11 and iOS 8. The current version number of visionOS is 1.0.0. Additionally,+ // on the Simulator, Apple only provides the Apple2 GPU capability, and the Apple2+ GPU capability covers the capabilities of Apple2.+ // Therefore, the following conditions can be used to determine if it is visionOS.+ // https://developer.apple.com/documentation/metal/developing_metal_apps_that_run_in_simulator+ let os_is_vision = version.majorVersion < 8 && device.supportsFamily(MTLGPUFamily::Apple2);+ let os_is_mac = device.supportsFeatureSet(MTLFeatureSet::macOS_GPUFamily1_v1);+ let os_is_tvos = device.supportsFeatureSet(MTLFeatureSet::tvOS_GPUFamily1_v1);+ if os_is_vision {+ Self::VisionOs+ } else if os_is_mac {+ Self::Macos+ } else if os_is_tvos {+ Self::Tvos+ } else {+ Self::Ios+ }+ }+}diff --git a/vendor/wgpu-hal/src/metal/command.rs b/vendor/wgpu-hal/src/metal/command.rsnew file mode 100644index 00000000..ba41a631--- /dev/null+++ b/vendor/wgpu-hal/src/metal/command.rs@@ -0,0 +1,1979 @@+use objc2::{+ rc::{autoreleasepool, Retained},+ runtime::ProtocolObject,+};+use objc2_foundation::{NSRange, NSString, NSUInteger};+use objc2_metal::{+ MTLAccelerationStructure, MTLAccelerationStructureCommandEncoder, MTLBlitCommandEncoder,+ MTLBlitPassDescriptor, MTLBuffer, MTLCommandBuffer, MTLCommandBufferStatus, MTLCommandEncoder,+ MTLCommandQueue, MTLComputeCommandEncoder, MTLComputePassDescriptor, MTLCounterDontSample,+ MTLDevice, MTLLoadAction, MTLPrimitiveType, MTLRenderCommandEncoder, MTLRenderPassDescriptor,+ MTLResidencySet, MTLResidencySetDescriptor, MTLSamplerState, MTLScissorRect, MTLSize,+ MTLStoreAction, MTLTexture, MTLVertexAmplificationViewMapping, MTLViewport,+ MTLVisibilityResultMode,+};++use super::{+ adapter::{self, MAX_BUFFERS},+ conv, TimestampQuerySupport,+};+use crate::CommandEncoder as _;+use alloc::{+ borrow::{Cow, ToOwned as _},+ sync::Arc,+ vec::Vec,+};+use core::{ops::Range, ptr::NonNull, sync::atomic};+use smallvec::SmallVec;++// has to match `Temp::binding_sizes`+const WORD_SIZE: usize = 4;++impl Default for super::CommandState {+ fn default() -> Self {+ Self {+ blit: None,+ acceleration_structure_builder: None,+ render: None,+ compute: None,+ raw_primitive_type: MTLPrimitiveType::Point,+ index: None,+ stage_infos: Default::default(),+ storage_buffer_length_map: Default::default(),+ vertex_buffer_size_map: Default::default(),+ immediates: Vec::new(),+ pending_timer_queries: Vec::new(),+ }+ }+}++/// Helper for passing encoders to `update_bind_group_state`.+///+/// Combines [`naga::ShaderStage`] and an encoder of the appropriate type for+/// that stage.+enum Encoder<'e> {+ Vertex(&'e ProtocolObject<dyn MTLRenderCommandEncoder>),+ Fragment(&'e ProtocolObject<dyn MTLRenderCommandEncoder>),+ Task(&'e ProtocolObject<dyn MTLRenderCommandEncoder>),+ Mesh(&'e ProtocolObject<dyn MTLRenderCommandEncoder>),+ Compute(&'e ProtocolObject<dyn MTLComputeCommandEncoder>),+}++impl Encoder<'_> {+ fn stage(&self) -> naga::ShaderStage {+ match self {+ Self::Vertex(_) => naga::ShaderStage::Vertex,+ Self::Fragment(_) => naga::ShaderStage::Fragment,+ Self::Task(_) => naga::ShaderStage::Task,+ Self::Mesh(_) => naga::ShaderStage::Mesh,+ Self::Compute(_) => naga::ShaderStage::Compute,+ }+ }++ fn set_buffer(+ &self,+ buffer: Option<&ProtocolObject<dyn MTLBuffer>>,+ offset: NSUInteger,+ index: NSUInteger,+ ) {+ unsafe {+ match *self {+ Self::Vertex(enc) => enc.setVertexBuffer_offset_atIndex(buffer, offset, index),+ Self::Fragment(enc) => enc.setFragmentBuffer_offset_atIndex(buffer, offset, index),+ Self::Task(enc) => enc.setObjectBuffer_offset_atIndex(buffer, offset, index),+ Self::Mesh(enc) => enc.setMeshBuffer_offset_atIndex(buffer, offset, index),+ Self::Compute(enc) => enc.setBuffer_offset_atIndex(buffer, offset, index),+ }+ }+ }++ fn set_acceleration_structure(+ &self,+ buffer: Option<&ProtocolObject<dyn MTLAccelerationStructure>>,+ index: NSUInteger,+ ) {+ unsafe {+ match *self {+ Self::Vertex(enc) => {+ enc.setVertexAccelerationStructure_atBufferIndex(buffer, index)+ }+ Self::Fragment(enc) => {+ enc.setFragmentAccelerationStructure_atBufferIndex(buffer, index)+ }+ Self::Task(_) => {+ unreachable!("Acceleration structures are not allowed in task shaders")+ }+ Self::Mesh(_) => {+ unreachable!("Acceleration structures are not allowed in mesh shaders")+ }+ Self::Compute(enc) => enc.setAccelerationStructure_atBufferIndex(buffer, index),+ }+ }+ }++ fn set_bytes(&self, bytes: NonNull<core::ffi::c_void>, length: NSUInteger, index: NSUInteger) {+ unsafe {+ match *self {+ Self::Vertex(enc) => enc.setVertexBytes_length_atIndex(bytes, length, index),+ Self::Fragment(enc) => enc.setFragmentBytes_length_atIndex(bytes, length, index),+ Self::Task(enc) => enc.setObjectBytes_length_atIndex(bytes, length, index),+ Self::Mesh(enc) => enc.setMeshBytes_length_atIndex(bytes, length, index),+ Self::Compute(enc) => enc.setBytes_length_atIndex(bytes, length, index),+ }+ }+ }++ fn set_sampler_state(+ &self,+ state: Option<&ProtocolObject<dyn MTLSamplerState>>,+ index: NSUInteger,+ ) {+ unsafe {+ match *self {+ Self::Vertex(enc) => enc.setVertexSamplerState_atIndex(state, index),+ Self::Fragment(enc) => enc.setFragmentSamplerState_atIndex(state, index),+ Self::Task(enc) => enc.setObjectSamplerState_atIndex(state, index),+ Self::Mesh(enc) => enc.setMeshSamplerState_atIndex(state, index),+ Self::Compute(enc) => enc.setSamplerState_atIndex(state, index),+ }+ }+ }++ fn set_texture(&self, texture: Option<&ProtocolObject<dyn MTLTexture>>, index: NSUInteger) {+ unsafe {+ match *self {+ Self::Vertex(enc) => enc.setVertexTexture_atIndex(texture, index),+ Self::Fragment(enc) => enc.setFragmentTexture_atIndex(texture, index),+ Self::Task(enc) => enc.setObjectTexture_atIndex(texture, index),+ Self::Mesh(enc) => enc.setMeshTexture_atIndex(texture, index),+ Self::Compute(enc) => enc.setTexture_atIndex(texture, index),+ }+ }+ }+}++impl super::CommandEncoder {+ pub fn raw_command_buffer(&self) -> Option<&ProtocolObject<dyn MTLCommandBuffer>> {+ self.raw_cmd_buf.as_deref()+ }++ fn enter_blit(&mut self) -> Retained<ProtocolObject<dyn MTLBlitCommandEncoder>> {+ if self.state.blit.is_none() {+ self.leave_acceleration_structure_builder();+ debug_assert!(self.state.render.is_none() && self.state.compute.is_none());+ let cmd_buf = self.raw_cmd_buf.as_ref().unwrap();++ // This code path is currently unused since the written timestamp is always 0.+ // We should see if we can get it working again, if not remove the code.+ //+ // Take care of pending timer queries.+ // If we can't use `sample_counters_in_buffer` we have to create a dummy blit encoder!+ //+ // There is a known bug in Metal where blit encoders won't write timestamps if they don't have a blit operation.+ // See https://github.com/gpuweb/gpuweb/issues/2046#issuecomment-1205793680 & https://source.chromium.org/chromium/chromium/src/+/006c4eb70c96229834bbaf271290f40418144cd3:third_party/dawn/src/dawn/native/metal/BackendMTL.mm;l=350+ //+ // To make things worse:+ // * what counts as a blit operation is a bit unclear, experimenting seemed to indicate that resolve_counters doesn't count.+ // * in some cases (when?) using `set_start_of_encoder_sample_index` doesn't work, so we have to use `set_end_of_encoder_sample_index` instead+ //+ // All this means that pretty much the only *reliable* thing as of writing is to:+ // * create a dummy blit encoder using set_end_of_encoder_sample_index+ // * do a dummy write that is known to be not optimized out.+ // * close the encoder since we used set_end_of_encoder_sample_index and don't want to get any extra stuff in there.+ // * create another encoder for whatever we actually had in mind.+ let supports_sample_counters_in_buffer = self+ .shared+ .private_caps+ .timestamp_query_support+ .contains(TimestampQuerySupport::ON_BLIT_ENCODER);++ if !self.state.pending_timer_queries.is_empty() && !supports_sample_counters_in_buffer {+ autoreleasepool(|_| {+ let descriptor = MTLBlitPassDescriptor::new();+ let mut last_query = None;+ for (i, (set, index)) in self.state.pending_timer_queries.drain(..).enumerate()+ {+ let sba_descriptor = unsafe {+ descriptor+ .sampleBufferAttachments()+ .objectAtIndexedSubscript(i)+ };+ sba_descriptor+ .setSampleBuffer(Some(set.counter_sample_buffer.as_ref().unwrap()));++ // Here be dragons:+ // As mentioned above, for some reasons using the start of the encoder won't yield any results sometimes!+ unsafe {+ sba_descriptor.setStartOfEncoderSampleIndex(MTLCounterDontSample)+ };+ unsafe { sba_descriptor.setEndOfEncoderSampleIndex(index as _) };++ last_query = Some((set, index));+ }+ let encoder = cmd_buf+ .blitCommandEncoderWithDescriptor(&descriptor)+ .unwrap();++ // As explained above, we need to do some write:+ // Conveniently, we have a buffer with every query set, that we can use for this for a dummy write,+ // since we know that it is going to be overwritten again on timer resolve and HAL doesn't define its state before that.+ let raw_range = NSRange {+ location: last_query.as_ref().unwrap().1 as usize+ * crate::QUERY_SIZE as usize,+ length: 1,+ };+ encoder.fillBuffer_range_value(+ &last_query.as_ref().unwrap().0.raw_buffer,+ raw_range,+ 255, // Don't write 0, so it's easier to identify if something went wrong.+ );++ encoder.endEncoding();+ });+ }++ autoreleasepool(|_| {+ self.state.blit = Some(cmd_buf.blitCommandEncoder().unwrap());+ });++ #[allow(clippy::panicking_unwrap, reason = "false positive (fixed by 1.93.1)")]+ let encoder = self.state.blit.as_ref().unwrap();++ // UNTESTED:+ // If the above described issue with empty blit encoder applies to `sample_counters_in_buffer` as well, we should use the same workaround instead!+ for (set, index) in self.state.pending_timer_queries.drain(..) {+ debug_assert!(supports_sample_counters_in_buffer);+ unsafe {+ encoder.sampleCountersInBuffer_atSampleIndex_withBarrier(+ set.counter_sample_buffer.as_ref().unwrap(),+ index as _,+ true,+ )+ };+ }+ }+ self.state.blit.as_ref().unwrap().clone()+ }++ pub(super) fn leave_blit(&mut self) {+ if let Some(encoder) = self.state.blit.take() {+ encoder.endEncoding();+ }+ }++ fn enter_acceleration_structure_builder(+ &mut self,+ ) -> Retained<ProtocolObject<dyn MTLAccelerationStructureCommandEncoder>> {+ if self.state.acceleration_structure_builder.is_none() {+ self.leave_blit();+ debug_assert!(+ self.state.render.is_none()+ && self.state.compute.is_none()+ && self.state.blit.is_none()+ );+ let cmd_buf = self.raw_cmd_buf.as_ref().unwrap();+ autoreleasepool(|_| {+ self.state.acceleration_structure_builder =+ cmd_buf.accelerationStructureCommandEncoder().to_owned();+ });+ }+ self.state.acceleration_structure_builder.clone().unwrap()+ }++ pub(super) fn leave_acceleration_structure_builder(&mut self) {+ if let Some(encoder) = self.state.acceleration_structure_builder.take() {+ encoder.endEncoding();+ }+ }++ fn active_encoder(&mut self) -> Option<&ProtocolObject<dyn MTLCommandEncoder>> {+ if let Some(ref encoder) = self.state.render {+ Some(ProtocolObject::from_ref(&**encoder))+ } else if let Some(ref encoder) = self.state.acceleration_structure_builder {+ Some(ProtocolObject::from_ref(&**encoder))+ } else if let Some(ref encoder) = self.state.compute {+ Some(ProtocolObject::from_ref(&**encoder))+ } else if let Some(ref encoder) = self.state.blit {+ Some(ProtocolObject::from_ref(&**encoder))+ } else {+ None+ }+ }++ fn begin_pass(&mut self) {+ self.state.reset();+ self.leave_blit();+ self.leave_acceleration_structure_builder();+ }++ /// Updates the bindings for a single shader stage, called in `set_bind_group`.+ fn update_bind_group_state(+ &mut self,+ encoder: Encoder<'_>,+ index_base: super::ResourceData<u32>,+ bg_info: &super::BindGroupLayoutInfo,+ dynamic_offsets: &[wgt::DynamicOffset],+ group_index: u32,+ group: &super::BindGroup,+ ) {+ use naga::ShaderStage as S;+ let resource_indices = match encoder.stage() {+ S::Vertex => &bg_info.base_resource_indices.vs,+ S::Fragment => &bg_info.base_resource_indices.fs,+ S::Task => &bg_info.base_resource_indices.ts,+ S::Mesh => &bg_info.base_resource_indices.ms,+ S::Compute => &bg_info.base_resource_indices.cs,+ S::RayGeneration | S::AnyHit | S::ClosestHit | S::Miss => unimplemented!(),+ };+ let buffers = match encoder.stage() {+ S::Vertex => group.counters.vs.buffers,+ S::Fragment => group.counters.fs.buffers,+ S::Task => group.counters.ts.buffers,+ S::Mesh => group.counters.ms.buffers,+ S::Compute => group.counters.cs.buffers,+ S::RayGeneration | S::AnyHit | S::ClosestHit | S::Miss => unimplemented!(),+ };+ let mut changes_sizes_buffer = false;+ for index in 0..buffers {+ let res = &group.buffers[(index_base.buffers + index) as usize];+ match res {+ super::BufferLikeResource::Buffer {+ ptr,+ mut offset,+ dynamic_index,+ binding_size,+ binding_location,+ } => {+ let buffer = Some(unsafe { ptr.as_ref() });+ if let Some(dyn_index) = dynamic_index {+ offset += dynamic_offsets[*dyn_index as usize] as wgt::BufferAddress;+ }+ let index = (resource_indices.buffers + index) as usize;+ encoder.set_buffer(buffer, offset as usize, index);+ let br = naga::ResourceBinding {+ group: group_index,+ binding: *binding_location,+ };+ if let Some(size) = binding_size {+ self.state.storage_buffer_length_map.insert((br, 0), *size);+ changes_sizes_buffer = true;+ }+ }+ super::BufferLikeResource::StorageBindingArray {+ ptr,+ array_element_sizes,+ binding_location,+ } => {+ let buffer = Some(unsafe { ptr.as_ref() });+ let index = (resource_indices.buffers + index) as usize;+ encoder.set_buffer(buffer, 0, index);+ let br = naga::ResourceBinding {+ group: group_index,+ binding: *binding_location,+ };+ for &(array_idx, size) in array_element_sizes {+ self.state+ .storage_buffer_length_map+ .insert((br, array_idx), size);+ changes_sizes_buffer = true;+ }+ }+ super::BufferLikeResource::AccelerationStructure(ptr) => {+ let buffer = Some(unsafe { ptr.as_ref() });+ let index = (resource_indices.buffers + index) as usize;+ encoder.set_acceleration_structure(buffer, index);+ }+ }+ }+ if changes_sizes_buffer {+ if let Some((index, sizes)) = self+ .state+ .make_sizes_buffer_update(encoder.stage(), &mut self.temp.binding_sizes)+ {+ let bytes_ptr = NonNull::new(sizes.as_ptr().cast_mut().cast()).unwrap();+ let length = sizes.len() * WORD_SIZE;+ let index = index as _;+ encoder.set_bytes(bytes_ptr, length, index);+ }+ }+ let samplers = match encoder.stage() {+ S::Vertex => group.counters.vs.samplers,+ S::Fragment => group.counters.fs.samplers,+ S::Task => group.counters.ts.samplers,+ S::Mesh => group.counters.ms.samplers,+ S::Compute => group.counters.cs.samplers,+ S::RayGeneration | S::AnyHit | S::ClosestHit | S::Miss => unimplemented!(),+ };+ for index in 0..samplers {+ let res = group.samplers[(index_base.samplers + index) as usize];+ let index = (resource_indices.samplers + index) as usize;+ let state = Some(unsafe { res.as_ref() });+ encoder.set_sampler_state(state, index);+ }++ let textures = match encoder.stage() {+ S::Vertex => group.counters.vs.textures,+ S::Fragment => group.counters.fs.textures,+ S::Task => group.counters.ts.textures,+ S::Mesh => group.counters.ms.textures,+ S::Compute => group.counters.cs.textures,+ S::RayGeneration | S::AnyHit | S::ClosestHit | S::Miss => unimplemented!(),+ };+ for index in 0..textures {+ let res = group.textures[(index_base.textures + index) as usize];+ let index = (resource_indices.textures + index) as usize;+ let texture = Some(unsafe { res.as_ref() });+ encoder.set_texture(texture, index);+ }+ }+}++impl super::CommandState {+ fn reset(&mut self) {+ self.storage_buffer_length_map.clear();+ self.vertex_buffer_size_map.clear();+ self.stage_infos.vs.clear();+ self.stage_infos.fs.clear();+ self.stage_infos.cs.clear();+ self.stage_infos.ts.clear();+ self.stage_infos.ms.clear();+ self.immediates.clear();+ }++ fn make_sizes_buffer_update<'a>(+ &self,+ stage: naga::ShaderStage,+ result_sizes: &'a mut Vec<u32>,+ ) -> Option<(u32, &'a [u32])> {+ let stage_info = &self.stage_infos[stage];+ let slot = stage_info.sizes_slot?;++ result_sizes.clear();+ result_sizes.extend(stage_info.sized_bindings.iter().map(|(br, array_idx)| {+ self.storage_buffer_length_map+ .get(&(*br, *array_idx))+ .map(|size| u32::try_from(size.get()).unwrap_or(u32::MAX))+ .unwrap_or_default()+ }));++ // Extend with the sizes of the mapped vertex buffers, in the order+ // they were added to the map.+ result_sizes.extend(stage_info.vertex_buffer_mappings.iter().map(|vbm| {+ self.vertex_buffer_size_map+ .get(&vbm.id)+ .map(|size| u32::try_from(size.get()).unwrap_or(u32::MAX))+ .unwrap_or_default()+ }));++ if !result_sizes.is_empty() {+ Some((slot as _, result_sizes))+ } else {+ None+ }+ }+}++impl crate::CommandEncoder for super::CommandEncoder {+ type A = super::Api;++ unsafe fn begin_encoding(&mut self, label: crate::Label) -> Result<(), crate::DeviceError> {+ let queue = &self.queue_shared.raw;+ let retain_references = self.shared.settings.retain_command_buffer_references;+ let relay = self.queue_shared.relay.get();++ // Guard against exhausting Metal's command buffer budget. Use the hard+ // limit (`MAX_COMMAND_BUFFERS`) so we fail before Metal can hang inside+ // `new_command_buffer`.+ let previous = self+ .queue_shared+ .command_buffer_created_not_submitted+ .fetch_add(1, atomic::Ordering::AcqRel);+ if previous >= adapter::MAX_COMMAND_BUFFERS {+ let current = previous + 1;+ log::warn!(+ "metal: refusing to create new command buffer; {current} outstanding command \+ buffers exceeds the limit of {}. Treating this as device lost. \+ Ensure command encoders are submitted or dropped rather than kept alive \+ to avoid exhausting Metal's command buffer budget.",+ adapter::MAX_COMMAND_BUFFERS+ );+ return Err(crate::DeviceError::Lost);+ }++ let raw = autoreleasepool(move |_| {+ let cmd_buf_ref = if retain_references {+ queue.commandBuffer()+ } else {+ queue.commandBufferWithUnretainedReferences()+ }+ .unwrap();+ if let Some(label) = label {+ cmd_buf_ref.setLabel(Some(&NSString::from_str(label)));+ }+ // If strict event sync is enabled on this queue, gate the+ // CB on the relay event at the value the next submit will+ // signal. The CB pauses at the start until the relay fires.+ if let Some(relay) = relay {+ let expected = relay.next_release_value.load(atomic::Ordering::Acquire);+ cmd_buf_ref.encodeWaitForEvent_value(relay.event.as_ref(), expected);+ }+ cmd_buf_ref.to_owned()+ });++ // Queries should either be closed out, or cleared in `discard_encoding`.+ // This assertion is here mainly to facilitate comparison with the other+ // backends, which clear in `begin_encoding` rather than `discard_encoding`.+ debug_assert!(self.state.pending_timer_queries.is_empty());++ self.raw_cmd_buf = Some(raw);++ Ok(())+ }++ unsafe fn discard_encoding(&mut self) {+ self.leave_blit();+ self.leave_acceleration_structure_builder();+ // when discarding, we don't have a guarantee that+ // everything is in a good state, so check carefully+ if let Some(encoder) = self.state.render.take() {+ encoder.endEncoding();+ }+ if let Some(encoder) = self.state.compute.take() {+ encoder.endEncoding();+ }+ self.state.pending_timer_queries.clear();+ let had_command_buffer = self.raw_cmd_buf.is_some();+ // Clear the Option first so the underlying `metal::CommandBuffer` is+ // dropped before we update the counter.+ self.raw_cmd_buf = None;+ if had_command_buffer {+ self.queue_shared+ .command_buffer_created_not_submitted+ .fetch_sub(1, atomic::Ordering::AcqRel);+ }+ }++ unsafe fn end_encoding(&mut self) -> Result<super::CommandBuffer, crate::DeviceError> {+ // Handle pending timer query if any.+ if !self.state.pending_timer_queries.is_empty() {+ self.leave_blit();+ self.enter_blit();+ }++ self.leave_blit();+ self.leave_acceleration_structure_builder();+ debug_assert!(self.state.render.is_none());+ debug_assert!(self.state.compute.is_none());+ debug_assert!(self.state.pending_timer_queries.is_empty());++ Ok(super::CommandBuffer {+ raw: self.raw_cmd_buf.take().unwrap(),+ queue_shared: Arc::clone(&self.queue_shared),+ })+ }++ unsafe fn reset_all<I>(&mut self, _cmd_bufs: I)+ where+ I: Iterator<Item = super::CommandBuffer>,+ {+ //do nothing+ }++ unsafe fn transition_buffers<'a, T>(&mut self, _barriers: T)+ where+ T: Iterator<Item = crate::BufferBarrier<'a, super::Buffer>>,+ {+ }++ unsafe fn transition_textures<'a, T>(&mut self, _barriers: T)+ where+ T: Iterator<Item = crate::TextureBarrier<'a, super::Texture>>,+ {+ }++ unsafe fn clear_buffer(&mut self, buffer: &super::Buffer, range: crate::MemoryRange) {+ let encoder = self.enter_blit();+ encoder.fillBuffer_range_value(&buffer.raw, conv::map_range(&range), 0);+ }++ unsafe fn copy_buffer_to_buffer<T>(+ &mut self,+ src: &super::Buffer,+ dst: &super::Buffer,+ regions: T,+ ) where+ T: Iterator<Item = crate::BufferCopy>,+ {+ let encoder = self.enter_blit();+ for copy in regions {+ unsafe {+ encoder.copyFromBuffer_sourceOffset_toBuffer_destinationOffset_size(+ &src.raw,+ copy.src_offset as usize,+ &dst.raw,+ copy.dst_offset as usize,+ copy.size.get() as usize,+ )+ };+ }+ }++ unsafe fn copy_texture_to_texture<T>(+ &mut self,+ src: &super::Texture,+ _src_usage: wgt::TextureUses,+ dst: &super::Texture,+ regions: T,+ ) where+ T: Iterator<Item = crate::TextureCopy>,+ {+ let dst_texture = if src.format != dst.format {+ let raw_format = self+ .shared+ .private_texture_format_caps+ .map_format(src.format);+ Cow::Owned(autoreleasepool(|_| {+ dst.raw.newTextureViewWithPixelFormat(raw_format).unwrap()+ }))+ } else {+ Cow::Borrowed(&dst.raw)+ };+ let encoder = self.enter_blit();+ for copy in regions {+ let src_origin = conv::map_origin(©.src_base.origin);+ let dst_origin = conv::map_origin(©.dst_base.origin);+ // no clamping is done: Metal expects physical sizes here+ let extent = conv::map_copy_extent(©.size);+ unsafe {+ encoder.copyFromTexture_sourceSlice_sourceLevel_sourceOrigin_sourceSize_toTexture_destinationSlice_destinationLevel_destinationOrigin(+ &src.raw,+ copy.src_base.array_layer as usize,+ copy.src_base.mip_level as usize,+ src_origin,+ extent,+ &dst_texture,+ copy.dst_base.array_layer as usize,+ copy.dst_base.mip_level as usize,+ dst_origin,+ )+ };+ }+ }++ unsafe fn copy_buffer_to_texture<T>(+ &mut self,+ src: &super::Buffer,+ dst: &super::Texture,+ regions: T,+ ) where+ T: Iterator<Item = crate::BufferTextureCopy>,+ {+ let encoder = self.enter_blit();+ for copy in regions {+ let dst_origin = conv::map_origin(©.texture_base.origin);+ // Metal expects buffer-texture copies in virtual sizes+ let extent = copy+ .texture_base+ .max_copy_size(&dst.copy_size)+ .min(©.size);+ let bytes_per_row = copy.buffer_layout.bytes_per_row.unwrap_or(0) as u64;+ let image_byte_stride = if extent.depth > 1 {+ copy.buffer_layout+ .rows_per_image+ .map_or(0, |v| v as u64 * bytes_per_row)+ } else {+ // Don't pass a stride when updating a single layer, otherwise metal validation+ // fails when updating a subset of the image due to the stride being larger than+ // the amount of data to copy.+ 0+ };+ unsafe {+ encoder.copyFromBuffer_sourceOffset_sourceBytesPerRow_sourceBytesPerImage_sourceSize_toTexture_destinationSlice_destinationLevel_destinationOrigin_options(+ &src.raw,+ copy.buffer_layout.offset as usize,+ bytes_per_row as usize,+ image_byte_stride as usize,+ conv::map_copy_extent(&extent),+ &dst.raw,+ copy.texture_base.array_layer as usize,+ copy.texture_base.mip_level as usize,+ dst_origin,+ conv::get_blit_option(dst.format, copy.texture_base.aspect),+ )+ };+ }+ }++ unsafe fn copy_texture_to_buffer<T>(+ &mut self,+ src: &super::Texture,+ _src_usage: wgt::TextureUses,+ dst: &super::Buffer,+ regions: T,+ ) where+ T: Iterator<Item = crate::BufferTextureCopy>,+ {+ let encoder = self.enter_blit();+ for copy in regions {+ let src_origin = conv::map_origin(©.texture_base.origin);+ // Metal expects texture-buffer copies in virtual sizes+ let extent = copy+ .texture_base+ .max_copy_size(&src.copy_size)+ .min(©.size);+ let bytes_per_row = copy.buffer_layout.bytes_per_row.unwrap_or(0) as u64;+ let bytes_per_image = copy+ .buffer_layout+ .rows_per_image+ .map_or(0, |v| v as u64 * bytes_per_row);+ unsafe {+ encoder.copyFromTexture_sourceSlice_sourceLevel_sourceOrigin_sourceSize_toBuffer_destinationOffset_destinationBytesPerRow_destinationBytesPerImage_options(+ &src.raw,+ copy.texture_base.array_layer as usize,+ copy.texture_base.mip_level as usize,+ src_origin,+ conv::map_copy_extent(&extent),+ &dst.raw,+ copy.buffer_layout.offset as usize,+ bytes_per_row as usize,+ bytes_per_image as usize,+ conv::get_blit_option(src.format, copy.texture_base.aspect),+ )+ };+ }+ }++ unsafe fn copy_acceleration_structure_to_acceleration_structure(+ &mut self,+ src: &super::AccelerationStructure,+ dst: &super::AccelerationStructure,+ copy: wgt::AccelerationStructureCopy,+ ) {+ let command_encoder = self.enter_acceleration_structure_builder();+ match copy {+ wgt::AccelerationStructureCopy::Clone => unsafe {+ command_encoder+ .copyAccelerationStructure_toAccelerationStructure(&src.raw, &dst.raw);+ },+ wgt::AccelerationStructureCopy::Compact => {+ command_encoder.copyAndCompactAccelerationStructure_toAccelerationStructure(+ &src.raw, &dst.raw,+ );+ }+ };+ }++ unsafe fn begin_query(&mut self, set: &super::QuerySet, index: u32) {+ match set.ty {+ wgt::QueryType::Occlusion => {+ self.state+ .render+ .as_ref()+ .unwrap()+ .setVisibilityResultMode_offset(+ MTLVisibilityResultMode::Boolean,+ index as usize * crate::QUERY_SIZE as usize,+ );+ }+ _ => {}+ }+ }+ unsafe fn end_query(&mut self, set: &super::QuerySet, index: u32) {+ match set.ty {+ wgt::QueryType::Occlusion => {+ self.state+ .render+ .as_ref()+ .unwrap()+ .setVisibilityResultMode_offset(+ MTLVisibilityResultMode::Disabled,+ index as usize * crate::QUERY_SIZE as usize,+ );+ }+ _ => {}+ }+ }+ unsafe fn write_timestamp(&mut self, set: &super::QuerySet, index: u32) {+ let support = self.shared.private_caps.timestamp_query_support;+ debug_assert!(+ support.contains(TimestampQuerySupport::STAGE_BOUNDARIES),+ "Timestamp queries are not supported"+ );+ let sample_buffer = set.counter_sample_buffer.as_ref().unwrap();+ let with_barrier = true;++ // Try to use an existing encoder for timestamp query if possible.+ // This works only if it's supported for the active encoder.+ if let (true, Some(encoder)) = (+ support.contains(TimestampQuerySupport::ON_BLIT_ENCODER),+ self.state.blit.as_ref(),+ ) {+ unsafe {+ encoder.sampleCountersInBuffer_atSampleIndex_withBarrier(+ sample_buffer,+ index as _,+ with_barrier,+ )+ };+ } else if let (true, Some(encoder)) = (+ support.contains(TimestampQuerySupport::ON_RENDER_ENCODER),+ self.state.render.as_ref(),+ ) {+ unsafe {+ encoder.sampleCountersInBuffer_atSampleIndex_withBarrier(+ sample_buffer,+ index as _,+ with_barrier,+ )+ };+ } else if let (true, Some(encoder)) = (+ support.contains(TimestampQuerySupport::ON_COMPUTE_ENCODER),+ self.state.compute.as_ref(),+ ) {+ unsafe {+ encoder.sampleCountersInBuffer_atSampleIndex_withBarrier(+ sample_buffer,+ index as _,+ with_barrier,+ )+ };+ } else {+ // If we're here it means we either have no encoder open, or it's not supported to sample within them.+ // If this happens with render/compute open, this is an invalid usage!+ debug_assert!(self.state.render.is_none() && self.state.compute.is_none());++ // But otherwise it means we'll put defer this to the next created encoder.+ self.state.pending_timer_queries.push((set.clone(), index));++ // Ensure we didn't already have a blit open.+ self.leave_blit();+ };+ }++ unsafe fn reset_queries(&mut self, _set: &super::QuerySet, _range: Range<u32>) {}++ unsafe fn copy_query_results(+ &mut self,+ set: &super::QuerySet,+ range: Range<u32>,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ _: wgt::BufferSize, // Metal doesn't support queries that are bigger than a single element are not supported+ ) {+ let encoder = self.enter_blit();+ match set.ty {+ wgt::QueryType::Occlusion => {+ let size = (range.end - range.start) as u64 * crate::QUERY_SIZE;+ unsafe {+ encoder.copyFromBuffer_sourceOffset_toBuffer_destinationOffset_size(+ &set.raw_buffer,+ range.start as usize * crate::QUERY_SIZE as usize,+ &buffer.raw,+ offset as usize,+ size as usize,+ )+ };+ }+ wgt::QueryType::Timestamp => {+ unsafe {+ encoder.resolveCounters_inRange_destinationBuffer_destinationOffset(+ set.counter_sample_buffer.as_ref().unwrap(),+ NSRange::new(range.start as usize, (range.end - range.start) as usize),+ &buffer.raw,+ offset as usize,+ )+ };+ }+ wgt::QueryType::PipelineStatistics(_) => todo!(),+ }+ }++ // render++ unsafe fn begin_render_pass(+ &mut self,+ desc: &crate::RenderPassDescriptor<super::QuerySet, super::TextureView>,+ ) -> Result<(), crate::DeviceError> {+ self.begin_pass();+ self.state.index = None;++ assert!(self.state.blit.is_none());+ assert!(self.state.compute.is_none());+ assert!(self.state.render.is_none());++ autoreleasepool(|_| {+ let descriptor = MTLRenderPassDescriptor::new();++ for (i, at) in desc.color_attachments.iter().enumerate() {+ if let Some(at) = at.as_ref() {+ let at_descriptor =+ unsafe { descriptor.colorAttachments().objectAtIndexedSubscript(i) };+ at_descriptor.setTexture(Some(&at.target.view.raw));+ if let Some(depth_slice) = at.depth_slice {+ at_descriptor.setDepthPlane(depth_slice as usize);+ }+ if let Some(ref resolve) = at.resolve_target {+ //Note: the selection of levels and slices is already handled by `TextureView`+ at_descriptor.setResolveTexture(Some(&resolve.view.raw));+ }+ let load_action = if at.ops.contains(crate::AttachmentOps::LOAD) {+ MTLLoadAction::Load+ } else if at.ops.contains(crate::AttachmentOps::LOAD_DONT_CARE) {+ MTLLoadAction::DontCare+ } else if at.ops.contains(crate::AttachmentOps::LOAD_CLEAR) {+ at_descriptor.setClearColor(conv::map_clear_color(&at.clear_value));+ MTLLoadAction::Clear+ } else {+ unreachable!()+ };+ let store_action = conv::map_store_action(+ at.ops.contains(crate::AttachmentOps::STORE),+ at.resolve_target.is_some(),+ );+ at_descriptor.setLoadAction(load_action);+ at_descriptor.setStoreAction(store_action);+ }+ }++ if let Some(ref at) = desc.depth_stencil_attachment {+ if at.target.view.aspects.contains(crate::FormatAspects::DEPTH) {+ let at_descriptor = descriptor.depthAttachment();+ at_descriptor.setTexture(Some(&at.target.view.raw));++ let load_action = if at.depth_ops.contains(crate::AttachmentOps::LOAD) {+ MTLLoadAction::Load+ } else if at.depth_ops.contains(crate::AttachmentOps::LOAD_DONT_CARE) {+ MTLLoadAction::DontCare+ } else if at.depth_ops.contains(crate::AttachmentOps::LOAD_CLEAR) {+ at_descriptor.setClearDepth(at.clear_value.0 as f64);+ MTLLoadAction::Clear+ } else {+ unreachable!();+ };+ let store_action = if at.depth_ops.contains(crate::AttachmentOps::STORE) {+ MTLStoreAction::Store+ } else {+ MTLStoreAction::DontCare+ };+ at_descriptor.setLoadAction(load_action);+ at_descriptor.setStoreAction(store_action);+ }+ if at+ .target+ .view+ .aspects+ .contains(crate::FormatAspects::STENCIL)+ {+ let at_descriptor = descriptor.stencilAttachment();+ at_descriptor.setTexture(Some(&at.target.view.raw));++ let load_action = if at.stencil_ops.contains(crate::AttachmentOps::LOAD) {+ MTLLoadAction::Load+ } else if at+ .stencil_ops+ .contains(crate::AttachmentOps::LOAD_DONT_CARE)+ {+ MTLLoadAction::DontCare+ } else if at.stencil_ops.contains(crate::AttachmentOps::LOAD_CLEAR) {+ at_descriptor.setClearStencil(at.clear_value.1);+ MTLLoadAction::Clear+ } else {+ unreachable!()+ };+ let store_action = if at.stencil_ops.contains(crate::AttachmentOps::STORE) {+ MTLStoreAction::Store+ } else {+ MTLStoreAction::DontCare+ };+ at_descriptor.setLoadAction(load_action);+ at_descriptor.setStoreAction(store_action);+ }+ }++ let mut sba_index = 0;+ let mut next_sba_descriptor = || {+ let sba_descriptor = unsafe {+ descriptor+ .sampleBufferAttachments()+ .objectAtIndexedSubscript(sba_index)+ };++ unsafe { sba_descriptor.setEndOfVertexSampleIndex(MTLCounterDontSample) };+ unsafe { sba_descriptor.setStartOfFragmentSampleIndex(MTLCounterDontSample) };++ sba_index += 1;+ sba_descriptor+ };++ for (set, index) in self.state.pending_timer_queries.drain(..) {+ let sba_descriptor = next_sba_descriptor();+ sba_descriptor.setSampleBuffer(Some(set.counter_sample_buffer.as_ref().unwrap()));+ unsafe { sba_descriptor.setStartOfVertexSampleIndex(index as _) };+ unsafe { sba_descriptor.setEndOfFragmentSampleIndex(MTLCounterDontSample) };+ }++ if let Some(ref timestamp_writes) = desc.timestamp_writes {+ let sba_descriptor = next_sba_descriptor();+ sba_descriptor.setSampleBuffer(Some(+ timestamp_writes+ .query_set+ .counter_sample_buffer+ .as_ref()+ .unwrap(),+ ));++ unsafe {+ sba_descriptor.setStartOfVertexSampleIndex(+ timestamp_writes+ .beginning_of_pass_write_index+ .map_or(MTLCounterDontSample, |i| i as _),+ )+ };+ unsafe {+ sba_descriptor.setEndOfFragmentSampleIndex(+ timestamp_writes+ .end_of_pass_write_index+ .map_or(MTLCounterDontSample, |i| i as _),+ )+ };+ }++ if let Some(occlusion_query_set) = desc.occlusion_query_set {+ descriptor.setVisibilityResultBuffer(Some(occlusion_query_set.raw_buffer.as_ref()))+ }+ // This strangely isn't mentioned in https://developer.apple.com/documentation/metal/improving-rendering-performance-with-vertex-amplification.+ // The docs for [`renderTargetArrayLength`](https://developer.apple.com/documentation/metal/mtlrenderpassdescriptor/rendertargetarraylength)+ // also say "The number of active layers that all attachments must have for layered rendering," implying it is only for layered rendering.+ // However, when I don't set this, I get undefined behavior in nonzero layers, and all non-apple examples of vertex amplification set it.+ // So this is just one of those undocumented requirements.+ if let Some(mv) = desc.multiview_mask {+ descriptor.setRenderTargetArrayLength(32 - mv.leading_zeros() as usize);+ }+ let raw = self.raw_cmd_buf.as_ref().unwrap();+ let encoder = raw.renderCommandEncoderWithDescriptor(&descriptor).unwrap();+ if let Some(mv) = desc.multiview_mask {+ // Most likely the API just wasn't thought about enough. It's not like they ever allow you+ // to use enough views to overflow a 32-bit bitmask.+ let mv = mv.get();+ let msb = 32 - mv.leading_zeros();+ let mut maps: SmallVec<[MTLVertexAmplificationViewMapping; 32]> = SmallVec::new();+ for i in 0..msb {+ if (mv & (1 << i)) != 0 {+ maps.push(MTLVertexAmplificationViewMapping {+ renderTargetArrayIndexOffset: i,+ viewportArrayIndexOffset: i,+ });+ }+ }+ unsafe {+ encoder.setVertexAmplificationCount_viewMappings(+ mv.count_ones() as usize,+ maps.as_ptr(),+ )+ };+ }+ if let Some(label) = desc.label {+ encoder.setLabel(Some(&NSString::from_str(label)));+ }+ self.state.render = Some(encoder);+ });++ Ok(())+ }++ unsafe fn end_render_pass(&mut self) {+ self.state.render.take().unwrap().endEncoding();+ }++ unsafe fn set_bind_group(+ &mut self,+ layout: &super::PipelineLayout,+ group_index: u32,+ group: &super::BindGroup,+ dynamic_offsets: &[wgt::DynamicOffset],+ ) {+ let bg_info = layout.bind_group_infos[group_index as usize]+ .as_ref()+ .unwrap();+ let render_encoder = self.state.render.clone();+ let compute_encoder = self.state.compute.clone();+ if let Some(encoder) = render_encoder {+ self.update_bind_group_state(+ Encoder::Vertex(&encoder),+ // All zeros, as vs comes first+ super::ResourceData::default(),+ bg_info,+ dynamic_offsets,+ group_index,+ group,+ );+ self.update_bind_group_state(+ Encoder::Task(&encoder),+ // All zeros, as ts comes first+ super::ResourceData::default(),+ bg_info,+ dynamic_offsets,+ group_index,+ group,+ );+ self.update_bind_group_state(+ Encoder::Mesh(&encoder),+ group.counters.ts.clone(),+ bg_info,+ dynamic_offsets,+ group_index,+ group,+ );+ self.update_bind_group_state(+ Encoder::Fragment(&encoder),+ super::ResourceData {+ buffers: group.counters.vs.buffers+ + group.counters.ts.buffers+ + group.counters.ms.buffers,+ textures: group.counters.vs.textures+ + group.counters.ts.textures+ + group.counters.ms.textures,+ samplers: group.counters.vs.samplers+ + group.counters.ts.samplers+ + group.counters.ms.samplers,+ },+ bg_info,+ dynamic_offsets,+ group_index,+ group,+ );+ // Call useResource on all textures and buffers used indirectly so they are alive+ for (resource, use_info) in group.resources_to_use.iter() {+ encoder.useResource_usage_stages(+ unsafe { resource.as_ref() },+ use_info.uses,+ use_info.stages,+ );+ }+ }+ if let Some(encoder) = compute_encoder {+ self.update_bind_group_state(+ Encoder::Compute(&encoder),+ super::ResourceData {+ buffers: group.counters.vs.buffers+ + group.counters.ts.buffers+ + group.counters.ms.buffers+ + group.counters.fs.buffers,+ textures: group.counters.vs.textures+ + group.counters.ts.textures+ + group.counters.ms.textures+ + group.counters.fs.textures,+ samplers: group.counters.vs.samplers+ + group.counters.ts.samplers+ + group.counters.ms.samplers+ + group.counters.fs.samplers,+ },+ bg_info,+ dynamic_offsets,+ group_index,+ group,+ );+ // Call useResource on all textures and buffers used indirectly so they are alive+ for (resource, use_info) in group.resources_to_use.iter() {+ if !use_info.visible_in_compute {+ continue;+ }+ encoder.useResource_usage(unsafe { resource.as_ref() }, use_info.uses);+ }+ }+ }++ unsafe fn set_immediates(+ &mut self,+ layout: &super::PipelineLayout,+ offset_bytes: u32,+ data: &[u32],+ ) {+ let state_pc = &mut self.state.immediates;+ if state_pc.len() < layout.total_immediates as usize {+ state_pc.resize(layout.total_immediates as usize, 0);+ }+ debug_assert_eq!(offset_bytes as usize % WORD_SIZE, 0);++ let offset_words = offset_bytes as usize / WORD_SIZE;+ state_pc[offset_words..offset_words + data.len()].copy_from_slice(data);++ let bytes = NonNull::new(state_pc.as_ptr().cast_mut().cast()).unwrap();+ if let Some(ref compute) = self.state.compute {+ unsafe {+ compute.setBytes_length_atIndex(+ bytes,+ layout.total_immediates as usize * WORD_SIZE,+ layout.immediates_infos.cs.unwrap().buffer_index as usize,+ )+ };+ }+ if let Some(ref render) = self.state.render {+ if let Some(vs) = layout.immediates_infos.vs {+ unsafe {+ render.setVertexBytes_length_atIndex(+ bytes,+ layout.total_immediates as usize * WORD_SIZE,+ vs.buffer_index as _,+ )+ }+ }+ if let Some(fs) = layout.immediates_infos.fs {+ unsafe {+ render.setFragmentBytes_length_atIndex(+ bytes,+ layout.total_immediates as usize * WORD_SIZE,+ fs.buffer_index as _,+ )+ }+ }+ if let Some(ts) = layout.immediates_infos.ts {+ if self.shared.private_caps.mesh_shaders {+ unsafe {+ render.setObjectBytes_length_atIndex(+ bytes,+ layout.total_immediates as usize * WORD_SIZE,+ ts.buffer_index as _,+ )+ }+ }+ }+ if let Some(ms) = layout.immediates_infos.ms {+ if self.shared.private_caps.mesh_shaders {+ unsafe {+ render.setMeshBytes_length_atIndex(+ bytes,+ layout.total_immediates as usize * WORD_SIZE,+ ms.buffer_index as _,+ )+ }+ }+ }+ }+ }++ unsafe fn insert_debug_marker(&mut self, label: &str) {+ if let Some(encoder) = self.active_encoder() {+ encoder.insertDebugSignpost(&NSString::from_str(label));+ }+ }+ unsafe fn begin_debug_marker(&mut self, group_label: &str) {+ if let Some(encoder) = self.active_encoder() {+ encoder.pushDebugGroup(&NSString::from_str(group_label));+ } else if let Some(ref buf) = self.raw_cmd_buf {+ buf.pushDebugGroup(&NSString::from_str(group_label));+ }+ }+ unsafe fn end_debug_marker(&mut self) {+ if let Some(encoder) = self.active_encoder() {+ encoder.popDebugGroup();+ } else if let Some(ref buf) = self.raw_cmd_buf {+ buf.popDebugGroup();+ }+ }++ unsafe fn set_render_pipeline(&mut self, pipeline: &super::RenderPipeline) {+ self.state.raw_primitive_type = pipeline.raw_primitive_type;+ match pipeline.vs_info {+ Some(ref info) => self.state.stage_infos.vs.assign_from(info),+ None => self.state.stage_infos.vs.clear(),+ }+ match pipeline.fs_info {+ Some(ref info) => self.state.stage_infos.fs.assign_from(info),+ None => self.state.stage_infos.fs.clear(),+ }+ match pipeline.ts_info {+ Some(ref info) => self.state.stage_infos.ts.assign_from(info),+ None => self.state.stage_infos.ts.clear(),+ }+ match pipeline.ms_info {+ Some(ref info) => self.state.stage_infos.ms.assign_from(info),+ None => self.state.stage_infos.ms.clear(),+ }++ let encoder = self.state.render.as_ref().unwrap();+ encoder.setRenderPipelineState(&pipeline.raw);+ encoder.setFrontFacingWinding(pipeline.raw_front_winding);+ encoder.setCullMode(pipeline.raw_cull_mode);+ encoder.setTriangleFillMode(pipeline.raw_triangle_fill_mode);+ if let Some(depth_clip) = pipeline.raw_depth_clip_mode {+ encoder.setDepthClipMode(depth_clip);+ }+ if let Some((ref state, bias)) = pipeline.depth_stencil {+ encoder.setDepthStencilState(Some(state));+ encoder.setDepthBias_slopeScale_clamp(+ bias.constant as f32,+ bias.slope_scale,+ bias.clamp,+ );+ }++ if pipeline.vs_info.is_some() {+ if let Some((index, sizes)) = self+ .state+ .make_sizes_buffer_update(naga::ShaderStage::Vertex, &mut self.temp.binding_sizes)+ {+ unsafe {+ encoder.setVertexBytes_length_atIndex(+ NonNull::new(sizes.as_ptr().cast_mut().cast()).unwrap(),+ sizes.len() * WORD_SIZE,+ index as _,+ )+ };+ }+ }+ if pipeline.fs_info.is_some() {+ if let Some((index, sizes)) = self+ .state+ .make_sizes_buffer_update(naga::ShaderStage::Fragment, &mut self.temp.binding_sizes)+ {+ unsafe {+ encoder.setFragmentBytes_length_atIndex(+ NonNull::new(sizes.as_ptr().cast_mut().cast()).unwrap(),+ sizes.len() * WORD_SIZE,+ index as _,+ )+ };+ }+ }+ if let Some(ts_info) = &pipeline.ts_info {+ // update the threadgroup memory sizes+ while self.state.stage_infos.ms.work_group_memory_sizes.len()+ < ts_info.work_group_memory_sizes.len()+ {+ self.state.stage_infos.ms.work_group_memory_sizes.push(0);+ }+ for (index, (cur_size, pipeline_size)) in self+ .state+ .stage_infos+ .ms+ .work_group_memory_sizes+ .iter_mut()+ .zip(ts_info.work_group_memory_sizes.iter())+ .enumerate()+ {+ let size = pipeline_size.next_multiple_of(16);+ if *cur_size != size {+ *cur_size = size;+ unsafe { encoder.setObjectThreadgroupMemoryLength_atIndex(size as _, index) };+ }+ }+ if let Some((index, sizes)) = self+ .state+ .make_sizes_buffer_update(naga::ShaderStage::Task, &mut self.temp.binding_sizes)+ {+ unsafe {+ encoder.setObjectBytes_length_atIndex(+ NonNull::new(sizes.as_ptr().cast_mut().cast()).unwrap(),+ sizes.len() * WORD_SIZE,+ index as _,+ )+ };+ }+ }+ if let Some(_ms_info) = &pipeline.ms_info {+ // So there isn't an equivalent to+ // https://developer.apple.com/documentation/metal/mtlrendercommandencoder/setthreadgroupmemorylength(_:offset:index:)+ // for mesh shaders. This is probably because the CPU has less control over the dispatch sizes and such. Interestingly+ // it also affects mesh shaders without task/object shaders, even though none of compute, task or fragment shaders+ // behave this way.+ if let Some((index, sizes)) = self+ .state+ .make_sizes_buffer_update(naga::ShaderStage::Mesh, &mut self.temp.binding_sizes)+ {+ unsafe {+ encoder.setMeshBytes_length_atIndex(+ NonNull::new(sizes.as_ptr().cast_mut().cast()).unwrap(),+ sizes.len() * WORD_SIZE,+ index as _,+ )+ };+ }+ }+ }++ unsafe fn set_index_buffer<'a>(+ &mut self,+ binding: crate::BufferBinding<'a, super::Buffer>,+ format: wgt::IndexFormat,+ ) {+ let (stride, raw_type) = conv::map_index_format(format);+ self.state.index = Some(super::IndexState {+ buffer_ptr: NonNull::from(&*binding.buffer.raw),+ offset: binding.offset,+ stride,+ raw_type,+ });+ }++ unsafe fn set_vertex_buffer<'a>(+ &mut self,+ index: u32,+ binding: crate::BufferBinding<'a, super::Buffer>,+ ) {+ let buffer_index = MAX_BUFFERS - 1 - index;+ let encoder = self.state.render.as_ref().unwrap();+ unsafe {+ encoder.setVertexBuffer_offset_atIndex(+ Some(&binding.buffer.raw),+ binding.offset as usize,+ buffer_index as usize,+ )+ };++ let buffer_size = binding.resolve_size();+ if buffer_size > 0 {+ self.state.vertex_buffer_size_map.insert(+ buffer_index,+ core::num::NonZeroU64::new(buffer_size).unwrap(),+ );+ } else {+ self.state.vertex_buffer_size_map.remove(&buffer_index);+ }++ if let Some((index, sizes)) = self+ .state+ .make_sizes_buffer_update(naga::ShaderStage::Vertex, &mut self.temp.binding_sizes)+ {+ unsafe {+ encoder.setVertexBytes_length_atIndex(+ NonNull::new(sizes.as_ptr().cast_mut().cast()).unwrap(),+ sizes.len() * WORD_SIZE,+ index as _,+ )+ };+ }+ }++ unsafe fn set_viewport(&mut self, rect: &crate::Rect<f32>, depth_range: Range<f32>) {+ let zfar = if self.shared.disabilities.broken_viewport_near_depth {+ depth_range.end - depth_range.start+ } else {+ depth_range.end+ };+ let encoder = self.state.render.as_ref().unwrap();+ encoder.setViewport(MTLViewport {+ originX: rect.x as _,+ originY: rect.y as _,+ width: rect.w as _,+ height: rect.h as _,+ znear: depth_range.start as _,+ zfar: zfar as _,+ });+ }+ unsafe fn set_scissor_rect(&mut self, rect: &crate::Rect<u32>) {+ //TODO: support empty scissors by modifying the viewport+ let scissor = MTLScissorRect {+ x: rect.x as _,+ y: rect.y as _,+ width: rect.w as _,+ height: rect.h as _,+ };+ let encoder = self.state.render.as_ref().unwrap();+ encoder.setScissorRect(scissor);+ }+ unsafe fn set_stencil_reference(&mut self, value: u32) {+ let encoder = self.state.render.as_ref().unwrap();+ encoder.setStencilFrontReferenceValue_backReferenceValue(value, value);+ }+ unsafe fn set_blend_constants(&mut self, color: &[f32; 4]) {+ let encoder = self.state.render.as_ref().unwrap();+ encoder.setBlendColorRed_green_blue_alpha(color[0], color[1], color[2], color[3]);+ }++ unsafe fn draw(+ &mut self,+ first_vertex: u32,+ vertex_count: u32,+ first_instance: u32,+ instance_count: u32,+ ) {+ let encoder = self.state.render.as_ref().unwrap();+ if first_instance != 0 {+ unsafe {+ encoder.drawPrimitives_vertexStart_vertexCount_instanceCount_baseInstance(+ self.state.raw_primitive_type,+ first_vertex as _,+ vertex_count as _,+ instance_count as _,+ first_instance as _,+ )+ };+ } else if instance_count != 1 {+ unsafe {+ encoder.drawPrimitives_vertexStart_vertexCount_instanceCount(+ self.state.raw_primitive_type,+ first_vertex as _,+ vertex_count as _,+ instance_count as _,+ )+ };+ } else {+ unsafe {+ encoder.drawPrimitives_vertexStart_vertexCount(+ self.state.raw_primitive_type,+ first_vertex as _,+ vertex_count as _,+ )+ };+ }+ }++ unsafe fn draw_indexed(+ &mut self,+ first_index: u32,+ index_count: u32,+ base_vertex: i32,+ first_instance: u32,+ instance_count: u32,+ ) {+ let encoder = self.state.render.as_ref().unwrap();+ let index = self.state.index.as_ref().unwrap();+ let offset = (index.offset + index.stride * first_index as wgt::BufferAddress) as usize;+ if base_vertex != 0 || first_instance != 0 {+ unsafe {+ encoder.drawIndexedPrimitives_indexCount_indexType_indexBuffer_indexBufferOffset_instanceCount_baseVertex_baseInstance(+ self.state.raw_primitive_type,+ index_count as _,+ index.raw_type,+ index.buffer_ptr.as_ref(),+ offset,+ instance_count as _,+ base_vertex as _,+ first_instance as _,+ )+ };+ } else if instance_count != 1 {+ unsafe {+ encoder.drawIndexedPrimitives_indexCount_indexType_indexBuffer_indexBufferOffset_instanceCount(+ self.state.raw_primitive_type,+ index_count as _,+ index.raw_type,+ index.buffer_ptr.as_ref(),+ offset,+ instance_count as _,+ )+ };+ } else {+ unsafe {+ encoder.drawIndexedPrimitives_indexCount_indexType_indexBuffer_indexBufferOffset(+ self.state.raw_primitive_type,+ index_count as _,+ index.raw_type,+ index.buffer_ptr.as_ref(),+ offset,+ )+ };+ }+ }++ unsafe fn draw_mesh_tasks(+ &mut self,+ group_count_x: u32,+ group_count_y: u32,+ group_count_z: u32,+ ) {+ let encoder = self.state.render.as_ref().unwrap();+ let size = MTLSize {+ width: group_count_x as usize,+ height: group_count_y as usize,+ depth: group_count_z as usize,+ };+ encoder.drawMeshThreadgroups_threadsPerObjectThreadgroup_threadsPerMeshThreadgroup(+ size,+ self.state.stage_infos.ts.raw_wg_size,+ self.state.stage_infos.ms.raw_wg_size,+ );+ }++ unsafe fn draw_indirect(+ &mut self,+ buffer: &super::Buffer,+ mut offset: wgt::BufferAddress,+ draw_count: u32,+ ) {+ let encoder = self.state.render.as_ref().unwrap();+ for _ in 0..draw_count {+ unsafe {+ encoder.drawPrimitives_indirectBuffer_indirectBufferOffset(+ self.state.raw_primitive_type,+ &buffer.raw,+ offset as usize,+ )+ };+ offset += size_of::<wgt::DrawIndirectArgs>() as wgt::BufferAddress;+ }+ }++ unsafe fn draw_indexed_indirect(+ &mut self,+ buffer: &super::Buffer,+ mut offset: wgt::BufferAddress,+ draw_count: u32,+ ) {+ let encoder = self.state.render.as_ref().unwrap();+ let index = self.state.index.as_ref().unwrap();+ for _ in 0..draw_count {+ unsafe {+ encoder.drawIndexedPrimitives_indexType_indexBuffer_indexBufferOffset_indirectBuffer_indirectBufferOffset(+ self.state.raw_primitive_type,+ index.raw_type,+ index.buffer_ptr.as_ref(),+ index.offset as usize,+ &buffer.raw,+ offset as usize,+ )+ };+ offset += size_of::<wgt::DrawIndexedIndirectArgs>() as wgt::BufferAddress;+ }+ }++ unsafe fn draw_mesh_tasks_indirect(+ &mut self,+ buffer: &<Self::A as crate::Api>::Buffer,+ mut offset: wgt::BufferAddress,+ draw_count: u32,+ ) {+ let encoder = self.state.render.as_ref().unwrap();+ for _ in 0..draw_count {+ unsafe {+ encoder.drawMeshThreadgroupsWithIndirectBuffer_indirectBufferOffset_threadsPerObjectThreadgroup_threadsPerMeshThreadgroup(+ &buffer.raw,+ offset as usize,+ self.state.stage_infos.ts.raw_wg_size,+ self.state.stage_infos.ms.raw_wg_size,+ )+ };+ offset += size_of::<wgt::DispatchIndirectArgs>() as wgt::BufferAddress;+ }+ }++ unsafe fn draw_indirect_count(+ &mut self,+ _buffer: &super::Buffer,+ _offset: wgt::BufferAddress,+ _count_buffer: &super::Buffer,+ _count_offset: wgt::BufferAddress,+ _max_count: u32,+ ) {+ //TODO+ }+ unsafe fn draw_indexed_indirect_count(+ &mut self,+ _buffer: &super::Buffer,+ _offset: wgt::BufferAddress,+ _count_buffer: &super::Buffer,+ _count_offset: wgt::BufferAddress,+ _max_count: u32,+ ) {+ //TODO+ }++ unsafe fn draw_mesh_tasks_indirect_count(+ &mut self,+ _buffer: &<Self::A as crate::Api>::Buffer,+ _offset: wgt::BufferAddress,+ _count_buffer: &<Self::A as crate::Api>::Buffer,+ _count_offset: wgt::BufferAddress,+ _max_count: u32,+ ) {+ unreachable!()+ }++ // compute++ unsafe fn begin_compute_pass(&mut self, desc: &crate::ComputePassDescriptor<super::QuerySet>) {+ self.begin_pass();++ debug_assert!(self.state.blit.is_none());+ debug_assert!(self.state.compute.is_none());+ debug_assert!(self.state.render.is_none());++ let raw = self.raw_cmd_buf.as_ref().unwrap();++ autoreleasepool(|_| {+ // TimeStamp Queries and ComputePassDescriptor were both introduced in Metal 2.3 (macOS 11, iOS 14)+ // and we currently only need ComputePassDescriptor for timestamp queries+ let encoder = if self.shared.private_caps.timestamp_query_support.is_empty() {+ raw.computeCommandEncoder().unwrap()+ } else {+ let descriptor = MTLComputePassDescriptor::new();++ let mut sba_index = 0;+ let mut next_sba_descriptor = || {+ let sba_descriptor = unsafe {+ descriptor+ .sampleBufferAttachments()+ .objectAtIndexedSubscript(sba_index)+ };+ sba_index += 1;+ sba_descriptor+ };++ for (set, index) in self.state.pending_timer_queries.drain(..) {+ let sba_descriptor = next_sba_descriptor();+ sba_descriptor+ .setSampleBuffer(Some(set.counter_sample_buffer.as_ref().unwrap()));+ unsafe { sba_descriptor.setStartOfEncoderSampleIndex(index as _) };+ unsafe { sba_descriptor.setEndOfEncoderSampleIndex(MTLCounterDontSample) };+ }++ if let Some(timestamp_writes) = desc.timestamp_writes.as_ref() {+ let sba_descriptor = next_sba_descriptor();+ sba_descriptor.setSampleBuffer(Some(+ timestamp_writes+ .query_set+ .counter_sample_buffer+ .as_ref()+ .unwrap(),+ ));++ unsafe {+ sba_descriptor.setStartOfEncoderSampleIndex(+ timestamp_writes+ .beginning_of_pass_write_index+ .map_or(MTLCounterDontSample, |i| i as _),+ )+ };+ unsafe {+ sba_descriptor.setEndOfEncoderSampleIndex(+ timestamp_writes+ .end_of_pass_write_index+ .map_or(MTLCounterDontSample, |i| i as _),+ )+ };+ }++ raw.computeCommandEncoderWithDescriptor(&descriptor)+ .unwrap()+ };++ if let Some(label) = desc.label {+ encoder.setLabel(Some(&NSString::from_str(label)));+ }++ self.state.compute = Some(encoder.to_owned());+ });+ }+ unsafe fn end_compute_pass(&mut self) {+ self.state.compute.take().unwrap().endEncoding();+ }++ unsafe fn set_compute_pipeline(&mut self, pipeline: &super::ComputePipeline) {+ let previous_sizes =+ core::mem::take(&mut self.state.stage_infos.cs.work_group_memory_sizes);+ self.state.stage_infos.cs.assign_from(&pipeline.cs_info);++ let encoder = self.state.compute.as_ref().unwrap();+ encoder.setComputePipelineState(&pipeline.raw);++ if let Some((index, sizes)) = self+ .state+ .make_sizes_buffer_update(naga::ShaderStage::Compute, &mut self.temp.binding_sizes)+ {+ unsafe {+ encoder.setBytes_length_atIndex(+ NonNull::new(sizes.as_ptr().cast_mut().cast()).unwrap(),+ sizes.len() * WORD_SIZE,+ index as _,+ )+ };+ }++ // update the threadgroup memory sizes+ for (i, current_size) in self+ .state+ .stage_infos+ .cs+ .work_group_memory_sizes+ .iter_mut()+ .enumerate()+ {+ let prev_size = if i < previous_sizes.len() {+ previous_sizes[i]+ } else {+ u32::MAX+ };+ let size: u32 = current_size.next_multiple_of(16);+ *current_size = size;+ if size != prev_size {+ unsafe { encoder.setThreadgroupMemoryLength_atIndex(size as _, i) };+ }+ }+ }++ unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]) {+ if count[0] > 0 && count[1] > 0 && count[2] > 0 {+ let encoder = self.state.compute.as_ref().unwrap();+ let raw_count = MTLSize {+ width: count[0] as usize,+ height: count[1] as usize,+ depth: count[2] as usize,+ };+ encoder.dispatchThreadgroups_threadsPerThreadgroup(+ raw_count,+ self.state.stage_infos.cs.raw_wg_size,+ );+ }+ }++ unsafe fn dispatch_workgroups_indirect(+ &mut self,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ ) {+ let encoder = self.state.compute.as_ref().unwrap();+ unsafe {+ encoder+ .dispatchThreadgroupsWithIndirectBuffer_indirectBufferOffset_threadsPerThreadgroup(+ &buffer.raw,+ offset as usize,+ self.state.stage_infos.cs.raw_wg_size,+ )+ };+ }++ unsafe fn build_acceleration_structures<'a, T>(+ &mut self,+ _descriptor_count: u32,+ descriptors: T,+ ) where+ super::Api: 'a,+ T: IntoIterator<+ Item = crate::BuildAccelerationStructureDescriptor<+ 'a,+ super::Buffer,+ super::AccelerationStructure,+ >,+ >,+ {+ let command_encoder = self.enter_acceleration_structure_builder();+ for descriptor in descriptors {+ let acceleration_structure_descriptor =+ conv::map_acceleration_structure_descriptor(descriptor.entries, descriptor.flags);+ match descriptor.mode {+ crate::AccelerationStructureBuildMode::Build => {+ command_encoder+ .buildAccelerationStructure_descriptor_scratchBuffer_scratchBufferOffset(+ &descriptor.destination_acceleration_structure.raw,+ &acceleration_structure_descriptor,+ &descriptor.scratch_buffer.raw,+ descriptor.scratch_buffer_offset as usize,+ );+ }+ crate::AccelerationStructureBuildMode::Update => unsafe {+ command_encoder.refitAccelerationStructure_descriptor_destination_scratchBuffer_scratchBufferOffset(+ &descriptor.source_acceleration_structure.unwrap().raw,+ &acceleration_structure_descriptor,+ Some(&descriptor.destination_acceleration_structure.raw),+ Some(&descriptor.scratch_buffer.raw),+ descriptor.scratch_buffer_offset as usize,+ );+ },+ }+ }+ }++ unsafe fn place_acceleration_structure_barrier(+ &mut self,+ _barriers: crate::AccelerationStructureBarrier,+ ) {+ }++ unsafe fn read_acceleration_structure_compact_size(+ &mut self,+ acceleration_structure: &super::AccelerationStructure,+ buffer: &super::Buffer,+ ) {+ let command_encoder = self.enter_acceleration_structure_builder();+ command_encoder.writeCompactedAccelerationStructureSize_toBuffer_offset(+ &acceleration_structure.raw,+ &buffer.raw,+ 0,+ );+ }++ unsafe fn set_acceleration_structure_dependencies(+ command_buffers: &[&super::CommandBuffer],+ dependencies: &[&super::AccelerationStructure],+ ) {+ let Some(first_command_buffer) = command_buffers.first() else {+ return;+ };+ let desc = MTLResidencySetDescriptor::new();+ desc.setLabel(first_command_buffer.raw.label().as_deref());+ let residency_set = first_command_buffer+ .raw+ .device()+ .newResidencySetWithDescriptor_error(&desc)+ .unwrap();+ for command_buffer in command_buffers {+ command_buffer.raw.useResidencySet(&residency_set);+ }+ for dependency in dependencies {+ residency_set.addAllocation(ProtocolObject::from_ref(&*dependency.raw));+ }+ residency_set.commit();+ }++ unsafe fn begin_ray_tracing_pass(&mut self, _desc: &crate::RayTracingPassDescriptor) {+ unreachable!("Ray tracing pipelines not supported")+ }++ unsafe fn end_ray_tracing_pass(&mut self) {+ unreachable!("Ray tracing pipelines not supported")+ }++ unsafe fn set_ray_tracing_pipeline(+ &mut self,+ _pipeline: &<Self::A as crate::Api>::RayTracingPipeline,+ ) {+ unreachable!("Ray tracing pipelines not supported")+ }++ unsafe fn trace_rays(+ &mut self,+ _count: [u32; 3],+ _ray_generation_group_data: crate::PipelineGroupData<super::Buffer>,+ _miss_group_data: crate::PipelineGroupData<super::Buffer>,+ _intersection_group_data: crate::PipelineGroupData<super::Buffer>,+ ) {+ unreachable!("Ray tracing pipelines not supported")+ }+}++impl Drop for super::CommandEncoder {+ fn drop(&mut self) {+ // Metal raises an assert when a MTLCommandEncoder is deallocated without a call+ // to endEncoding. This isn't documented in the general case at+ // https://developer.apple.com/documentation/metal/mtlcommandencoder, but for the+ // more-specific MTLComputeCommandEncoder it is stated as a requirement at+ // https://developer.apple.com/documentation/metal/mtlcomputecommandencoder. It+ // appears to be a requirement for all MTLCommandEncoder objects. Failing to call+ // endEncoding causes a crash with the message 'Command encoder released without+ // endEncoding'. To prevent this, we explicitiy call discard_encoding, which+ // calls endEncoding on any still-held MTLCommandEncoders.+ unsafe {+ self.discard_encoding();+ }+ self.counters.command_encoders.sub(1);+ }+}++impl Drop for super::CommandBuffer {+ fn drop(&mut self) {+ // `command_buffer_created_not_submitted` is usually decremented when the command+ // buffer is submitted. But if we're dropping a command buffer that was never+ // submitted, we need to decrement the count here.+ let status = self.raw.status();+ if status == MTLCommandBufferStatus::NotEnqueued+ || status == MTLCommandBufferStatus::Enqueued+ {+ let previous = self+ .queue_shared+ .command_buffer_created_not_submitted+ .fetch_sub(1, atomic::Ordering::AcqRel);+ debug_assert!(previous > 0);+ }+ }+}diff --git a/vendor/wgpu-hal/src/metal/conv.rs b/vendor/wgpu-hal/src/metal/conv.rsnew file mode 100644index 00000000..0b761642--- /dev/null+++ b/vendor/wgpu-hal/src/metal/conv.rs@@ -0,0 +1,494 @@+use objc2::rc::Retained;+use objc2_foundation::{NSArray, NSRange};+use objc2_metal::{+ MTLAccelerationStructureBoundingBoxGeometryDescriptor, MTLAccelerationStructureDescriptor,+ MTLAccelerationStructureGeometryDescriptor, MTLAccelerationStructureInstanceDescriptorType,+ MTLAccelerationStructureTriangleGeometryDescriptor, MTLAccelerationStructureUsage,+ MTLAttributeFormat, MTLBlendFactor, MTLBlendOperation, MTLBlitOption, MTLClearColor,+ MTLColorWriteMask, MTLCompareFunction, MTLCullMode, MTLIndexType,+ MTLInstanceAccelerationStructureDescriptor, MTLOrigin,+ MTLPrimitiveAccelerationStructureDescriptor, MTLPrimitiveTopologyClass, MTLPrimitiveType,+ MTLRenderStages, MTLResourceUsage, MTLSamplerAddressMode, MTLSamplerBorderColor,+ MTLSamplerMinMagFilter, MTLSize, MTLStencilOperation, MTLStoreAction, MTLTextureType,+ MTLTextureUsage, MTLVertexFormat, MTLVertexStepFunction, MTLWinding,+};++pub fn map_texture_usage(format: wgt::TextureFormat, usage: wgt::TextureUses) -> MTLTextureUsage {+ use wgt::TextureUses as Tu;++ let mut mtl_usage = MTLTextureUsage::Unknown;++ mtl_usage.set(+ MTLTextureUsage::RenderTarget,+ usage.intersects(Tu::COLOR_TARGET | Tu::DEPTH_STENCIL_READ | Tu::DEPTH_STENCIL_WRITE),+ );+ mtl_usage.set(+ MTLTextureUsage::ShaderRead,+ usage.intersects(+ Tu::RESOURCE | Tu::DEPTH_STENCIL_READ | Tu::STORAGE_READ_ONLY | Tu::STORAGE_READ_WRITE,+ ),+ );+ mtl_usage.set(+ MTLTextureUsage::ShaderWrite,+ usage.intersects(Tu::STORAGE_WRITE_ONLY | Tu::STORAGE_READ_WRITE),+ );+ // needed for combined depth/stencil formats since we might+ // create a stencil-only view from them+ mtl_usage.set(+ MTLTextureUsage::PixelFormatView,+ format.is_combined_depth_stencil_format(),+ );++ mtl_usage.set(+ MTLTextureUsage::ShaderAtomic,+ usage.intersects(Tu::STORAGE_ATOMIC),+ );++ mtl_usage+}++pub fn map_texture_view_dimension(dim: wgt::TextureViewDimension) -> MTLTextureType {+ use wgt::TextureViewDimension as Tvd;+ use MTLTextureType as MTL;+ match dim {+ Tvd::D1 => MTL::Type1D,+ Tvd::D2 => MTL::Type2D,+ Tvd::D2Array => MTL::Type2DArray,+ Tvd::D3 => MTL::Type3D,+ Tvd::Cube => MTL::TypeCube,+ Tvd::CubeArray => MTL::TypeCubeArray,+ }+}++pub fn map_compare_function(fun: wgt::CompareFunction) -> MTLCompareFunction {+ use wgt::CompareFunction as Cf;+ use MTLCompareFunction as MTL;+ match fun {+ Cf::Never => MTL::Never,+ Cf::Less => MTL::Less,+ Cf::LessEqual => MTL::LessEqual,+ Cf::Equal => MTL::Equal,+ Cf::GreaterEqual => MTL::GreaterEqual,+ Cf::Greater => MTL::Greater,+ Cf::NotEqual => MTL::NotEqual,+ Cf::Always => MTL::Always,+ }+}++pub fn map_filter_mode(filter: wgt::FilterMode) -> MTLSamplerMinMagFilter {+ use MTLSamplerMinMagFilter as MTL;+ match filter {+ wgt::FilterMode::Nearest => MTL::Nearest,+ wgt::FilterMode::Linear => MTL::Linear,+ }+}++pub fn map_address_mode(address: wgt::AddressMode) -> MTLSamplerAddressMode {+ use wgt::AddressMode as Fm;+ use MTLSamplerAddressMode as MTL;+ match address {+ Fm::Repeat => MTL::Repeat,+ Fm::MirrorRepeat => MTL::MirrorRepeat,+ Fm::ClampToEdge => MTL::ClampToEdge,+ Fm::ClampToBorder => MTL::ClampToBorderColor,+ //Fm::MirrorClamp => MTL::MirrorClampToEdge,+ }+}++pub fn map_border_color(border_color: wgt::SamplerBorderColor) -> MTLSamplerBorderColor {+ use MTLSamplerBorderColor as MTL;+ match border_color {+ wgt::SamplerBorderColor::TransparentBlack => MTL::TransparentBlack,+ wgt::SamplerBorderColor::OpaqueBlack => MTL::OpaqueBlack,+ wgt::SamplerBorderColor::OpaqueWhite => MTL::OpaqueWhite,+ wgt::SamplerBorderColor::Zero => unreachable!(),+ }+}++pub fn map_primitive_topology(+ topology: wgt::PrimitiveTopology,+) -> (MTLPrimitiveTopologyClass, MTLPrimitiveType) {+ use wgt::PrimitiveTopology as Pt;+ match topology {+ Pt::PointList => (MTLPrimitiveTopologyClass::Point, MTLPrimitiveType::Point),+ Pt::LineList => (MTLPrimitiveTopologyClass::Line, MTLPrimitiveType::Line),+ Pt::LineStrip => (MTLPrimitiveTopologyClass::Line, MTLPrimitiveType::LineStrip),+ Pt::TriangleList => (+ MTLPrimitiveTopologyClass::Triangle,+ MTLPrimitiveType::Triangle,+ ),+ Pt::TriangleStrip => (+ MTLPrimitiveTopologyClass::Triangle,+ MTLPrimitiveType::TriangleStrip,+ ),+ }+}++pub fn map_color_write(mask: wgt::ColorWrites) -> MTLColorWriteMask {+ let mut raw_mask = MTLColorWriteMask::empty();++ if mask.contains(wgt::ColorWrites::RED) {+ raw_mask |= MTLColorWriteMask::Red;+ }+ if mask.contains(wgt::ColorWrites::GREEN) {+ raw_mask |= MTLColorWriteMask::Green;+ }+ if mask.contains(wgt::ColorWrites::BLUE) {+ raw_mask |= MTLColorWriteMask::Blue;+ }+ if mask.contains(wgt::ColorWrites::ALPHA) {+ raw_mask |= MTLColorWriteMask::Alpha;+ }++ raw_mask+}++pub fn map_blend_factor(factor: wgt::BlendFactor) -> MTLBlendFactor {+ use wgt::BlendFactor as Bf;+ use MTLBlendFactor as MTL;++ match factor {+ Bf::Zero => MTL::Zero,+ Bf::One => MTL::One,+ Bf::Src => MTL::SourceColor,+ Bf::OneMinusSrc => MTL::OneMinusSourceColor,+ Bf::Dst => MTL::DestinationColor,+ Bf::OneMinusDst => MTL::OneMinusDestinationColor,+ Bf::SrcAlpha => MTL::SourceAlpha,+ Bf::OneMinusSrcAlpha => MTL::OneMinusSourceAlpha,+ Bf::DstAlpha => MTL::DestinationAlpha,+ Bf::OneMinusDstAlpha => MTL::OneMinusDestinationAlpha,+ Bf::Constant => MTL::BlendColor,+ Bf::OneMinusConstant => MTL::OneMinusBlendColor,+ Bf::SrcAlphaSaturated => MTL::SourceAlphaSaturated,+ Bf::Src1 => MTL::Source1Color,+ Bf::OneMinusSrc1 => MTL::OneMinusSource1Color,+ Bf::Src1Alpha => MTL::Source1Alpha,+ Bf::OneMinusSrc1Alpha => MTL::OneMinusSource1Alpha,+ }+}++pub fn map_blend_op(operation: wgt::BlendOperation) -> MTLBlendOperation {+ use wgt::BlendOperation as Bo;+ use MTLBlendOperation as MTL;++ match operation {+ Bo::Add => MTL::Add,+ Bo::Subtract => MTL::Subtract,+ Bo::ReverseSubtract => MTL::ReverseSubtract,+ Bo::Min => MTL::Min,+ Bo::Max => MTL::Max,+ }+}++pub fn map_blend_component(+ component: &wgt::BlendComponent,+) -> (MTLBlendOperation, MTLBlendFactor, MTLBlendFactor) {+ (+ map_blend_op(component.operation),+ map_blend_factor(component.src_factor),+ map_blend_factor(component.dst_factor),+ )+}++pub fn map_vertex_format(format: wgt::VertexFormat) -> MTLVertexFormat {+ use wgt::VertexFormat as Vf;+ use MTLVertexFormat as MTL;++ match format {+ Vf::Unorm8 => MTL::UCharNormalized,+ Vf::Snorm8 => MTL::CharNormalized,+ Vf::Uint8 => MTL::UChar,+ Vf::Sint8 => MTL::Char,+ Vf::Unorm8x2 => MTL::UChar2Normalized,+ Vf::Snorm8x2 => MTL::Char2Normalized,+ Vf::Uint8x2 => MTL::UChar2,+ Vf::Sint8x2 => MTL::Char2,+ Vf::Unorm8x4 => MTL::UChar4Normalized,+ Vf::Snorm8x4 => MTL::Char4Normalized,+ Vf::Uint8x4 => MTL::UChar4,+ Vf::Sint8x4 => MTL::Char4,+ Vf::Unorm16 => MTL::UShortNormalized,+ Vf::Snorm16 => MTL::ShortNormalized,+ Vf::Uint16 => MTL::UShort,+ Vf::Sint16 => MTL::Short,+ Vf::Float16 => MTL::Half,+ Vf::Unorm16x2 => MTL::UShort2Normalized,+ Vf::Snorm16x2 => MTL::Short2Normalized,+ Vf::Uint16x2 => MTL::UShort2,+ Vf::Sint16x2 => MTL::Short2,+ Vf::Float16x2 => MTL::Half2,+ Vf::Unorm16x4 => MTL::UShort4Normalized,+ Vf::Snorm16x4 => MTL::Short4Normalized,+ Vf::Uint16x4 => MTL::UShort4,+ Vf::Sint16x4 => MTL::Short4,+ Vf::Float16x4 => MTL::Half4,+ Vf::Uint32 => MTL::UInt,+ Vf::Sint32 => MTL::Int,+ Vf::Float32 => MTL::Float,+ Vf::Uint32x2 => MTL::UInt2,+ Vf::Sint32x2 => MTL::Int2,+ Vf::Float32x2 => MTL::Float2,+ Vf::Uint32x3 => MTL::UInt3,+ Vf::Sint32x3 => MTL::Int3,+ Vf::Float32x3 => MTL::Float3,+ Vf::Uint32x4 => MTL::UInt4,+ Vf::Sint32x4 => MTL::Int4,+ Vf::Float32x4 => MTL::Float4,+ Vf::Unorm10_10_10_2 => MTL::UInt1010102Normalized,+ Vf::Unorm8x4Bgra => MTL::UChar4Normalized_BGRA,+ Vf::Float64 | Vf::Float64x2 | Vf::Float64x3 | Vf::Float64x4 => unimplemented!(),+ }+}++pub fn map_index_format(format: wgt::IndexFormat) -> (u64, MTLIndexType) {+ match format {+ wgt::IndexFormat::Uint16 => (2, MTLIndexType::UInt16),+ wgt::IndexFormat::Uint32 => (4, MTLIndexType::UInt32),+ }+}++pub fn map_step_mode(mode: wgt::VertexStepMode) -> MTLVertexStepFunction {+ match mode {+ wgt::VertexStepMode::Vertex => MTLVertexStepFunction::PerVertex,+ wgt::VertexStepMode::Instance => MTLVertexStepFunction::PerInstance,+ }+}++pub fn map_stencil_op(op: wgt::StencilOperation) -> MTLStencilOperation {+ use wgt::StencilOperation as So;+ use MTLStencilOperation as MTL;++ match op {+ So::Keep => MTL::Keep,+ So::Zero => MTL::Zero,+ So::Replace => MTL::Replace,+ So::IncrementClamp => MTL::IncrementClamp,+ So::IncrementWrap => MTL::IncrementWrap,+ So::DecrementClamp => MTL::DecrementClamp,+ So::DecrementWrap => MTL::DecrementWrap,+ So::Invert => MTL::Invert,+ }+}++pub fn map_winding(winding: wgt::FrontFace) -> MTLWinding {+ match winding {+ wgt::FrontFace::Cw => MTLWinding::Clockwise,+ wgt::FrontFace::Ccw => MTLWinding::CounterClockwise,+ }+}++pub fn map_cull_mode(face: Option<wgt::Face>) -> MTLCullMode {+ match face {+ None => MTLCullMode::None,+ Some(wgt::Face::Front) => MTLCullMode::Front,+ Some(wgt::Face::Back) => MTLCullMode::Back,+ }+}++pub fn map_range(range: &crate::MemoryRange) -> NSRange {+ NSRange {+ location: range.start as usize,+ length: (range.end - range.start) as usize,+ }+}++pub fn map_copy_extent(extent: &crate::CopyExtent) -> MTLSize {+ MTLSize {+ width: extent.width as usize,+ height: extent.height as usize,+ depth: extent.depth as usize,+ }+}++pub fn map_origin(origin: &wgt::Origin3d) -> MTLOrigin {+ MTLOrigin {+ x: origin.x as usize,+ y: origin.y as usize,+ z: origin.z as usize,+ }+}++pub fn map_store_action(store: bool, resolve: bool) -> MTLStoreAction {+ use MTLStoreAction as MTL;+ match (store, resolve) {+ (true, true) => MTL::StoreAndMultisampleResolve,+ (false, true) => MTL::MultisampleResolve,+ (true, false) => MTL::Store,+ (false, false) => MTL::DontCare,+ }+}++pub fn map_clear_color(color: &wgt::Color) -> MTLClearColor {+ MTLClearColor {+ red: color.r,+ green: color.g,+ blue: color.b,+ alpha: color.a,+ }+}++pub fn get_blit_option(format: wgt::TextureFormat, aspect: crate::FormatAspects) -> MTLBlitOption {+ if format.is_combined_depth_stencil_format() {+ match aspect {+ crate::FormatAspects::DEPTH => MTLBlitOption::DepthFromDepthStencil,+ crate::FormatAspects::STENCIL => MTLBlitOption::StencilFromDepthStencil,+ _ => unreachable!(),+ }+ } else {+ MTLBlitOption::None+ }+}++pub fn map_render_stages(stage: wgt::ShaderStages) -> MTLRenderStages {+ let mut raw_stages = MTLRenderStages::empty();++ if stage.contains(wgt::ShaderStages::VERTEX) {+ raw_stages |= MTLRenderStages::Vertex;+ }+ if stage.contains(wgt::ShaderStages::FRAGMENT) {+ raw_stages |= MTLRenderStages::Fragment;+ }++ raw_stages+}++pub fn map_resource_usage(ty: &wgt::BindingType) -> MTLResourceUsage {+ match ty {+ #[allow(deprecated)]+ wgt::BindingType::Texture { .. } => MTLResourceUsage::Sample,+ wgt::BindingType::StorageTexture { access, .. } => match access {+ wgt::StorageTextureAccess::WriteOnly => MTLResourceUsage::Write,+ wgt::StorageTextureAccess::ReadOnly => MTLResourceUsage::Read,+ wgt::StorageTextureAccess::Atomic | wgt::StorageTextureAccess::ReadWrite => {+ MTLResourceUsage::Read | MTLResourceUsage::Write+ }+ },+ wgt::BindingType::Sampler(..) => MTLResourceUsage::empty(),+ wgt::BindingType::Buffer { ty, .. } => match ty {+ wgt::BufferBindingType::Uniform+ | wgt::BufferBindingType::Storage { read_only: true } => MTLResourceUsage::Read,+ wgt::BufferBindingType::Storage { read_only: false } => {+ MTLResourceUsage::Read | MTLResourceUsage::Write+ }+ },+ _ => unreachable!(),+ }+}++pub fn map_acceleration_structure_descriptor<'a>(+ entries: &'a crate::AccelerationStructureEntries<'a, super::Buffer>,+ flags: crate::AccelerationStructureBuildFlags,+) -> Retained<MTLAccelerationStructureDescriptor> {+ let descriptor = match entries {+ crate::AccelerationStructureEntries::Instances(instances) => {+ let descriptor = MTLInstanceAccelerationStructureDescriptor::new();+ descriptor.setInstanceDescriptorType(+ MTLAccelerationStructureInstanceDescriptorType::Indirect,+ );+ descriptor.setInstanceCount(instances.count as usize);+ descriptor.setInstanceDescriptorBuffer(Some(&instances.buffer.unwrap().raw));+ unsafe {+ descriptor.setInstanceDescriptorBufferOffset(instances.offset as usize);+ }+ descriptor.into_super()+ }+ crate::AccelerationStructureEntries::Triangles(entries) => {+ let geometry_descriptors = entries+ .iter()+ .map(|triangles| {+ let descriptor = MTLAccelerationStructureTriangleGeometryDescriptor::new();+ if let Some(indices) = triangles.indices.as_ref() {+ descriptor.setIndexBuffer(Some(&indices.buffer.unwrap().raw));+ unsafe {+ descriptor.setIndexBufferOffset(indices.offset as usize);+ }+ descriptor.setIndexType(map_index_format(indices.format).1);+ descriptor.setTriangleCount(indices.count as usize / 3);+ } else {+ descriptor.setTriangleCount(triangles.vertex_count as usize / 3);+ }+ descriptor.setVertexBuffer(Some(&triangles.vertex_buffer.unwrap().raw));+ unsafe {+ descriptor.setVertexBufferOffset(+ triangles.first_vertex as usize * triangles.vertex_stride as usize,+ );+ }+ descriptor.setVertexStride(triangles.vertex_stride as usize);+ // Safety: MTLVertexFormat and MTLAttributeFormat are identical.+ // https://docs.rs/objc2-metal/latest/objc2_metal/struct.MTLAttributeFormat.html+ // https://docs.rs/objc2-metal/latest/objc2_metal/struct.MTLVertexFormat.html+ descriptor.setVertexFormat(unsafe {+ core::mem::transmute::<MTLVertexFormat, MTLAttributeFormat>(+ map_vertex_format(triangles.vertex_format),+ )+ });+ if let Some(transform) = triangles.transform.as_ref() {+ unsafe {+ descriptor.setTransformationMatrixBuffer(Some(&transform.buffer.raw));+ descriptor+ .setTransformationMatrixBufferOffset(transform.offset as usize);+ }+ }+ descriptor.setOpaque(+ triangles+ .flags+ .contains(wgt::AccelerationStructureGeometryFlags::OPAQUE),+ );+ if !triangles.flags.contains(+ wgt::AccelerationStructureGeometryFlags::NO_DUPLICATE_ANY_HIT_INVOCATION,+ ) {+ descriptor.allowDuplicateIntersectionFunctionInvocation();+ }+ // descriptor.setIntersectionFunctionTableOffset(offset);+ descriptor.into_super()+ })+ .collect::<alloc::vec::Vec<Retained<MTLAccelerationStructureGeometryDescriptor>>>();+ let descriptor = MTLPrimitiveAccelerationStructureDescriptor::new();+ descriptor.setGeometryDescriptors(Some(&NSArray::from_retained_slice(+ geometry_descriptors.as_slice(),+ )));+ descriptor.into_super()+ }+ crate::AccelerationStructureEntries::AABBs(entries) => {+ let geometry_descriptors = entries+ .iter()+ .map(|aabbs| {+ let descriptor = MTLAccelerationStructureBoundingBoxGeometryDescriptor::new();+ descriptor.setBoundingBoxBuffer(Some(&aabbs.buffer.unwrap().raw));+ descriptor.setBoundingBoxCount(aabbs.count as usize);+ unsafe {+ descriptor.setBoundingBoxStride(aabbs.stride as usize);+ descriptor.setBoundingBoxBufferOffset(aabbs.offset as usize);+ }+ descriptor.setOpaque(+ aabbs+ .flags+ .contains(wgt::AccelerationStructureGeometryFlags::OPAQUE),+ );+ if !aabbs.flags.contains(+ wgt::AccelerationStructureGeometryFlags::NO_DUPLICATE_ANY_HIT_INVOCATION,+ ) {+ descriptor.allowDuplicateIntersectionFunctionInvocation();+ }+ // descriptor.setIntersectionFunctionTableOffset(offset);+ descriptor.into_super()+ })+ .collect::<alloc::vec::Vec<Retained<MTLAccelerationStructureGeometryDescriptor>>>();+ let descriptor = MTLPrimitiveAccelerationStructureDescriptor::new();+ descriptor.setGeometryDescriptors(Some(&NSArray::from_retained_slice(+ geometry_descriptors.as_slice(),+ )));+ descriptor.into_super()+ }+ };+ let mut usage = MTLAccelerationStructureUsage::None;+ if flags.contains(wgt::AccelerationStructureFlags::ALLOW_UPDATE) {+ usage |= MTLAccelerationStructureUsage::Refit;+ }+ if flags.contains(wgt::AccelerationStructureFlags::PREFER_FAST_BUILD) {+ usage |= MTLAccelerationStructureUsage::PreferFastBuild;+ }+ descriptor.setUsage(usage);+ descriptor+}diff --git a/vendor/wgpu-hal/src/metal/device.rs b/vendor/wgpu-hal/src/metal/device.rsnew file mode 100644index 00000000..d7287b97--- /dev/null+++ b/vendor/wgpu-hal/src/metal/device.rs@@ -0,0 +1,2170 @@+use alloc::{borrow::ToOwned as _, sync::Arc, vec::Vec};+use core::{mem::align_of, ptr::NonNull};++use bytemuck::TransparentWrapper;+use objc2::{+ available,+ rc::{autoreleasepool, Retained},+ runtime::ProtocolObject,+};+use objc2_foundation::{ns_string, NSError, NSRange, NSString, NSUInteger};+use objc2_metal::{+ MTLAccelerationStructure, MTLAccelerationStructureInstanceOptions, MTLBuffer,+ MTLCaptureManager, MTLCaptureScope, MTLCompileOptions, MTLComputePipelineDescriptor,+ MTLComputePipelineState, MTLCounterSampleBufferDescriptor, MTLCounterSet, MTLDepthClipMode,+ MTLDepthStencilDescriptor, MTLDevice, MTLFunction,+ MTLIndirectAccelerationStructureInstanceDescriptor, MTLLanguageVersion, MTLLibrary,+ MTLMeshRenderPipelineDescriptor, MTLMutability, MTLPackedFloat3, MTLPackedFloat4x3,+ MTLPipelineBufferDescriptorArray, MTLPipelineOption, MTLPixelFormat, MTLPrimitiveTopologyClass,+ MTLRenderPipelineColorAttachmentDescriptorArray, MTLRenderPipelineDescriptor, MTLResource,+ MTLResourceID, MTLResourceOptions, MTLSamplerAddressMode, MTLSamplerDescriptor,+ MTLSamplerMipFilter, MTLSamplerState, MTLSize, MTLStencilDescriptor, MTLStorageMode,+ MTLTexture, MTLTextureDescriptor, MTLTextureType, MTLTriangleFillMode, MTLVertexDescriptor,+ MTLVertexStepFunction,+};+use parking_lot::{Condvar, Mutex, RwLock};++use super::{adapter::MAX_BUFFERS, conv, PassthroughShader, ShaderModuleSource};+use crate::{auxil::map_naga_stage, DropCallback, DropGuard, TlasInstance};++type DeviceResult<T> = Result<T, crate::DeviceError>;++/// True on arm64_32 (watchOS ILP32) targets.+///+/// There are no Apple OSes that support both 32-bit applications and Metal,+/// so `target_pointer_width = "32"` is a reliable proxy for ILP32 watchOS+/// devices (Apple Watch S4–S9, SE, Ultra). Several AGXMetalS4 driver bugs+/// require workarounds gated on this flag.+const IS_WATCHOS_ILP32: bool = cfg!(target_pointer_width = "32");++struct CompiledShader {+ library: Retained<ProtocolObject<dyn MTLLibrary>>,+ function: Retained<ProtocolObject<dyn MTLFunction>>,+ wg_size: MTLSize,+ wg_memory_sizes: Vec<u32>,++ /// Bindings of WGSL `storage` globals that contain variable-sized arrays,+ /// paired with a binding-array element index when applicable.+ ///+ /// In order to implement bounds checks and the `arrayLength` function for+ /// WGSL runtime-sized arrays, we pass the entry point a struct with a+ /// `u32` member for each such binding. For ordinary storage buffers the+ /// element index is zero; for storage binding arrays there is one entry per+ /// layout element. Each member holds that element's total size in bytes---+ /// the size of the `Buffer` supplying its contents for the draw or dispatch.+ sized_bindings: Vec<(naga::ResourceBinding, u32)>,++ immutable_buffer_mask: usize,+}++fn create_stencil_desc(+ face: &wgt::StencilFaceState,+ read_mask: u32,+ write_mask: u32,+) -> Retained<MTLStencilDescriptor> {+ let desc = MTLStencilDescriptor::new();+ desc.setStencilCompareFunction(conv::map_compare_function(face.compare));+ desc.setReadMask(read_mask);+ desc.setWriteMask(write_mask);+ desc.setStencilFailureOperation(conv::map_stencil_op(face.fail_op));+ desc.setDepthFailureOperation(conv::map_stencil_op(face.depth_fail_op));+ desc.setDepthStencilPassOperation(conv::map_stencil_op(face.pass_op));+ desc+}++fn create_depth_stencil_desc(+ state: &wgt::DepthStencilState,+) -> Retained<MTLDepthStencilDescriptor> {+ let desc = MTLDepthStencilDescriptor::new();+ desc.setDepthCompareFunction(conv::map_compare_function(+ state.depth_compare.unwrap_or_default(),+ ));+ desc.setDepthWriteEnabled(state.depth_write_enabled.unwrap_or_default());+ let s = &state.stencil;+ if s.is_enabled() {+ let front_desc = create_stencil_desc(&s.front, s.read_mask, s.write_mask);+ desc.setFrontFaceStencil(Some(&front_desc));+ let back_desc = create_stencil_desc(&s.back, s.read_mask, s.write_mask);+ desc.setBackFaceStencil(Some(&back_desc));+ }+ desc+}++#[allow(+ clippy::mut_from_ref,+ reason = "MTLBuffer shared contents are writable through `&MTLBuffer`"+)]+fn bindless_id_table_mut(+ buffer: &ProtocolObject<dyn MTLBuffer>,+ count: u32,+) -> &mut [MTLResourceID] {+ let ptr = buffer.contents().cast::<u8>().as_ptr();+ // SAFETY: The buffer is aligned to the size of `MTLResourceID`.+ assert_eq!(ptr as usize % align_of::<MTLResourceID>(), 0);+ let ptr = ptr.cast::<MTLResourceID>();+ unsafe { core::slice::from_raw_parts_mut(ptr, count as usize) }+}++const fn convert_vertex_format_to_naga(format: wgt::VertexFormat) -> nt::VertexFormat {+ match format {+ wgt::VertexFormat::Uint8 => nt::VertexFormat::Uint8,+ wgt::VertexFormat::Uint8x2 => nt::VertexFormat::Uint8x2,+ wgt::VertexFormat::Uint8x4 => nt::VertexFormat::Uint8x4,+ wgt::VertexFormat::Sint8 => nt::VertexFormat::Sint8,+ wgt::VertexFormat::Sint8x2 => nt::VertexFormat::Sint8x2,+ wgt::VertexFormat::Sint8x4 => nt::VertexFormat::Sint8x4,+ wgt::VertexFormat::Unorm8 => nt::VertexFormat::Unorm8,+ wgt::VertexFormat::Unorm8x2 => nt::VertexFormat::Unorm8x2,+ wgt::VertexFormat::Unorm8x4 => nt::VertexFormat::Unorm8x4,+ wgt::VertexFormat::Snorm8 => nt::VertexFormat::Snorm8,+ wgt::VertexFormat::Snorm8x2 => nt::VertexFormat::Snorm8x2,+ wgt::VertexFormat::Snorm8x4 => nt::VertexFormat::Snorm8x4,+ wgt::VertexFormat::Uint16 => nt::VertexFormat::Uint16,+ wgt::VertexFormat::Uint16x2 => nt::VertexFormat::Uint16x2,+ wgt::VertexFormat::Uint16x4 => nt::VertexFormat::Uint16x4,+ wgt::VertexFormat::Sint16 => nt::VertexFormat::Sint16,+ wgt::VertexFormat::Sint16x2 => nt::VertexFormat::Sint16x2,+ wgt::VertexFormat::Sint16x4 => nt::VertexFormat::Sint16x4,+ wgt::VertexFormat::Unorm16 => nt::VertexFormat::Unorm16,+ wgt::VertexFormat::Unorm16x2 => nt::VertexFormat::Unorm16x2,+ wgt::VertexFormat::Unorm16x4 => nt::VertexFormat::Unorm16x4,+ wgt::VertexFormat::Snorm16 => nt::VertexFormat::Snorm16,+ wgt::VertexFormat::Snorm16x2 => nt::VertexFormat::Snorm16x2,+ wgt::VertexFormat::Snorm16x4 => nt::VertexFormat::Snorm16x4,+ wgt::VertexFormat::Float16 => nt::VertexFormat::Float16,+ wgt::VertexFormat::Float16x2 => nt::VertexFormat::Float16x2,+ wgt::VertexFormat::Float16x4 => nt::VertexFormat::Float16x4,+ wgt::VertexFormat::Float32 => nt::VertexFormat::Float32,+ wgt::VertexFormat::Float32x2 => nt::VertexFormat::Float32x2,+ wgt::VertexFormat::Float32x3 => nt::VertexFormat::Float32x3,+ wgt::VertexFormat::Float32x4 => nt::VertexFormat::Float32x4,+ wgt::VertexFormat::Uint32 => nt::VertexFormat::Uint32,+ wgt::VertexFormat::Uint32x2 => nt::VertexFormat::Uint32x2,+ wgt::VertexFormat::Uint32x3 => nt::VertexFormat::Uint32x3,+ wgt::VertexFormat::Uint32x4 => nt::VertexFormat::Uint32x4,+ wgt::VertexFormat::Sint32 => nt::VertexFormat::Sint32,+ wgt::VertexFormat::Sint32x2 => nt::VertexFormat::Sint32x2,+ wgt::VertexFormat::Sint32x3 => nt::VertexFormat::Sint32x3,+ wgt::VertexFormat::Sint32x4 => nt::VertexFormat::Sint32x4,+ wgt::VertexFormat::Unorm10_10_10_2 => nt::VertexFormat::Unorm10_10_10_2,+ wgt::VertexFormat::Unorm8x4Bgra => nt::VertexFormat::Unorm8x4Bgra,++ wgt::VertexFormat::Float64+ | wgt::VertexFormat::Float64x2+ | wgt::VertexFormat::Float64x3+ | wgt::VertexFormat::Float64x4 => {+ unimplemented!()+ }+ }+}++impl super::Device {+ fn load_shader(+ &self,+ stage: &crate::ProgrammableStage<super::ShaderModule>,+ vertex_buffer_mappings: &[naga::back::msl::VertexBufferMapping],+ layout: &super::PipelineLayout,+ primitive_class: MTLPrimitiveTopologyClass,+ naga_stage: naga::ShaderStage,+ ) -> Result<CompiledShader, crate::PipelineError> {+ match stage.module.source {+ ShaderModuleSource::Naga(ref naga_shader) => {+ let stage_bit = map_naga_stage(naga_stage);+ let (module, module_info) = naga::back::pipeline_constants::process_overrides(+ &naga_shader.module,+ &naga_shader.info,+ Some((naga_stage, stage.entry_point)),+ stage.constants,+ )+ .map_err(|e| {+ crate::PipelineError::PipelineConstants(stage_bit, format!("MSL: {e:?}"))+ })?;++ let ep_resources = &layout.per_stage_map[naga_stage];++ let bounds_check_policy = if stage.module.runtime_checks.bounds_checks {+ naga::proc::BoundsCheckPolicy::Restrict+ } else {+ naga::proc::BoundsCheckPolicy::Unchecked+ };++ let options = naga::back::msl::Options {+ lang_version: match self.shared.private_caps.msl_version {+ #[allow(deprecated)]+ MTLLanguageVersion::Version1_0 => (1, 0),+ MTLLanguageVersion::Version1_1 => (1, 1),+ MTLLanguageVersion::Version1_2 => (1, 2),+ MTLLanguageVersion::Version2_0 => (2, 0),+ MTLLanguageVersion::Version2_1 => (2, 1),+ MTLLanguageVersion::Version2_2 => (2, 2),+ MTLLanguageVersion::Version2_3 => (2, 3),+ MTLLanguageVersion::Version2_4 => (2, 4),+ MTLLanguageVersion::Version3_0 => (3, 0),+ MTLLanguageVersion::Version3_1 => (3, 1),+ MTLLanguageVersion::Version3_2 => (3, 2),+ MTLLanguageVersion::Version4_0 => (4, 0),+ // Newer version, fall back to 3.2+ _ => (4, 0),+ },+ inline_samplers: Default::default(),+ spirv_cross_compatibility: false,+ fake_missing_bindings: false,+ per_entry_point_map: naga::back::msl::EntryPointResourceMap::from([(+ stage.entry_point.to_owned(),+ ep_resources.clone(),+ )]),+ bounds_check_policies: naga::proc::BoundsCheckPolicies {+ index: bounds_check_policy,+ buffer: bounds_check_policy,+ image_load: bounds_check_policy,+ // TODO: support bounds checks on binding arrays+ binding_array: naga::proc::BoundsCheckPolicy::Unchecked,+ },+ zero_initialize_workgroup_memory: stage.zero_initialize_workgroup_memory,+ force_loop_bounding: stage.module.runtime_checks.force_loop_bounding,+ task_dispatch_limits: stage+ .module+ .runtime_checks+ .task_shader_dispatch_tracking+ .then_some(naga::back::TaskDispatchLimits {+ max_mesh_workgroups_per_dim: self+ .limits+ .max_mesh_workgroups_per_dimension,+ max_mesh_workgroups_total: self.limits.max_mesh_workgroup_total_count,+ }),+ mesh_shader_primitive_indices_clamp: stage+ .module+ .runtime_checks+ .mesh_shader_primitive_indices_clamp,+ emit_int_div_checks: stage.module.runtime_checks.int_div_checks,+ ray_query_initialization_tracking: stage+ .module+ .runtime_checks+ .ray_query_initialization_tracking,+ };++ let pipeline_options = naga::back::msl::PipelineOptions {+ entry_point: Some((naga_stage, stage.entry_point.to_owned())),+ allow_and_force_point_size: match primitive_class {+ MTLPrimitiveTopologyClass::Point => true,+ _ => false,+ },+ vertex_pulling_transform: true,+ vertex_buffer_mappings: vertex_buffer_mappings.to_vec(),+ binding_array_length_map: layout.binding_array_length_map.clone(),+ };++ let (source, info) = naga::back::msl::write_string(+ &module,+ &module_info,+ &options,+ &pipeline_options,+ )+ .map_err(|e| crate::PipelineError::Linkage(stage_bit, format!("MSL: {e:?}")))?;++ log::debug!(+ "Naga generated shader for entry point '{}' and stage {:?}\n{}",+ stage.entry_point,+ naga_stage,+ &source+ );++ let options = MTLCompileOptions::new();+ options.setLanguageVersion(self.shared.private_caps.msl_version);++ // https://developer.apple.com/documentation/metal/mtlcompileoptions/preserveinvariance+ if available!(macos = 11.0, ios = 13.0, tvos = 14.0, visionos = 1.0) {+ options.setPreserveInvariance(true);+ }++ let library = self+ .shared+ .device+ .newLibraryWithSource_options_error(+ &NSString::from_str(&source),+ Some(&options),+ )+ .map_err(|err| {+ log::debug!("Naga generated shader:\n{source}");+ crate::PipelineError::Linkage(stage_bit, format!("Metal: {err}"))+ })?;++ let ep_index = module+ .entry_points+ .iter()+ .position(|ep| ep.stage == naga_stage && ep.name == stage.entry_point)+ .ok_or(crate::PipelineError::EntryPoint(naga_stage))?;+ let ep = &module.entry_points[ep_index];+ let translated_ep_name = info.entry_point_names[0]+ .as_ref()+ .map_err(|e| crate::PipelineError::Linkage(stage_bit, format!("{e}")))?;++ let wg_size = MTLSize {+ width: ep.workgroup_size[0] as _,+ height: ep.workgroup_size[1] as _,+ depth: ep.workgroup_size[2] as _,+ };++ let function = library+ .newFunctionWithName(&NSString::from_str(translated_ep_name))+ .ok_or_else(|| {+ log::error!("Function '{translated_ep_name}' does not exist");+ crate::PipelineError::EntryPoint(naga_stage)+ })?;++ // collect sizes indices, immutable buffers, and work group memory sizes+ let ep_info = &module_info.get_entry_point(ep_index);+ let mut wg_memory_sizes = Vec::new();+ let mut sized_bindings = Vec::new();+ let mut immutable_buffer_mask = 0;+ for (var_handle, var) in module.global_variables.iter() {+ match var.space {+ naga::AddressSpace::WorkGroup => {+ if !ep_info[var_handle].is_empty() {+ let size = module.types[var.ty].inner.size(module.to_ctx());+ wg_memory_sizes.push(size);+ }+ }+ naga::AddressSpace::Uniform | naga::AddressSpace::Storage { .. } => {+ let br = match var.binding {+ Some(br) => br,+ None => continue,+ };+ let storage_access_store = match var.space {+ naga::AddressSpace::Storage { access } => {+ access.contains(naga::StorageAccess::STORE)+ }+ _ => false,+ };++ // check for an immutable buffer+ if !ep_info[var_handle].is_empty() && !storage_access_store {+ let slot = ep_resources.resources[&br].buffer.unwrap();+ immutable_buffer_mask |= 1 << slot;+ }++ if module.types[var.ty]+ .inner+ .needs_host_buffer_byte_size(&module.types)+ {+ let n = match module.types[var.ty].inner {+ naga::TypeInner::BindingArray { size, .. } => {+ let from_shader = match size {+ naga::ArraySize::Constant(n) => n.get(),+ naga::ArraySize::Pending(_)+ | naga::ArraySize::Dynamic => 0,+ };+ let from_layout = layout+ .binding_array_length_map+ .get(&br)+ .copied()+ .unwrap_or(0);+ from_shader.max(from_layout).max(1)+ }+ _ => 1,+ };+ sized_bindings.extend((0..n).map(|i| (br, i)));+ }+ }+ _ => {}+ }+ }++ Ok(CompiledShader {+ library,+ function,+ wg_size,+ wg_memory_sizes,+ sized_bindings,+ immutable_buffer_mask,+ })+ }+ ShaderModuleSource::Passthrough(ref shader) => {+ let size = shader.num_workgroups[stage.entry_point];+ Ok(CompiledShader {+ library: shader.library.clone(),+ function: shader+ .library+ .newFunctionWithName(&NSString::from_str(stage.entry_point))+ .ok_or(crate::PipelineError::EntryPoint(naga_stage))?,+ wg_size: MTLSize {+ width: size.0 as usize,+ height: size.1 as usize,+ depth: size.2 as usize,+ },+ wg_memory_sizes: vec![],+ sized_bindings: vec![],+ immutable_buffer_mask: 0,+ })+ }+ }+ }++ fn set_buffers_mutability(+ buffers: &MTLPipelineBufferDescriptorArray,+ mut immutable_mask: usize,+ ) {+ while immutable_mask != 0 {+ let slot = immutable_mask.trailing_zeros();+ immutable_mask ^= 1 << slot;+ unsafe { buffers.objectAtIndexedSubscript(slot as usize) }+ .setMutability(MTLMutability::Immutable);+ }+ }++ pub unsafe fn texture_from_raw(+ raw: Retained<ProtocolObject<dyn MTLTexture>>,+ format: wgt::TextureFormat,+ raw_type: MTLTextureType,+ array_layers: u32,+ mip_levels: u32,+ copy_size: crate::CopyExtent,+ drop_callback: Option<DropCallback>,+ ) -> super::Texture {+ super::Texture {+ raw,+ format,+ raw_type,+ array_layers,+ mip_levels,+ copy_size,+ _drop_guard: DropGuard::from_option(drop_callback),+ }+ }++ pub unsafe fn device_from_raw(+ raw: Retained<ProtocolObject<dyn MTLDevice>>,+ features: wgt::Features,+ limits: &wgt::Limits,+ ) -> super::Device {+ let capabilities_query = super::CapabilitiesQuery::new(&raw);+ let shared = super::AdapterShared::new(raw, &capabilities_query);+ super::Device {+ shared: Arc::new(shared),+ features,+ counters: Default::default(),+ limits: limits.clone(),+ }+ }++ pub unsafe fn buffer_from_raw(+ raw: Retained<ProtocolObject<dyn MTLBuffer>>,+ size: wgt::BufferAddress,+ ) -> super::Buffer {+ super::Buffer { raw, size }+ }++ pub fn raw_device(&self) -> &Retained<ProtocolObject<dyn MTLDevice>> {+ &self.shared.device+ }+}++impl crate::Device for super::Device {+ type A = super::Api;++ unsafe fn create_buffer(&self, desc: &crate::BufferDescriptor) -> DeviceResult<super::Buffer> {+ let map_read = desc.usage.contains(wgt::BufferUses::MAP_READ);+ let map_write = desc.usage.contains(wgt::BufferUses::MAP_WRITE);++ let mut options = MTLResourceOptions::empty();+ options |= if map_read || map_write {+ // `crate::MemoryFlags::PREFER_COHERENT` is ignored here+ MTLResourceOptions::StorageModeShared+ } else {+ MTLResourceOptions::StorageModePrivate+ };+ options.set(MTLResourceOptions::CPUCacheModeWriteCombined, map_write);++ //TODO: HazardTrackingModeUntracked++ autoreleasepool(|_| {+ let raw = self+ .shared+ .device+ .newBufferWithLength_options(desc.size as usize, options)+ .ok_or(crate::DeviceError::OutOfMemory)?;+ if let Some(label) = desc.label {+ raw.setLabel(Some(&NSString::from_str(label)));+ }+ self.counters.buffers.add(1);+ Ok(super::Buffer {+ raw,+ size: desc.size,+ })+ })+ }+ unsafe fn destroy_buffer(&self, _buffer: super::Buffer) {+ self.counters.buffers.sub(1);+ }++ unsafe fn add_raw_buffer(&self, _buffer: &super::Buffer) {+ self.counters.buffers.add(1);+ }++ unsafe fn map_buffer(+ &self,+ buffer: &super::Buffer,+ range: crate::MemoryRange,+ ) -> DeviceResult<crate::BufferMapping> {+ let ptr = buffer.raw.contents().cast::<u8>();+ Ok(crate::BufferMapping {+ ptr: NonNull::new(unsafe { ptr.as_ptr().offset(range.start as isize) }).unwrap(),+ is_coherent: true,+ })+ }++ unsafe fn unmap_buffer(&self, _buffer: &super::Buffer) {}+ unsafe fn flush_mapped_ranges<I>(&self, _buffer: &super::Buffer, _ranges: I) {}+ unsafe fn invalidate_mapped_ranges<I>(&self, _buffer: &super::Buffer, _ranges: I) {}++ unsafe fn create_texture(+ &self,+ desc: &crate::TextureDescriptor,+ ) -> DeviceResult<super::Texture> {+ let mtl_format = self+ .shared+ .private_texture_format_caps+ .map_format(desc.format);++ autoreleasepool(|_| {+ let descriptor = MTLTextureDescriptor::new();++ let mtl_type = match desc.dimension {+ wgt::TextureDimension::D1 => MTLTextureType::Type1D,+ wgt::TextureDimension::D2 => {+ if desc.sample_count > 1 {+ unsafe { descriptor.setSampleCount(desc.sample_count as usize) };++ if desc.size.depth_or_array_layers > 1 {+ unsafe {+ descriptor.setArrayLength(desc.size.depth_or_array_layers as usize)+ };+ MTLTextureType::Type2DMultisampleArray+ } else {+ MTLTextureType::Type2DMultisample+ }+ } else if desc.size.depth_or_array_layers > 1 {+ unsafe {+ descriptor.setArrayLength(desc.size.depth_or_array_layers as usize)+ };+ MTLTextureType::Type2DArray+ } else {+ MTLTextureType::Type2D+ }+ }+ wgt::TextureDimension::D3 => {+ unsafe { descriptor.setDepth(desc.size.depth_or_array_layers as usize) };+ MTLTextureType::Type3D+ }+ };++ let mtl_storage_mode = if desc.usage.contains(wgt::TextureUses::TRANSIENT)+ && self.shared.private_caps.supports_memoryless_storage+ {+ MTLStorageMode::Memoryless+ } else if IS_WATCHOS_ILP32 {+ // The AGXMetalS4 driver (A13/S6 GPU) crashes in+ // copyFromTexture:toBuffer: on Private textures — null deref at+ // offset 0x50 in the driver's internal texture state. Use Shared+ // storage which works correctly on Apple's unified memory+ // architecture and matches what native Swift Metal code uses on+ // these devices.+ MTLStorageMode::Shared+ } else {+ MTLStorageMode::Private+ };++ descriptor.setTextureType(mtl_type);+ unsafe { descriptor.setWidth(desc.size.width as usize) };+ unsafe { descriptor.setHeight(desc.size.height as usize) };+ unsafe { descriptor.setMipmapLevelCount(desc.mip_level_count as usize) };+ descriptor.setPixelFormat(mtl_format);+ descriptor.setUsage(conv::map_texture_usage(desc.format, desc.usage));+ descriptor.setStorageMode(mtl_storage_mode);++ let raw = self+ .shared+ .device+ .newTextureWithDescriptor(&descriptor)+ .ok_or(crate::DeviceError::OutOfMemory)?;+ if let Some(label) = desc.label {+ raw.setLabel(Some(&NSString::from_str(label)));+ }++ self.counters.textures.add(1);++ Ok(super::Texture {+ raw,+ format: desc.format,+ raw_type: mtl_type,+ mip_levels: desc.mip_level_count,+ array_layers: desc.array_layer_count(),+ copy_size: desc.copy_extent(),+ _drop_guard: None,+ })+ })+ }++ unsafe fn destroy_texture(&self, _texture: super::Texture) {+ self.counters.textures.sub(1);+ }++ unsafe fn add_raw_texture(&self, _texture: &super::Texture) {+ self.counters.textures.add(1);+ }++ unsafe fn create_texture_view(+ &self,+ texture: &super::Texture,+ desc: &crate::TextureViewDescriptor,+ ) -> DeviceResult<super::TextureView> {+ let raw_type = if texture.raw_type == MTLTextureType::Type2DMultisample+ || texture.raw_type == MTLTextureType::Type2DMultisampleArray+ {+ texture.raw_type+ } else {+ conv::map_texture_view_dimension(desc.dimension)+ };++ let aspects = crate::FormatAspects::new(texture.format, desc.range.aspect);++ let raw_format = self+ .shared+ .private_texture_format_caps+ .map_view_format(desc.format, aspects);++ let format_equal = raw_format+ == self+ .shared+ .private_texture_format_caps+ .map_format(texture.format);+ let type_equal = raw_type == texture.raw_type;+ let range_full_resource =+ desc.range+ .is_full_resource(desc.format, texture.mip_levels, texture.array_layers);++ let raw = if format_equal && type_equal && range_full_resource {+ // Some images are marked as framebuffer-only, and we can't create aliases of them.+ // Also helps working around Metal bugs with aliased array textures.+ texture.raw.to_owned()+ } else {+ let mip_level_count = desc+ .range+ .mip_level_count+ .unwrap_or(texture.mip_levels - desc.range.base_mip_level);+ let array_layer_count = desc+ .range+ .array_layer_count+ .unwrap_or(texture.array_layers - desc.range.base_array_layer);++ autoreleasepool(|_| {+ let level_range = NSRange {+ location: desc.range.base_mip_level as _,+ length: mip_level_count as _,+ };+ let slice_range = NSRange {+ location: desc.range.base_array_layer as _,+ length: array_layer_count as _,+ };+ let raw = unsafe {+ texture+ .raw+ .newTextureViewWithPixelFormat_textureType_levels_slices(+ raw_format,+ raw_type,+ level_range,+ slice_range,+ )+ .unwrap()+ };+ if let Some(label) = desc.label {+ raw.setLabel(Some(&NSString::from_str(label)));+ }+ raw+ })+ };++ self.counters.texture_views.add(1);++ Ok(super::TextureView { raw, aspects })+ }++ unsafe fn destroy_texture_view(&self, _view: super::TextureView) {+ self.counters.texture_views.sub(1);+ }++ unsafe fn create_sampler(+ &self,+ desc: &crate::SamplerDescriptor,+ ) -> DeviceResult<super::Sampler> {+ autoreleasepool(|_| {+ let descriptor = MTLSamplerDescriptor::new();++ descriptor.setMinFilter(conv::map_filter_mode(desc.min_filter));+ descriptor.setMagFilter(conv::map_filter_mode(desc.mag_filter));+ descriptor.setMipFilter(match desc.mipmap_filter {+ wgt::MipmapFilterMode::Nearest if desc.lod_clamp == (0.0..0.0) => {+ MTLSamplerMipFilter::NotMipmapped+ }+ wgt::MipmapFilterMode::Nearest => MTLSamplerMipFilter::Nearest,+ wgt::MipmapFilterMode::Linear => MTLSamplerMipFilter::Linear,+ });++ let [s, t, r] = desc.address_modes;+ descriptor.setSAddressMode(conv::map_address_mode(s));+ descriptor.setTAddressMode(conv::map_address_mode(t));+ descriptor.setRAddressMode(conv::map_address_mode(r));++ // Anisotropy is always supported on mac up to 16x+ descriptor.setMaxAnisotropy(desc.anisotropy_clamp as _);++ descriptor.setLodMinClamp(desc.lod_clamp.start);+ descriptor.setLodMaxClamp(desc.lod_clamp.end);++ if let Some(fun) = desc.compare {+ descriptor.setCompareFunction(conv::map_compare_function(fun));+ }++ if let Some(border_color) = desc.border_color {+ if let wgt::SamplerBorderColor::Zero = border_color {+ if s == wgt::AddressMode::ClampToBorder {+ descriptor.setSAddressMode(MTLSamplerAddressMode::ClampToZero);+ }++ if t == wgt::AddressMode::ClampToBorder {+ descriptor.setTAddressMode(MTLSamplerAddressMode::ClampToZero);+ }++ if r == wgt::AddressMode::ClampToBorder {+ descriptor.setRAddressMode(MTLSamplerAddressMode::ClampToZero);+ }+ } else {+ descriptor.setBorderColor(conv::map_border_color(border_color));+ }+ }++ if let Some(label) = desc.label {+ descriptor.setLabel(Some(&NSString::from_str(label)));+ }+ if self.features.contains(wgt::Features::TEXTURE_BINDING_ARRAY) {+ descriptor.setSupportArgumentBuffers(true);+ }+ let raw = self+ .shared+ .device+ .newSamplerStateWithDescriptor(&descriptor)+ .unwrap();++ self.counters.samplers.add(1);++ Ok(super::Sampler { raw })+ })+ }+ unsafe fn destroy_sampler(&self, _sampler: super::Sampler) {+ self.counters.samplers.sub(1);+ }++ unsafe fn create_command_encoder(+ &self,+ desc: &crate::CommandEncoderDescriptor<super::Queue>,+ ) -> Result<super::CommandEncoder, crate::DeviceError> {+ self.counters.command_encoders.add(1);+ Ok(super::CommandEncoder {+ shared: Arc::clone(&self.shared),+ queue_shared: Arc::clone(&desc.queue.shared),+ raw_cmd_buf: None,+ state: super::CommandState::default(),+ temp: super::Temp::default(),+ counters: Arc::clone(&self.counters),+ })+ }++ unsafe fn create_bind_group_layout(+ &self,+ desc: &crate::BindGroupLayoutDescriptor,+ ) -> DeviceResult<super::BindGroupLayout> {+ self.counters.bind_group_layouts.add(1);++ Ok(super::BindGroupLayout {+ entries: Arc::from(desc.entries),+ })+ }++ unsafe fn destroy_bind_group_layout(&self, _bg_layout: super::BindGroupLayout) {+ self.counters.bind_group_layouts.sub(1);+ }++ unsafe fn create_pipeline_layout(+ &self,+ desc: &crate::PipelineLayoutDescriptor<super::BindGroupLayout>,+ ) -> DeviceResult<super::PipelineLayout> {+ #[derive(Debug)]+ struct StageInfo {+ stage: naga::ShaderStage,+ counters: super::ResourceData<super::ResourceIndex>,+ pc_buffer: Option<super::ResourceIndex>,+ pc_limit: u32,+ sizes_buffer: Option<super::ResourceIndex>,+ need_sizes_buffer: bool,+ resources: naga::back::msl::BindingMap,+ }++ let mut stage_data = super::NAGA_STAGES.map(|stage| StageInfo {+ stage,+ counters: super::ResourceData::default(),+ pc_buffer: None,+ pc_limit: 0,+ sizes_buffer: None,+ need_sizes_buffer: false,+ resources: Default::default(),+ });+ let mut bind_group_infos = [const { None }; crate::MAX_BIND_GROUPS];+ let mut binding_array_length_map = naga::FastHashMap::default();++ // First, place the immediates+ for info in stage_data.iter_mut() {+ info.pc_limit = desc.immediate_size;++ // handle the immediate data buffer assignment and shader overrides+ if info.pc_limit != 0 {+ info.pc_buffer = Some(info.counters.buffers);+ info.counters.buffers += 1;+ }+ }++ // Second, place the described resources+ for (group_index, bgl) in desc.bind_group_layouts.iter().enumerate() {+ let Some(bgl) = bgl else {+ continue;+ };++ // remember where the resources for this set start at each shader stage+ let base_resource_indices = stage_data.map_ref(|info| info.counters.clone());++ for entry in bgl.entries.iter() {+ let br = naga::ResourceBinding {+ group: group_index as u32,+ binding: entry.binding,+ };+ if let Some(count) = entry.count {+ if matches!(+ entry.ty,+ wgt::BindingType::Buffer {+ ty: wgt::BufferBindingType::Storage { .. },+ ..+ }+ ) {+ binding_array_length_map.insert(br, count.get());+ }+ }++ if let wgt::BindingType::Buffer {+ ty: wgt::BufferBindingType::Storage { .. },+ ..+ } = entry.ty+ {+ for info in stage_data.iter_mut() {+ if entry.visibility.contains(map_naga_stage(info.stage)) {+ info.need_sizes_buffer = true;+ }+ }+ }++ for info in stage_data.iter_mut() {+ if !entry.visibility.contains(map_naga_stage(info.stage)) {+ continue;+ }++ let mut target = naga::back::msl::BindTarget::default();+ // Bindless path+ if let Some(_) = entry.count {+ target.buffer = Some(info.counters.buffers as _);+ info.counters.buffers += 1;+ } else {+ match entry.ty {+ wgt::BindingType::Buffer { ty, .. } => {+ target.buffer = Some(info.counters.buffers as _);+ info.counters.buffers += 1;+ if let wgt::BufferBindingType::Storage { read_only } = ty {+ target.mutable = !read_only;+ }+ }+ wgt::BindingType::Sampler { .. } => {+ target.sampler =+ Some(naga::back::msl::BindSamplerTarget::Resource(+ info.counters.samplers as _,+ ));+ info.counters.samplers += 1;+ }+ wgt::BindingType::Texture { .. } => {+ target.texture = Some(info.counters.textures as _);+ info.counters.textures += 1;+ }+ wgt::BindingType::StorageTexture { access, .. } => {+ target.texture = Some(info.counters.textures as _);+ info.counters.textures += 1;+ target.mutable = match access {+ wgt::StorageTextureAccess::ReadOnly => false,+ wgt::StorageTextureAccess::WriteOnly => true,+ wgt::StorageTextureAccess::ReadWrite => true,+ wgt::StorageTextureAccess::Atomic => true,+ };+ }+ wgt::BindingType::AccelerationStructure { .. } => {+ target.buffer = Some(info.counters.buffers as _);+ info.counters.buffers += 1;+ }+ wgt::BindingType::ExternalTexture => {+ target.external_texture =+ Some(naga::back::msl::BindExternalTextureTarget {+ planes: [+ info.counters.textures as _,+ (info.counters.textures + 1) as _,+ (info.counters.textures + 2) as _,+ ],+ params: info.counters.buffers as _,+ });+ info.counters.textures += 3;+ info.counters.buffers += 1;+ }+ }+ }++ info.resources.insert(br, target);+ }+ }++ bind_group_infos[group_index] = Some(super::BindGroupLayoutInfo {+ base_resource_indices,+ });+ }++ // Finally, make sure we fit the limits+ for info in stage_data.iter_mut() {+ if info.need_sizes_buffer || info.stage == naga::ShaderStage::Vertex {+ // Set aside space for the sizes_buffer, which is required+ // for variable-length buffers, or to support vertex pulling.+ info.sizes_buffer = Some(info.counters.buffers);+ info.counters.buffers += 1;+ }+ }++ let immediates_infos = stage_data.map_ref(|info| {+ info.pc_buffer.map(|buffer_index| super::ImmediateDataInfo {+ count: info.pc_limit,+ buffer_index,+ })+ });++ let per_stage_map = stage_data.map(|info| naga::back::msl::EntryPointResources {+ immediates_buffer: info+ .pc_buffer+ .map(|buffer_index| buffer_index as naga::back::msl::Slot),+ sizes_buffer: info+ .sizes_buffer+ .map(|buffer_index| buffer_index as naga::back::msl::Slot),+ resources: info.resources,+ });++ self.counters.pipeline_layouts.add(1);++ Ok(super::PipelineLayout {+ bind_group_infos,+ immediates_infos,+ total_immediates: desc.immediate_size,+ per_stage_map,+ binding_array_length_map,+ })+ }++ unsafe fn destroy_pipeline_layout(&self, _pipeline_layout: super::PipelineLayout) {+ self.counters.pipeline_layouts.sub(1);+ }++ unsafe fn create_bind_group(+ &self,+ desc: &crate::BindGroupDescriptor<+ super::BindGroupLayout,+ super::Buffer,+ super::Sampler,+ super::TextureView,+ super::AccelerationStructure,+ >,+ ) -> DeviceResult<super::BindGroup> {+ autoreleasepool(|_| {+ let mut bg = super::BindGroup::default();+ for (&stage, counter) in super::NAGA_STAGES.iter().zip(bg.counters.iter_mut()) {+ let stage_bit = map_naga_stage(stage);+ let mut dynamic_offsets_count = 0u32;+ let layout_and_entry_iter = desc.entries.iter().map(|entry| {+ let layout = desc+ .layout+ .entries+ .iter()+ .find(|layout_entry| layout_entry.binding == entry.binding)+ .expect("internal error: no layout entry found with binding slot");+ (entry, layout)+ });+ for (entry, layout) in layout_and_entry_iter {+ // Bindless path+ if layout.count.is_some() {+ if !layout.visibility.contains(stage_bit) {+ continue;+ }++ let count = entry.count;++ let stages = conv::map_render_stages(layout.visibility);+ let uses = conv::map_resource_usage(&layout.ty);++ // Create argument buffer for this array+ let argument_buffer = self+ .shared+ .device+ .newBufferWithLength_options(+ 8 * count as usize,+ MTLResourceOptions::HazardTrackingModeUntracked+ | MTLResourceOptions::StorageModeShared,+ )+ .unwrap();++ let mut array_element_sizes = Vec::new();+ match layout.ty {+ wgt::BindingType::Texture { .. }+ | wgt::BindingType::StorageTexture { .. } => {+ let resource_ids =+ bindless_id_table_mut(argument_buffer.as_ref(), count);+ let start = entry.resource_index as usize;+ let end = start + count as usize;+ let textures = &desc.textures[start..end];++ for (idx, tex) in textures.iter().enumerate() {+ resource_ids[idx] = tex.view.raw.gpuResourceID();++ let use_info = bg+ .resources_to_use+ .entry(tex.view.as_raw().cast())+ .or_default();+ use_info.stages |= stages;+ use_info.uses |= uses;+ use_info.visible_in_compute |=+ layout.visibility.contains(wgt::ShaderStages::COMPUTE);+ }+ }+ wgt::BindingType::Sampler { .. } => {+ let resource_ids =+ bindless_id_table_mut(argument_buffer.as_ref(), count);+ let start = entry.resource_index as usize;+ let end = start + count as usize;+ let samplers = &desc.samplers[start..end];++ for (idx, &sampler) in samplers.iter().enumerate() {+ resource_ids[idx] = sampler.raw.gpuResourceID();+ // Samplers aren't resources like buffers and textures, so don't+ // need to be passed to useResource+ }+ }+ wgt::BindingType::Buffer { ty, .. } => {+ let start = entry.resource_index as usize;+ let end = start + count as usize;+ let buffers = &desc.buffers[start..end];+ let pointers = argument_buffer.contents().cast::<u64>();+ for (idx, source) in buffers.iter().enumerate() {+ let addr = source.buffer.raw.gpuAddress() + source.offset;+ unsafe {+ pointers.add(idx).write(addr);+ }++ if let wgt::BufferBindingType::Storage { .. } = ty {+ let remaining_size = wgt::BufferSize::new(+ source.buffer.size - source.offset,+ );+ if let Some(binding_size) = source.size.or(remaining_size) {+ array_element_sizes.push((idx as u32, binding_size));+ }+ }++ let use_info = bg+ .resources_to_use+ .entry(source.buffer.as_raw().cast())+ .or_default();+ use_info.stages |= stages;+ use_info.uses |= uses;+ use_info.visible_in_compute |=+ layout.visibility.contains(wgt::ShaderStages::COMPUTE);+ }+ }+ wgt::BindingType::AccelerationStructure { .. } => {+ let resource_ids =+ bindless_id_table_mut(argument_buffer.as_ref(), count);+ let start = entry.resource_index as usize;+ let end = start + count as usize;+ let acceleration_structures =+ &desc.acceleration_structures[start..end];++ for (idx, &acceleration_structure) in+ acceleration_structures.iter().enumerate()+ {+ resource_ids[idx] = acceleration_structure.raw.gpuResourceID();++ let use_info = bg+ .resources_to_use+ .entry(acceleration_structure.as_raw().cast())+ .or_default();+ use_info.stages |= stages;+ use_info.uses |= uses;+ use_info.visible_in_compute |=+ layout.visibility.contains(wgt::ShaderStages::COMPUTE);+ }+ }+ _ => {+ unimplemented!();+ }+ }++ if matches!(+ layout.ty,+ wgt::BindingType::Buffer {+ ty: wgt::BufferBindingType::Storage { .. },+ ..+ }+ ) {+ bg.buffers+ .push(super::BufferLikeResource::StorageBindingArray {+ ptr: NonNull::from(&*argument_buffer),+ array_element_sizes,+ binding_location: layout.binding,+ });+ } else {+ bg.buffers.push(super::BufferLikeResource::Buffer {+ ptr: NonNull::from(&*argument_buffer),+ offset: 0,+ dynamic_index: None,+ binding_size: None,+ binding_location: layout.binding,+ });+ }+ counter.buffers += 1;++ bg.argument_buffers.push(argument_buffer)+ }+ // Bindfull path+ else {+ if let wgt::BindingType::Buffer {+ has_dynamic_offset: true,+ ..+ } = layout.ty+ {+ dynamic_offsets_count += 1;+ }+ if !layout.visibility.contains(stage_bit) {+ continue;+ }+ match layout.ty {+ wgt::BindingType::Buffer {+ ty,+ has_dynamic_offset,+ ..+ } => {+ let start = entry.resource_index as usize;+ let end = start + 1;+ bg.buffers+ .extend(desc.buffers[start..end].iter().map(|source| {+ // Given the restrictions on `BufferBinding::offset`,+ // this should never be `None`.+ let remaining_size = wgt::BufferSize::new(+ source.buffer.size - source.offset,+ );+ let binding_size = match ty {+ wgt::BufferBindingType::Storage { .. } => {+ source.size.or(remaining_size)+ }+ _ => None,+ };+ super::BufferLikeResource::Buffer {+ ptr: source.buffer.as_raw(),+ offset: source.offset,+ dynamic_index: if has_dynamic_offset {+ Some(dynamic_offsets_count - 1)+ } else {+ None+ },+ binding_size,+ binding_location: layout.binding,+ }+ }));+ counter.buffers += 1;+ }+ wgt::BindingType::Sampler { .. } => {+ let start = entry.resource_index as usize;+ let end = start + 1;+ bg.samplers.extend(+ desc.samplers[start..end].iter().map(|samp| samp.as_raw()),+ );+ counter.samplers += 1;+ }+ wgt::BindingType::Texture { .. }+ | wgt::BindingType::StorageTexture { .. } => {+ let start = entry.resource_index as usize;+ let end = start + 1;+ bg.textures.extend(+ desc.textures[start..end]+ .iter()+ .map(|tex| tex.view.as_raw()),+ );+ counter.textures += 1;+ }+ wgt::BindingType::AccelerationStructure { .. } => {+ let start = entry.resource_index as usize;+ let end = start + 1;+ bg.buffers.extend(+ desc.acceleration_structures[start..end].iter().map(+ |acceleration_structure| {+ super::BufferLikeResource::AccelerationStructure(+ acceleration_structure.as_raw(),+ )+ },+ ),+ );+ counter.buffers += 1;+ }+ wgt::BindingType::ExternalTexture => {+ // We don't yet support binding arrays of external textures.+ // https://github.com/gfx-rs/wgpu/issues/8027+ assert_eq!(entry.count, 1);+ let external_texture =+ &desc.external_textures[entry.resource_index as usize];+ bg.textures.extend(+ external_texture+ .planes+ .iter()+ .map(|plane| plane.view.as_raw()),+ );+ bg.buffers.push(super::BufferLikeResource::Buffer {+ ptr: external_texture.params.buffer.as_raw(),+ offset: external_texture.params.offset,+ dynamic_index: None,+ binding_size: None,+ binding_location: layout.binding,+ });+ counter.textures += 3;+ counter.buffers += 1;+ }+ }+ }+ }+ }++ self.counters.bind_groups.add(1);++ Ok(bg)+ })+ }++ unsafe fn destroy_bind_group(&self, _group: super::BindGroup) {+ self.counters.bind_groups.sub(1);+ }++ unsafe fn create_shader_module(+ &self,+ desc: &crate::ShaderModuleDescriptor,+ shader: crate::ShaderInput,+ ) -> Result<super::ShaderModule, crate::ShaderError> {+ self.counters.shader_modules.add(1);++ match shader {+ crate::ShaderInput::Naga(naga) => Ok(super::ShaderModule {+ source: ShaderModuleSource::Naga(naga),+ runtime_checks: desc.runtime_checks,+ }),+ crate::ShaderInput::MetalLib {+ file,+ num_workgroups,+ } => {+ // SAFETY: this creates a reference to `file` that is dropped before `file` is dropped.+ let library = super::library_from_metallib::new_library_from_metallib_bytes(+ &self.shared.device,+ file,+ )+ .map_err(|e| crate::ShaderError::Compilation(format!("Metallib: {e:?}")))?;+ Ok(super::ShaderModule {+ source: ShaderModuleSource::Passthrough(PassthroughShader {+ library,+ num_workgroups,+ }),+ // This goes unused for passthrough shaders+ runtime_checks: wgt::ShaderRuntimeChecks::unchecked(),+ })+ }+ crate::ShaderInput::Msl {+ shader: source,+ num_workgroups,+ } => {+ let options = MTLCompileOptions::new();+ // Obtain the device from shared+ let device = &self.shared.device;+ let library = device+ .newLibraryWithSource_options_error(&NSString::from_str(source), Some(&options))+ .map_err(|e| crate::ShaderError::Compilation(format!("MSL: {e:?}")))?;++ Ok(super::ShaderModule {+ source: ShaderModuleSource::Passthrough(PassthroughShader {+ library,+ num_workgroups,+ }),+ runtime_checks: desc.runtime_checks,+ })+ }+ crate::ShaderInput::SpirV(_)+ | crate::ShaderInput::Dxil { .. }+ | crate::ShaderInput::Hlsl { .. }+ | crate::ShaderInput::Glsl { .. } => unreachable!(),+ }+ }++ unsafe fn destroy_shader_module(&self, _module: super::ShaderModule) {+ self.counters.shader_modules.sub(1);+ }++ unsafe fn create_render_pipeline(+ &self,+ desc: &crate::RenderPipelineDescriptor<+ super::PipelineLayout,+ super::ShaderModule,+ super::PipelineCache,+ >,+ ) -> Result<super::RenderPipeline, crate::PipelineError> {+ autoreleasepool(|_| {+ enum MetalGenericRenderPipelineDescriptor {+ Standard(Retained<MTLRenderPipelineDescriptor>),+ Mesh(Retained<MTLMeshRenderPipelineDescriptor>),+ }+ macro_rules! descriptor_fn {+ ($descriptor:ident . $method:ident $( ( $($args:expr),* ) )? ) => {+ match $descriptor {+ MetalGenericRenderPipelineDescriptor::Standard(ref inner) => inner.$method$(($($args),*))?,+ MetalGenericRenderPipelineDescriptor::Mesh(ref inner) => inner.$method$(($($args),*))?,+ }+ };+ }+ #[allow(non_snake_case)]+ impl MetalGenericRenderPipelineDescriptor {+ unsafe fn setFragmentFunction(+ &self,+ function: Option<&ProtocolObject<dyn MTLFunction>>,+ ) {+ unsafe { descriptor_fn!(self.setFragmentFunction(function)) };+ }+ fn fragmentBuffers(&self) -> Retained<MTLPipelineBufferDescriptorArray> {+ descriptor_fn!(self.fragmentBuffers())+ }+ fn setDepthAttachmentPixelFormat(&self, pixel_format: MTLPixelFormat) {+ descriptor_fn!(self.setDepthAttachmentPixelFormat(pixel_format));+ }+ fn colorAttachments(+ &self,+ ) -> Retained<MTLRenderPipelineColorAttachmentDescriptorArray> {+ descriptor_fn!(self.colorAttachments())+ }+ fn setStencilAttachmentPixelFormat(&self, pixel_format: MTLPixelFormat) {+ descriptor_fn!(self.setStencilAttachmentPixelFormat(pixel_format));+ }+ fn setAlphaToCoverageEnabled(&self, enabled: bool) {+ descriptor_fn!(self.setAlphaToCoverageEnabled(enabled));+ }+ fn setLabel(&self, label: Option<&NSString>) {+ descriptor_fn!(self.setLabel(label));+ }+ unsafe fn setMaxVertexAmplificationCount(&self, count: NSUInteger) {+ unsafe { descriptor_fn!(self.setMaxVertexAmplificationCount(count)) }+ }+ }++ // https://developer.apple.com/documentation/metal/mtlpipelinebufferdescriptor/mutability+ // Disabled on watchOS ILP32: the AGXMetalS4 driver exhibits instability+ // when mutability hints are combined with Shared storage mode textures.+ // Conservative disable until broader device coverage.+ let supports_mutability = !IS_WATCHOS_ILP32+ && available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0);++ let (primitive_class, raw_primitive_type) =+ conv::map_primitive_topology(desc.primitive.topology);++ let vs_info;+ let ts_info;+ let ms_info;++ // Create the pipeline descriptor and do vertex/mesh pipeline specific setup+ let descriptor = match desc.vertex_processor {+ crate::VertexProcessor::Standard {+ vertex_buffers,+ ref vertex_stage,+ } => {+ // Vertex pipeline specific setup++ let descriptor = MTLRenderPipelineDescriptor::new();+ ts_info = None;+ ms_info = None;++ // Collect vertex buffer mappings+ let mut vertex_buffer_mappings =+ Vec::<naga::back::msl::VertexBufferMapping>::new();+ for (i, vbl) in vertex_buffers.iter().enumerate() {+ let Some(vbl) = vbl else {+ continue;+ };+ let mut attributes = Vec::<naga::back::msl::AttributeMapping>::new();+ for attribute in vbl.attributes.iter() {+ attributes.push(naga::back::msl::AttributeMapping {+ shader_location: attribute.shader_location,+ offset: attribute.offset as u32,+ format: convert_vertex_format_to_naga(attribute.format),+ });+ }++ let mapping = naga::back::msl::VertexBufferMapping {+ id: MAX_BUFFERS - 1 - i as u32,+ stride: if vbl.array_stride > 0 {+ vbl.array_stride.try_into().unwrap()+ } else {+ vbl.attributes+ .iter()+ .map(|attribute| attribute.offset + attribute.format.size())+ .max()+ .unwrap_or(0)+ .try_into()+ .unwrap()+ },+ step_mode: match (vbl.array_stride == 0, vbl.step_mode) {+ (true, _) => naga::back::msl::VertexBufferStepMode::Constant,+ (false, wgt::VertexStepMode::Vertex) => {+ naga::back::msl::VertexBufferStepMode::ByVertex+ }+ (false, wgt::VertexStepMode::Instance) => {+ naga::back::msl::VertexBufferStepMode::ByInstance+ }+ },+ attributes,+ };+ vertex_buffer_mappings.push(mapping);+ }++ // Setup vertex shader+ {+ let vs = self.load_shader(+ vertex_stage,+ &vertex_buffer_mappings,+ desc.layout,+ primitive_class,+ naga::ShaderStage::Vertex,+ )?;++ descriptor.setVertexFunction(Some(&vs.function));++ if supports_mutability {+ Self::set_buffers_mutability(+ &descriptor.vertexBuffers(),+ vs.immutable_buffer_mask,+ );+ }++ vs_info = Some(super::PipelineStageInfo {+ immediates: desc.layout.immediates_infos.vs,+ sizes_slot: desc.layout.per_stage_map.vs.sizes_buffer,+ sized_bindings: vs.sized_bindings,+ vertex_buffer_mappings,+ library: Some(vs.library),+ raw_wg_size: MTLSize {+ width: 0,+ height: 0,+ depth: 0,+ },+ work_group_memory_sizes: vec![],+ });+ }++ // Set the pipeline vertex buffer info+ if !vertex_buffers.is_empty() {+ let vertex_descriptor = MTLVertexDescriptor::new();+ for (i, vb) in vertex_buffers.iter().enumerate() {+ let Some(vb) = vb else {+ continue;+ };++ let buffer_index = MAX_BUFFERS as usize - 1 - i;+ let buffer_desc = unsafe {+ vertex_descriptor+ .layouts()+ .objectAtIndexedSubscript(buffer_index)+ };++ // Metal expects the stride to be the actual size of the attributes.+ // The semantics of array_stride == 0 can be achieved by setting+ // the step function to constant and rate to 0.+ if vb.array_stride == 0 {+ let stride = vb+ .attributes+ .iter()+ .map(|attribute| attribute.offset + attribute.format.size())+ .max()+ .unwrap_or(0);+ unsafe {+ buffer_desc.setStride(wgt::math::align_to(+ NSUInteger::try_from(stride).unwrap(),+ 4,+ ))+ };+ buffer_desc.setStepFunction(MTLVertexStepFunction::Constant);+ unsafe { buffer_desc.setStepRate(0) };+ } else {+ unsafe { buffer_desc.setStride(vb.array_stride as _) };+ buffer_desc.setStepFunction(conv::map_step_mode(vb.step_mode));+ }++ for at in vb.attributes {+ let attribute_desc = unsafe {+ vertex_descriptor+ .attributes()+ .objectAtIndexedSubscript(at.shader_location as _)+ };+ attribute_desc.setFormat(conv::map_vertex_format(at.format));+ unsafe { attribute_desc.setBufferIndex(buffer_index) };+ unsafe { attribute_desc.setOffset(at.offset as _) };+ }+ }+ descriptor.setVertexDescriptor(Some(&vertex_descriptor));+ }++ MetalGenericRenderPipelineDescriptor::Standard(descriptor)+ }+ crate::VertexProcessor::Mesh {+ ref task_stage,+ ref mesh_stage,+ } => {+ // Mesh pipeline specific setup++ vs_info = None;+ let descriptor = MTLMeshRenderPipelineDescriptor::new();++ // Setup task stage+ if let Some(ref task_stage) = task_stage {+ let ts = self.load_shader(+ task_stage,+ &[],+ desc.layout,+ primitive_class,+ naga::ShaderStage::Task,+ )?;+ unsafe { descriptor.setObjectFunction(Some(&ts.function)) };+ if supports_mutability {+ Self::set_buffers_mutability(+ &descriptor.meshBuffers(),+ ts.immutable_buffer_mask,+ );+ }+ ts_info = Some(super::PipelineStageInfo {+ immediates: desc.layout.immediates_infos.ts,+ sizes_slot: desc.layout.per_stage_map.ts.sizes_buffer,+ sized_bindings: ts.sized_bindings,+ vertex_buffer_mappings: vec![],+ library: Some(ts.library),+ raw_wg_size: ts.wg_size,+ work_group_memory_sizes: ts.wg_memory_sizes,+ });+ } else {+ ts_info = None;+ }++ // Setup mesh stage+ {+ let ms = self.load_shader(+ mesh_stage,+ &[],+ desc.layout,+ primitive_class,+ naga::ShaderStage::Mesh,+ )?;+ unsafe { descriptor.setMeshFunction(Some(&ms.function)) };+ if supports_mutability {+ Self::set_buffers_mutability(+ &descriptor.meshBuffers(),+ ms.immutable_buffer_mask,+ );+ }+ ms_info = Some(super::PipelineStageInfo {+ immediates: desc.layout.immediates_infos.ms,+ sizes_slot: desc.layout.per_stage_map.ms.sizes_buffer,+ sized_bindings: ms.sized_bindings,+ vertex_buffer_mappings: vec![],+ library: Some(ms.library),+ raw_wg_size: ms.wg_size,+ work_group_memory_sizes: ms.wg_memory_sizes,+ });+ }++ MetalGenericRenderPipelineDescriptor::Mesh(descriptor)+ }+ };++ let raw_triangle_fill_mode = match desc.primitive.polygon_mode {+ wgt::PolygonMode::Fill => MTLTriangleFillMode::Fill,+ wgt::PolygonMode::Line => MTLTriangleFillMode::Lines,+ wgt::PolygonMode::Point => panic!(+ "{:?} is not enabled for this backend",+ wgt::Features::POLYGON_MODE_POINT+ ),+ };++ // Fragment shader+ let fs_info = match desc.fragment_stage {+ Some(ref stage) => {+ let fs = self.load_shader(+ stage,+ &[],+ desc.layout,+ primitive_class,+ naga::ShaderStage::Fragment,+ )?;++ unsafe { descriptor.setFragmentFunction(Some(&fs.function)) };+ if supports_mutability {+ Self::set_buffers_mutability(+ &descriptor.fragmentBuffers(),+ fs.immutable_buffer_mask,+ );+ }++ Some(super::PipelineStageInfo {+ immediates: desc.layout.immediates_infos.fs,+ sizes_slot: desc.layout.per_stage_map.fs.sizes_buffer,+ sized_bindings: fs.sized_bindings,+ vertex_buffer_mappings: vec![],+ library: Some(fs.library),+ raw_wg_size: MTLSize {+ width: 0,+ height: 0,+ depth: 0,+ },+ work_group_memory_sizes: vec![],+ })+ }+ None => {+ // TODO: This is a workaround for what appears to be a Metal validation bug+ // A pixel format is required even though no attachments are provided+ if desc.color_targets.is_empty() && desc.depth_stencil.is_none() {+ descriptor.setDepthAttachmentPixelFormat(MTLPixelFormat::Depth32Float);+ }+ None+ }+ };++ // Setup pipeline color attachments+ for (i, ct) in desc.color_targets.iter().enumerate() {+ let at_descriptor =+ unsafe { descriptor.colorAttachments().objectAtIndexedSubscript(i) };+ let ct = if let Some(color_target) = ct.as_ref() {+ color_target+ } else {+ at_descriptor.setPixelFormat(MTLPixelFormat::Invalid);+ continue;+ };++ let raw_format = self+ .shared+ .private_texture_format_caps+ .map_format(ct.format);+ at_descriptor.setPixelFormat(raw_format);+ at_descriptor.setWriteMask(conv::map_color_write(ct.write_mask));++ if let Some(ref blend) = ct.blend {+ at_descriptor.setBlendingEnabled(true);+ let (color_op, color_src, color_dst) = conv::map_blend_component(&blend.color);+ let (alpha_op, alpha_src, alpha_dst) = conv::map_blend_component(&blend.alpha);++ at_descriptor.setRgbBlendOperation(color_op);+ at_descriptor.setSourceRGBBlendFactor(color_src);+ at_descriptor.setDestinationRGBBlendFactor(color_dst);++ at_descriptor.setAlphaBlendOperation(alpha_op);+ at_descriptor.setSourceAlphaBlendFactor(alpha_src);+ at_descriptor.setDestinationAlphaBlendFactor(alpha_dst);+ }+ }++ // Setup depth stencil state+ let depth_stencil = match desc.depth_stencil {+ Some(ref ds) => {+ let raw_format = self+ .shared+ .private_texture_format_caps+ .map_format(ds.format);+ let aspects = crate::FormatAspects::from(ds.format);+ if aspects.contains(crate::FormatAspects::DEPTH) {+ descriptor.setDepthAttachmentPixelFormat(raw_format);+ }+ if aspects.contains(crate::FormatAspects::STENCIL) {+ descriptor.setStencilAttachmentPixelFormat(raw_format);+ }++ let ds_descriptor = create_depth_stencil_desc(ds);+ let raw = self+ .shared+ .device+ .newDepthStencilStateWithDescriptor(&ds_descriptor)+ .unwrap();+ Some((raw, ds.bias))+ }+ None => None,+ };++ // Setup multisample state+ if desc.multisample.count != 1 {+ //TODO: handle sample mask+ match descriptor {+ MetalGenericRenderPipelineDescriptor::Standard(ref inner) => {+ #[allow(deprecated)]+ inner.setSampleCount(desc.multisample.count as _);+ }+ MetalGenericRenderPipelineDescriptor::Mesh(ref inner) => {+ unsafe { inner.setRasterSampleCount(desc.multisample.count as _) };+ }+ }+ descriptor.setAlphaToCoverageEnabled(desc.multisample.alpha_to_coverage_enabled);+ //descriptor.set_alpha_to_one_enabled(desc.multisample.alpha_to_one_enabled);+ }++ // Set debug label+ if let Some(name) = desc.label {+ descriptor.setLabel(Some(&NSString::from_str(name)));+ }+ if let Some(mv) = desc.multiview_mask {+ unsafe {+ descriptor.setMaxVertexAmplificationCount(mv.get().count_ones() as usize)+ };+ }++ // Create the pipeline from descriptor+ let raw = match descriptor {+ MetalGenericRenderPipelineDescriptor::Standard(d) => self+ .shared+ .device+ .newRenderPipelineStateWithDescriptor_error(&d),+ MetalGenericRenderPipelineDescriptor::Mesh(d) => self+ .shared+ .device+ .newRenderPipelineStateWithMeshDescriptor_options_reflection_error(+ &d,+ MTLPipelineOption::empty(),+ None,+ ),+ }+ .map_err(|e| {+ crate::PipelineError::Linkage(+ wgt::ShaderStages::VERTEX | wgt::ShaderStages::FRAGMENT,+ format!("new_render_pipeline_state: {e:?}"),+ )+ })?;++ self.counters.render_pipelines.add(1);++ Ok(super::RenderPipeline {+ raw,+ vs_info,+ fs_info,+ ts_info,+ ms_info,+ raw_primitive_type,+ raw_triangle_fill_mode,+ raw_front_winding: conv::map_winding(desc.primitive.front_face),+ raw_cull_mode: conv::map_cull_mode(desc.primitive.cull_mode),+ raw_depth_clip_mode: if self.features.contains(wgt::Features::DEPTH_CLIP_CONTROL) {+ Some(if desc.primitive.unclipped_depth {+ MTLDepthClipMode::Clamp+ } else {+ MTLDepthClipMode::Clip+ })+ } else {+ None+ },+ depth_stencil,+ })+ })+ }++ unsafe fn destroy_render_pipeline(&self, _pipeline: super::RenderPipeline) {+ self.counters.render_pipelines.sub(1);+ }++ unsafe fn create_compute_pipeline(+ &self,+ desc: &crate::ComputePipelineDescriptor<+ super::PipelineLayout,+ super::ShaderModule,+ super::PipelineCache,+ >,+ ) -> Result<super::ComputePipeline, crate::PipelineError> {+ autoreleasepool(|_| {+ let descriptor = MTLComputePipelineDescriptor::new();++ let module = desc.stage.module;+ let cs = if let ShaderModuleSource::Passthrough(passthrough_desc) = &module.source {+ let size = passthrough_desc.num_workgroups[desc.stage.entry_point];+ CompiledShader {+ library: passthrough_desc.library.clone(),+ function: passthrough_desc+ .library+ .newFunctionWithName(&NSString::from_str(desc.stage.entry_point))+ .ok_or(crate::PipelineError::EntryPoint(naga::ShaderStage::Compute))?,+ wg_size: MTLSize {+ width: size.0 as usize,+ height: size.1 as usize,+ depth: size.2 as usize,+ },+ wg_memory_sizes: vec![],+ sized_bindings: vec![],+ immutable_buffer_mask: 0,+ }+ } else {+ self.load_shader(+ &desc.stage,+ &[],+ desc.layout,+ MTLPrimitiveTopologyClass::Unspecified,+ naga::ShaderStage::Compute,+ )?+ };++ descriptor.setComputeFunction(Some(&cs.function));++ // https://developer.apple.com/documentation/metal/mtlpipelinebufferdescriptor/mutability+ if available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0) {+ Self::set_buffers_mutability(&descriptor.buffers(), cs.immutable_buffer_mask);+ }++ let cs_info = super::PipelineStageInfo {+ library: Some(cs.library),+ immediates: desc.layout.immediates_infos.cs,+ sizes_slot: desc.layout.per_stage_map.cs.sizes_buffer,+ sized_bindings: cs.sized_bindings,+ vertex_buffer_mappings: vec![],+ raw_wg_size: cs.wg_size,+ work_group_memory_sizes: cs.wg_memory_sizes,+ };++ if let Some(name) = desc.label {+ descriptor.setLabel(Some(&NSString::from_str(name)));+ }++ let raw = self+ .shared+ .device+ .newComputePipelineStateWithDescriptor_options_reflection_error(+ &descriptor,+ MTLPipelineOption::empty(),+ None,+ );++ let raw: Retained<ProtocolObject<dyn MTLComputePipelineState>> =+ raw.map_err(|e: Retained<NSError>| {+ crate::PipelineError::Linkage(+ wgt::ShaderStages::COMPUTE,+ format!("new_compute_pipeline_state: {e:?}"),+ )+ })?;++ self.counters.compute_pipelines.add(1);++ Ok(super::ComputePipeline { raw, cs_info })+ })+ }++ unsafe fn destroy_compute_pipeline(&self, _pipeline: super::ComputePipeline) {+ self.counters.compute_pipelines.sub(1);+ }++ unsafe fn create_ray_tracing_pipeline(+ &self,+ _desc: &crate::RayTracingPipelineDescriptor<+ super::PipelineLayout,+ super::ShaderModule,+ super::PipelineCache,+ >,+ ) -> Result<super::RayTracingPipeline, crate::PipelineError> {+ unimplemented!("Ray tracing pipelines are unsupported on Metal")+ }++ unsafe fn destroy_ray_tracing_pipeline(&self, _pipeline: super::RayTracingPipeline) {+ unimplemented!("Ray tracing pipelines are unsupported on Metal")+ }++ unsafe fn get_raytracing_pipeline_group_data(+ &self,+ _pipeline: &super::RayTracingPipeline,+ _groups: core::ops::Range<u32>,+ ) -> Result<Vec<u8>, crate::DeviceError> {+ unimplemented!("Ray tracing pipelines are unsupported on Metal")+ }++ unsafe fn create_pipeline_cache(+ &self,+ _desc: &crate::PipelineCacheDescriptor<'_>,+ ) -> Result<super::PipelineCache, crate::PipelineCacheError> {+ Ok(super::PipelineCache)+ }+ unsafe fn destroy_pipeline_cache(&self, _: super::PipelineCache) {}++ unsafe fn create_query_set(+ &self,+ desc: &wgt::QuerySetDescriptor<crate::Label>,+ ) -> DeviceResult<super::QuerySet> {+ autoreleasepool(|_| {+ match desc.ty {+ wgt::QueryType::Occlusion => {+ let size = desc.count as u64 * crate::QUERY_SIZE;+ let options = MTLResourceOptions::empty();+ //TODO: HazardTrackingModeUntracked+ let raw_buffer = self+ .shared+ .device+ .newBufferWithLength_options(size as usize, options)+ .unwrap();+ if let Some(label) = desc.label {+ raw_buffer.setLabel(Some(&NSString::from_str(label)));+ }+ Ok(super::QuerySet {+ raw_buffer,+ counter_sample_buffer: None,+ ty: desc.ty,+ })+ }+ wgt::QueryType::Timestamp => {+ let size = desc.count as u64 * crate::QUERY_SIZE;+ let device = &self.shared.device;+ let destination_buffer = device+ .newBufferWithLength_options(size as usize, MTLResourceOptions::empty())+ .unwrap();++ let csb_desc = MTLCounterSampleBufferDescriptor::new();+ csb_desc.setStorageMode(MTLStorageMode::Shared);+ unsafe { csb_desc.setSampleCount(desc.count as _) };+ if let Some(label) = desc.label {+ csb_desc.setLabel(&NSString::from_str(label));+ }++ let counter_sets = device.counterSets().unwrap();+ let timestamp_counter = match counter_sets+ .iter()+ .find(|cs| &*cs.name() == ns_string!("timestamp"))+ {+ Some(counter) => counter,+ None => {+ log::error!("Failed to obtain timestamp counter set.");+ return Err(crate::DeviceError::Unexpected);+ }+ };+ csb_desc.setCounterSet(Some(×tamp_counter));++ let counter_sample_buffer =+ match device.newCounterSampleBufferWithDescriptor_error(&csb_desc) {+ Ok(buffer) => buffer,+ Err(err) => {+ log::error!("Failed to create counter sample buffer: {err:?}");+ return Err(crate::DeviceError::Unexpected);+ }+ };++ self.counters.query_sets.add(1);++ Ok(super::QuerySet {+ raw_buffer: destination_buffer,+ counter_sample_buffer: Some(counter_sample_buffer),+ ty: desc.ty,+ })+ }+ _ => {+ todo!()+ }+ }+ })+ }++ unsafe fn destroy_query_set(&self, _set: super::QuerySet) {+ self.counters.query_sets.sub(1);+ }++ unsafe fn create_fence(&self) -> DeviceResult<super::Fence> {+ self.counters.fences.add(1);+ // https://developer.apple.com/documentation/metal/mtlsharedevent+ let shared_event = if available!(macos = 10.14, ios = 12.0, tvos = 12.0, visionos = 1.0) {+ self.shared.device.newSharedEvent() // This should be supported on said devices, but some sandbox environments may still restrict it, making it return `None`.+ } else {+ None+ };+ Ok(super::Fence {+ sync: Arc::new((Mutex::new(0), Condvar::new())),+ pending_command_buffers: RwLock::new(Vec::new()),+ shared_event,+ })+ }++ unsafe fn destroy_fence(&self, _fence: super::Fence) {+ self.counters.fences.sub(1);+ }++ unsafe fn get_fence_value(&self, fence: &super::Fence) -> DeviceResult<crate::FenceValue> {+ Ok(fence.get_latest())+ }+ unsafe fn wait(+ &self,+ fence: &super::Fence,+ wait_value: crate::FenceValue,+ timeout: Option<core::time::Duration>,+ ) -> DeviceResult<bool> {+ let (ref mutex, ref condvar) = *fence.sync;+ let mut lock = mutex.lock();++ if wait_value <= *lock {+ return Ok(true);+ }++ {+ let pending_command_buffers = fence.pending_command_buffers.read();+ if !pending_command_buffers+ .iter()+ .any(|&(value, _)| value >= wait_value)+ {+ log::error!("No active command buffers for fence value {wait_value}");+ return Err(crate::DeviceError::Lost);+ }+ }++ if let Some(timeout) = timeout {+ let result = condvar.wait_while_for(&mut lock, |value| *value < wait_value, timeout);+ if result.timed_out() {+ return Ok(*lock >= wait_value);+ }+ } else {+ condvar.wait_while(&mut lock, |value| *value < wait_value);+ }++ Ok(true)+ }++ unsafe fn start_graphics_debugger_capture(&self) -> bool {+ // https://developer.apple.com/documentation/metal/mtlcapturemanager+ if !available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0) {+ return false;+ }+ let device = &self.shared.device;+ let shared_capture_manager = unsafe { MTLCaptureManager::sharedCaptureManager() };+ let default_capture_scope = shared_capture_manager.newCaptureScopeWithDevice(device);+ shared_capture_manager.setDefaultCaptureScope(Some(&default_capture_scope));+ #[allow(deprecated)]+ shared_capture_manager.startCaptureWithScope(&default_capture_scope);+ default_capture_scope.beginScope();+ true+ }++ unsafe fn stop_graphics_debugger_capture(&self) {+ let shared_capture_manager = unsafe { MTLCaptureManager::sharedCaptureManager() };+ if let Some(default_capture_scope) = shared_capture_manager.defaultCaptureScope() {+ default_capture_scope.endScope();+ }+ shared_capture_manager.stopCapture();+ }++ unsafe fn get_acceleration_structure_build_sizes(+ &self,+ descriptor: &crate::GetAccelerationStructureBuildSizesDescriptor<super::Buffer>,+ ) -> crate::AccelerationStructureBuildSizes {+ let acceleration_structure_descriptor =+ conv::map_acceleration_structure_descriptor(descriptor.entries, descriptor.flags);+ let info = self+ .shared+ .device+ .accelerationStructureSizesWithDescriptor(&acceleration_structure_descriptor);+ crate::AccelerationStructureBuildSizes {+ acceleration_structure_size: info.accelerationStructureSize as u64,+ update_scratch_size: info.refitScratchBufferSize as u64,+ build_scratch_size: info.buildScratchBufferSize as u64,+ }+ }++ unsafe fn get_acceleration_structure_device_address(+ &self,+ acceleration_structure: &super::AccelerationStructure,+ ) -> wgt::BufferAddress {+ acceleration_structure.raw.gpuResourceID().to_raw()+ }++ unsafe fn create_acceleration_structure(+ &self,+ descriptor: &crate::AccelerationStructureDescriptor,+ ) -> Result<super::AccelerationStructure, crate::DeviceError> {+ // self.counters.acceleration_structures.add(1);+ autoreleasepool(|_| {+ Ok(super::AccelerationStructure {+ raw: self+ .shared+ .device+ .newAccelerationStructureWithSize(descriptor.size as usize)+ .ok_or(crate::DeviceError::OutOfMemory)?,+ })+ })+ }++ unsafe fn destroy_acceleration_structure(+ &self,+ _acceleration_structure: super::AccelerationStructure,+ ) {+ // self.counters.acceleration_structures.sub(1);+ }++ fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8> {+ let temp = MTLIndirectAccelerationStructureInstanceDescriptor {+ transformationMatrix: MTLPackedFloat4x3 {+ columns: [+ MTLPackedFloat3 {+ x: instance.transform[0],+ y: instance.transform[4],+ z: instance.transform[8],+ },+ MTLPackedFloat3 {+ x: instance.transform[1],+ y: instance.transform[5],+ z: instance.transform[9],+ },+ MTLPackedFloat3 {+ x: instance.transform[2],+ y: instance.transform[6],+ z: instance.transform[10],+ },+ MTLPackedFloat3 {+ x: instance.transform[3],+ y: instance.transform[7],+ z: instance.transform[11],+ },+ ],+ },+ options: MTLAccelerationStructureInstanceOptions::None,+ mask: instance.mask as u32,+ intersectionFunctionTableOffset: instance.pipeline_intersection_data_offset,+ userID: instance.custom_data,+ accelerationStructureID: unsafe { MTLResourceID::from_raw(instance.blas_address) },+ };++ wgt::bytemuck_wrapper!(unsafe struct Desc(MTLIndirectAccelerationStructureInstanceDescriptor));++ bytemuck::bytes_of(&Desc::wrap(temp)).to_vec()+ }++ fn get_internal_counters(&self) -> wgt::HalCounters {+ self.counters.as_ref().clone()+ }++ fn check_if_oom(&self) -> Result<(), crate::DeviceError> {+ // TODO: see https://github.com/gfx-rs/wgpu/issues/7460++ Ok(())+ }+}diff --git a/vendor/wgpu-hal/src/metal/library_from_metallib.rs b/vendor/wgpu-hal/src/metal/library_from_metallib.rsnew file mode 100644index 00000000..23898474--- /dev/null+++ b/vendor/wgpu-hal/src/metal/library_from_metallib.rs@@ -0,0 +1,48 @@+use core::{ffi::c_void, ptr::NonNull};++use objc2::{+ msg_send,+ rc::Retained,+ runtime::{AnyObject, ProtocolObject},+};+use objc2_foundation::NSError;+use objc2_metal::{MTLDevice, MTLLibrary};++#[link(name = "System")] // libdispatch lives inside libSystem+extern "C" {+ fn dispatch_data_create(+ buffer: NonNull<c_void>,+ size: usize,+ queue: Option<NonNull<c_void>>, // dispatch_queue_t _Nullable+ destructor: *mut c_void, // dispatch_block_t _Nullable+ ) -> *mut AnyObject; // dispatch_data_t++ fn dispatch_release(object: *mut AnyObject); // dispatch_data_t+}++const DISPATCH_DATA_DESTRUCTOR_DEFAULT: *mut c_void = core::ptr::null_mut();++/// See <https://github.com/madsmtm/objc2/issues/819>.+///+/// newLibraryWithData requires DispatchData, part of the Dispatch library.+/// Not only is this a big additional dependency for a glorified `Vec<u8>`+/// in this case, but the code in the dispatch2 rust crate is a WIP and+/// Mozilla has decided not to vendor it yet.+///+/// This function allows us to deal with DispatchData without dealing+/// with these problems.+pub(crate) fn new_library_from_metallib_bytes(+ device: &ProtocolObject<dyn MTLDevice>,+ data: &[u8],+) -> Result<Retained<ProtocolObject<dyn MTLLibrary>>, Retained<NSError>> {+ let buffer = NonNull::new(data.as_ptr().cast_mut()).unwrap().cast();+ let data =+ unsafe { dispatch_data_create(buffer, data.len(), None, DISPATCH_DATA_DESTRUCTOR_DEFAULT) };+ assert!(!data.is_null());++ let res: Result<Retained<ProtocolObject<dyn MTLLibrary>>, Retained<NSError>> =+ unsafe { msg_send![device, newLibraryWithData: data, error: _] };++ unsafe { dispatch_release(data) };+ res+}diff --git a/vendor/wgpu-hal/src/metal/mod.rs b/vendor/wgpu-hal/src/metal/mod.rsnew file mode 100644index 00000000..e227590e--- /dev/null+++ b/vendor/wgpu-hal/src/metal/mod.rs@@ -0,0 +1,1410 @@+/*!+# Metal API internals.++## Pipeline Layout++In Metal, immediates, vertex buffers, and resources in the bind groups+are all placed together in the native resource bindings, which work similarly to D3D11:+there are tables of textures, buffers, and samplers.++We put immediates first (if any) in the table, followed by bind group 0+resources, followed by other bind groups. The vertex buffers are bound at the very+end of the VS buffer table.++!*/++#[allow(+ deprecated,+ reason = "MTLFeatureSet` is superseded by `MTLGpuFamily`.+ However, `MTLGpuFamily` is only supported starting MacOS 10.15, whereas our minimum target is MacOS 10.13,+ See https://github.com/gpuweb/gpuweb/issues/1069 for minimum spec.+ TODO: Eventually all deprecated features should be abstracted and use new api when available."+)]+mod adapter;+mod command;+mod conv;+mod device;+mod library_from_metallib;+mod surface;+mod time;++use alloc::{+ string::{String, ToString as _},+ sync::Arc,+ vec::Vec,+};+use core::{fmt, iter, ops, ptr::NonNull, sync::atomic};+use std::sync::OnceLock;++use bitflags::bitflags;+use hashbrown::HashMap;+use naga::FastHashMap;+use objc2::{+ available,+ rc::{autoreleasepool, Retained},+ runtime::ProtocolObject,+};+use objc2_foundation::ns_string;+use objc2_metal::{+ MTLAccelerationStructure, MTLAccelerationStructureCommandEncoder, MTLArgumentBuffersTier,+ MTLBlitCommandEncoder, MTLBuffer, MTLCommandBuffer, MTLCommandBufferStatus, MTLCommandQueue,+ MTLComputeCommandEncoder, MTLComputePipelineState, MTLCounterSampleBuffer, MTLCullMode,+ MTLDepthClipMode, MTLDepthStencilState, MTLDevice, MTLDrawable, MTLIndexType,+ MTLLanguageVersion, MTLLibrary, MTLPrimitiveType, MTLReadWriteTextureTier,+ MTLRenderCommandEncoder, MTLRenderPipelineState, MTLRenderStages, MTLResource,+ MTLResourceUsage, MTLSamplerState, MTLSharedEvent, MTLSize, MTLTexture, MTLTextureType,+ MTLTriangleFillMode, MTLWinding,+};+use objc2_quartz_core::CAMetalLayer;+use parking_lot::{Condvar, Mutex, RwLock};++#[derive(Clone, Debug)]+pub struct Api;++type ResourceIndex = u32;++impl crate::Api for Api {+ const VARIANT: wgt::Backend = wgt::Backend::Metal;++ type Instance = Instance;+ type Surface = Surface;+ type Adapter = Adapter;+ type Device = Device;++ type Queue = Queue;+ type CommandEncoder = CommandEncoder;+ type CommandBuffer = CommandBuffer;++ type Buffer = Buffer;+ type Texture = Texture;+ type SurfaceTexture = SurfaceTexture;+ type TextureView = TextureView;+ type Sampler = Sampler;+ type QuerySet = QuerySet;+ type Fence = Fence;++ type BindGroupLayout = BindGroupLayout;+ type BindGroup = BindGroup;+ type PipelineLayout = PipelineLayout;+ type ShaderModule = ShaderModule;+ type RenderPipeline = RenderPipeline;+ type RayTracingPipeline = RayTracingPipeline;+ type ComputePipeline = ComputePipeline;+ type PipelineCache = PipelineCache;++ type AccelerationStructure = AccelerationStructure;+}++crate::impl_dyn_resource!(+ Adapter,+ AccelerationStructure,+ BindGroup,+ BindGroupLayout,+ Buffer,+ CommandBuffer,+ CommandEncoder,+ ComputePipeline,+ Device,+ Fence,+ Instance,+ PipelineCache,+ PipelineLayout,+ QuerySet,+ Queue,+ RenderPipeline,+ RayTracingPipeline,+ Sampler,+ ShaderModule,+ Surface,+ SurfaceTexture,+ Texture,+ TextureView+);++/// Provides availability information about Mac APIs.+///+/// This may include Metal features that depend only on software support.+/// Features with varying hardware support are in [`CapabilitiesQuery`]+///+/// When feature detection is only needed once, it may also be done inline.+struct OsFeatures;++impl OsFeatures {+ fn display_sync() -> bool {+ // https://developer.apple.com/documentation/quartzcore/cametallayer/displaysyncenabled+ available!(macos = 10.13) || cfg!(target_abi = "macabi")+ }+}++#[derive(Debug)]+pub struct Instance {+ flags: wgt::InstanceFlags,+}++impl Instance {+ pub fn create_surface_from_layer(&self, layer: &CAMetalLayer) -> Surface {+ Surface::from_layer(layer)+ }+}++impl crate::Instance for Instance {+ type A = Api;++ unsafe fn init(desc: &crate::InstanceDescriptor<'_>) -> Result<Self, crate::InstanceError> {+ profiling::scope!("Init Metal Backend");+ // We do not enable metal validation based on the validation flags as it affects the entire+ // process. Instead, we enable the validation inside the test harness itself in tests/src/native.rs.+ Ok(Instance { flags: desc.flags })+ }++ unsafe fn create_surface(+ &self,+ display_handle: raw_window_handle::RawDisplayHandle,+ window_handle: raw_window_handle::RawWindowHandle,+ ) -> Result<Surface, crate::InstanceError> {+ let layer = match (display_handle, window_handle) {+ (+ raw_window_handle::RawDisplayHandle::AppKit(_),+ raw_window_handle::RawWindowHandle::AppKit(handle),+ ) => unsafe { raw_window_metal::Layer::from_ns_view(handle.ns_view) },+ (+ raw_window_handle::RawDisplayHandle::UiKit(_),+ raw_window_handle::RawWindowHandle::UiKit(handle),+ ) => unsafe { raw_window_metal::Layer::from_ui_view(handle.ui_view) },+ _ => {+ return Err(crate::InstanceError::new(format!(+ "window handle {window_handle:?} is not a Metal-compatible handle"+ )))+ }+ };++ // SAFETY: The layer is an initialized instance of `CAMetalLayer`, and+ // we transfer the retain count to `Retained` using `into_raw`.+ let layer = unsafe {+ Retained::from_raw(layer.into_raw().cast::<CAMetalLayer>().as_ptr()).unwrap()+ };++ Ok(Surface::new(layer))+ }++ unsafe fn enumerate_adapters(+ &self,+ _surface_hint: Option<&Surface>,+ ) -> Vec<crate::ExposedAdapter<Api>> {+ let devices = objc2_metal::MTLCopyAllDevices();+ let instance_flags = self.flags;+ let mut adapters: Vec<crate::ExposedAdapter<Api>> = devices+ .into_iter()+ .map(|d| AdapterShared::expose(d, instance_flags))+ .collect();+ adapters.sort_by_key(|ad| {+ (+ ad.adapter.shared.private_caps.low_power,+ ad.adapter.shared.private_caps.headless,+ )+ });+ adapters+ }+}++bitflags!(+ /// Similar to `MTLCounterSamplingPoint`, but a bit higher abstracted for our purposes.+ #[derive(Debug, Copy, Clone)]+ pub struct TimestampQuerySupport: u32 {+ /// On creating Metal encoders.+ const STAGE_BOUNDARIES = 1 << 1;+ /// Within existing draw encoders.+ const ON_RENDER_ENCODER = Self::STAGE_BOUNDARIES.bits() | (1 << 2);+ /// Within existing dispatch encoders.+ const ON_COMPUTE_ENCODER = Self::STAGE_BOUNDARIES.bits() | (1 << 3);+ /// Within existing blit encoders.+ const ON_BLIT_ENCODER = Self::STAGE_BOUNDARIES.bits() | (1 << 4);++ /// Within any wgpu render/compute pass.+ const INSIDE_WGPU_PASSES = Self::ON_RENDER_ENCODER.bits() | Self::ON_COMPUTE_ENCODER.bits();+ }+);++#[allow(dead_code)]+struct CapabilitiesQuery {+ msl_version: MTLLanguageVersion,+ fragment_rw_storage: bool,+ read_write_texture_tier: MTLReadWriteTextureTier,+ msaa_desktop: bool,+ msaa_apple3: bool,+ msaa_apple7: bool,+ resource_heaps: bool,+ argument_buffers: Option<MTLArgumentBuffersTier>,+ mutable_comparison_samplers: bool,+ sampler_clamp_to_border: bool,+ indirect_draw_dispatch: bool,+ base_vertex_first_instance_drawing: bool,+ dual_source_blending: bool,+ low_power: bool,+ headless: bool,+ layered_rendering: bool,+ function_specialization: bool,+ depth_clip_mode: bool,+ texture_cube_array: bool,+ supports_float_filtering: bool,+ format_depth24_stencil8: bool,+ format_depth32_stencil8_filter: bool,+ format_depth32_stencil8_none: bool,+ format_min_srgb_channels: u8,+ format_b5: bool,+ format_bc: bool,+ format_eac_etc: bool,+ format_astc: bool,+ format_astc_hdr: bool,+ format_astc_3d: bool,+ format_any8_unorm_srgb_all: bool,+ format_any8_unorm_srgb_no_write: bool,+ format_any8_snorm_all: bool,+ format_r16_norm_all: bool,+ format_r32_all: bool,+ format_r32_no_write: bool,+ format_r32float_no_write_no_filter: bool,+ format_r32float_no_filter: bool,+ format_r32float_all: bool,+ format_rgba8_srgb_all: bool,+ format_rgba8_srgb_no_write: bool,+ format_rgb10a2_unorm_all: bool,+ format_rgb10a2_unorm_no_write: bool,+ format_rgb10a2_uint_write: bool,+ format_rg11b10_all: bool,+ format_rg11b10_no_write: bool,+ format_rgb9e5_all: bool,+ format_rgb9e5_no_write: bool,+ format_rgb9e5_filter_only: bool,+ format_rg32_color: bool,+ format_rg32_color_write: bool,+ format_rg32float_all: bool,+ format_rg32float_color_blend: bool,+ format_rg32float_no_filter: bool,+ format_rgba32int_color: bool,+ format_rgba32int_color_write: bool,+ format_rgba32float_color: bool,+ format_rgba32float_color_write: bool,+ format_rgba32float_all: bool,+ format_depth16unorm: bool,+ format_depth16unorm_filter: bool,+ format_depth32float_filter: bool,+ format_depth32float_none: bool,+ format_bgr10a2_all: bool,+ format_bgr10a2_no_write: bool,+ max_textures_per_stage: (ResourceIndex, ResourceIndex),+ max_binding_array_elements: ResourceIndex,+ max_sampler_binding_array_elements: ResourceIndex,+ buffer_alignment: u64,+ constant_buffer_offset_alignment: u32,+ max_buffer_size: u64,+ max_texture_size: u64,+ max_texture_3d_size: u64,+ max_texture_layers: u64,+ max_fragment_input_components: u64,+ max_color_render_targets: u8,+ max_color_attachment_bytes_per_sample: u8,+ max_inter_stage_shader_variables: u32,+ max_threads_per_group: u32,+ max_total_threadgroup_memory: u32,+ sample_count_mask: crate::TextureFormatCapabilities,+ supports_debug_markers: bool,+ supports_binary_archives: bool,+ supports_arrays_of_textures: bool,+ supports_arrays_of_textures_write: bool,+ supports_depth_clip_control: bool,+ supports_shader_primitive_index: bool,+ has_unified_memory: Option<bool>,+ timestamp_query_support: TimestampQuerySupport,+ supports_simd_scoped_operations: bool,+ supports_cooperative_matrix: bool,+ int64: bool,+ int64_atomics_min_max: bool,+ int64_atomics: bool,+ float_atomics: bool,+ mesh_shaders: bool,+ max_task_workgroup_count: u32,+ max_mesh_workgroup_count: u32,+ max_task_payload_size: u32,+ supported_vertex_amplification_factor: u32,+ shader_barycentrics: bool,+ supports_memoryless_storage: bool,+ supports_raytracing: bool,+ shader_per_vertex: bool,+ supports_multisample_array: bool,+}++#[derive(Debug)]+struct PrivateCapabilities {+ msl_version: MTLLanguageVersion,+ low_power: bool,+ headless: bool,+ has_unified_memory: Option<bool>,+ timestamp_query_support: TimestampQuerySupport,+ supports_memoryless_storage: bool,+ mesh_shaders: bool,+}++#[derive(Debug)]+struct PrivateTextureFormatCapabilities {+ read_write_texture_tier: MTLReadWriteTextureTier,+ sample_count_mask: crate::TextureFormatCapabilities,+ int64_atomics: bool,+ msaa_desktop: bool,+ msaa_apple3: bool,+ msaa_apple7: bool,+ format_r32float_all: bool,+ format_rgba8_srgb_all: bool,+ format_rgb10a2_uint_write: bool,+ format_rgb10a2_unorm_all: bool,+ format_rg11b10_all: bool,+ format_rg32float_all: bool,+ format_rgba32float_all: bool,+ format_depth16unorm: bool,+ format_depth16unorm_filter: bool,+ format_depth32float_filter: bool,+ format_depth24_stencil8: bool,+ format_bc: bool,+ format_eac_etc: bool,+ format_astc: bool,+ format_astc_hdr: bool,+}++#[derive(Clone, Debug)]+struct PrivateDisabilities {+ /// Near depth is not respected properly on some Intel GPUs.+ broken_viewport_near_depth: bool,+ /// Multi-target clears don't appear to work properly on Intel GPUs.+ #[allow(dead_code)]+ broken_layered_clear_image: bool,+}++#[derive(Debug)]+struct Settings {+ retain_command_buffer_references: bool,+}++impl Default for Settings {+ fn default() -> Self {+ Self {+ retain_command_buffer_references: true,+ }+ }+}++#[derive(Debug)]+struct AdapterShared {+ device: Retained<ProtocolObject<dyn MTLDevice>>,+ disabilities: PrivateDisabilities,+ private_caps: PrivateCapabilities,+ private_texture_format_caps: PrivateTextureFormatCapabilities,+ settings: Settings,+ presentation_timer: time::PresentationTimer,+}++#[cfg(send_sync)]+static_assertions::assert_impl_all!(AdapterShared: Send, Sync);++impl AdapterShared {+ fn new(+ device: Retained<ProtocolObject<dyn MTLDevice>>,+ capabilities_query: &CapabilitiesQuery,+ ) -> Self {+ let private_caps = capabilities_query.private_capabilities();+ let private_texture_format_caps = capabilities_query.private_texture_format_capabilities();+ log::debug!("{private_caps:#?}");+ log::debug!("{private_texture_format_caps:#?}");++ Self {+ disabilities: PrivateDisabilities::new(&device),+ private_caps,+ private_texture_format_caps,+ device,+ settings: Settings::default(),+ presentation_timer: time::PresentationTimer::new(),+ }+ }++ fn expose(+ device: Retained<ProtocolObject<dyn MTLDevice>>,+ instance_flags: wgt::InstanceFlags,+ ) -> crate::ExposedAdapter<Api> {+ autoreleasepool(|_| {+ let name = device.name().to_string();+ let capabilities_query = CapabilitiesQuery::new(&device);+ let shared = AdapterShared::new(device, &capabilities_query);+ let features = capabilities_query.features();+ let capabilities = capabilities_query.capabilities(instance_flags);+ crate::ExposedAdapter {+ info: wgt::AdapterInfo {+ name,+ // These are hardcoded based on typical values for Metal devices+ //+ // See <https://github.com/gpuweb/gpuweb/blob/main/proposals/subgroups.md#adapter-info>+ // for more information.+ subgroup_min_size: 4,+ subgroup_max_size: 64,+ transient_saves_memory: Some(shared.private_caps.supports_memoryless_storage),+ ..wgt::AdapterInfo::new(shared.private_caps.device_type(), wgt::Backend::Metal)+ },+ features,+ capabilities,+ adapter: Adapter::new(Arc::new(shared)),+ }+ })+ }+}++#[derive(Debug)]+pub struct Adapter {+ shared: Arc<AdapterShared>,+}++#[cfg(send_sync)]+static_assertions::assert_impl_all!(Adapter: Send, Sync);++#[derive(Debug)]+pub struct Queue {+ shared: Arc<QueueShared>,+ timestamp_period: f32,+}++#[cfg(send_sync)]+static_assertions::assert_impl_all!(Queue: Send, Sync);++impl Queue {+ pub unsafe fn queue_from_raw(+ raw: Retained<ProtocolObject<dyn MTLCommandQueue>>,+ timestamp_period: f32,+ ) -> Self {+ Self {+ shared: Arc::new(QueueShared {+ raw,+ command_buffer_created_not_submitted: atomic::AtomicUsize::new(0),+ pending_waits: Mutex::new(Vec::new()),+ pending_signals: Mutex::new(Vec::new()),+ relay: OnceLock::new(),+ }),+ timestamp_period,+ }+ }++ pub fn as_raw(&self) -> &ProtocolObject<dyn MTLCommandQueue> {+ &self.shared.raw+ }++ /// Enable strict GPU-side ordering for [`Self::add_wait_event`].+ ///+ /// By default, `add_wait_event` encodes the wait on a separate+ /// internal command buffer. Metal allows independent command+ /// buffers in a queue to overlap on the GPU, so a wait CB does+ /// not strictly gate subsequent user command buffers when those+ /// CBs share no Metal-tracked resources with it. Single-stream+ /// pipelines often serialize anyway because the GPU has no other+ /// concurrent work to fill the slot, but mixed workloads (decode+ /// + compute + render) can race.+ ///+ /// When enabled, every [`crate::CommandEncoder::begin_encoding`]+ /// pre-encodes a wait on an internal `MTLSharedEvent` at the start+ /// of the new command buffer; every [`crate::Queue::submit`] then+ /// signals that event after draining the staged external waits.+ /// All command buffers since the previous submit are released in+ /// lockstep once the foreign signals arrive, regardless of GPU+ /// concurrency.+ ///+ /// Costs one extra `encodeWaitForEvent` per command buffer plus+ /// one extra internal command buffer per submit on this queue.+ /// Other queues are unaffected.+ ///+ /// Idempotent. Cannot be disabled - once enabled, the queue stays+ /// in strict mode for its lifetime, since command buffers already+ /// encoded would be stranded if the relay stopped firing.+ pub fn enable_strict_event_sync(&self) -> Result<(), crate::DeviceError> {+ if self.shared.relay.get().is_some() {+ return Ok(());+ }+ let event = self+ .shared+ .raw+ .device()+ .newSharedEvent()+ .ok_or(crate::DeviceError::OutOfMemory)?;+ let _ = self.shared.relay.set(Relay {+ event,+ next_release_value: atomic::AtomicU64::new(1),+ commit_lock: Mutex::new(()),+ });+ Ok(())+ }++ /// Stage an `MTLCommandBuffer::encodeWaitForEvent(event, value)` for+ /// the next [`crate::Queue::submit`]. Lets external producers be waited+ /// on without a CPU block.+ ///+ /// By default the wait is encoded onto a dedicated internal command+ /// buffer committed before the submit's user CBs - best-effort under+ /// cross-CB GPU concurrency, see [`Self::enable_strict_event_sync`]+ /// for strict gating. With strict mode enabled, the wait is chained+ /// through an internal relay event that gates every user command+ /// buffer encoded since the previous submit.+ ///+ /// Staging is queue-wide, not per-thread or per-submit: any+ /// `add_wait_event` call is consumed by whichever+ /// [`crate::Queue::submit`] runs next on this queue. If you stage+ /// events from multiple threads, coordinate the staging and the+ /// submit yourself, or another thread's submit may drain your+ /// pending waits.+ pub fn add_wait_event(&self, event: Retained<ProtocolObject<dyn MTLSharedEvent>>, value: u64) {+ self.shared.pending_waits.lock().push((event, value));+ }++ /// Remove `event` from the pending wait list if it is still present.+ /// Returns `true` if it was found and removed.+ pub fn remove_wait_event(&self, event: &ProtocolObject<dyn MTLSharedEvent>) -> bool {+ let target: *const ProtocolObject<dyn MTLSharedEvent> = event;+ let mut waits = self.shared.pending_waits.lock();+ let before = waits.len();+ waits.retain(|(e, _)| Retained::as_ptr(e) != target);+ waits.len() != before+ }++ /// Stage an `MTLCommandBuffer::encodeSignalEvent(event, value)` for+ /// the next [`crate::Queue::submit`]. The signal is encoded after+ /// the submit's own completion signal, so a foreign API waiting on+ /// `(event, value)` observes the wgpu work as done.+ ///+ /// Staging is queue-wide, not per-thread or per-submit: see+ /// [`Self::add_wait_event`] for the threading caveat.+ pub fn add_signal_event(+ &self,+ event: Retained<ProtocolObject<dyn MTLSharedEvent>>,+ value: u64,+ ) {+ self.shared.pending_signals.lock().push((event, value));+ }++ /// Remove `event` from the pending signal list if it is still present.+ /// Returns `true` if it was found and removed.+ pub fn remove_signal_event(&self, event: &ProtocolObject<dyn MTLSharedEvent>) -> bool {+ let target: *const ProtocolObject<dyn MTLSharedEvent> = event;+ let mut signals = self.shared.pending_signals.lock();+ let before = signals.len();+ signals.retain(|(e, _)| Retained::as_ptr(e) != target);+ signals.len() != before+ }+}++type PendingEvents = Mutex<Vec<(Retained<ProtocolObject<dyn MTLSharedEvent>>, u64)>>;++/// Internal relay used by [`Queue::enable_strict_event_sync`] to chain+/// staged waits across all CBs in a submit.+///+/// `begin_encoding` reads `next_release_value` and pre-encodes+/// `encodeWaitForEvent(event, expected)` at the start of each CB.+/// `submit` claims the value via `fetch_add`, encodes the foreign+/// waits + `encodeSignalEvent(event, claimed)` on a wait CB, and+/// commits it. `commit_lock` serializes the claim+commit pair so+/// concurrent submits land their signals in monotonic *commit* order+/// on the CPU. GPU-side execution of the resulting wait CBs may still+/// reorder under concurrency; see the comment in `submit` for why+/// that's harmless.+#[derive(Debug)]+struct Relay {+ event: Retained<ProtocolObject<dyn MTLSharedEvent>>,+ next_release_value: atomic::AtomicU64,+ commit_lock: Mutex<()>,+}++#[derive(Debug)]+pub struct QueueShared {+ raw: Retained<ProtocolObject<dyn MTLCommandQueue>>,+ // Tracks command buffers created via `CommandEncoder::begin_encoding` that+ // have not yet been submitted or discarded. Used to proactively fail+ // before hitting Metal's `maxCommandBufferCount`.+ //+ // (In a few places we call `.commandBuffer{,WithUnretainedReferences}` directly+ // to create command buffers for internal purposes. In those cases we always+ // commit the buffer immediately, so we don't adjust the counter for them.)+ command_buffer_created_not_submitted: atomic::AtomicUsize,+ pending_waits: PendingEvents,+ pending_signals: PendingEvents,+ relay: OnceLock<Relay>,+}++#[derive(Debug)]+pub struct Device {+ shared: Arc<AdapterShared>,+ features: wgt::Features,+ counters: Arc<wgt::HalCounters>,+ limits: wgt::Limits,+}++#[derive(Debug)]+pub struct Surface {+ render_layer: Mutex<Retained<CAMetalLayer>>,+ swapchain_format: RwLock<Option<wgt::TextureFormat>>,+ extent: RwLock<wgt::Extent3d>,+}++unsafe impl Send for Surface {}+unsafe impl Sync for Surface {}++#[derive(Debug)]+pub struct SurfaceTexture {+ texture: Texture,+ // Useful for UI-intensive applications that are sensitive to+ // window resizing.+ drawable: Retained<ProtocolObject<dyn MTLDrawable>>,+ present_with_transaction: bool,+}++impl crate::DynSurfaceTexture for SurfaceTexture {}++impl core::borrow::Borrow<Texture> for SurfaceTexture {+ fn borrow(&self) -> &Texture {+ &self.texture+ }+}++impl core::borrow::Borrow<dyn crate::DynTexture> for SurfaceTexture {+ fn borrow(&self) -> &dyn crate::DynTexture {+ &self.texture+ }+}++unsafe impl Send for SurfaceTexture {}+unsafe impl Sync for SurfaceTexture {}++impl crate::Queue for Queue {+ type A = Api;++ unsafe fn submit(+ &self,+ command_buffers: &[&CommandBuffer],+ _surface_textures: &[&SurfaceTexture],+ (signal_fence, signal_value): (&Fence, crate::FenceValue),+ ) -> Result<(), crate::DeviceError> {+ autoreleasepool(|_| {+ // Drain caller-staged waits onto a dedicated command buffer+ // committed before the user CBs.+ //+ // When strict event sync is enabled, this CB also signals+ // the relay event to release every user CB encoded since+ // the previous submit (see `Queue::enable_strict_event_sync`).+ // The `commit_lock` is held across `fetch_add` + `commit` so+ // concurrent submits land their relay signals in monotonic+ // *commit* order on the CPU side; otherwise a later-claimed+ // signal could commit first and the subsequent backward+ // signal would temporarily regress the relay's signaledValue.+ //+ // GPU-side ordering across independent wait CBs remains+ // best-effort: Metal may run them in parallel, so a wait CB+ // with foreign waits can fire its signal after a later+ // submit's wait-free signal. CBs already released stay+ // released (`MTLSharedEvent` waits are `>=`), and future+ // submits' signals catch the value back up, so the regression+ // is harmless - but users wanting strict GPU-side ordering+ // across concurrent submits must serialize submits themselves.+ //+ // Without strict mode, we only emit a wait CB when there are+ // pending waits - keeps the common-case submit overhead-free.+ {+ let relay = self.shared.relay.get();+ let mut waits = self.shared.pending_waits.lock();+ if relay.is_some() || !waits.is_empty() {+ let _commit_guard = relay.map(|r| r.commit_lock.lock());+ // We do not bother adjusting `command_buffer_created_not_submitted`+ // because we immediately commit this buffer.+ let wait_cb = self+ .shared+ .raw+ .commandBufferWithUnretainedReferences()+ .ok_or(crate::DeviceError::Lost)?;+ wait_cb.setLabel(Some(ns_string!("(wgpu internal) Wait")));+ for (event, value) in waits.drain(..) {+ wait_cb.encodeWaitForEvent_value(event.as_ref(), value);+ }+ if let Some(relay) = relay {+ let release = relay+ .next_release_value+ .fetch_add(1, atomic::Ordering::AcqRel);+ wait_cb.encodeSignalEvent_value(relay.event.as_ref(), release);+ }+ wait_cb.commit();+ }+ }++ let extra_command_buffer = {+ let fence_sync = Arc::clone(&signal_fence.sync);+ let block = block2::RcBlock::new(move |_cmd_buf| {+ *fence_sync.0.lock() = signal_value;+ fence_sync.1.notify_all();+ });++ let raw = match command_buffers.last() {+ Some(&cmd_buf) => cmd_buf.raw.clone(),+ None => {+ // We do not bother adjusting `command_buffer_created_not_submitted`+ // because we immediately commit this buffer.+ self.shared+ .raw+ .commandBufferWithUnretainedReferences()+ .ok_or(crate::DeviceError::Lost)?+ }+ };+ raw.setLabel(Some(ns_string!("(wgpu internal) Signal")));+ unsafe { raw.addCompletedHandler(block2::RcBlock::as_ptr(&block)) };++ signal_fence.maintain();+ signal_fence+ .pending_command_buffers+ .write()+ .push((signal_value, raw.clone()));++ if let Some(shared_event) = &signal_fence.shared_event {+ raw.encodeSignalEvent_value(shared_event.as_ref(), signal_value);+ }++ // Drain caller-staged signals after our own signal so each+ // additional event value publishes once the submit completes.+ {+ let mut signals = self.shared.pending_signals.lock();+ for (event, value) in signals.drain(..) {+ raw.encodeSignalEvent_value(event.as_ref(), value);+ }+ }++ // only return an extra one if it's extra+ match command_buffers.last() {+ Some(_) => None,+ None => Some(raw),+ }+ };++ for cmd_buffer in command_buffers {+ cmd_buffer.raw.commit();+ // One command buffer per `end_encoding` call moves from the+ // "created but not yet submitted" bucket into the submitted+ // set, so update the counter.+ let previous = self+ .shared+ .command_buffer_created_not_submitted+ .fetch_sub(1, atomic::Ordering::AcqRel);+ debug_assert!(previous > 0);+ }++ if let Some(raw) = extra_command_buffer {+ raw.commit();+ }+ Ok(())+ })+ }+ unsafe fn present(+ &self,+ _surface: &Surface,+ texture: SurfaceTexture,+ ) -> Result<(), crate::SurfaceError> {+ autoreleasepool(|_| {+ // We do not bother adjusting `command_buffer_created_not_submitted`+ // because we immediately commit this buffer.+ let command_buffer = self.shared.raw.commandBuffer().unwrap();+ command_buffer.setLabel(Some(ns_string!("(wgpu internal) Present")));++ // https://developer.apple.com/documentation/quartzcore/cametallayer/1478157-presentswithtransaction?language=objc+ if !texture.present_with_transaction {+ command_buffer.presentDrawable(&texture.drawable);+ }++ command_buffer.commit();++ if texture.present_with_transaction {+ command_buffer.waitUntilScheduled();+ texture.drawable.present();+ }+ });+ Ok(())+ }++ unsafe fn get_timestamp_period(&self) -> f32 {+ self.timestamp_period+ }++ unsafe fn wait_for_idle(&self) -> Result<(), crate::DeviceError> {+ autoreleasepool(|_| {+ let command_buffer = self.shared.raw.commandBuffer().unwrap();+ command_buffer.setLabel(Some(ns_string!("(wgpu internal) wait_for_idle")));+ command_buffer.commit();+ command_buffer.waitUntilCompleted();+ });+ Ok(())+ }+}++#[derive(Debug)]+pub struct Buffer {+ raw: Retained<ProtocolObject<dyn MTLBuffer>>,+ size: wgt::BufferAddress,+}++unsafe impl Send for Buffer {}+unsafe impl Sync for Buffer {}++impl crate::DynBuffer for Buffer {}++impl Buffer {+ fn as_raw(&self) -> NonNull<ProtocolObject<dyn MTLBuffer>> {+ unsafe { NonNull::new_unchecked(Retained::as_ptr(&self.raw) as *mut _) }+ }+}++impl crate::BufferBinding<'_, Buffer> {+ fn resolve_size(&self) -> wgt::BufferAddress {+ match self.size {+ Some(size) => size.get(),+ None => self.buffer.size - self.offset,+ }+ }+}++#[derive(Debug)]+pub struct Texture {+ raw: Retained<ProtocolObject<dyn MTLTexture>>,+ format: wgt::TextureFormat,+ raw_type: MTLTextureType,+ array_layers: u32,+ mip_levels: u32,+ copy_size: crate::CopyExtent,++ // The `drop_guard` field must be the last field of this struct so it is dropped last.+ // Do not add new fields after it.+ _drop_guard: Option<crate::DropGuard>,+}++impl Texture {+ pub fn raw_handle(&self) -> &ProtocolObject<dyn MTLTexture> {+ &self.raw+ }+}++impl crate::DynTexture for Texture {}++unsafe impl Send for Texture {}+unsafe impl Sync for Texture {}++#[derive(Debug)]+pub struct TextureView {+ raw: Retained<ProtocolObject<dyn MTLTexture>>,+ aspects: crate::FormatAspects,+}++impl crate::DynTextureView for TextureView {}++unsafe impl Send for TextureView {}+unsafe impl Sync for TextureView {}++impl TextureView {+ fn as_raw(&self) -> NonNull<ProtocolObject<dyn MTLTexture>> {+ unsafe { NonNull::new_unchecked(Retained::as_ptr(&self.raw) as *mut _) }+ }+}++#[derive(Debug)]+pub struct Sampler {+ raw: Retained<ProtocolObject<dyn MTLSamplerState>>,+}++impl crate::DynSampler for Sampler {}++#[cfg(send_sync)]+static_assertions::assert_impl_all!(Sampler: Send, Sync);++impl Sampler {+ fn as_raw(&self) -> NonNull<ProtocolObject<dyn MTLSamplerState>> {+ unsafe { NonNull::new_unchecked(Retained::as_ptr(&self.raw) as *mut _) }+ }+}++#[derive(Debug)]+pub struct BindGroupLayout {+ /// Sorted list of BGL entries.+ entries: Arc<[wgt::BindGroupLayoutEntry]>,+}++impl crate::DynBindGroupLayout for BindGroupLayout {}++#[derive(Clone, Debug, Default)]+struct ResourceData<T> {+ buffers: T,+ textures: T,+ samplers: T,+}++#[derive(Clone, Debug, Default)]+struct MultiStageData<T> {+ vs: T,+ fs: T,+ cs: T,+ ts: T,+ ms: T,+}++const NAGA_STAGES: MultiStageData<naga::ShaderStage> = MultiStageData {+ vs: naga::ShaderStage::Vertex,+ fs: naga::ShaderStage::Fragment,+ cs: naga::ShaderStage::Compute,+ ts: naga::ShaderStage::Task,+ ms: naga::ShaderStage::Mesh,+};++impl<T> ops::Index<naga::ShaderStage> for MultiStageData<T> {+ type Output = T;+ fn index(&self, stage: naga::ShaderStage) -> &T {+ match stage {+ naga::ShaderStage::Vertex => &self.vs,+ naga::ShaderStage::Fragment => &self.fs,+ naga::ShaderStage::Compute => &self.cs,+ naga::ShaderStage::Task => &self.ts,+ naga::ShaderStage::Mesh => &self.ms,+ naga::ShaderStage::RayGeneration+ | naga::ShaderStage::AnyHit+ | naga::ShaderStage::ClosestHit+ | naga::ShaderStage::Miss => unimplemented!(),+ }+ }+}++impl<T> MultiStageData<T> {+ fn map_ref<Y>(&self, fun: impl Fn(&T) -> Y) -> MultiStageData<Y> {+ MultiStageData {+ vs: fun(&self.vs),+ fs: fun(&self.fs),+ cs: fun(&self.cs),+ ts: fun(&self.ts),+ ms: fun(&self.ms),+ }+ }+ fn map<Y>(self, fun: impl Fn(T) -> Y) -> MultiStageData<Y> {+ MultiStageData {+ vs: fun(self.vs),+ fs: fun(self.fs),+ cs: fun(self.cs),+ ts: fun(self.ts),+ ms: fun(self.ms),+ }+ }+ fn iter<'a>(&'a self) -> impl Iterator<Item = &'a T> {+ iter::once(&self.vs)+ .chain(iter::once(&self.fs))+ .chain(iter::once(&self.cs))+ .chain(iter::once(&self.ts))+ .chain(iter::once(&self.ms))+ }+ fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut T> {+ iter::once(&mut self.vs)+ .chain(iter::once(&mut self.fs))+ .chain(iter::once(&mut self.cs))+ .chain(iter::once(&mut self.ts))+ .chain(iter::once(&mut self.ms))+ }+}++type MultiStageResourceCounters = MultiStageData<ResourceData<ResourceIndex>>;+type MultiStageResources = MultiStageData<naga::back::msl::EntryPointResources>;++#[derive(Debug)]+struct BindGroupLayoutInfo {+ base_resource_indices: MultiStageResourceCounters,+}++#[derive(Copy, Clone, Debug, Eq, PartialEq)]+struct ImmediateDataInfo {+ count: u32,+ buffer_index: ResourceIndex,+}++#[derive(Debug)]+pub struct PipelineLayout {+ bind_group_infos: [Option<BindGroupLayoutInfo>; crate::MAX_BIND_GROUPS],+ immediates_infos: MultiStageData<Option<ImmediateDataInfo>>,+ total_immediates: u32,+ per_stage_map: MultiStageResources,+ binding_array_length_map: FastHashMap<naga::ResourceBinding, u32>,+}++impl crate::DynPipelineLayout for PipelineLayout {}++#[derive(Debug)]+enum BufferLikeResource {+ Buffer {+ ptr: NonNull<ProtocolObject<dyn MTLBuffer>>,+ offset: wgt::BufferAddress,+ dynamic_index: Option<u32>,++ /// The buffer's size, if it is a [`Storage`] binding. Otherwise `None`.+ ///+ /// Buffers with the [`wgt::BufferBindingType::Storage`] binding type can+ /// hold WGSL runtime-sized arrays. When one does, we must pass its size to+ /// shader entry points to implement bounds checks and WGSL's `arrayLength`+ /// function. See `device::CompiledShader::sized_bindings` for details.+ ///+ /// [`Storage`]: wgt::BufferBindingType::Storage+ binding_size: Option<wgt::BufferSize>,++ binding_location: u32,+ },++ /// Bindless storage `binding_array`: one argument [`MTLBuffer`] (pointer table) plus element+ /// byte sizes `(array index, size)` for `_buffer_sizes` / runtime-sized arrays.+ ///+ /// [`MTLBuffer`]: objc2_metal::MTLBuffer+ StorageBindingArray {+ ptr: NonNull<ProtocolObject<dyn MTLBuffer>>,+ array_element_sizes: Vec<(u32, wgt::BufferSize)>,+ binding_location: u32,+ },+ AccelerationStructure(NonNull<ProtocolObject<dyn MTLAccelerationStructure>>),+}++#[derive(Debug)]+struct UseResourceInfo {+ uses: MTLResourceUsage,+ stages: MTLRenderStages,+ visible_in_compute: bool,+}++impl Default for UseResourceInfo {+ fn default() -> Self {+ Self {+ uses: MTLResourceUsage::empty(),+ stages: MTLRenderStages::empty(),+ visible_in_compute: false,+ }+ }+}++#[derive(Debug, Default)]+pub struct BindGroup {+ counters: MultiStageResourceCounters,+ buffers: Vec<BufferLikeResource>,+ samplers: Vec<NonNull<ProtocolObject<dyn MTLSamplerState>>>,+ textures: Vec<NonNull<ProtocolObject<dyn MTLTexture>>>,++ argument_buffers: Vec<Retained<ProtocolObject<dyn MTLBuffer>>>,+ resources_to_use: HashMap<NonNull<ProtocolObject<dyn MTLResource>>, UseResourceInfo>,+}++impl crate::DynBindGroup for BindGroup {}++unsafe impl Send for BindGroup {}+unsafe impl Sync for BindGroup {}++#[derive(Debug)]+pub enum ShaderModuleSource {+ Naga(crate::NagaShader),+ Passthrough(PassthroughShader),+}++#[derive(Debug)]+pub struct PassthroughShader {+ pub library: Retained<ProtocolObject<dyn MTLLibrary>>,+ pub num_workgroups: HashMap<String, (u32, u32, u32)>,+}++#[cfg(send_sync)]+static_assertions::assert_impl_all!(PassthroughShader: Send, Sync);++#[derive(Debug)]+pub struct ShaderModule {+ source: ShaderModuleSource,+ runtime_checks: wgt::ShaderRuntimeChecks,+}++impl crate::DynShaderModule for ShaderModule {}++#[derive(Debug)]+struct PipelineStageInfo {+ #[allow(dead_code)]+ library: Option<Retained<ProtocolObject<dyn MTLLibrary>>>,+ immediates: Option<ImmediateDataInfo>,++ /// The buffer argument table index at which we pass runtime-sized arrays' buffer sizes.+ ///+ /// See `device::CompiledShader::sized_bindings` for more details.+ sizes_slot: Option<naga::back::msl::Slot>,++ /// Bindings of all WGSL `storage` globals that contain runtime-sized arrays.+ ///+ /// See `device::CompiledShader::sized_bindings` for more details.+ sized_bindings: Vec<(naga::ResourceBinding, u32)>,++ /// Info on all bound vertex buffers.+ vertex_buffer_mappings: Vec<naga::back::msl::VertexBufferMapping>,++ /// The workgroup size for compute, task or mesh stages+ raw_wg_size: MTLSize,++ /// The workgroup memory sizes for compute task or mesh stages+ work_group_memory_sizes: Vec<u32>,+}++// TODO(madsmtm): Derive this when a release with+// https://github.com/madsmtm/objc2/issues/804 is available (likely 0.4).+impl Default for PipelineStageInfo {+ fn default() -> Self {+ Self {+ library: Default::default(),+ immediates: Default::default(),+ sizes_slot: Default::default(),+ sized_bindings: Default::default(),+ vertex_buffer_mappings: Default::default(),+ raw_wg_size: MTLSize {+ width: 0,+ height: 0,+ depth: 0,+ },+ work_group_memory_sizes: Default::default(),+ }+ }+}++impl PipelineStageInfo {+ fn clear(&mut self) {+ self.immediates = None;+ self.sizes_slot = None;+ self.sized_bindings.clear();+ self.vertex_buffer_mappings.clear();+ self.library = None;+ self.work_group_memory_sizes.clear();+ self.raw_wg_size = MTLSize {+ width: 0,+ height: 0,+ depth: 0,+ };+ }++ fn assign_from(&mut self, other: &Self) {+ self.immediates = other.immediates;+ self.sizes_slot = other.sizes_slot;+ self.sized_bindings.clear();+ self.sized_bindings.extend_from_slice(&other.sized_bindings);+ self.vertex_buffer_mappings.clear();+ self.vertex_buffer_mappings+ .extend_from_slice(&other.vertex_buffer_mappings);+ self.library = Some(other.library.as_ref().unwrap().clone());+ self.raw_wg_size = other.raw_wg_size;+ self.work_group_memory_sizes.clear();+ self.work_group_memory_sizes+ .extend_from_slice(&other.work_group_memory_sizes);+ }+}++#[derive(Debug)]+pub struct RenderPipeline {+ raw: Retained<ProtocolObject<dyn MTLRenderPipelineState>>,+ vs_info: Option<PipelineStageInfo>,+ fs_info: Option<PipelineStageInfo>,+ ts_info: Option<PipelineStageInfo>,+ ms_info: Option<PipelineStageInfo>,+ raw_primitive_type: MTLPrimitiveType,+ raw_triangle_fill_mode: MTLTriangleFillMode,+ raw_front_winding: MTLWinding,+ raw_cull_mode: MTLCullMode,+ raw_depth_clip_mode: Option<MTLDepthClipMode>,+ depth_stencil: Option<(+ Retained<ProtocolObject<dyn MTLDepthStencilState>>,+ wgt::DepthBiasState,+ )>,+}++#[cfg(send_sync)]+static_assertions::assert_impl_all!(RenderPipeline: Send, Sync);++impl crate::DynRenderPipeline for RenderPipeline {}++#[derive(Debug)]+pub struct ComputePipeline {+ raw: Retained<ProtocolObject<dyn MTLComputePipelineState>>,+ cs_info: PipelineStageInfo,+}++#[cfg(send_sync)]+static_assertions::assert_impl_all!(ComputePipeline: Send, Sync);++impl crate::DynComputePipeline for ComputePipeline {}++#[derive(Debug)]+pub struct RayTracingPipeline {}++impl crate::DynRayTracingPipeline for RayTracingPipeline {}++#[derive(Debug, Clone)]+pub struct QuerySet {+ raw_buffer: Retained<ProtocolObject<dyn MTLBuffer>>,+ //Metal has a custom buffer for counters.+ counter_sample_buffer: Option<Retained<ProtocolObject<dyn MTLCounterSampleBuffer>>>,+ ty: wgt::QueryType,+}++impl crate::DynQuerySet for QuerySet {}++unsafe impl Send for QuerySet {}+unsafe impl Sync for QuerySet {}++#[derive(Debug)]+pub struct Fence {+ sync: Arc<(Mutex<crate::FenceValue>, Condvar)>,+ /// The pending fence values have to be ascending.+ pending_command_buffers: RwLock<Vec<PendingCommandBuffer>>,+ shared_event: Option<Retained<ProtocolObject<dyn MTLSharedEvent>>>,+}++type PendingCommandBuffer = (+ crate::FenceValue,+ Retained<ProtocolObject<dyn MTLCommandBuffer>>,+);++impl crate::DynFence for Fence {}++unsafe impl Send for Fence {}+unsafe impl Sync for Fence {}++impl Fence {+ fn get_latest(&self) -> crate::FenceValue {+ let mut max_value = *self.sync.0.lock();+ let pending_command_buffers = self.pending_command_buffers.read();+ for &(value, ref cmd_buf) in pending_command_buffers.iter() {+ match cmd_buf.status() {+ MTLCommandBufferStatus::Completed | MTLCommandBufferStatus::Error => {+ max_value = value;+ }+ _ => {}+ }+ }+ max_value+ }++ fn maintain(&self) {+ let latest = self.get_latest();+ self.pending_command_buffers+ .write()+ .retain(|&(value, _)| value > latest);+ }++ pub fn raw_shared_event(&self) -> Option<&ProtocolObject<dyn MTLSharedEvent>> {+ self.shared_event.as_deref()+ }+}++struct IndexState {+ buffer_ptr: NonNull<ProtocolObject<dyn MTLBuffer>>,+ offset: wgt::BufferAddress,+ stride: wgt::BufferAddress,+ raw_type: MTLIndexType,+}++#[derive(Default)]+struct Temp {+ binding_sizes: Vec<u32>,+}++// Any state in this struct that may be dirty after an abandoned encoding must+// be reset in `discard_encoding` for possible encoder reuse.+struct CommandState {+ blit: Option<Retained<ProtocolObject<dyn MTLBlitCommandEncoder>>>,+ acceleration_structure_builder:+ Option<Retained<ProtocolObject<dyn MTLAccelerationStructureCommandEncoder>>>,+ render: Option<Retained<ProtocolObject<dyn MTLRenderCommandEncoder>>>,+ compute: Option<Retained<ProtocolObject<dyn MTLComputeCommandEncoder>>>,+ raw_primitive_type: MTLPrimitiveType,+ index: Option<IndexState>,+ stage_infos: MultiStageData<PipelineStageInfo>,++ /// Sizes of currently bound [`wgt::BufferBindingType::Storage`] buffers.+ ///+ /// Specifically:+ ///+ /// - The keys are [`ResourceBinding`] values (that is, the WGSL `@group`+ /// and `@binding` attributes) for `var<storage>` global variables in the+ /// current module that contain runtime-sized arrays.+ ///+ /// - The values are the actual sizes of the buffers currently bound to+ /// provide those globals' contents, which are needed to implement bounds+ /// checks and the WGSL `arrayLength` function.+ ///+ /// For each stage `S` in `stage_infos`, we consult this to find the sizes+ /// of the buffers listed in `stage_infos.S.sized_bindings`, which we must+ /// pass to the entry point.+ ///+ /// See `device::CompiledShader::sized_bindings` for more details.+ ///+ /// [`ResourceBinding`]: naga::ResourceBinding+ storage_buffer_length_map: FastHashMap<(naga::ResourceBinding, u32), wgt::BufferSize>,++ vertex_buffer_size_map: FastHashMap<u32, wgt::BufferSize>,++ immediates: Vec<u32>,++ /// Timer query that should be executed when the next pass starts.+ pending_timer_queries: Vec<(QuerySet, u32)>,+}++// Any state in this struct that may be dirty after an abandoned encoding must+// be reset in `discard_encoding` for possible encoder reuse.+pub struct CommandEncoder {+ shared: Arc<AdapterShared>,+ queue_shared: Arc<QueueShared>,+ raw_cmd_buf: Option<Retained<ProtocolObject<dyn MTLCommandBuffer>>>,+ state: CommandState,+ temp: Temp,+ counters: Arc<wgt::HalCounters>,+}++impl fmt::Debug for CommandEncoder {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ f.debug_struct("CommandEncoder")+ .field("raw_cmd_buf", &self.raw_cmd_buf)+ .finish()+ }+}++unsafe impl Send for CommandEncoder {}+unsafe impl Sync for CommandEncoder {}++#[derive(Debug)]+pub struct CommandBuffer {+ raw: Retained<ProtocolObject<dyn MTLCommandBuffer>>,+ queue_shared: Arc<QueueShared>,+}++impl crate::DynCommandBuffer for CommandBuffer {}++unsafe impl Send for CommandBuffer {}+unsafe impl Sync for CommandBuffer {}++#[derive(Debug)]+pub struct PipelineCache;++impl crate::DynPipelineCache for PipelineCache {}++#[derive(Debug)]+pub struct AccelerationStructure {+ raw: Retained<ProtocolObject<dyn MTLAccelerationStructure>>,+}++impl AccelerationStructure {+ fn as_raw(&self) -> NonNull<ProtocolObject<dyn MTLAccelerationStructure>> {+ unsafe { NonNull::new_unchecked(Retained::as_ptr(&self.raw) as *mut _) }+ }+}++impl crate::DynAccelerationStructure for AccelerationStructure {}+unsafe impl Send for AccelerationStructure {}+unsafe impl Sync for AccelerationStructure {}++#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum OsType {+ Macos,+ Ios,+ Tvos,+ VisionOs,+}diff --git a/vendor/wgpu-hal/src/metal/surface.rs b/vendor/wgpu-hal/src/metal/surface.rsnew file mode 100644index 00000000..f8028a4f--- /dev/null+++ b/vendor/wgpu-hal/src/metal/surface.rs@@ -0,0 +1,409 @@+use std::sync::LazyLock;++use alloc::borrow::ToOwned as _;++use objc2::{+ available,+ rc::{autoreleasepool, Retained},+ runtime::ProtocolObject,+ ClassType, Message,+};+use objc2_core_foundation::{CFString, CGSize};+use objc2_core_graphics::CGColorSpace;+use objc2_foundation::NSObjectProtocol;+use objc2_metal::MTLTextureType;+use objc2_quartz_core::{CAMetalDrawable, CAMetalLayer};+use parking_lot::{Mutex, RwLock};++use super::OsFeatures;++/// Walks up from `start` to the `NSWindow` hosting this layer: the first ancestor+/// layer with a delegate is the backing `NSView`, and we return its `window`.+/// `None` if no ancestor has a delegate.+#[cfg(target_os = "macos")]+fn hosting_window(+ start: Retained<objc2_quartz_core::CALayer>,+) -> Option<Retained<objc2::runtime::NSObject>> {+ let mut current = Some(start);+ while let Some(layer) = current {+ if let Some(delegate) = layer.delegate() {+ return unsafe { objc2::msg_send![&*delegate, window] };+ }+ current = layer.superlayer();+ }+ None+}++impl super::Surface {+ pub fn new(layer: Retained<CAMetalLayer>) -> Self {+ Self {+ render_layer: Mutex::new(layer),+ swapchain_format: RwLock::new(None),+ extent: RwLock::new(wgt::Extent3d::default()),+ }+ }++ pub fn from_layer(layer: &CAMetalLayer) -> Self {+ assert!(layer.isKindOfClass(CAMetalLayer::class()));+ Self::new(layer.retain())+ }++ pub fn render_layer(&self) -> &Mutex<Retained<CAMetalLayer>> {+ &self.render_layer+ }++ /// Returns the EDR headroom of the screen hosting this surface, as a+ /// [`wgt::DisplayHdrInfo`].+ ///+ /// macOS only. Returns `None` on other Apple platforms (iOS, tvOS, visionOS),+ /// when the hosting screen can't be resolved (e.g. an off-screen window), or+ /// when called off the main thread: `NSScreen` and `NSWindow` are+ /// main-thread-only, and off-thread access is undefined behavior.+ ///+ /// Only `headroom` and the coarse `high_dynamic_range` bit are filled: Apple+ /// exposes a relative EDR multiplier (`1.0` == SDR white), no absolute nits,+ /// and no HDR-mode flag. `high_dynamic_range` tracks the live `current`+ /// multiplier (`> 1.0`), so it reports HDR active, not merely capable.+ pub(super) fn display_hdr_info(&self) -> Option<wgt::DisplayHdrInfo> {+ #[cfg(target_os = "macos")]+ {+ use objc2::rc::Retained;+ use objc2::runtime::NSObject;++ // Bail before any message send if we are not on the main thread.+ // `MainThreadMarker::new()` is `None` off the main thread and does no+ // Objective-C work, so it is a cheap, safe gate.+ if objc2::MainThreadMarker::new().is_none() {+ // Of the paths that yield `None`, this is the only one a caller+ // can fix (the others — off-screen window, non-macOS — are+ // environmental), so leave a breadcrumb instead of failing+ // silently. Warn once per process so a caller that polls this+ // per frame is not spammed.+ static WARN_ONCE: std::sync::Once = std::sync::Once::new();+ WARN_ONCE.call_once(|| {+ log::warn!(+ "Surface::display_hdr_info() was called from thread {:?} \+ and will return None. On the Metal backend, it must be \+ called from the main thread to succeed.",+ std::thread::current().id()+ );+ });+ return None;+ }++ // Take an owned reference to the layer and drop the lock before+ // accessing the window and screen, so a main-thread AppKit callback+ // can never deadlock against this lock.+ let render_layer = {+ let guard = self.render_layer.lock();+ guard.clone()+ };++ // Resolve the hosting `NSScreen` from the hosting window. `NSWindow.screen`+ // is nil when the window is off-screen, so this can legitimately yield `None`.+ let screen: Retained<NSObject> = autoreleasepool(|_| {+ hosting_window(Retained::into_super(render_layer))+ .and_then(|window| unsafe { objc2::msg_send![&*window, screen] })+ })?;++ // AppKit documents these EDR properties as finite multipliers+ // (`1.0` == SDR white), but guard against a non-finite read anyway so+ // the advisory values stay finite and the coarse `high_dynamic_range`+ // bit reports unknown (`None`) rather than a false `Some(false)`,+ // mirroring the `is_finite` discipline in+ // [`wgt::DisplayHdrInfo::tone_map_headroom`]. The EDR properties return+ // `CGFloat` (`f64` on 64-bit macOS).+ let finite = |v: f64| v.is_finite().then_some(v as f32);++ // `maximumExtendedDynamicRangeColorComponentValue` is macOS 10.11+, so+ // it is safe at our 10.13 minimum.+ let current: f64 = unsafe {+ objc2::msg_send![&*screen, maximumExtendedDynamicRangeColorComponentValue]+ };++ // Apple exposes no discrete HDR-mode flag, so derive the coarse+ // dynamic-range bit from the live `current` EDR multiplier (`> 1.0`),+ // not `potential` (which is `> 1.0` on nearly every Apple display and+ // would conflate "capable" with "active").+ let high_dynamic_range = current.is_finite().then_some(current > 1.0);++ // `maximumPotential...` and `maximumReference...` are macOS 10.15+,+ // below which sending them would raise an unrecognized-selector+ // exception, so only message them where available; otherwise leave+ // them `None`.+ let (potential, reference) = if available!(macos = 10.15) {+ let potential: f64 = unsafe {+ objc2::msg_send![+ &*screen,+ maximumPotentialExtendedDynamicRangeColorComponentValue+ ]+ };+ let reference: f64 = unsafe {+ objc2::msg_send![+ &*screen,+ maximumReferenceExtendedDynamicRangeColorComponentValue+ ]+ };+ // AppKit reports `0.0` when there is no reference value; treat that+ // (and any non-finite read) as "unknown" rather than a real `0.0`.+ (finite(potential), finite(reference).filter(|&v| v > 0.0))+ } else {+ (None, None)+ };+ let headroom = wgt::DisplayHeadroom {+ current: finite(current),+ potential,+ reference,+ };++ // `NSScreen.colorSpace` returns generic names on HDR panels+ // post-Monterey, so deriving a gamut bucket from it would lie; leave+ // `gamut` as `None` on macOS.+ let coarse = wgt::DisplayCoarseRange {+ high_dynamic_range,+ gamut: None,+ };++ // Apple exposes no absolute nits, CIE-xy primaries, or panel bit+ // depth, so `luminance` / `chromaticity` / `bits_per_color` stay `None`.+ let info = wgt::DisplayHdrInfo {+ luminance: None,+ headroom: Some(headroom),+ chromaticity: None,+ coarse: Some(coarse),+ bits_per_color: None,+ };+ Some(info)+ }+ #[cfg(not(target_os = "macos"))]+ {+ // TODO: iOS/tvOS/visionOS could report EDR headroom via+ // `UIScreen.currentEDRHeadroom`.+ None+ }+ }++ /// Gets the current dimensions of the `Surface`.+ ///+ /// This function is safe to call off of the main thread. However, note that+ /// `bounds` and `contentsScale` may be modified by the main thread while+ /// this function is running, possibly resulting in the two values being out+ /// of sync. This is sound, as these properties are accessed atomically.+ /// See: <https://github.com/gfx-rs/wgpu/pull/7692>+ pub(super) fn dimensions(&self) -> wgt::Extent3d {+ let (size, scale) = {+ let render_layer = self.render_layer.lock();+ let bounds = render_layer.bounds();+ let contents_scale = render_layer.contentsScale();+ (bounds.size, contents_scale)+ };++ wgt::Extent3d {+ width: (size.width * scale) as u32,+ height: (size.height * scale) as u32,+ depth_or_array_layers: 1,+ }+ }+}++// objc2 strings are not, in general, thread-safe, but these should be constants.+unsafe impl Send for ColorSpaces {}+unsafe impl Sync for ColorSpaces {}++struct ColorSpaces {+ extended_display_p3: &'static CFString,+ itur_bt2100_pq: &'static CFString,+ itur_bt2100_hlg: &'static CFString,+}++static COLOR_SPACES: LazyLock<Result<ColorSpaces, crate::SurfaceError>> = LazyLock::new(|| {+ // Stable Rust doesn't support weak linkage, so objc2 doesn't+ // offer it. To avoid link errors on old OS versions, resolve+ // these dynamically.+ let lib = unsafe {+ libloading::Library::new("/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics")+ .map_err(|_| crate::SurfaceError::Other("error loading CoreGraphics"))?+ };+ fn lookup(+ lib: &libloading::Library,+ name: &[u8],+ ) -> Result<&'static CFString, crate::SurfaceError> {+ let sym = unsafe { lib.get(name) }+ .map_err(|_| crate::SurfaceError::Other("error resolving symbol in CoreGraphics"))?;+ Ok(*sym)+ }+ let extended_display_p3 = lookup(&lib, b"kCGColorSpaceExtendedDisplayP3\0")?;+ let itur_bt2100_pq = lookup(&lib, b"kCGColorSpaceITUR_2100_PQ\0")?;+ let itur_bt2100_hlg = lookup(&lib, b"kCGColorSpaceITUR_2100_HLG\0")?;+ Ok(ColorSpaces {+ extended_display_p3,+ itur_bt2100_pq,+ itur_bt2100_hlg,+ })+});++impl crate::Surface for super::Surface {+ type A = super::Api;++ unsafe fn configure(+ &self,+ device: &super::Device,+ config: &crate::SurfaceConfiguration,+ ) -> Result<(), crate::SurfaceError> {+ log::debug!("build swapchain {config:?}");++ let caps = &device.shared.private_texture_format_caps;+ *self.swapchain_format.write() = Some(config.format);+ *self.extent.write() = config.extent;++ let render_layer = self.render_layer.lock();+ let framebuffer_only = config.usage == wgt::TextureUses::COLOR_TARGET;+ let display_sync = match config.present_mode {+ wgt::PresentMode::Fifo => true,+ wgt::PresentMode::Immediate => false,+ m => unreachable!("Unsupported present mode: {m:?}"),+ };+ // CGFloat is f64 on 64-bit, f32 on 32-bit (arm64_32/ILP32)+ let drawable_size = CGSize::new(config.extent.width as _, config.extent.height as _);++ match config.composite_alpha_mode {+ wgt::CompositeAlphaMode::Opaque => render_layer.setOpaque(true),+ wgt::CompositeAlphaMode::PostMultiplied => render_layer.setOpaque(false),+ _ => (),+ }++ let device_raw = &device.shared.device;+ render_layer.setDevice(Some(device_raw));+ render_layer.setPixelFormat(caps.map_format(config.format));+ render_layer.setFramebufferOnly(framebuffer_only);+ // Opt into Metal EDR for the HDR color spaces (more display power, memory,+ // and bandwidth). The HDR spaces are exactly those `is_hdr()` classifies.+ let wants_edr = config.color_space.is_hdr();+ if wants_edr != render_layer.wantsExtendedDynamicRangeContent() {+ render_layer.setWantsExtendedDynamicRangeContent(wants_edr);+ }++ let colorspace_name: Option<&'static CFString> = match config.color_space {+ wgt::SurfaceColorSpace::Auto => {+ unreachable!("wgpu-core resolves `Auto` before configuring the surface")+ }+ // Reset to the layer's default, which treats contents as sRGB.+ wgt::SurfaceColorSpace::Srgb => None,+ wgt::SurfaceColorSpace::ExtendedSrgbLinear => {+ Some(unsafe { objc2_core_graphics::kCGColorSpaceExtendedLinearSRGB })+ }+ wgt::SurfaceColorSpace::ExtendedSrgb => {+ Some(unsafe { objc2_core_graphics::kCGColorSpaceExtendedSRGB })+ }+ wgt::SurfaceColorSpace::ExtendedDisplayP3 => {+ // TODO: This needs protection in `surface_capabilities` like the BT.2100 cases.+ Some(+ COLOR_SPACES+ .as_ref()+ .map_err(|e| e.clone())?+ .extended_display_p3,+ )+ }+ wgt::SurfaceColorSpace::DisplayP3 => {+ Some(unsafe { objc2_core_graphics::kCGColorSpaceDisplayP3 })+ }+ wgt::SurfaceColorSpace::Bt2100Pq | wgt::SurfaceColorSpace::Bt2100Hlg => {+ // The ITUR_2100 color space constants require macOS 11.0/iOS 14.0;+ // `surface_capabilities` only reports BT.2100 PQ/HLG on those OS versions.+ if !available!(macos = 11.0, ios = 14.0, tvos = 14.0, visionos = 1.0) {+ unreachable!("BT.2100 PQ/HLG color spaces are only reported on macOS 11.0+/iOS 14.0+/tvOS 14.0+");+ }+ Some(if config.color_space == wgt::SurfaceColorSpace::Bt2100Pq {+ COLOR_SPACES.as_ref().map_err(|e| e.clone())?.itur_bt2100_pq+ } else {+ COLOR_SPACES+ .as_ref()+ .map_err(|e| e.clone())?+ .itur_bt2100_hlg+ })+ }+ };+ let colorspace = colorspace_name.and_then(|name| CGColorSpace::with_name(Some(name)));+ render_layer.setColorspace(colorspace.as_deref());++ // this gets ignored on iOS for certain OS/device combinations (iphone5s iOS 10.3)+ render_layer.setMaximumDrawableCount(config.maximum_frame_latency as usize + 1);+ render_layer.setDrawableSize(drawable_size);+ // https://developer.apple.com/documentation/quartzcore/cametallayer/allowsnextdrawabletimeout+ if available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0) {+ render_layer.setAllowsNextDrawableTimeout(false);+ }+ if OsFeatures::display_sync() {+ render_layer.setDisplaySyncEnabled(display_sync);+ }++ Ok(())+ }++ unsafe fn unconfigure(&self, _device: &super::Device) {+ *self.swapchain_format.write() = None;+ }++ unsafe fn acquire_texture(+ &self,+ _timeout: Option<core::time::Duration>, // TODO+ _fence: &super::Fence,+ ) -> Result<crate::AcquiredSurfaceTexture<super::Api>, crate::SurfaceError> {+ let render_layer = self.render_layer.lock();++ #[cfg(target_os = "macos")]+ {+ // Workaround for https://github.com/gfx-rs/wgpu/issues/8309+ // When the window is occluded on macOS, presented drawables get stuck waiting+ // for vsync. Check the window's occlusion state and skip acquisition if+ // the window is not visible - this avoids a 1-second hang in nextDrawable().+ use objc2::rc::Retained;++ // The CAMetalLayer is typically a sublayer; find the hosting window+ // and skip acquisition while it is occluded.+ if let Some(window) = hosting_window(Retained::into_super(render_layer.clone())) {+ const NS_WINDOW_OCCLUSION_STATE_VISIBLE: usize = 1 << 1;+ let occlusion_state: usize = unsafe { objc2::msg_send![&*window, occlusionState] };+ if occlusion_state & NS_WINDOW_OCCLUSION_STATE_VISIBLE == 0 {+ return Err(crate::SurfaceError::Occluded);+ }+ }+ }++ let (drawable, texture) = match autoreleasepool(|_| {+ render_layer+ .nextDrawable()+ .map(|drawable| (drawable.to_owned(), drawable.texture().to_owned()))+ }) {+ Some(pair) => pair,+ None => return Err(crate::SurfaceError::Timeout),+ };++ let swapchain_format = self.swapchain_format.read().unwrap();+ let extent = self.extent.read();+ let suf_texture = super::SurfaceTexture {+ texture: super::Texture {+ raw: texture,+ format: swapchain_format,+ raw_type: MTLTextureType::Type2D,+ array_layers: 1,+ mip_levels: 1,+ copy_size: crate::CopyExtent {+ width: extent.width,+ height: extent.height,+ depth: 1,+ },+ _drop_guard: None,+ },+ drawable: ProtocolObject::from_retained(drawable),+ present_with_transaction: render_layer.presentsWithTransaction(),+ };++ Ok(crate::AcquiredSurfaceTexture {+ texture: suf_texture,+ suboptimal: false,+ })+ }++ unsafe fn discard_texture(&self, _texture: super::SurfaceTexture) {}+}diff --git a/vendor/wgpu-hal/src/metal/time.rs b/vendor/wgpu-hal/src/metal/time.rsnew file mode 100644index 00000000..5c6bec10--- /dev/null+++ b/vendor/wgpu-hal/src/metal/time.rs@@ -0,0 +1,38 @@+//! Handling of global timestamps.++#[repr(C)]+#[derive(Debug)]+struct MachTimebaseInfo {+ numerator: u32,+ denominator: u32,+}+extern "C" {+ fn mach_timebase_info(out: *mut MachTimebaseInfo) -> u32;+ fn mach_absolute_time() -> u64;+}++/// A timer which uses mach_absolute_time to get its time. This is what the metal callbacks use.+#[derive(Debug)]+pub struct PresentationTimer {+ scale: MachTimebaseInfo,+}+impl PresentationTimer {+ /// Generates a new timer.+ pub fn new() -> Self {+ // Default to 1 / 1 in case the call to timebase_info fails.+ let mut scale = MachTimebaseInfo {+ numerator: 1,+ denominator: 1,+ };+ unsafe { mach_timebase_info(&mut scale) };++ Self { scale }+ }++ /// Gets the current time in nanoseconds.+ pub fn get_timestamp_ns(&self) -> u128 {+ let time = unsafe { mach_absolute_time() };++ (time as u128 * self.scale.numerator as u128) / self.scale.denominator as u128+ }+}diff --git a/vendor/wgpu-hal/src/noop/buffer.rs b/vendor/wgpu-hal/src/noop/buffer.rsnew file mode 100644index 00000000..9b8ab17a--- /dev/null+++ b/vendor/wgpu-hal/src/noop/buffer.rs@@ -0,0 +1,88 @@+use alloc::vec::Vec;+use core::{cell::UnsafeCell, ops::Range, ptr};++cfg_if::cfg_if! {+ if #[cfg(supports_ptr_atomics)] {+ use alloc::sync::Arc;+ } else if #[cfg(feature = "portable-atomic")] {+ use portable_atomic_util::Arc;+ }+}++#[derive(Clone, Debug)]+pub struct Buffer {+ /// This data is potentially accessed mutably in arbitrary non-overlapping slices,+ /// so we must store it in `UnsafeCell` to avoid making any too-strong no-aliasing claims.+ storage: Arc<UnsafeCell<[u8]>>,++ /// Size of the allocation.+ ///+ /// This is redundant with `storage.get().len()`, but that method is not+ /// available until our MSRV is 1.79 or greater.+ size: usize,+}++/// SAFETY:+/// This shared mutable data will not be accessed in a way which causes data races;+/// the obligation to do so is on the caller of the HAL API.+/// For safe code, `wgpu-core` validation manages appropriate access.+unsafe impl Send for Buffer {}+unsafe impl Sync for Buffer {}++impl Buffer {+ pub(super) fn new(desc: &crate::BufferDescriptor) -> Result<Self, crate::DeviceError> {+ let &crate::BufferDescriptor {+ label: _,+ size,+ usage: _,+ memory_flags: _,+ } = desc;++ let size = usize::try_from(size).map_err(|_| crate::DeviceError::OutOfMemory)?;++ let mut vector: Vec<u8> = Vec::new();+ vector+ .try_reserve_exact(size)+ .map_err(|_| crate::DeviceError::OutOfMemory)?;+ vector.resize(size, 0);+ let storage: Arc<[u8]> = Arc::from(vector);+ debug_assert_eq!(storage.len(), size);++ // SAFETY: `UnsafeCell<[u8]>` and `[u8]` have the same layout.+ // This is just adding a wrapper type without changing any layout,+ // because there is not currently a safe language/`std` way to accomplish this.+ let storage: Arc<UnsafeCell<[u8]>> =+ unsafe { Arc::from_raw(Arc::into_raw(storage) as *mut UnsafeCell<[u8]>) };++ Ok(Buffer { storage, size })+ }++ /// Returns a pointer to the memory owned by this buffer within the given `range`.+ ///+ /// This may be used to create any number of simultaneous pointers;+ /// aliasing is only a concern when actually reading, writing, or converting the pointer+ /// to a reference.+ pub(super) fn get_slice_ptr(&self, range: crate::MemoryRange) -> *mut [u8] {+ let base_ptr = self.storage.get();+ let range = range_to_usize(range, self.size);++ // We must obtain a slice pointer without ever creating a slice reference+ // that could alias with another slice.+ ptr::slice_from_raw_parts_mut(+ // SAFETY: `range_to_usize` bounds checks this addition.+ unsafe { base_ptr.cast::<u8>().add(range.start) },+ range.len(),+ )+ }+}++/// Convert a [`crate::MemoryRange`] to `Range<usize>` and bounds check it.+fn range_to_usize(range: crate::MemoryRange, upper_bound: usize) -> Range<usize> {+ // Note: these assertions should be impossible to trigger from safe code.+ // We're doing them anyway since this entire backend is for testing+ // (except for when it is an unused placeholder)+ let start = usize::try_from(range.start).expect("range too large");+ let end = usize::try_from(range.end).expect("range too large");+ assert!(start <= end && end <= upper_bound, "range out of bounds");+ start..end+}diff --git a/vendor/wgpu-hal/src/noop/command.rs b/vendor/wgpu-hal/src/noop/command.rsnew file mode 100644index 00000000..16cca58d--- /dev/null+++ b/vendor/wgpu-hal/src/noop/command.rs@@ -0,0 +1,356 @@+use alloc::vec::Vec;+use core::mem;+use core::ops::Range;++use super::{Api, Buffer, DeviceResult, Resource};++/// Command buffer type, which performs double duty as the command encoder type too.+#[derive(Debug)]+pub struct CommandBuffer {+ commands: Vec<Command>,+}++#[derive(Debug)]+enum Command {+ ClearBuffer {+ buffer: Buffer,+ range: crate::MemoryRange,+ },+ CopyBufferToBuffer {+ src: Buffer,+ dst: Buffer,+ regions: Vec<crate::BufferCopy>,+ },+}++impl CommandBuffer {+ /// # Safety+ ///+ /// Must be called with appropriate synchronization for the resources affected by the command,+ /// such as ensuring that buffers are not accessed by a command while aliasing references exist.+ pub(crate) unsafe fn execute(&self) {+ for command in &self.commands {+ unsafe { command.execute() };+ }+ }++ pub(crate) fn new() -> Self {+ Self {+ commands: Vec::new(),+ }+ }+}++impl crate::CommandEncoder for CommandBuffer {+ type A = Api;++ unsafe fn begin_encoding(&mut self, label: crate::Label) -> DeviceResult<()> {+ assert!(self.commands.is_empty());+ Ok(())+ }+ unsafe fn discard_encoding(&mut self) {+ self.commands.clear();+ }+ unsafe fn end_encoding(&mut self) -> DeviceResult<CommandBuffer> {+ Ok(CommandBuffer {+ commands: mem::take(&mut self.commands),+ })+ }+ unsafe fn reset_all<I>(&mut self, command_buffers: I) {}++ unsafe fn transition_buffers<'a, T>(&mut self, barriers: T)+ where+ T: Iterator<Item = crate::BufferBarrier<'a, Buffer>>,+ {+ }++ unsafe fn transition_textures<'a, T>(&mut self, barriers: T)+ where+ T: Iterator<Item = crate::TextureBarrier<'a, Resource>>,+ {+ }++ unsafe fn clear_buffer(&mut self, buffer: &Buffer, range: crate::MemoryRange) {+ self.commands.push(Command::ClearBuffer {+ buffer: buffer.clone(),+ range,+ })+ }++ unsafe fn copy_buffer_to_buffer<T>(&mut self, src: &Buffer, dst: &Buffer, regions: T)+ where+ T: Iterator<Item = crate::BufferCopy>,+ {+ self.commands.push(Command::CopyBufferToBuffer {+ src: src.clone(),+ dst: dst.clone(),+ regions: regions.collect(),+ });+ }++ #[cfg(webgl)]+ unsafe fn copy_external_image_to_texture<T>(+ &mut self,+ src: &wgt::CopyExternalImageSourceInfo,+ dst: &Resource,+ dst_premultiplication: bool,+ regions: T,+ ) where+ T: Iterator<Item = crate::TextureCopy>,+ {+ }++ unsafe fn copy_texture_to_texture<T>(+ &mut self,+ src: &Resource,+ src_usage: wgt::TextureUses,+ dst: &Resource,+ regions: T,+ ) {+ // TODO: consider implementing this and other texture manipulation+ }++ unsafe fn copy_buffer_to_texture<T>(&mut self, src: &Buffer, dst: &Resource, regions: T) {+ // TODO: consider implementing this and other texture manipulation+ }++ unsafe fn copy_texture_to_buffer<T>(+ &mut self,+ src: &Resource,+ src_usage: wgt::TextureUses,+ dst: &Buffer,+ regions: T,+ ) {+ // TODO: consider implementing this and other texture manipulation+ }++ unsafe fn begin_query(&mut self, set: &Resource, index: u32) {}+ unsafe fn end_query(&mut self, set: &Resource, index: u32) {}+ unsafe fn write_timestamp(&mut self, set: &Resource, index: u32) {}+ unsafe fn read_acceleration_structure_compact_size(+ &mut self,+ acceleration_structure: &Resource,+ buf: &Buffer,+ ) {+ }+ unsafe fn reset_queries(&mut self, set: &Resource, range: Range<u32>) {}+ unsafe fn copy_query_results(+ &mut self,+ set: &Resource,+ range: Range<u32>,+ buffer: &Buffer,+ offset: wgt::BufferAddress,+ stride: wgt::BufferSize,+ ) {+ }++ // render++ unsafe fn begin_render_pass(+ &mut self,+ desc: &crate::RenderPassDescriptor<Resource, Resource>,+ ) -> DeviceResult<()> {+ Ok(())+ }+ unsafe fn end_render_pass(&mut self) {}++ unsafe fn set_bind_group(+ &mut self,+ layout: &Resource,+ index: u32,+ group: &Resource,+ dynamic_offsets: &[wgt::DynamicOffset],+ ) {+ }+ unsafe fn set_immediates(&mut self, layout: &Resource, offset_bytes: u32, data: &[u32]) {}++ unsafe fn insert_debug_marker(&mut self, label: &str) {}+ unsafe fn begin_debug_marker(&mut self, group_label: &str) {}+ unsafe fn end_debug_marker(&mut self) {}++ unsafe fn set_render_pipeline(&mut self, pipeline: &Resource) {}++ unsafe fn set_index_buffer<'a>(+ &mut self,+ binding: crate::BufferBinding<'a, Buffer>,+ format: wgt::IndexFormat,+ ) {+ }+ unsafe fn set_vertex_buffer<'a>(+ &mut self,+ index: u32,+ binding: crate::BufferBinding<'a, Buffer>,+ ) {+ }+ unsafe fn set_viewport(&mut self, rect: &crate::Rect<f32>, depth_range: Range<f32>) {}+ unsafe fn set_scissor_rect(&mut self, rect: &crate::Rect<u32>) {}+ unsafe fn set_stencil_reference(&mut self, value: u32) {}+ unsafe fn set_blend_constants(&mut self, color: &[f32; 4]) {}++ unsafe fn draw(+ &mut self,+ first_vertex: u32,+ vertex_count: u32,+ first_instance: u32,+ instance_count: u32,+ ) {+ }+ unsafe fn draw_indexed(+ &mut self,+ first_index: u32,+ index_count: u32,+ base_vertex: i32,+ first_instance: u32,+ instance_count: u32,+ ) {+ }+ unsafe fn draw_mesh_tasks(+ &mut self,+ group_count_x: u32,+ group_count_y: u32,+ group_count_z: u32,+ ) {+ }+ unsafe fn draw_indirect(+ &mut self,+ buffer: &Buffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ ) {+ }+ unsafe fn draw_indexed_indirect(+ &mut self,+ buffer: &Buffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ ) {+ }+ unsafe fn draw_mesh_tasks_indirect(+ &mut self,+ buffer: &<Self::A as crate::Api>::Buffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ ) {+ }+ unsafe fn draw_indirect_count(+ &mut self,+ buffer: &Buffer,+ offset: wgt::BufferAddress,+ count_buffer: &Buffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ ) {+ }+ unsafe fn draw_indexed_indirect_count(+ &mut self,+ buffer: &Buffer,+ offset: wgt::BufferAddress,+ count_buffer: &Buffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ ) {+ }+ unsafe fn draw_mesh_tasks_indirect_count(+ &mut self,+ buffer: &<Self::A as crate::Api>::Buffer,+ offset: wgt::BufferAddress,+ count_buffer: &<Self::A as crate::Api>::Buffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ ) {+ }++ // compute++ unsafe fn begin_compute_pass(&mut self, desc: &crate::ComputePassDescriptor<Resource>) {}+ unsafe fn end_compute_pass(&mut self) {}++ unsafe fn set_compute_pipeline(&mut self, pipeline: &Resource) {}++ unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]) {}+ unsafe fn dispatch_workgroups_indirect(&mut self, buffer: &Buffer, offset: wgt::BufferAddress) {+ }++ unsafe fn build_acceleration_structures<'a, T>(+ &mut self,+ _descriptor_count: u32,+ descriptors: T,+ ) where+ Api: 'a,+ T: IntoIterator<Item = crate::BuildAccelerationStructureDescriptor<'a, Buffer, Resource>>,+ {+ }++ unsafe fn place_acceleration_structure_barrier(+ &mut self,+ _barriers: crate::AccelerationStructureBarrier,+ ) {+ }++ unsafe fn copy_acceleration_structure_to_acceleration_structure(+ &mut self,+ src: &Resource,+ dst: &Resource,+ copy: wgt::AccelerationStructureCopy,+ ) {+ }++ unsafe fn set_acceleration_structure_dependencies(+ command_buffers: &[&CommandBuffer],+ dependencies: &[&Resource],+ ) {+ }++ unsafe fn begin_ray_tracing_pass(&mut self, _desc: &crate::RayTracingPassDescriptor) {}++ unsafe fn end_ray_tracing_pass(&mut self) {}++ unsafe fn set_ray_tracing_pipeline(+ &mut self,+ _pipeline: &<Self::A as crate::Api>::RayTracingPipeline,+ ) {+ }++ unsafe fn trace_rays(+ &mut self,+ _count: [u32; 3],+ _ray_generation_group_data: crate::PipelineGroupData<Buffer>,+ _miss_group_data: crate::PipelineGroupData<Buffer>,+ _intersection_group_data: crate::PipelineGroupData<Buffer>,+ ) {+ }+}++impl Command {+ /// # Safety+ ///+ /// Must be called with appropriate synchronization for the resources affected by the command,+ /// such as ensuring that buffers are not accessed by a command while aliasing references exist.+ unsafe fn execute(&self) {+ match self {+ Command::ClearBuffer { ref buffer, range } => {+ // SAFETY:+ // Caller is responsible for ensuring this does not alias.+ let buffer_slice: &mut [u8] = unsafe { &mut *buffer.get_slice_ptr(range.clone()) };+ buffer_slice.fill(0);+ }++ Command::CopyBufferToBuffer { src, dst, regions } => {+ for &crate::BufferCopy {+ src_offset,+ dst_offset,+ size,+ } in regions+ {+ // SAFETY:+ // Caller is responsible for ensuring this does not alias.+ let src_region: &[u8] =+ unsafe { &*src.get_slice_ptr(src_offset..src_offset + size.get()) };+ let dst_region: &mut [u8] =+ unsafe { &mut *dst.get_slice_ptr(dst_offset..dst_offset + size.get()) };+ dst_region.copy_from_slice(src_region);+ }+ }+ }+ }+}diff --git a/vendor/wgpu-hal/src/noop/mod.rs b/vendor/wgpu-hal/src/noop/mod.rsnew file mode 100644index 00000000..b2e397d3--- /dev/null+++ b/vendor/wgpu-hal/src/noop/mod.rs@@ -0,0 +1,487 @@+#![allow(unused_variables)]++use alloc::{string::String, sync::Arc, vec, vec::Vec};+use core::{ptr, sync::atomic::Ordering, time::Duration};++#[cfg(supports_64bit_atomics)]+use core::sync::atomic::AtomicU64;+#[cfg(not(supports_64bit_atomics))]+use portable_atomic::AtomicU64;++use crate::TlasInstance;++mod buffer;+pub use buffer::Buffer;+mod command;+pub use command::CommandBuffer;++#[derive(Clone, Debug)]+pub struct Api;++#[derive(Debug)]+pub struct Context {+ options: Arc<wgt::NoopBackendOptions>,+}+#[derive(Debug)]+pub struct Encoder;+#[derive(Debug)]+pub struct Resource;++#[derive(Debug)]+pub struct Fence {+ value: AtomicU64,+}++type DeviceResult<T> = Result<T, crate::DeviceError>;++impl crate::Api for Api {+ const VARIANT: wgt::Backend = wgt::Backend::Noop;++ type Instance = Context;+ type Surface = Context;+ type Adapter = Context;+ type Device = Context;++ type Queue = Context;+ type CommandEncoder = CommandBuffer;+ type CommandBuffer = CommandBuffer;++ type Buffer = Buffer;+ type Texture = Resource;+ type SurfaceTexture = Resource;+ type TextureView = Resource;+ type Sampler = Resource;+ type QuerySet = Resource;+ type Fence = Fence;+ type AccelerationStructure = Resource;+ type PipelineCache = Resource;++ type BindGroupLayout = Resource;+ type BindGroup = Resource;+ type PipelineLayout = Resource;+ type ShaderModule = Resource;+ type RenderPipeline = Resource;+ type RayTracingPipeline = Resource;+ type ComputePipeline = Resource;+}++crate::impl_dyn_resource!(Buffer, CommandBuffer, Context, Fence, Resource);++impl crate::DynAccelerationStructure for Resource {}+impl crate::DynBindGroup for Resource {}+impl crate::DynBindGroupLayout for Resource {}+impl crate::DynBuffer for Buffer {}+impl crate::DynCommandBuffer for CommandBuffer {}+impl crate::DynComputePipeline for Resource {}+impl crate::DynFence for Fence {}+impl crate::DynPipelineCache for Resource {}+impl crate::DynPipelineLayout for Resource {}+impl crate::DynQuerySet for Resource {}+impl crate::DynRenderPipeline for Resource {}+impl crate::DynRayTracingPipeline for Resource {}+impl crate::DynSampler for Resource {}+impl crate::DynShaderModule for Resource {}+impl crate::DynSurfaceTexture for Resource {}+impl crate::DynTexture for Resource {}+impl crate::DynTextureView for Resource {}++impl core::borrow::Borrow<dyn crate::DynTexture> for Resource {+ fn borrow(&self) -> &dyn crate::DynTexture {+ self+ }+}++impl crate::Instance for Context {+ type A = Api;++ unsafe fn init(desc: &crate::InstanceDescriptor<'_>) -> Result<Self, crate::InstanceError> {+ let options = Arc::new(desc.backend_options.noop.clone());+ if options.enable {+ Ok(Context { options })+ } else {+ Err(crate::InstanceError::new(String::from(+ "noop backend disabled because NoopBackendOptions::enable is false",+ )))+ }+ }+ unsafe fn create_surface(+ &self,+ _display_handle: raw_window_handle::RawDisplayHandle,+ _window_handle: raw_window_handle::RawWindowHandle,+ ) -> Result<Context, crate::InstanceError> {+ Ok(Context {+ options: Arc::clone(&self.options),+ })+ }+ unsafe fn enumerate_adapters(+ &self,+ _surface_hint: Option<&Context>,+ ) -> Vec<crate::ExposedAdapter<Api>> {+ let device_type = self.options.device_type.unwrap_or(wgt::DeviceType::Other);+ let subgroup_min_size = self+ .options+ .subgroup_min_size+ .unwrap_or(wgt::MINIMUM_SUBGROUP_MIN_SIZE);+ let subgroup_max_size = self+ .options+ .subgroup_max_size+ .unwrap_or(wgt::MAXIMUM_SUBGROUP_MAX_SIZE);+ let features = self.options.features.unwrap_or(wgt::Features::all());+ let limits = self+ .options+ .limits+ .clone()+ .unwrap_or(CAPABILITIES.limits.clone());++ let info = wgt::AdapterInfo {+ subgroup_min_size,+ subgroup_max_size,+ ..adapter_info()+ };++ let capabilities = crate::Capabilities {+ limits,+ ..CAPABILITIES+ };++ vec![crate::ExposedAdapter {+ adapter: Context {+ options: Arc::clone(&self.options),+ },+ info,+ features,+ capabilities,+ }]+ }+}++/// Returns the adapter info for the noop backend.+///+/// This is used in the test harness to construct info about+/// the noop backend adapter without actually initializing wgpu.+pub fn adapter_info() -> wgt::AdapterInfo {+ wgt::AdapterInfo {+ name: String::from("noop wgpu backend"),+ driver: String::from("wgpu"),+ ..wgt::AdapterInfo::new(wgt::DeviceType::Cpu, wgt::Backend::Noop)+ }+}++/// The capabilities of the noop backend.+///+/// This is used in the test harness to construct capabilities+/// of the noop backend without actually initializing wgpu.+pub const CAPABILITIES: crate::Capabilities = {+ crate::Capabilities {+ limits: wgt::Limits::unlimited(),+ alignments: crate::Alignments {+ // All maximally permissive+ buffer_copy_offset: wgt::BufferSize::MIN,+ buffer_copy_pitch: wgt::BufferSize::MIN,+ uniform_bounds_check_alignment: wgt::BufferSize::MIN,+ raw_tlas_instance_size: 0,+ ray_tracing_scratch_buffer_alignment: 1,+ ray_tracing_pipeline_group_data_size: 1,+ ray_tracing_pipeline_group_data_alignment: 1,+ ray_tracing_pipeline_data_offset_alignment: 1,+ },+ downlevel: wgt::DownlevelCapabilities {+ flags: wgt::DownlevelFlags::all(),+ limits: wgt::DownlevelLimits {},+ shader_model: wgt::ShaderModel::Sm5,+ },+ cooperative_matrix_properties: Vec::new(),+ }+};++impl crate::Surface for Context {+ type A = Api;++ unsafe fn configure(+ &self,+ device: &Context,+ config: &crate::SurfaceConfiguration,+ ) -> Result<(), crate::SurfaceError> {+ Ok(())+ }++ unsafe fn unconfigure(&self, device: &Context) {}++ unsafe fn acquire_texture(+ &self,+ _timeout: Option<Duration>,+ _fence: &Fence,+ ) -> Result<crate::AcquiredSurfaceTexture<Api>, crate::SurfaceError> {+ Err(crate::SurfaceError::Timeout)+ }+ unsafe fn discard_texture(&self, texture: Resource) {}+}++impl crate::Adapter for Context {+ type A = Api;++ unsafe fn open(+ &self,+ features: wgt::Features,+ _limits: &wgt::Limits,+ _memory_hints: &wgt::MemoryHints,+ ) -> DeviceResult<crate::OpenDevice<Api>> {+ Ok(crate::OpenDevice {+ device: Context {+ options: Arc::clone(&self.options),+ },+ queue: Context {+ options: Arc::clone(&self.options),+ },+ })+ }+ unsafe fn texture_format_capabilities(+ &self,+ format: wgt::TextureFormat,+ ) -> crate::TextureFormatCapabilities {+ crate::TextureFormatCapabilities::empty()+ }++ unsafe fn surface_capabilities(&self, surface: &Context) -> Option<crate::SurfaceCapabilities> {+ None+ }++ unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {+ wgt::PresentationTimestamp::INVALID_TIMESTAMP+ }++ fn get_ordered_buffer_usages(&self) -> wgt::BufferUses {+ wgt::BufferUses::INCLUSIVE | wgt::BufferUses::MAP_WRITE+ }++ fn get_ordered_texture_usages(&self) -> wgt::TextureUses {+ wgt::TextureUses::INCLUSIVE+ | wgt::TextureUses::COLOR_TARGET+ | wgt::TextureUses::DEPTH_STENCIL_WRITE+ }+}++impl crate::Queue for Context {+ type A = Api;++ unsafe fn submit(+ &self,+ command_buffers: &[&CommandBuffer],+ surface_textures: &[&Resource],+ (fence, fence_value): (&Fence, crate::FenceValue),+ ) -> DeviceResult<()> {+ // All commands are executed synchronously.+ for cb in command_buffers {+ // SAFETY: Caller is responsible for ensuring synchronization between commands and+ // other mutations.+ unsafe {+ cb.execute();+ }+ }+ fence.value.store(fence_value, Ordering::Release);+ Ok(())+ }+ unsafe fn present(+ &self,+ surface: &Context,+ texture: Resource,+ ) -> Result<(), crate::SurfaceError> {+ Ok(())+ }++ unsafe fn get_timestamp_period(&self) -> f32 {+ 1.0+ }++ unsafe fn wait_for_idle(&self) -> Result<(), crate::DeviceError> {+ Ok(())+ }+}++impl crate::Device for Context {+ type A = Api;++ unsafe fn create_buffer(&self, desc: &crate::BufferDescriptor) -> DeviceResult<Buffer> {+ Buffer::new(desc)+ }++ unsafe fn destroy_buffer(&self, buffer: Buffer) {}+ unsafe fn add_raw_buffer(&self, _buffer: &Buffer) {}++ unsafe fn map_buffer(+ &self,+ buffer: &Buffer,+ range: crate::MemoryRange,+ ) -> DeviceResult<crate::BufferMapping> {+ // Safety: the `wgpu-core` validation layer will prevent any user-accessible aliasing+ // mappings from being created, so we don’t need to perform any checks here, except for+ // bounds checks on the range which are built into `get_slice_ptr()`.+ Ok(crate::BufferMapping {+ ptr: ptr::NonNull::new(buffer.get_slice_ptr(range).cast::<u8>()).unwrap(),+ is_coherent: true,+ })+ }+ unsafe fn unmap_buffer(&self, buffer: &Buffer) {}+ unsafe fn flush_mapped_ranges<I>(&self, buffer: &Buffer, ranges: I) {}+ unsafe fn invalidate_mapped_ranges<I>(&self, buffer: &Buffer, ranges: I) {}++ unsafe fn create_texture(&self, desc: &crate::TextureDescriptor) -> DeviceResult<Resource> {+ Ok(Resource)+ }+ unsafe fn destroy_texture(&self, texture: Resource) {}+ unsafe fn add_raw_texture(&self, _texture: &Resource) {}++ unsafe fn create_texture_view(+ &self,+ texture: &Resource,+ desc: &crate::TextureViewDescriptor,+ ) -> DeviceResult<Resource> {+ Ok(Resource)+ }+ unsafe fn destroy_texture_view(&self, view: Resource) {}+ unsafe fn create_sampler(&self, desc: &crate::SamplerDescriptor) -> DeviceResult<Resource> {+ Ok(Resource)+ }+ unsafe fn destroy_sampler(&self, sampler: Resource) {}++ unsafe fn create_command_encoder(+ &self,+ desc: &crate::CommandEncoderDescriptor<Context>,+ ) -> DeviceResult<CommandBuffer> {+ Ok(CommandBuffer::new())+ }++ unsafe fn create_bind_group_layout(+ &self,+ desc: &crate::BindGroupLayoutDescriptor,+ ) -> DeviceResult<Resource> {+ Ok(Resource)+ }+ unsafe fn destroy_bind_group_layout(&self, bg_layout: Resource) {}+ unsafe fn create_pipeline_layout(+ &self,+ desc: &crate::PipelineLayoutDescriptor<Resource>,+ ) -> DeviceResult<Resource> {+ Ok(Resource)+ }+ unsafe fn destroy_pipeline_layout(&self, pipeline_layout: Resource) {}+ unsafe fn create_bind_group(+ &self,+ desc: &crate::BindGroupDescriptor<Resource, Buffer, Resource, Resource, Resource>,+ ) -> DeviceResult<Resource> {+ Ok(Resource)+ }+ unsafe fn destroy_bind_group(&self, group: Resource) {}++ unsafe fn create_shader_module(+ &self,+ desc: &crate::ShaderModuleDescriptor,+ shader: crate::ShaderInput,+ ) -> Result<Resource, crate::ShaderError> {+ Ok(Resource)+ }+ unsafe fn destroy_shader_module(&self, module: Resource) {}+ unsafe fn create_render_pipeline(+ &self,+ desc: &crate::RenderPipelineDescriptor<Resource, Resource, Resource>,+ ) -> Result<Resource, crate::PipelineError> {+ Ok(Resource)+ }+ unsafe fn destroy_render_pipeline(&self, pipeline: Resource) {}+ unsafe fn create_compute_pipeline(+ &self,+ desc: &crate::ComputePipelineDescriptor<Resource, Resource, Resource>,+ ) -> Result<Resource, crate::PipelineError> {+ Ok(Resource)+ }+ unsafe fn destroy_compute_pipeline(&self, pipeline: Resource) {}+ unsafe fn create_ray_tracing_pipeline(+ &self,+ desc: &crate::RayTracingPipelineDescriptor<Resource, Resource, Resource>,+ ) -> Result<Resource, crate::PipelineError> {+ Ok(Resource)+ }+ unsafe fn destroy_ray_tracing_pipeline(&self, pipeline: Resource) {}+ unsafe fn get_raytracing_pipeline_group_data(+ &self,+ pipeline: &Resource,+ groups: core::ops::Range<u32>,+ ) -> Result<Vec<u8>, crate::DeviceError> {+ Ok(vec![0; groups.count()])+ }+ unsafe fn create_pipeline_cache(+ &self,+ desc: &crate::PipelineCacheDescriptor<'_>,+ ) -> Result<Resource, crate::PipelineCacheError> {+ Ok(Resource)+ }+ unsafe fn destroy_pipeline_cache(&self, cache: Resource) {}++ unsafe fn create_query_set(+ &self,+ desc: &wgt::QuerySetDescriptor<crate::Label>,+ ) -> DeviceResult<Resource> {+ Ok(Resource)+ }+ unsafe fn destroy_query_set(&self, set: Resource) {}+ unsafe fn create_fence(&self) -> DeviceResult<Fence> {+ Ok(Fence {+ value: AtomicU64::new(0),+ })+ }+ unsafe fn destroy_fence(&self, fence: Fence) {}+ unsafe fn get_fence_value(&self, fence: &Fence) -> DeviceResult<crate::FenceValue> {+ Ok(fence.value.load(Ordering::Acquire))+ }+ unsafe fn wait(+ &self,+ fence: &Fence,+ value: crate::FenceValue,+ timeout: Option<Duration>,+ ) -> DeviceResult<bool> {+ // The relevant commands must have already been submitted, and noop-backend commands are+ // executed synchronously, so there is no waiting — either it is already done,+ // or this method was called incorrectly.+ assert!(+ fence.value.load(Ordering::Acquire) >= value,+ "submission must have already been done"+ );+ Ok(true)+ }++ unsafe fn start_graphics_debugger_capture(&self) -> bool {+ false+ }+ unsafe fn stop_graphics_debugger_capture(&self) {}+ unsafe fn create_acceleration_structure(+ &self,+ desc: &crate::AccelerationStructureDescriptor,+ ) -> DeviceResult<Resource> {+ Ok(Resource)+ }+ unsafe fn get_acceleration_structure_build_sizes<'a>(+ &self,+ _desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, Buffer>,+ ) -> crate::AccelerationStructureBuildSizes {+ Default::default()+ }+ unsafe fn get_acceleration_structure_device_address(+ &self,+ _acceleration_structure: &Resource,+ ) -> wgt::BufferAddress {+ Default::default()+ }+ unsafe fn destroy_acceleration_structure(&self, _acceleration_structure: Resource) {}++ fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8> {+ vec![]+ }++ fn get_internal_counters(&self) -> wgt::HalCounters {+ Default::default()+ }++ fn check_if_oom(&self) -> DeviceResult<()> {+ Ok(())+ }+}diff --git a/vendor/wgpu-hal/src/validation_canary.rs b/vendor/wgpu-hal/src/validation_canary.rsnew file mode 100644index 00000000..bd23db33--- /dev/null+++ b/vendor/wgpu-hal/src/validation_canary.rs@@ -0,0 +1,40 @@+use alloc::{string::String, vec::Vec};++use parking_lot::Mutex;++/// Stores the text of any validation errors that have occurred since+/// the last call to `get_and_reset`.+///+/// Each value is a validation error and a message associated with it,+/// or `None` if the error has no message from the api.+///+/// This is used for internal wgpu testing only and _must not_ be used+/// as a way to check for errors.+///+/// This works as a static because `cargo nextest` runs all of our+/// tests in separate processes, so each test gets its own canary.+///+/// This prevents the issue of one validation error terminating the+/// entire process.+pub static VALIDATION_CANARY: ValidationCanary = ValidationCanary {+ inner: Mutex::new(Vec::new()),+};++/// Flag for internal testing.+#[derive(Debug)]+pub struct ValidationCanary {+ inner: Mutex<Vec<String>>,+}++impl ValidationCanary {+ #[allow(dead_code, reason = "in some configurations this function is dead")]+ pub(crate) fn add(&self, msg: String) {+ self.inner.lock().push(msg);+ }++ /// Returns any API validation errors that have occurred in this process+ /// since the last call to this function.+ pub fn get_and_reset(&self) -> Vec<String> {+ self.inner.lock().drain(..).collect()+ }+}diff --git a/vendor/wgpu-hal/src/vulkan/adapter.rs b/vendor/wgpu-hal/src/vulkan/adapter.rsnew file mode 100644index 00000000..196daf4c--- /dev/null+++ b/vendor/wgpu-hal/src/vulkan/adapter.rs@@ -0,0 +1,3551 @@+use alloc::{borrow::ToOwned as _, boxed::Box, collections::BTreeMap, sync::Arc, vec::Vec};+use core::{ffi::CStr, marker::PhantomData};++use ash::{ext, google, khr, vk};+use parking_lot::Mutex;++use crate::{vulkan::semaphore_list::SemaphoreList, AllocationSizes};++use super::semaphore_list::SemaphoreListMode;++fn depth_stencil_required_flags() -> vk::FormatFeatureFlags {+ vk::FormatFeatureFlags::SAMPLED_IMAGE | vk::FormatFeatureFlags::DEPTH_STENCIL_ATTACHMENT+}++const INDEXING_FEATURES: wgt::Features = wgt::Features::TEXTURE_BINDING_ARRAY+ .union(wgt::Features::BUFFER_BINDING_ARRAY)+ .union(wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY)+ .union(wgt::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING)+ .union(wgt::Features::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING)+ .union(wgt::Features::UNIFORM_BUFFER_BINDING_ARRAYS)+ .union(wgt::Features::PARTIALLY_BOUND_BINDING_ARRAY);+#[expect(rustdoc::private_intra_doc_links)]+/// Features supported by a [`vk::PhysicalDevice`] and its extensions.+///+/// This is used in two phases:+///+/// - When enumerating adapters, this represents the features offered by the+/// adapter. [`Instance::expose_adapter`] calls `vkGetPhysicalDeviceFeatures2`+/// (or `vkGetPhysicalDeviceFeatures` if that is not available) to collect+/// this information about the `VkPhysicalDevice` represented by the+/// `wgpu_hal::ExposedAdapter`.+///+/// - When opening a device, this represents the features we would like to+/// enable. At `wgpu_hal::Device` construction time,+/// [`PhysicalDeviceFeatures::from_extensions_and_requested_features`]+/// constructs an value of this type indicating which Vulkan features to+/// enable, based on the `wgpu_types::Features` requested.+///+/// [`Instance::expose_adapter`]: super::Instance::expose_adapter+#[derive(Debug, Default)]+pub struct PhysicalDeviceFeatures {+ /// Basic Vulkan 1.0 features.+ core: vk::PhysicalDeviceFeatures,++ /// Features provided by `VK_EXT_descriptor_indexing`, promoted to Vulkan 1.2.+ pub(super) descriptor_indexing:+ Option<vk::PhysicalDeviceDescriptorIndexingFeaturesEXT<'static>>,++ /// Features provided by `VK_KHR_timeline_semaphore`, promoted to Vulkan 1.2+ timeline_semaphore: Option<vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR<'static>>,++ /// Features provided by `VK_EXT_image_robustness`, promoted to Vulkan 1.3+ image_robustness: Option<vk::PhysicalDeviceImageRobustnessFeaturesEXT<'static>>,++ /// Features provided by `VK_EXT_robustness2`.+ robustness2: Option<vk::PhysicalDeviceRobustness2FeaturesEXT<'static>>,++ /// Features provided by `VK_KHR_multiview`, promoted to Vulkan 1.1.+ multiview: Option<vk::PhysicalDeviceMultiviewFeaturesKHR<'static>>,++ /// Features provided by `VK_KHR_sampler_ycbcr_conversion`, promoted to Vulkan 1.1.+ sampler_ycbcr_conversion: Option<vk::PhysicalDeviceSamplerYcbcrConversionFeatures<'static>>,++ /// Features provided by `VK_EXT_texture_compression_astc_hdr`, promoted to Vulkan 1.3.+ astc_hdr: Option<vk::PhysicalDeviceTextureCompressionASTCHDRFeaturesEXT<'static>>,++ /// Features provided by `VK_KHR_shader_float16_int8`, promoted to Vulkan 1.2+ shader_float16_int8: Option<vk::PhysicalDeviceShaderFloat16Int8Features<'static>>,++ /// Features provided by `VK_KHR_16bit_storage`, promoted to Vulkan 1.1+ _16bit_storage: Option<vk::PhysicalDevice16BitStorageFeatures<'static>>,++ /// Features provided by `VK_KHR_acceleration_structure`.+ acceleration_structure: Option<vk::PhysicalDeviceAccelerationStructureFeaturesKHR<'static>>,++ /// Features provided by `VK_KHR_buffer_device_address`, promoted to Vulkan 1.2.+ ///+ /// We only use this feature for+ /// [`Features::EXPERIMENTAL_RAY_QUERY`], which requires+ /// `VK_KHR_acceleration_structure`, which depends on+ /// `VK_KHR_buffer_device_address`, so [`Instance::expose_adapter`] only+ /// bothers to check if `VK_KHR_acceleration_structure` is available,+ /// leaving this `None`.+ ///+ /// However, we do populate this when creating a device if+ /// [`Features::EXPERIMENTAL_RAY_QUERY`] is requested.+ ///+ /// [`Instance::expose_adapter`]: super::Instance::expose_adapter+ /// [`Features::EXPERIMENTAL_RAY_QUERY`]: wgt::Features::EXPERIMENTAL_RAY_QUERY+ buffer_device_address: Option<vk::PhysicalDeviceBufferDeviceAddressFeaturesKHR<'static>>,++ /// Features provided by `VK_KHR_ray_query`,+ ///+ /// Vulkan requires that the feature be present if the `VK_KHR_ray_query`+ /// extension is present, so [`Instance::expose_adapter`] doesn't bother retrieving+ /// this from `vkGetPhysicalDeviceFeatures2`.+ ///+ /// However, we do populate this when creating a device if ray tracing is requested.+ ///+ /// [`Instance::expose_adapter`]: super::Instance::expose_adapter+ ray_query: Option<vk::PhysicalDeviceRayQueryFeaturesKHR<'static>>,++ /// Features provided by `VK_KHR_ray_tracing_pipeline`.+ ray_tracing_pipeline: Option<vk::PhysicalDeviceRayTracingPipelineFeaturesKHR<'static>>,++ /// Features provided by `VK_KHR_zero_initialize_workgroup_memory`, promoted+ /// to Vulkan 1.3.+ zero_initialize_workgroup_memory:+ Option<vk::PhysicalDeviceZeroInitializeWorkgroupMemoryFeatures<'static>>,+ position_fetch: Option<vk::PhysicalDeviceRayTracingPositionFetchFeaturesKHR<'static>>,++ /// Features provided by `VK_KHR_shader_atomic_int64`, promoted to Vulkan 1.2.+ shader_atomic_int64: Option<vk::PhysicalDeviceShaderAtomicInt64Features<'static>>,++ /// Features provided by `VK_EXT_shader_image_atomic_int64`+ shader_image_atomic_int64: Option<vk::PhysicalDeviceShaderImageAtomicInt64FeaturesEXT<'static>>,++ /// Features provided by `VK_EXT_shader_atomic_float`.+ shader_atomic_float: Option<vk::PhysicalDeviceShaderAtomicFloatFeaturesEXT<'static>>,++ /// Features provided by `VK_EXT_subgroup_size_control`, promoted to Vulkan 1.3.+ subgroup_size_control: Option<vk::PhysicalDeviceSubgroupSizeControlFeatures<'static>>,++ /// Features provided by `VK_KHR_maintenance4`, promoted to Vulkan 1.3.+ maintenance4: Option<vk::PhysicalDeviceMaintenance4FeaturesKHR<'static>>,++ /// Features proved by `VK_EXT_mesh_shader`+ mesh_shader: Option<vk::PhysicalDeviceMeshShaderFeaturesEXT<'static>>,++ /// Features provided by `VK_KHR_shader_integer_dot_product`, promoted to Vulkan 1.3.+ shader_integer_dot_product:+ Option<vk::PhysicalDeviceShaderIntegerDotProductFeaturesKHR<'static>>,++ /// Features provided by `VK_KHR_fragment_shader_barycentric`+ shader_barycentrics: Option<vk::PhysicalDeviceFragmentShaderBarycentricFeaturesKHR<'static>>,++ /// Features provided by `VK_KHR_portability_subset`.+ ///+ /// Strictly speaking this tells us what features we *don't* have compared to core.+ portability_subset: Option<vk::PhysicalDevicePortabilitySubsetFeaturesKHR<'static>>,++ /// Features provided by `VK_KHR_cooperative_matrix`+ cooperative_matrix: Option<vk::PhysicalDeviceCooperativeMatrixFeaturesKHR<'static>>,++ /// Features provided by `VK_KHR_vulkan_memory_model`, promoted to Vulkan 1.2+ vulkan_memory_model: Option<vk::PhysicalDeviceVulkanMemoryModelFeaturesKHR<'static>>,++ shader_draw_parameters: Option<vk::PhysicalDeviceShaderDrawParametersFeatures<'static>>,+}++impl PhysicalDeviceFeatures {+ pub fn get_core(&self) -> vk::PhysicalDeviceFeatures {+ self.core+ }++ /// Add the members of `self` into `info.enabled_features` and its `p_next` chain.+ pub fn add_to_device_create<'a>(+ &'a mut self,+ mut info: vk::DeviceCreateInfo<'a>,+ ) -> vk::DeviceCreateInfo<'a> {+ info = info.enabled_features(&self.core);+ if let Some(ref mut feature) = self.descriptor_indexing {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.timeline_semaphore {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.image_robustness {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.robustness2 {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.multiview {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.astc_hdr {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.shader_float16_int8 {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self._16bit_storage {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.zero_initialize_workgroup_memory {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.acceleration_structure {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.buffer_device_address {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.ray_query {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.ray_tracing_pipeline {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.shader_atomic_int64 {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.position_fetch {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.shader_image_atomic_int64 {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.shader_atomic_float {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.subgroup_size_control {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.maintenance4 {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.mesh_shader {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.shader_integer_dot_product {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.shader_barycentrics {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.portability_subset {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.cooperative_matrix {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.vulkan_memory_model {+ info = info.push_next(feature);+ }+ if let Some(ref mut feature) = self.shader_draw_parameters {+ info = info.push_next(feature);+ }+ info+ }++ fn supports_storage_input_output_16(&self) -> bool {+ self._16bit_storage+ .as_ref()+ .map(|features| features.storage_input_output16 != 0)+ .unwrap_or(false)+ }++ /// Create a `PhysicalDeviceFeatures` that can be used to create a logical+ /// device.+ ///+ /// Return a `PhysicalDeviceFeatures` value capturing all the Vulkan+ /// features needed for the given [`Features`], [`DownlevelFlags`], and+ /// [`PrivateCapabilities`]. You can use the returned value's+ /// [`add_to_device_create`] method to configure a+ /// [`vk::DeviceCreateInfo`] to build a logical device providing those+ /// features.+ ///+ /// To ensure that the returned value is able to select all the Vulkan+ /// features needed to express `requested_features`, `downlevel_flags`, and+ /// `private_caps`:+ ///+ /// - The given `enabled_extensions` set must include all the extensions+ /// selected by [`Adapter::required_device_extensions`] when passed+ /// `features`.+ ///+ /// - The given `device_api_version` must be the Vulkan API version of the+ /// physical device we will use to create the logical device.+ ///+ /// [`Features`]: wgt::Features+ /// [`DownlevelFlags`]: wgt::DownlevelFlags+ /// [`PrivateCapabilities`]: super::PrivateCapabilities+ /// [`add_to_device_create`]: PhysicalDeviceFeatures::add_to_device_create+ /// [`Adapter::required_device_extensions`]: super::Adapter::required_device_extensions+ fn from_extensions_and_requested_features(+ phd_capabilities: &PhysicalDeviceProperties,+ phd_features: &PhysicalDeviceFeatures,+ enabled_extensions: &[&'static CStr],+ requested_features: wgt::Features,+ downlevel_flags: wgt::DownlevelFlags,+ private_caps: &super::PrivateCapabilities,+ ) -> Self {+ let device_api_version = phd_capabilities.device_api_version;+ let needs_bindless = requested_features.intersects(+ wgt::Features::TEXTURE_BINDING_ARRAY+ | wgt::Features::BUFFER_BINDING_ARRAY+ | wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY+ | wgt::Features::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING+ | wgt::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING,+ );+ let needs_partially_bound =+ requested_features.intersects(wgt::Features::PARTIALLY_BOUND_BINDING_ARRAY);++ Self {+ // vk::PhysicalDeviceFeatures is a struct composed of Bool32's while+ // Features is a bitfield so we need to map everything manually+ core: vk::PhysicalDeviceFeatures::default()+ .robust_buffer_access(private_caps.robust_buffer_access)+ .independent_blend(downlevel_flags.contains(wgt::DownlevelFlags::INDEPENDENT_BLEND))+ .sample_rate_shading(+ downlevel_flags.contains(wgt::DownlevelFlags::MULTISAMPLED_SHADING),+ )+ .image_cube_array(+ downlevel_flags.contains(wgt::DownlevelFlags::CUBE_ARRAY_TEXTURES),+ )+ .draw_indirect_first_instance(+ requested_features.contains(wgt::Features::INDIRECT_FIRST_INSTANCE),+ )+ //.dual_src_blend(requested_features.contains(wgt::Features::DUAL_SRC_BLENDING))+ .multi_draw_indirect(phd_features.core.multi_draw_indirect != 0)+ .fill_mode_non_solid(requested_features.intersects(+ wgt::Features::POLYGON_MODE_LINE | wgt::Features::POLYGON_MODE_POINT,+ ))+ //.depth_bounds(requested_features.contains(wgt::Features::DEPTH_BOUNDS))+ //.alpha_to_one(requested_features.contains(wgt::Features::ALPHA_TO_ONE))+ //.multi_viewport(requested_features.contains(wgt::Features::MULTI_VIEWPORTS))+ .sampler_anisotropy(+ downlevel_flags.contains(wgt::DownlevelFlags::ANISOTROPIC_FILTERING),+ )+ .texture_compression_etc2(+ requested_features.contains(wgt::Features::TEXTURE_COMPRESSION_ETC2),+ )+ .texture_compression_astc_ldr(+ requested_features.contains(wgt::Features::TEXTURE_COMPRESSION_ASTC),+ )+ .texture_compression_bc(+ requested_features.contains(wgt::Features::TEXTURE_COMPRESSION_BC),+ // BC provides formats for Sliced 3D+ )+ //.occlusion_query_precise(requested_features.contains(wgt::Features::PRECISE_OCCLUSION_QUERY))+ .pipeline_statistics_query(+ requested_features.contains(wgt::Features::PIPELINE_STATISTICS_QUERY),+ )+ .vertex_pipeline_stores_and_atomics(+ requested_features.contains(wgt::Features::VERTEX_WRITABLE_STORAGE),+ )+ .fragment_stores_and_atomics(+ downlevel_flags.contains(wgt::DownlevelFlags::FRAGMENT_WRITABLE_STORAGE),+ )+ //.shader_image_gather_extended(+ //.shader_storage_image_extended_formats(+ .shader_uniform_buffer_array_dynamic_indexing(+ requested_features.contains(wgt::Features::BUFFER_BINDING_ARRAY),+ )+ .shader_storage_buffer_array_dynamic_indexing(requested_features.contains(+ wgt::Features::BUFFER_BINDING_ARRAY+ | wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY,+ ))+ .shader_sampled_image_array_dynamic_indexing(+ requested_features.contains(wgt::Features::TEXTURE_BINDING_ARRAY),+ )+ .shader_storage_buffer_array_dynamic_indexing(requested_features.contains(+ wgt::Features::TEXTURE_BINDING_ARRAY+ | wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY,+ ))+ //.shader_storage_image_array_dynamic_indexing(+ .shader_clip_distance(requested_features.contains(wgt::Features::CLIP_DISTANCES))+ //.shader_cull_distance(requested_features.contains(wgt::Features::SHADER_CULL_DISTANCE))+ .shader_float64(requested_features.contains(wgt::Features::SHADER_F64))+ .shader_int64(requested_features.contains(wgt::Features::SHADER_INT64))+ .shader_int16(requested_features.contains(wgt::Features::SHADER_I16))+ //.shader_resource_residency(requested_features.contains(wgt::Features::SHADER_RESOURCE_RESIDENCY))+ .geometry_shader(requested_features.contains(wgt::Features::PRIMITIVE_INDEX))+ .depth_clamp(requested_features.contains(wgt::Features::DEPTH_CLIP_CONTROL))+ .dual_src_blend(requested_features.contains(wgt::Features::DUAL_SOURCE_BLENDING)),+ descriptor_indexing: if requested_features.intersects(INDEXING_FEATURES) {+ Some(+ vk::PhysicalDeviceDescriptorIndexingFeaturesEXT::default()+ .shader_sampled_image_array_non_uniform_indexing(needs_bindless)+ .shader_storage_image_array_non_uniform_indexing(needs_bindless)+ .shader_storage_buffer_array_non_uniform_indexing(needs_bindless)+ .descriptor_binding_sampled_image_update_after_bind(needs_bindless)+ .descriptor_binding_storage_image_update_after_bind(needs_bindless)+ .descriptor_binding_storage_buffer_update_after_bind(needs_bindless)+ .descriptor_binding_partially_bound(needs_partially_bound),+ )+ } else {+ None+ },+ timeline_semaphore: if device_api_version >= vk::API_VERSION_1_2+ || enabled_extensions.contains(&khr::timeline_semaphore::NAME)+ {+ Some(+ vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR::default()+ .timeline_semaphore(private_caps.timeline_semaphores),+ )+ } else {+ None+ },+ image_robustness: if device_api_version >= vk::API_VERSION_1_3+ || enabled_extensions.contains(&ext::image_robustness::NAME)+ {+ Some(+ vk::PhysicalDeviceImageRobustnessFeaturesEXT::default()+ .robust_image_access(private_caps.robust_image_access),+ )+ } else {+ None+ },+ robustness2: if enabled_extensions.contains(&ext::robustness2::NAME) {+ Some(+ vk::PhysicalDeviceRobustness2FeaturesEXT::default()+ .robust_buffer_access2(private_caps.robust_buffer_access2)+ .robust_image_access2(private_caps.robust_image_access2),+ )+ } else {+ None+ },+ multiview: if device_api_version >= vk::API_VERSION_1_1+ || enabled_extensions.contains(&khr::multiview::NAME)+ {+ Some(+ vk::PhysicalDeviceMultiviewFeatures::default()+ .multiview(requested_features.contains(wgt::Features::MULTIVIEW)),+ )+ } else {+ None+ },+ sampler_ycbcr_conversion: if device_api_version >= vk::API_VERSION_1_1+ || enabled_extensions.contains(&khr::sampler_ycbcr_conversion::NAME)+ {+ Some(+ vk::PhysicalDeviceSamplerYcbcrConversionFeatures::default(), // .sampler_ycbcr_conversion(requested_features.contains(wgt::Features::TEXTURE_FORMAT_NV12))+ )+ } else {+ None+ },+ astc_hdr: if enabled_extensions.contains(&ext::texture_compression_astc_hdr::NAME) {+ Some(+ vk::PhysicalDeviceTextureCompressionASTCHDRFeaturesEXT::default()+ .texture_compression_astc_hdr(true),+ )+ } else {+ None+ },+ shader_float16_int8: match requested_features.contains(wgt::Features::SHADER_F16) {+ shader_float16 if shader_float16 || private_caps.shader_int8 => Some(+ vk::PhysicalDeviceShaderFloat16Int8Features::default()+ .shader_float16(shader_float16)+ .shader_int8(private_caps.shader_int8),+ ),+ _ => None,+ },+ _16bit_storage: if requested_features+ .intersects(wgt::Features::SHADER_F16 | wgt::Features::SHADER_I16)+ {+ Some(+ vk::PhysicalDevice16BitStorageFeatures::default()+ .storage_buffer16_bit_access(true)+ .storage_input_output16(phd_features.supports_storage_input_output_16())+ .uniform_and_storage_buffer16_bit_access(true),+ )+ } else {+ None+ },+ acceleration_structure: if enabled_extensions+ .contains(&khr::acceleration_structure::NAME)+ {+ Some(+ vk::PhysicalDeviceAccelerationStructureFeaturesKHR::default()+ .acceleration_structure(true)+ .descriptor_binding_acceleration_structure_update_after_bind(+ requested_features+ .contains(wgt::Features::ACCELERATION_STRUCTURE_BINDING_ARRAY),+ ),+ )+ } else {+ None+ },+ buffer_device_address: if enabled_extensions.contains(&khr::buffer_device_address::NAME)+ {+ Some(+ vk::PhysicalDeviceBufferDeviceAddressFeaturesKHR::default()+ .buffer_device_address(true),+ )+ } else {+ None+ },+ ray_query: if enabled_extensions.contains(&khr::ray_query::NAME) {+ Some(vk::PhysicalDeviceRayQueryFeaturesKHR::default().ray_query(true))+ } else {+ None+ },+ ray_tracing_pipeline: if enabled_extensions.contains(&khr::ray_tracing_pipeline::NAME) {+ Some(+ vk::PhysicalDeviceRayTracingPipelineFeaturesKHR::default()+ .ray_tracing_pipeline(true),+ )+ } else {+ None+ },+ zero_initialize_workgroup_memory: if device_api_version >= vk::API_VERSION_1_3+ || enabled_extensions.contains(&khr::zero_initialize_workgroup_memory::NAME)+ {+ Some(+ vk::PhysicalDeviceZeroInitializeWorkgroupMemoryFeatures::default()+ .shader_zero_initialize_workgroup_memory(+ private_caps.zero_initialize_workgroup_memory,+ ),+ )+ } else {+ None+ },+ shader_atomic_int64: if device_api_version >= vk::API_VERSION_1_2+ || enabled_extensions.contains(&khr::shader_atomic_int64::NAME)+ {+ let needed = requested_features.intersects(+ wgt::Features::SHADER_INT64_ATOMIC_ALL_OPS+ | wgt::Features::SHADER_INT64_ATOMIC_MIN_MAX,+ );+ Some(+ vk::PhysicalDeviceShaderAtomicInt64Features::default()+ .shader_buffer_int64_atomics(needed)+ .shader_shared_int64_atomics(needed),+ )+ } else {+ None+ },+ shader_image_atomic_int64: if enabled_extensions+ .contains(&ext::shader_image_atomic_int64::NAME)+ {+ let needed = requested_features.intersects(wgt::Features::TEXTURE_INT64_ATOMIC);+ Some(+ vk::PhysicalDeviceShaderImageAtomicInt64FeaturesEXT::default()+ .shader_image_int64_atomics(needed),+ )+ } else {+ None+ },+ shader_atomic_float: if enabled_extensions.contains(&ext::shader_atomic_float::NAME) {+ let needed = requested_features.contains(wgt::Features::SHADER_FLOAT32_ATOMIC);+ Some(+ vk::PhysicalDeviceShaderAtomicFloatFeaturesEXT::default()+ .shader_buffer_float32_atomics(needed)+ .shader_buffer_float32_atomic_add(needed),+ )+ } else {+ None+ },+ subgroup_size_control: if device_api_version >= vk::API_VERSION_1_3+ || enabled_extensions.contains(&ext::subgroup_size_control::NAME)+ {+ Some(+ vk::PhysicalDeviceSubgroupSizeControlFeatures::default()+ .subgroup_size_control(true),+ )+ } else {+ None+ },+ position_fetch: if enabled_extensions.contains(&khr::ray_tracing_position_fetch::NAME) {+ Some(+ vk::PhysicalDeviceRayTracingPositionFetchFeaturesKHR::default()+ .ray_tracing_position_fetch(true),+ )+ } else {+ None+ },+ mesh_shader: if enabled_extensions.contains(&ext::mesh_shader::NAME) {+ let needed = requested_features.contains(wgt::Features::EXPERIMENTAL_MESH_SHADER);+ let multiview_needed =+ requested_features.contains(wgt::Features::EXPERIMENTAL_MESH_SHADER_MULTIVIEW);+ Some(+ vk::PhysicalDeviceMeshShaderFeaturesEXT::default()+ .mesh_shader(needed)+ .task_shader(needed)+ .multiview_mesh_shader(multiview_needed),+ )+ } else {+ None+ },+ maintenance4: if device_api_version >= vk::API_VERSION_1_3+ || enabled_extensions.contains(&khr::maintenance4::NAME)+ {+ let needed = requested_features.contains(wgt::Features::EXPERIMENTAL_MESH_SHADER);+ Some(vk::PhysicalDeviceMaintenance4Features::default().maintenance4(needed))+ } else {+ None+ },+ shader_integer_dot_product: if device_api_version >= vk::API_VERSION_1_3+ || enabled_extensions.contains(&khr::shader_integer_dot_product::NAME)+ {+ Some(+ vk::PhysicalDeviceShaderIntegerDotProductFeaturesKHR::default()+ .shader_integer_dot_product(private_caps.shader_integer_dot_product),+ )+ } else {+ None+ },+ shader_barycentrics: if enabled_extensions+ .contains(&khr::fragment_shader_barycentric::NAME)+ {+ let needed = requested_features.intersects(+ wgt::Features::SHADER_BARYCENTRICS | wgt::Features::SHADER_PER_VERTEX,+ );+ Some(+ vk::PhysicalDeviceFragmentShaderBarycentricFeaturesKHR::default()+ .fragment_shader_barycentric(needed),+ )+ } else {+ None+ },+ portability_subset: if enabled_extensions.contains(&khr::portability_subset::NAME) {+ let multisample_array_needed =+ requested_features.intersects(wgt::Features::MULTISAMPLE_ARRAY);++ Some(+ vk::PhysicalDevicePortabilitySubsetFeaturesKHR::default()+ .multisample_array_image(multisample_array_needed),+ )+ } else {+ None+ },+ cooperative_matrix: if enabled_extensions.contains(&khr::cooperative_matrix::NAME) {+ let needed =+ requested_features.contains(wgt::Features::EXPERIMENTAL_COOPERATIVE_MATRIX);+ Some(+ vk::PhysicalDeviceCooperativeMatrixFeaturesKHR::default()+ .cooperative_matrix(needed),+ )+ } else {+ None+ },+ vulkan_memory_model: if device_api_version >= vk::API_VERSION_1_2+ || enabled_extensions.contains(&khr::vulkan_memory_model::NAME)+ {+ let needed =+ requested_features.contains(wgt::Features::EXPERIMENTAL_COOPERATIVE_MATRIX);+ Some(+ vk::PhysicalDeviceVulkanMemoryModelFeaturesKHR::default()+ .vulkan_memory_model(needed)+ // The SPIR-V backend emits storage atomics with `Device`+ // memory scope, so whenever the Vulkan memory model is+ // enabled we must also enable device scope, otherwise the+ // validation layers report+ // `VUID-RuntimeSpirv-vulkanMemoryModel-06265`.+ .vulkan_memory_model_device_scope(needed),+ )+ } else {+ None+ },+ shader_draw_parameters: if device_api_version >= vk::API_VERSION_1_1 {+ let needed = requested_features.contains(wgt::Features::SHADER_DRAW_INDEX);+ Some(+ vk::PhysicalDeviceShaderDrawParametersFeatures::default()+ .shader_draw_parameters(needed),+ )+ } else {+ None+ },+ }+ }++ /// Compute the wgpu [`Features`] and [`DownlevelFlags`] supported by a physical device.+ ///+ /// Given `self`, together with the instance and physical device it was+ /// built from, and a `caps` also built from those, determine which wgpu+ /// features and downlevel flags the device can support.+ ///+ /// [`Features`]: wgt::Features+ /// [`DownlevelFlags`]: wgt::DownlevelFlags+ fn to_wgpu(+ &self,+ instance: &ash::Instance,+ phd: vk::PhysicalDevice,+ caps: &PhysicalDeviceProperties,+ queue_props: &vk::QueueFamilyProperties,+ ) -> (wgt::Features, wgt::DownlevelFlags) {+ use wgt::{DownlevelFlags as Df, Features as F};+ let mut features = F::empty()+ | F::MAPPABLE_PRIMARY_BUFFERS+ | F::IMMEDIATES+ | F::ADDRESS_MODE_CLAMP_TO_BORDER+ | F::ADDRESS_MODE_CLAMP_TO_ZERO+ | F::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES+ | F::CLEAR_TEXTURE+ | F::PIPELINE_CACHE+ | F::SHADER_EARLY_DEPTH_TEST+ | F::TEXTURE_ATOMIC+ | F::PASSTHROUGH_SHADERS+ | F::MEMORY_DECORATION_COHERENT+ | F::MEMORY_DECORATION_VOLATILE;++ let mut dl_flags = Df::COMPUTE_SHADERS+ | Df::BASE_VERTEX+ | Df::READ_ONLY_DEPTH_STENCIL+ | Df::NON_POWER_OF_TWO_MIPMAPPED_TEXTURES+ | Df::COMPARISON_SAMPLERS+ | Df::VERTEX_STORAGE+ | Df::FRAGMENT_STORAGE+ | Df::DEPTH_TEXTURE_AND_BUFFER_COPIES+ | Df::BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED+ | Df::UNRESTRICTED_INDEX_BUFFER+ | Df::INDIRECT_EXECUTION+ | Df::VIEW_FORMATS+ | Df::UNRESTRICTED_EXTERNAL_TEXTURE_COPIES+ | Df::NONBLOCKING_QUERY_RESOLVE+ | Df::SHADER_F16_IN_F32+ | Df::MSL2_1;++ dl_flags.set(+ Df::SURFACE_VIEW_FORMATS,+ caps.supports_extension(khr::swapchain_mutable_format::NAME)+ || !caps.supports_extension(khr::swapchain::NAME),+ );+ dl_flags.set(Df::CUBE_ARRAY_TEXTURES, self.core.image_cube_array != 0);+ dl_flags.set(Df::ANISOTROPIC_FILTERING, self.core.sampler_anisotropy != 0);+ dl_flags.set(+ Df::FRAGMENT_WRITABLE_STORAGE,+ self.core.fragment_stores_and_atomics != 0,+ );+ dl_flags.set(Df::MULTISAMPLED_SHADING, self.core.sample_rate_shading != 0);+ dl_flags.set(Df::INDEPENDENT_BLEND, self.core.independent_blend != 0);+ dl_flags.set(+ Df::FULL_DRAW_INDEX_UINT32,+ self.core.full_draw_index_uint32 != 0,+ );+ dl_flags.set(Df::DEPTH_BIAS_CLAMP, self.core.depth_bias_clamp != 0);++ features.set(+ F::TIMESTAMP_QUERY+ | F::TIMESTAMP_QUERY_INSIDE_ENCODERS+ | F::TIMESTAMP_QUERY_INSIDE_PASSES,+ // Vulkan strictly defines this as either 36-64, or zero.+ queue_props.timestamp_valid_bits >= 36,+ );+ features.set(+ F::INDIRECT_FIRST_INSTANCE,+ self.core.draw_indirect_first_instance != 0,+ );+ //if self.core.dual_src_blend != 0+ features.set(F::POLYGON_MODE_LINE, self.core.fill_mode_non_solid != 0);+ features.set(F::POLYGON_MODE_POINT, self.core.fill_mode_non_solid != 0);+ //if self.core.depth_bounds != 0 {+ //if self.core.alpha_to_one != 0 {+ //if self.core.multi_viewport != 0 {+ features.set(+ F::TEXTURE_COMPRESSION_ETC2,+ self.core.texture_compression_etc2 != 0,+ );+ features.set(+ F::TEXTURE_COMPRESSION_ASTC,+ self.core.texture_compression_astc_ldr != 0,+ );+ features.set(+ F::TEXTURE_COMPRESSION_BC,+ self.core.texture_compression_bc != 0,+ );+ features.set(+ F::TEXTURE_COMPRESSION_BC_SLICED_3D,+ self.core.texture_compression_bc != 0, // BC guarantees Sliced 3D+ );+ features.set(+ F::PIPELINE_STATISTICS_QUERY,+ self.core.pipeline_statistics_query != 0,+ );+ features.set(+ F::VERTEX_WRITABLE_STORAGE,+ self.core.vertex_pipeline_stores_and_atomics != 0,+ );++ features.set(F::SHADER_F64, self.core.shader_float64 != 0);+ features.set(F::SHADER_INT64, self.core.shader_int64 != 0);+ if let Some(ref bit16) = self._16bit_storage {+ features.set(+ F::SHADER_I16,+ self.core.shader_int16 != 0+ && bit16.storage_buffer16_bit_access != 0+ && bit16.uniform_and_storage_buffer16_bit_access != 0,+ );+ }++ features.set(F::PRIMITIVE_INDEX, self.core.geometry_shader != 0);++ if let Some(ref shader_atomic_int64) = self.shader_atomic_int64 {+ features.set(+ F::SHADER_INT64_ATOMIC_ALL_OPS | F::SHADER_INT64_ATOMIC_MIN_MAX,+ shader_atomic_int64.shader_buffer_int64_atomics != 0+ && shader_atomic_int64.shader_shared_int64_atomics != 0,+ );+ }++ if let Some(ref shader_image_atomic_int64) = self.shader_image_atomic_int64 {+ features.set(+ F::TEXTURE_INT64_ATOMIC,+ shader_image_atomic_int64+ .shader_image_int64_atomics(true)+ .shader_image_int64_atomics+ != 0,+ );+ }++ if let Some(ref shader_atomic_float) = self.shader_atomic_float {+ features.set(+ F::SHADER_FLOAT32_ATOMIC,+ shader_atomic_float.shader_buffer_float32_atomics != 0+ && shader_atomic_float.shader_buffer_float32_atomic_add != 0,+ );+ }++ if let Some(ref shader_barycentrics) = self.shader_barycentrics {+ features.set(+ F::SHADER_BARYCENTRICS | F::SHADER_PER_VERTEX,+ shader_barycentrics.fragment_shader_barycentric != 0,+ );+ }++ //if caps.supports_extension(khr::sampler_mirror_clamp_to_edge::NAME) {+ //if caps.supports_extension(ext::sampler_filter_minmax::NAME) {+ features.set(+ F::MULTI_DRAW_INDIRECT_COUNT,+ caps.supports_extension(khr::draw_indirect_count::NAME),+ );+ features.set(+ F::CONSERVATIVE_RASTERIZATION,+ caps.supports_extension(ext::conservative_rasterization::NAME),+ );+ features.set(+ F::EXPERIMENTAL_RAY_HIT_VERTEX_RETURN,+ caps.supports_extension(khr::ray_tracing_position_fetch::NAME),+ );++ if let Some(ref descriptor_indexing) = self.descriptor_indexing {+ // We use update-after-bind descriptors for all bind groups containing binding arrays.+ //+ // In those bind groups, we allow all binding types except uniform buffers to be present.+ //+ // As we can only switch between update-after-bind and not on a per bind group basis,+ // all supported binding types need to be able to be marked update after bind.+ //+ // As such, we enable all features as a whole, rather individually.+ let supports_descriptor_indexing =+ // Sampled Images+ descriptor_indexing.shader_sampled_image_array_non_uniform_indexing != 0+ && descriptor_indexing.descriptor_binding_sampled_image_update_after_bind != 0+ // Storage Images+ && descriptor_indexing.shader_storage_image_array_non_uniform_indexing != 0+ && descriptor_indexing.descriptor_binding_storage_image_update_after_bind != 0+ // Storage Buffers+ && descriptor_indexing.shader_storage_buffer_array_non_uniform_indexing != 0+ && descriptor_indexing.descriptor_binding_storage_buffer_update_after_bind != 0;++ let descriptor_indexing_features = F::BUFFER_BINDING_ARRAY+ | F::TEXTURE_BINDING_ARRAY+ | F::STORAGE_RESOURCE_BINDING_ARRAY+ | F::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING+ | F::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING;++ features.set(descriptor_indexing_features, supports_descriptor_indexing);++ let supports_partially_bound =+ descriptor_indexing.descriptor_binding_partially_bound != 0;++ features.set(F::PARTIALLY_BOUND_BINDING_ARRAY, supports_partially_bound);+ }++ features.set(F::DEPTH_CLIP_CONTROL, self.core.depth_clamp != 0);+ features.set(F::DUAL_SOURCE_BLENDING, self.core.dual_src_blend != 0);+ features.set(F::CLIP_DISTANCES, self.core.shader_clip_distance != 0);++ if let Some(ref multiview) = self.multiview {+ features.set(F::MULTIVIEW, multiview.multiview != 0);+ features.set(F::SELECTIVE_MULTIVIEW, multiview.multiview != 0);+ }++ features.set(+ F::TEXTURE_FORMAT_16BIT_NORM,+ is_format_16bit_norm_supported(instance, phd),+ );++ if let Some(ref astc_hdr) = self.astc_hdr {+ features.set(+ F::TEXTURE_COMPRESSION_ASTC_HDR,+ astc_hdr.texture_compression_astc_hdr != 0,+ );+ }++ if self.core.texture_compression_astc_ldr != 0 {+ features.set(+ F::TEXTURE_COMPRESSION_ASTC_SLICED_3D,+ supports_astc_3d(instance, phd),+ );+ }++ if let (Some(ref f16_i8), Some(ref bit16)) = (self.shader_float16_int8, self._16bit_storage)+ {+ // Note `storage_input_output16` is not required, we polyfill `f16` I/O using `f32`+ // types when this capability is not available+ features.set(+ F::SHADER_F16,+ f16_i8.shader_float16 != 0+ && bit16.storage_buffer16_bit_access != 0+ && bit16.uniform_and_storage_buffer16_bit_access != 0,+ );+ }++ if let Some(ref subgroup) = caps.subgroup {+ if (caps.device_api_version >= vk::API_VERSION_1_3+ || caps.supports_extension(ext::subgroup_size_control::NAME))+ && subgroup.supported_operations.contains(+ vk::SubgroupFeatureFlags::BASIC+ | vk::SubgroupFeatureFlags::VOTE+ | vk::SubgroupFeatureFlags::ARITHMETIC+ | vk::SubgroupFeatureFlags::BALLOT+ | vk::SubgroupFeatureFlags::SHUFFLE+ | vk::SubgroupFeatureFlags::SHUFFLE_RELATIVE+ | vk::SubgroupFeatureFlags::QUAD,+ )+ {+ features.set(+ F::SUBGROUP,+ subgroup+ .supported_stages+ .contains(vk::ShaderStageFlags::COMPUTE | vk::ShaderStageFlags::FRAGMENT),+ );+ features.set(+ F::SUBGROUP_VERTEX,+ subgroup+ .supported_stages+ .contains(vk::ShaderStageFlags::VERTEX),+ );+ features.insert(F::SUBGROUP_BARRIER);+ }+ }++ let supports_depth_format = |format| {+ supports_format(+ instance,+ phd,+ format,+ vk::ImageTiling::OPTIMAL,+ depth_stencil_required_flags(),+ )+ };++ let texture_s8 = supports_depth_format(vk::Format::S8_UINT);+ let texture_d32 = supports_depth_format(vk::Format::D32_SFLOAT);+ let texture_d24_s8 = supports_depth_format(vk::Format::D24_UNORM_S8_UINT);+ let texture_d32_s8 = supports_depth_format(vk::Format::D32_SFLOAT_S8_UINT);++ let stencil8 = texture_s8 || texture_d24_s8;+ let depth24_plus_stencil8 = texture_d24_s8 || texture_d32_s8;++ dl_flags.set(+ Df::WEBGPU_TEXTURE_FORMAT_SUPPORT,+ stencil8 && depth24_plus_stencil8 && texture_d32,+ );++ features.set(F::DEPTH32FLOAT_STENCIL8, texture_d32_s8);++ let supports_acceleration_structures = caps+ .supports_extension(khr::deferred_host_operations::NAME)+ && caps.supports_extension(khr::acceleration_structure::NAME)+ && caps.supports_extension(khr::buffer_device_address::NAME);++ let supports_ray_query =+ supports_acceleration_structures && caps.supports_extension(khr::ray_query::NAME);+ let supports_acceleration_structure_binding_array = supports_ray_query+ && self+ .acceleration_structure+ .as_ref()+ .is_some_and(|features| {+ features.descriptor_binding_acceleration_structure_update_after_bind != 0+ });++ features.set(+ F::EXPERIMENTAL_RAY_QUERY+ // Although this doesn't really require ray queries, it does not make sense to be enabled if acceleration structures+ // aren't enabled.+ | F::EXTENDED_ACCELERATION_STRUCTURE_VERTEX_FORMATS,+ supports_ray_query,+ );++ // Binding arrays of TLAS are supported on Vulkan when ray queries are supported.+ //+ // Note: this flag is used for shader-side `binding_array<acceleration_structure>` as well as+ // allowing `BindGroupLayoutEntry::count = Some(...)` for `BindingType::AccelerationStructure`.+ features.set(+ F::ACCELERATION_STRUCTURE_BINDING_ARRAY,+ supports_acceleration_structure_binding_array,+ );++ if supports_acceleration_structures+ && caps.supports_extension(khr::ray_tracing_pipeline::NAME)+ {+ features.insert(+ F::EXPERIMENTAL_RAY_TRACING_PIPELINES+ // Same reason as for ray queries.+ | F::EXTENDED_ACCELERATION_STRUCTURE_VERTEX_FORMATS,+ );+ }++ let rg11b10ufloat_renderable = supports_format(+ instance,+ phd,+ vk::Format::B10G11R11_UFLOAT_PACK32,+ vk::ImageTiling::OPTIMAL,+ vk::FormatFeatureFlags::COLOR_ATTACHMENT+ | vk::FormatFeatureFlags::COLOR_ATTACHMENT_BLEND,+ );+ features.set(F::RG11B10UFLOAT_RENDERABLE, rg11b10ufloat_renderable);++ features.set(+ F::BGRA8UNORM_STORAGE,+ supports_bgra8unorm_storage(instance, phd, caps.device_api_version),+ );++ features.set(+ F::FLOAT32_FILTERABLE,+ is_float32_filterable_supported(instance, phd),+ );++ features.set(+ F::FLOAT32_BLENDABLE,+ is_float32_blendable_supported(instance, phd),+ );++ if let Some(ref _sampler_ycbcr_conversion) = self.sampler_ycbcr_conversion {+ features.set(+ F::TEXTURE_FORMAT_NV12,+ supports_format(+ instance,+ phd,+ vk::Format::G8_B8R8_2PLANE_420_UNORM,+ vk::ImageTiling::OPTIMAL,+ vk::FormatFeatureFlags::SAMPLED_IMAGE+ | vk::FormatFeatureFlags::TRANSFER_SRC+ | vk::FormatFeatureFlags::TRANSFER_DST,+ ) && !caps.is_driver(vk::DriverId::MOLTENVK),+ );+ }++ if let Some(ref _sampler_ycbcr_conversion) = self.sampler_ycbcr_conversion {+ features.set(+ F::TEXTURE_FORMAT_P010,+ supports_format(+ instance,+ phd,+ vk::Format::G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16,+ vk::ImageTiling::OPTIMAL,+ vk::FormatFeatureFlags::SAMPLED_IMAGE+ | vk::FormatFeatureFlags::TRANSFER_SRC+ | vk::FormatFeatureFlags::TRANSFER_DST,+ ) && !caps.is_driver(vk::DriverId::MOLTENVK),+ );+ }++ features.set(+ F::VULKAN_GOOGLE_DISPLAY_TIMING,+ caps.supports_extension(google::display_timing::NAME),+ );++ features.set(+ F::VULKAN_EXTERNAL_MEMORY_WIN32,+ caps.supports_extension(khr::external_memory_win32::NAME),+ );+ features.set(+ F::VULKAN_EXTERNAL_MEMORY_FD,+ caps.supports_extension(khr::external_memory_fd::NAME),+ );+ features.set(+ F::VULKAN_EXTERNAL_MEMORY_DMA_BUF,+ caps.supports_extension(khr::external_memory_fd::NAME)+ && caps.supports_extension(ext::external_memory_dma_buf::NAME)+ && caps.supports_extension(ext::image_drm_format_modifier::NAME),+ );+ features.set(+ F::EXPERIMENTAL_MESH_SHADER,+ caps.supports_extension(ext::mesh_shader::NAME),+ );+ features.set(+ F::EXPERIMENTAL_MESH_SHADER_POINTS,+ caps.supports_extension(ext::mesh_shader::NAME),+ );+ if let Some(ref mesh_shader) = self.mesh_shader {+ features.set(+ F::EXPERIMENTAL_MESH_SHADER_MULTIVIEW,+ mesh_shader.multiview_mesh_shader != 0,+ );+ }++ // Not supported by default by `VK_KHR_portability_subset`, which we use on apple platforms.+ features.set(+ F::MULTISAMPLE_ARRAY,+ self.portability_subset+ .map(|p| p.multisample_array_image == vk::TRUE)+ .unwrap_or(true),+ );+ // Enable cooperative matrix if any configuration is supported. The SPIR-V+ // we emit for it uses `Device`-scope atomics under the Vulkan memory model,+ // so the device must also support `vulkanMemoryModelDeviceScope`, otherwise+ // those shaders trip `VUID-RuntimeSpirv-vulkanMemoryModel-06265`.+ features.set(+ F::EXPERIMENTAL_COOPERATIVE_MATRIX,+ !caps.cooperative_matrix_properties.is_empty()+ && self.vulkan_memory_model.is_some_and(|m| {+ m.vulkan_memory_model == vk::TRUE+ && m.vulkan_memory_model_device_scope == vk::TRUE+ }),+ );++ features.set(+ F::SHADER_DRAW_INDEX,+ self.shader_draw_parameters+ .is_some_and(|a| a.shader_draw_parameters != 0)+ || caps.supports_extension(c"VK_KHR_shader_draw_parameters"),+ );++ (features, dl_flags)+ }+}++/// Vulkan "properties" structures gathered about a physical device.+///+/// This structure holds the properties of a [`vk::PhysicalDevice`]:+/// - the standard Vulkan device properties+/// - the `VkExtensionProperties` structs for all available extensions, and+/// - the per-extension properties structures for the available extensions that+/// `wgpu` cares about.+///+/// Generally, if you get it from any of these functions, it's stored+/// here:+/// - `vkEnumerateDeviceExtensionProperties`+/// - `vkGetPhysicalDeviceProperties`+/// - `vkGetPhysicalDeviceProperties2`+///+/// This also includes a copy of the device API version, since we can+/// use that as a shortcut for searching for an extension, if the+/// extension has been promoted to core in the current version.+///+/// This does not include device features; for those, see+/// [`PhysicalDeviceFeatures`].+#[derive(Default, Debug)]+pub struct PhysicalDeviceProperties {+ /// Extensions supported by the `vk::PhysicalDevice`,+ /// as returned by `vkEnumerateDeviceExtensionProperties`.+ supported_extensions: Vec<vk::ExtensionProperties>,++ /// Properties of the `vk::PhysicalDevice`, as returned by+ /// `vkGetPhysicalDeviceProperties`.+ properties: vk::PhysicalDeviceProperties,++ /// Additional `vk::PhysicalDevice` properties from the+ /// `VK_KHR_maintenance3` extension, promoted to Vulkan 1.1.+ maintenance_3: Option<vk::PhysicalDeviceMaintenance3Properties<'static>>,++ /// Additional `vk::PhysicalDevice` properties from the+ /// `VK_KHR_maintenance4` extension, promoted to Vulkan 1.3.+ maintenance_4: Option<vk::PhysicalDeviceMaintenance4Properties<'static>>,++ /// Additional `vk::PhysicalDevice` properties from the+ /// `VK_EXT_descriptor_indexing` extension, promoted to Vulkan 1.2.+ descriptor_indexing: Option<vk::PhysicalDeviceDescriptorIndexingPropertiesEXT<'static>>,++ /// Additional `vk::PhysicalDevice` properties from the+ /// `VK_KHR_acceleration_structure` extension.+ acceleration_structure: Option<vk::PhysicalDeviceAccelerationStructurePropertiesKHR<'static>>,++ /// Additional `vk::PhysicalDevice` properties from the+ /// `VK_KHR_ray_tracing_pipeline` extension.+ ray_tracing_pipeline: Option<vk::PhysicalDeviceRayTracingPipelinePropertiesKHR<'static>>,++ /// Additional `vk::PhysicalDevice` properties from the+ /// `VK_KHR_driver_properties` extension, promoted to Vulkan 1.2.+ driver: Option<vk::PhysicalDeviceDriverPropertiesKHR<'static>>,++ /// Additional `vk::PhysicalDevice` properties from Vulkan 1.1.+ subgroup: Option<vk::PhysicalDeviceSubgroupProperties<'static>>,++ /// Additional `vk::PhysicalDevice` properties from the+ /// `VK_EXT_subgroup_size_control` extension, promoted to Vulkan 1.3.+ subgroup_size_control: Option<vk::PhysicalDeviceSubgroupSizeControlProperties<'static>>,++ /// Additional `vk::PhysicalDevice` properties from the+ /// `VK_EXT_robustness2` extension.+ robustness2: Option<vk::PhysicalDeviceRobustness2PropertiesEXT<'static>>,++ /// Additional `vk::PhysicalDevice` properties from the+ /// `VK_EXT_mesh_shader` extension.+ mesh_shader: Option<vk::PhysicalDeviceMeshShaderPropertiesEXT<'static>>,++ /// Additional `vk::PhysicalDevice` properties from the+ /// `VK_KHR_multiview` extension.+ multiview: Option<vk::PhysicalDeviceMultiviewPropertiesKHR<'static>>,++ /// `VK_EXT_pci_bus_info` extension.+ pci_bus_info: Option<vk::PhysicalDevicePCIBusInfoPropertiesEXT<'static>>,++ /// The device API version.+ ///+ /// Which is the version of Vulkan supported for device-level functionality.+ ///+ /// It is associated with a `VkPhysicalDevice` and its children.+ device_api_version: u32,++ /// Supported cooperative matrix configurations.+ ///+ /// This is determined by querying `vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR`.+ cooperative_matrix_properties: Vec<wgt::CooperativeMatrixProperties>,+}++impl PhysicalDeviceProperties {+ pub fn properties(&self) -> vk::PhysicalDeviceProperties {+ self.properties+ }++ pub fn supports_extension(&self, extension: &CStr) -> bool {+ self.supported_extensions+ .iter()+ .any(|ep| ep.extension_name_as_c_str() == Ok(extension))+ }++ pub fn is_driver(&self, id: vk::DriverId) -> bool {+ self.driver.is_some_and(|driver| driver.driver_id == id)+ }++ /// Map `requested_features` to the list of Vulkan extension strings required to create the logical device.+ fn get_required_extensions(&self, requested_features: wgt::Features) -> Vec<&'static CStr> {+ let mut extensions = Vec::new();++ // Note that quite a few extensions depend on the `VK_KHR_get_physical_device_properties2` instance extension.+ // We enable `VK_KHR_get_physical_device_properties2` unconditionally (if available).++ // Require `VK_KHR_swapchain`+ extensions.push(khr::swapchain::NAME);++ if self.device_api_version < vk::API_VERSION_1_1 {+ // Require `VK_KHR_maintenance1`+ extensions.push(khr::maintenance1::NAME);++ // Optional `VK_KHR_maintenance2`+ if self.supports_extension(khr::maintenance2::NAME) {+ extensions.push(khr::maintenance2::NAME);+ }++ // Optional `VK_KHR_maintenance3`+ if self.supports_extension(khr::maintenance3::NAME) {+ extensions.push(khr::maintenance3::NAME);+ }++ // Require `VK_KHR_storage_buffer_storage_class`+ extensions.push(khr::storage_buffer_storage_class::NAME);++ // Require `VK_KHR_multiview` if the associated feature was requested+ if requested_features.contains(wgt::Features::MULTIVIEW) {+ extensions.push(khr::multiview::NAME);+ }++ // Require `VK_KHR_sampler_ycbcr_conversion` if the associated feature was requested+ if requested_features.contains(wgt::Features::TEXTURE_FORMAT_NV12) {+ extensions.push(khr::sampler_ycbcr_conversion::NAME);+ }++ // Require `VK_KHR_16bit_storage` if `SHADER_F16` or `SHADER_I16` was requested+ if requested_features.intersects(wgt::Features::SHADER_F16 | wgt::Features::SHADER_I16)+ {+ // - Feature `SHADER_F16` also requires `VK_KHR_shader_float16_int8`, but we always+ // require that anyway (if it is available) below.+ // - `VK_KHR_16bit_storage` requires `VK_KHR_storage_buffer_storage_class`, however+ // we require that one already.+ extensions.push(khr::_16bit_storage::NAME);+ }++ if requested_features.contains(wgt::Features::SHADER_DRAW_INDEX) {+ extensions.push(khr::shader_draw_parameters::NAME);+ }+ }++ if self.device_api_version < vk::API_VERSION_1_2 {+ // Optional `VK_KHR_image_format_list`+ if self.supports_extension(khr::image_format_list::NAME) {+ extensions.push(khr::image_format_list::NAME);+ }++ // Optional `VK_KHR_driver_properties`+ if self.supports_extension(khr::driver_properties::NAME) {+ extensions.push(khr::driver_properties::NAME);+ }++ // Optional `VK_KHR_timeline_semaphore`+ if self.supports_extension(khr::timeline_semaphore::NAME) {+ extensions.push(khr::timeline_semaphore::NAME);+ }++ // Require `VK_EXT_descriptor_indexing` if one of the associated features was requested+ if requested_features.intersects(INDEXING_FEATURES) {+ extensions.push(ext::descriptor_indexing::NAME);+ }++ // Always require `VK_KHR_shader_float16_int8` if available as it enables+ // Int8 optimizations. Also require it even if it's not available but+ // requested so that we get a corresponding error message.+ if requested_features.contains(wgt::Features::SHADER_F16)+ || self.supports_extension(khr::shader_float16_int8::NAME)+ {+ extensions.push(khr::shader_float16_int8::NAME);+ }++ if requested_features.intersects(wgt::Features::EXPERIMENTAL_MESH_SHADER) {+ extensions.push(khr::spirv_1_4::NAME);+ }++ //extensions.push(khr::sampler_mirror_clamp_to_edge::NAME);+ //extensions.push(ext::sampler_filter_minmax::NAME);+ }++ if self.device_api_version < vk::API_VERSION_1_3 {+ // Optional `VK_KHR_maintenance4`+ if self.supports_extension(khr::maintenance4::NAME) {+ extensions.push(khr::maintenance4::NAME);+ }++ // Optional `VK_EXT_image_robustness`+ if self.supports_extension(ext::image_robustness::NAME) {+ extensions.push(ext::image_robustness::NAME);+ }++ // Require `VK_EXT_subgroup_size_control` if the associated feature was requested+ if requested_features.contains(wgt::Features::SUBGROUP) {+ extensions.push(ext::subgroup_size_control::NAME);+ }++ // Optional `VK_KHR_shader_integer_dot_product`+ if self.supports_extension(khr::shader_integer_dot_product::NAME) {+ extensions.push(khr::shader_integer_dot_product::NAME);+ }+ }++ // Optional `VK_KHR_swapchain_mutable_format`+ if self.supports_extension(khr::swapchain_mutable_format::NAME) {+ extensions.push(khr::swapchain_mutable_format::NAME);+ }++ // Optional `VK_EXT_robustness2`+ if self.supports_extension(ext::robustness2::NAME) {+ extensions.push(ext::robustness2::NAME);+ }++ // Optional `VK_KHR_external_memory_win32`+ if self.supports_extension(khr::external_memory_win32::NAME) {+ extensions.push(khr::external_memory_win32::NAME);+ }++ // Optional `VK_KHR_external_memory_fd`+ if self.supports_extension(khr::external_memory_fd::NAME) {+ extensions.push(khr::external_memory_fd::NAME);+ }++ // Optional `VK_EXT_external_memory_dma`+ if self.supports_extension(ext::external_memory_dma_buf::NAME) {+ extensions.push(ext::external_memory_dma_buf::NAME);+ }++ // Optional `VK_EXT_image_drm_format_modifier`+ if self.supports_extension(ext::image_drm_format_modifier::NAME) {+ extensions.push(ext::image_drm_format_modifier::NAME);+ }++ // Optional `VK_EXT_memory_budget`+ if self.supports_extension(ext::memory_budget::NAME) {+ extensions.push(ext::memory_budget::NAME);+ } else {+ log::debug!("VK_EXT_memory_budget is not available.")+ }++ // Require `VK_KHR_draw_indirect_count` if the associated feature was requested+ // Even though Vulkan 1.2 has promoted the extension to core, we must require the extension to avoid+ // large amounts of spaghetti involved with using PhysicalDeviceVulkan12Features.+ if requested_features.contains(wgt::Features::MULTI_DRAW_INDIRECT_COUNT) {+ extensions.push(khr::draw_indirect_count::NAME);+ }++ // Require `VK_KHR_deferred_host_operations`, `VK_KHR_acceleration_structure` `VK_KHR_buffer_device_address` (for acceleration structures) if either `EXPERIMENTAL_RAY_QUERY` or `EXPERIMENTAL_RAY_TRACING_PIPELINES` were requested.+ if requested_features.intersects(+ wgt::Features::EXPERIMENTAL_RAY_QUERY+ | wgt::Features::EXPERIMENTAL_RAY_TRACING_PIPELINES,+ ) {+ extensions.push(khr::deferred_host_operations::NAME);+ extensions.push(khr::acceleration_structure::NAME);+ extensions.push(khr::buffer_device_address::NAME);+ }++ // Require `VK_KHR_ray_query` if `EXPERIMENTAL_RAY_QUERY` was requested+ if requested_features.contains(wgt::Features::EXPERIMENTAL_RAY_QUERY) {+ extensions.push(khr::ray_query::NAME);+ }++ // Require `VK_KHR_ray_tracing_pipeline` if `EXPERIMENTAL_RAY_TRACING_PIPELINES` was requested+ if requested_features.contains(wgt::Features::EXPERIMENTAL_RAY_TRACING_PIPELINES) {+ extensions.push(khr::ray_tracing_pipeline::NAME);+ }++ if requested_features.contains(wgt::Features::EXPERIMENTAL_RAY_HIT_VERTEX_RETURN) {+ extensions.push(khr::ray_tracing_position_fetch::NAME)+ }++ // Require `VK_EXT_conservative_rasterization` if the associated feature was requested+ if requested_features.contains(wgt::Features::CONSERVATIVE_RASTERIZATION) {+ extensions.push(ext::conservative_rasterization::NAME);+ }++ // Require `VK_KHR_portability_subset` on macOS/iOS+ #[cfg(target_vendor = "apple")]+ extensions.push(khr::portability_subset::NAME);++ // Require `VK_EXT_texture_compression_astc_hdr` if the associated feature was requested+ if requested_features.contains(wgt::Features::TEXTURE_COMPRESSION_ASTC_HDR) {+ extensions.push(ext::texture_compression_astc_hdr::NAME);+ }++ // Require `VK_KHR_shader_atomic_int64` if the associated feature was requested+ if requested_features.intersects(+ wgt::Features::SHADER_INT64_ATOMIC_ALL_OPS | wgt::Features::SHADER_INT64_ATOMIC_MIN_MAX,+ ) {+ extensions.push(khr::shader_atomic_int64::NAME);+ }++ // Require `VK_EXT_shader_image_atomic_int64` if the associated feature was requested+ if requested_features.intersects(wgt::Features::TEXTURE_INT64_ATOMIC) {+ extensions.push(ext::shader_image_atomic_int64::NAME);+ }++ // Require `VK_EXT_shader_atomic_float` if the associated feature was requested+ if requested_features.contains(wgt::Features::SHADER_FLOAT32_ATOMIC) {+ extensions.push(ext::shader_atomic_float::NAME);+ }++ // Require VK_GOOGLE_display_timing if the associated feature was requested+ if requested_features.contains(wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING) {+ extensions.push(google::display_timing::NAME);+ }++ if requested_features.contains(wgt::Features::EXPERIMENTAL_MESH_SHADER) {+ extensions.push(ext::mesh_shader::NAME);+ }++ // Require `VK_KHR_fragment_shader_barycentric` if an associated feature was requested+ // Vulkan bundles both barycentrics and per-vertex attributes under the same feature.+ if requested_features+ .intersects(wgt::Features::SHADER_BARYCENTRICS | wgt::Features::SHADER_PER_VERTEX)+ {+ extensions.push(khr::fragment_shader_barycentric::NAME);+ }++ // Require `VK_KHR_cooperative_matrix` if the associated feature was requested+ if requested_features.contains(wgt::Features::EXPERIMENTAL_COOPERATIVE_MATRIX) {+ extensions.push(khr::cooperative_matrix::NAME);+ }++ extensions+ }++ fn to_wgpu_limits(&self) -> wgt::Limits {+ let limits = &self.properties.limits;++ // Default is only implemented for tuples up to a certain size.+ let (+ mut max_task_workgroup_total_count,+ mut max_task_workgroups_per_dimension,+ mut max_mesh_workgroup_total_count,+ mut max_mesh_workgroups_per_dimension,+ ) = Default::default();+ let (+ mut max_task_invocations_per_workgroup,+ mut max_task_invocations_per_dimension,+ mut max_mesh_invocations_per_workgroup,+ mut max_mesh_invocations_per_dimension,+ mut max_task_payload_size,+ mut max_mesh_output_vertices,+ mut max_mesh_output_primitives,+ mut max_mesh_output_layers,+ mut max_mesh_multiview_view_count,+ ) = Default::default();+ if let Some(m) = self.mesh_shader {+ max_task_workgroup_total_count = m.max_task_work_group_total_count;+ max_task_workgroups_per_dimension =+ m.max_task_work_group_count.into_iter().min().unwrap();+ max_mesh_workgroup_total_count = m.max_mesh_work_group_total_count;+ max_mesh_workgroups_per_dimension =+ m.max_mesh_work_group_count.into_iter().min().unwrap();+ max_task_invocations_per_workgroup = m.max_task_work_group_invocations;+ max_task_invocations_per_dimension =+ m.max_task_work_group_size.into_iter().min().unwrap();+ max_mesh_invocations_per_workgroup = m.max_mesh_work_group_invocations;+ max_mesh_invocations_per_dimension =+ m.max_mesh_work_group_size.into_iter().min().unwrap();+ max_task_payload_size = m.max_task_payload_size;+ max_mesh_output_vertices = m.max_mesh_output_vertices;+ max_mesh_output_primitives = m.max_mesh_output_primitives;+ max_mesh_output_layers = m.max_mesh_output_layers;+ max_mesh_multiview_view_count = m.max_mesh_multiview_view_count;+ }++ let max_memory_allocation_size = self+ .maintenance_3+ .map(|maintenance_3| maintenance_3.max_memory_allocation_size)+ .unwrap_or(u64::MAX);+ let max_buffer_size = self+ .maintenance_4+ .map(|maintenance_4| maintenance_4.max_buffer_size)+ .unwrap_or(u64::MAX);+ let max_buffer_size = max_buffer_size.min(max_memory_allocation_size);++ // Prevent very large buffers on mesa and most android devices, and in all cases+ // don't risk confusing JS by exceeding the range of a double.+ let is_nvidia = self.properties.vendor_id == crate::auxil::db::nvidia::VENDOR;+ let max_buffer_size_cap =+ if (cfg!(target_os = "linux") || cfg!(target_os = "android")) && !is_nvidia {+ i32::MAX as u64+ } else {+ 1u64 << 52+ };++ let max_buffer_size = max_buffer_size.min(max_buffer_size_cap);++ let mut max_binding_array_elements = 0;+ let mut max_sampler_binding_array_elements = 0;+ if let Some(ref descriptor_indexing) = self.descriptor_indexing {+ max_binding_array_elements = descriptor_indexing+ .max_descriptor_set_update_after_bind_sampled_images+ .min(descriptor_indexing.max_descriptor_set_update_after_bind_storage_images)+ .min(descriptor_indexing.max_descriptor_set_update_after_bind_storage_buffers)+ .min(descriptor_indexing.max_per_stage_descriptor_update_after_bind_sampled_images)+ .min(descriptor_indexing.max_per_stage_descriptor_update_after_bind_storage_images)+ .min(+ descriptor_indexing.max_per_stage_descriptor_update_after_bind_storage_buffers,+ );++ max_sampler_binding_array_elements = descriptor_indexing+ .max_descriptor_set_update_after_bind_samplers+ .min(descriptor_indexing.max_per_stage_descriptor_update_after_bind_samplers);+ }++ const MAX_SHADER_STAGES_PER_PIPELINE: u32 = 2;++ // When summed, the 3 limits below must be under Vulkan's maxFragmentCombinedOutputResources.+ // https://gpuweb.github.io/gpuweb/correspondence/#vulkan-maxFragmentCombinedOutputResources+ //+ // - maxStorageTexturesPerShaderStage, WebGPU default: 4+ // - maxStorageBuffersPerShaderStage, WebGPU default: 8+ // - maxColorAttachments, WebGPU default: 8+ //+ // However, maxFragmentCombinedOutputResources should be ignored on+ // intel/nvidia/amd/imgtec since it's not reported correctly.+ //+ // https://github.com/gpuweb/gpuweb/issues/3631#issuecomment-1498747606+ // https://github.com/gpuweb/gpuweb/issues/4018+ let mut max_storage_textures_per_shader_stage = limits+ .max_per_stage_descriptor_storage_images+ .min(limits.max_descriptor_set_storage_images / MAX_SHADER_STAGES_PER_PIPELINE);+ let mut max_storage_buffers_per_shader_stage = limits+ .max_per_stage_descriptor_storage_buffers+ .min(limits.max_descriptor_set_storage_buffers / MAX_SHADER_STAGES_PER_PIPELINE);+ let mut max_color_attachments = limits+ .max_color_attachments+ .min(limits.max_fragment_output_attachments);++ let ignore_max_fragment_combined_output_resources_by_device = [+ crate::auxil::db::intel::VENDOR,+ crate::auxil::db::nvidia::VENDOR,+ crate::auxil::db::amd::VENDOR,+ crate::auxil::db::imgtec::VENDOR,+ ]+ .contains(&self.properties.vendor_id);+ let ignore_max_fragment_combined_output_resources_by_driver =+ self.is_driver(vk::DriverId::MESA_AGXV);+ let ignore_max_fragment_combined_output_resources =+ ignore_max_fragment_combined_output_resources_by_device+ || ignore_max_fragment_combined_output_resources_by_driver;++ if !ignore_max_fragment_combined_output_resources {+ crate::auxil::cap_limits_to_be_under_the_sum_limit(+ [+ &mut max_storage_textures_per_shader_stage,+ &mut max_storage_buffers_per_shader_stage,+ &mut max_color_attachments,+ ],+ limits.max_fragment_combined_output_resources,+ );+ }++ // When summed, the 5 limits below must be under Vulkan's maxPerStageResources.+ //+ // - maxUniformBuffersPerShaderStage, WebGPU default: 12+ // - maxSampledTexturesPerShaderStage, WebGPU default: 16+ // - maxStorageTexturesPerShaderStage, WebGPU default: 4+ // - maxStorageBuffersPerShaderStage, WebGPU default: 8+ // - maxColorAttachments, WebGPU default: 8+ //+ // Note: Vulkan's texel buffers and input attachments also count towards+ // maxPerStageResources but we don't make use of them.+ let mut max_sampled_textures_per_shader_stage = limits+ .max_per_stage_descriptor_sampled_images+ .min(limits.max_descriptor_set_sampled_images / MAX_SHADER_STAGES_PER_PIPELINE);+ let mut max_uniform_buffers_per_shader_stage = limits+ .max_per_stage_descriptor_uniform_buffers+ .min(limits.max_descriptor_set_uniform_buffers / MAX_SHADER_STAGES_PER_PIPELINE);++ crate::auxil::cap_limits_to_be_under_the_sum_limit(+ [+ &mut max_sampled_textures_per_shader_stage,+ &mut max_uniform_buffers_per_shader_stage,+ &mut max_storage_textures_per_shader_stage,+ &mut max_storage_buffers_per_shader_stage,+ &mut max_color_attachments,+ ],+ limits.max_per_stage_resources,+ );++ // Acceleration structure limits+ let mut max_blas_geometry_count = 0;+ let mut max_blas_primitive_count = 0;+ let mut max_tlas_instance_count = 0;+ let mut max_acceleration_structures_per_shader_stage = 0;+ if let Some(properties) = self.acceleration_structure {+ max_blas_geometry_count = properties.max_geometry_count as u32;+ max_blas_primitive_count = properties.max_primitive_count as u32;+ max_tlas_instance_count = properties.max_instance_count as u32;+ max_acceleration_structures_per_shader_stage = properties+ .max_per_stage_descriptor_acceleration_structures+ .min(+ properties.max_descriptor_set_acceleration_structures+ / MAX_SHADER_STAGES_PER_PIPELINE,+ );+ }++ // When summed, the 6 limits below must be under Vulkan's+ // maxPerSetDescriptors / MAX_SHADER_STAGES_PER_PIPELINE.+ //+ // - maxUniformBuffersPerShaderStage, WebGPU default: 12+ // - maxSampledTexturesPerShaderStage, WebGPU default: 16+ // - maxStorageTexturesPerShaderStage, WebGPU default: 4+ // - maxStorageBuffersPerShaderStage, WebGPU default: 8+ // - maxSamplersPerShaderStage, WebGPU default: 16+ // - maxAccelerationStructuresPerShaderStage, Native only+ //+ // Note: All Vulkan's descriptor types count towards maxPerSetDescriptors but+ // we don't use all of them.+ // See https://registry.khronos.org/vulkan/specs/latest/html/vkspec.html#interfaces-resources-limits+ let max_per_set_descriptors = self+ .maintenance_3+ .map(|maintenance_3| maintenance_3.max_per_set_descriptors)+ // The lowest value seen in reports is 312, use 256 as a safe default.+ // https://vulkan.gpuinfo.org/displayextensionproperty.php?extensionname=VK_KHR_maintenance3&extensionproperty=maxPerSetDescriptors&platform=all+ // https://vulkan.gpuinfo.org/displaycoreproperty.php?core=1.1&name=maxPerSetDescriptors&platform=all+ .unwrap_or(256);++ let mut max_samplers_per_shader_stage = limits+ .max_per_stage_descriptor_samplers+ .min(limits.max_descriptor_set_samplers / MAX_SHADER_STAGES_PER_PIPELINE);++ crate::auxil::cap_limits_to_be_under_the_sum_limit(+ [+ &mut max_sampled_textures_per_shader_stage,+ &mut max_uniform_buffers_per_shader_stage,+ &mut max_storage_textures_per_shader_stage,+ &mut max_storage_buffers_per_shader_stage,+ &mut max_samplers_per_shader_stage,+ &mut max_acceleration_structures_per_shader_stage,+ ],+ max_per_set_descriptors / MAX_SHADER_STAGES_PER_PIPELINE,+ );++ // Use max(default, maxPerSetDescriptors) since the spec requires this+ // limit to be at least 1000. This is ok because we already lowered+ // all the other relevant per stage limits so their sum is lower+ // than maxPerSetDescriptors.+ let max_bindings_per_bind_group = 1000.max(max_per_set_descriptors);++ // TODO: programmatically determine this, if possible. It's unclear whether we can+ // as of https://github.com/gpuweb/gpuweb/issues/2965#issuecomment-1361315447.+ //+ // In theory some tilers may not support this much. We can't tell however, and+ // the driver will throw a DEVICE_REMOVED if it goes too high in usage. This is fine.+ let max_color_attachment_bytes_per_sample =+ max_color_attachments * wgt::TextureFormat::MAX_TARGET_PIXEL_BYTE_COST;++ let mut max_ray_dispatch_count = 0;+ let mut max_ray_recursion_depth = 0;++ if let Some(properties) = self.ray_tracing_pipeline {+ max_ray_dispatch_count = properties.max_ray_dispatch_invocation_count;+ max_ray_recursion_depth = properties.max_ray_recursion_depth;+ }++ let max_multiview_view_count = self+ .multiview+ .map(|a| a.max_multiview_view_count.min(32))+ .unwrap_or(0);++ crate::auxil::adjust_raw_limits(wgt::Limits {+ //+ // WebGPU LIMITS:+ // Based on https://gpuweb.github.io/gpuweb/correspondence/#limits+ //+ max_texture_dimension_1d: limits.max_image_dimension1_d,+ max_texture_dimension_2d: limits+ .max_image_dimension2_d+ .min(limits.max_image_dimension_cube)+ .min(limits.max_framebuffer_width)+ .min(limits.max_framebuffer_height),+ max_texture_dimension_3d: limits.max_image_dimension3_d,+ max_texture_array_layers: limits.max_image_array_layers,+ max_bind_groups: limits.max_bound_descriptor_sets,+ // No limit.+ max_bind_groups_plus_vertex_buffers: u32::MAX,+ max_bindings_per_bind_group,+ max_dynamic_uniform_buffers_per_pipeline_layout: limits+ .max_descriptor_set_uniform_buffers_dynamic,+ max_dynamic_storage_buffers_per_pipeline_layout: limits+ .max_descriptor_set_storage_buffers_dynamic,+ max_samplers_per_shader_stage,+ max_sampled_textures_per_shader_stage,+ max_storage_textures_per_shader_stage,+ max_storage_buffers_per_shader_stage,+ max_uniform_buffers_per_shader_stage,+ max_vertex_buffers: limits.max_vertex_input_bindings,+ max_buffer_size,+ max_uniform_buffer_binding_size: limits+ .max_uniform_buffer_range+ .min(crate::auxil::MAX_I32_BINDING_SIZE)+ .into(),+ max_storage_buffer_binding_size: limits+ .max_storage_buffer_range+ .min(crate::auxil::MAX_I32_BINDING_SIZE)+ .into(),+ min_uniform_buffer_offset_alignment: limits.min_uniform_buffer_offset_alignment as u32,+ min_storage_buffer_offset_alignment: limits.min_storage_buffer_offset_alignment as u32,+ max_vertex_attributes: limits.max_vertex_input_attributes,+ max_vertex_buffer_array_stride: limits.max_vertex_input_binding_stride,+ max_inter_stage_shader_variables: limits+ .max_vertex_output_components+ .min(limits.max_fragment_input_components)+ / 4+ - 1, // -1 for position+ max_color_attachments,+ max_color_attachment_bytes_per_sample,+ max_compute_workgroup_storage_size: limits.max_compute_shared_memory_size,+ max_compute_invocations_per_workgroup: limits.max_compute_work_group_invocations,+ max_compute_workgroup_size_x: limits.max_compute_work_group_size[0],+ max_compute_workgroup_size_y: limits.max_compute_work_group_size[1],+ max_compute_workgroup_size_z: limits.max_compute_work_group_size[2],+ max_compute_workgroups_per_dimension: limits.max_compute_work_group_count[0]+ .min(limits.max_compute_work_group_count[1])+ .min(limits.max_compute_work_group_count[2]),+ max_immediate_size: limits.max_push_constants_size,+ //+ // NATIVE (Non-WebGPU) LIMITS:+ //+ max_non_sampler_bindings: u32::MAX,++ max_binding_array_elements_per_shader_stage: max_binding_array_elements,+ max_binding_array_sampler_elements_per_shader_stage: max_sampler_binding_array_elements,+ max_binding_array_acceleration_structure_elements_per_shader_stage: if self+ .descriptor_indexing+ .is_some()+ {+ max_acceleration_structures_per_shader_stage+ } else {+ 0+ },++ max_task_workgroup_total_count,+ max_task_workgroups_per_dimension,+ max_mesh_workgroup_total_count,+ max_mesh_workgroups_per_dimension,++ max_task_invocations_per_workgroup,+ max_task_invocations_per_dimension,++ max_mesh_invocations_per_workgroup,+ max_mesh_invocations_per_dimension,++ max_task_payload_size,+ max_mesh_output_vertices,+ max_mesh_output_primitives,+ max_mesh_output_layers,+ max_mesh_multiview_view_count,++ max_blas_primitive_count,+ max_blas_geometry_count,+ max_tlas_instance_count,+ max_acceleration_structures_per_shader_stage,+ max_buffers_and_acceleration_structures_per_shader_stage: u32::MAX,++ max_multiview_view_count,++ max_ray_dispatch_count,+ max_ray_recursion_depth,+ })+ }++ /// Return a `wgpu_hal::Alignments` structure describing this adapter.+ ///+ /// The `using_robustness2` argument says how this adapter will implement+ /// `wgpu_hal`'s guarantee that shaders can only read the [accessible+ /// region][ar] of bindgroup's buffer bindings:+ ///+ /// - If this adapter will depend on `VK_EXT_robustness2`'s+ /// `robustBufferAccess2` feature to apply bounds checks to shader buffer+ /// access, `using_robustness2` must be `true`.+ ///+ /// - Otherwise, this adapter must use Naga to inject bounds checks on+ /// buffer accesses, and `using_robustness2` must be `false`.+ ///+ /// [ar]: ../../struct.BufferBinding.html#accessible-region+ fn to_hal_alignments(&self, using_robustness2: bool) -> crate::Alignments {+ let limits = &self.properties.limits;+ crate::Alignments {+ buffer_copy_offset: wgt::BufferSize::new(limits.optimal_buffer_copy_offset_alignment)+ .unwrap(),+ buffer_copy_pitch: wgt::BufferSize::new(limits.optimal_buffer_copy_row_pitch_alignment)+ .unwrap(),+ uniform_bounds_check_alignment: {+ let alignment = if using_robustness2 {+ self.robustness2+ .unwrap() // if we're using it, we should have its properties+ .robust_uniform_buffer_access_size_alignment+ } else {+ // If the `robustness2` properties are unavailable, then `robustness2` is not available either Naga-injected bounds checks are precise.+ 1+ };+ wgt::BufferSize::new(alignment).unwrap()+ },+ raw_tlas_instance_size: 64,+ ray_tracing_scratch_buffer_alignment: self.acceleration_structure.map_or(+ 0,+ |acceleration_structure| {+ acceleration_structure.min_acceleration_structure_scratch_offset_alignment+ },+ ),+ ray_tracing_pipeline_group_data_size: self+ .ray_tracing_pipeline+ .map_or(0, |ray_tracing_pipeline| {+ ray_tracing_pipeline.shader_group_handle_size+ }),+ ray_tracing_pipeline_group_data_alignment: self+ .ray_tracing_pipeline+ .map_or(0, |ray_tracing_pipeline| {+ ray_tracing_pipeline.shader_group_handle_alignment+ }),+ ray_tracing_pipeline_data_offset_alignment: self+ .ray_tracing_pipeline+ .map_or(0, |ray_tracing_pipeline| {+ ray_tracing_pipeline.shader_group_base_alignment+ }),+ }+ }+}++impl super::InstanceShared {+ fn inspect(+ &self,+ phd: vk::PhysicalDevice,+ ) -> (PhysicalDeviceProperties, PhysicalDeviceFeatures) {+ let capabilities = {+ let mut capabilities = PhysicalDeviceProperties::default();+ capabilities.supported_extensions =+ unsafe { self.raw.enumerate_device_extension_properties(phd).unwrap() };+ capabilities.properties = unsafe { self.raw.get_physical_device_properties(phd) };+ capabilities.device_api_version = capabilities.properties.api_version;++ let supports_multiview = capabilities.device_api_version >= vk::API_VERSION_1_1+ || capabilities.supports_extension(khr::multiview::NAME);++ if let Some(ref get_device_properties) = self.get_physical_device_properties {+ // Get these now to avoid borrowing conflicts later+ let supports_maintenance3 = capabilities.device_api_version >= vk::API_VERSION_1_1+ || capabilities.supports_extension(khr::maintenance3::NAME);+ let supports_maintenance4 = capabilities.device_api_version >= vk::API_VERSION_1_3+ || capabilities.supports_extension(khr::maintenance4::NAME);+ let supports_descriptor_indexing = capabilities.device_api_version+ >= vk::API_VERSION_1_2+ || capabilities.supports_extension(ext::descriptor_indexing::NAME);+ let supports_driver_properties = capabilities.device_api_version+ >= vk::API_VERSION_1_2+ || capabilities.supports_extension(khr::driver_properties::NAME);+ let supports_subgroup_size_control = capabilities.device_api_version+ >= vk::API_VERSION_1_3+ || capabilities.supports_extension(ext::subgroup_size_control::NAME);+ let supports_robustness2 = capabilities.supports_extension(ext::robustness2::NAME);+ let supports_pci_bus_info =+ capabilities.supports_extension(ext::pci_bus_info::NAME);++ let supports_acceleration_structure =+ capabilities.supports_extension(khr::acceleration_structure::NAME);++ let supports_ray_tracing_pipeline =+ capabilities.supports_extension(khr::ray_tracing_pipeline::NAME);++ let supports_mesh_shader = capabilities.supports_extension(ext::mesh_shader::NAME);++ let mut properties2 = vk::PhysicalDeviceProperties2KHR::default();+ if supports_maintenance3 {+ let next = capabilities+ .maintenance_3+ .insert(vk::PhysicalDeviceMaintenance3Properties::default());+ properties2 = properties2.push_next(next);+ }++ if supports_maintenance4 {+ let next = capabilities+ .maintenance_4+ .insert(vk::PhysicalDeviceMaintenance4Properties::default());+ properties2 = properties2.push_next(next);+ }++ if supports_descriptor_indexing {+ let next = capabilities+ .descriptor_indexing+ .insert(vk::PhysicalDeviceDescriptorIndexingPropertiesEXT::default());+ properties2 = properties2.push_next(next);+ }++ if supports_acceleration_structure {+ let next = capabilities+ .acceleration_structure+ .insert(vk::PhysicalDeviceAccelerationStructurePropertiesKHR::default());+ properties2 = properties2.push_next(next);+ }++ if supports_ray_tracing_pipeline {+ let next = capabilities+ .ray_tracing_pipeline+ .insert(vk::PhysicalDeviceRayTracingPipelinePropertiesKHR::default());+ properties2 = properties2.push_next(next);+ }++ if supports_driver_properties {+ let next = capabilities+ .driver+ .insert(vk::PhysicalDeviceDriverPropertiesKHR::default());+ properties2 = properties2.push_next(next);+ }++ if capabilities.device_api_version >= vk::API_VERSION_1_1 {+ let next = capabilities+ .subgroup+ .insert(vk::PhysicalDeviceSubgroupProperties::default());+ properties2 = properties2.push_next(next);+ }++ if supports_subgroup_size_control {+ let next = capabilities+ .subgroup_size_control+ .insert(vk::PhysicalDeviceSubgroupSizeControlProperties::default());+ properties2 = properties2.push_next(next);+ }++ if supports_robustness2 {+ let next = capabilities+ .robustness2+ .insert(vk::PhysicalDeviceRobustness2PropertiesEXT::default());+ properties2 = properties2.push_next(next);+ }++ if supports_pci_bus_info {+ let next = capabilities+ .pci_bus_info+ .insert(vk::PhysicalDevicePCIBusInfoPropertiesEXT::default());+ properties2 = properties2.push_next(next);+ }++ if supports_mesh_shader {+ let next = capabilities+ .mesh_shader+ .insert(vk::PhysicalDeviceMeshShaderPropertiesEXT::default());+ properties2 = properties2.push_next(next);+ }++ if supports_multiview {+ let next = capabilities+ .multiview+ .insert(vk::PhysicalDeviceMultiviewProperties::default());+ properties2 = properties2.push_next(next);+ }++ unsafe {+ get_device_properties.get_physical_device_properties2(phd, &mut properties2)+ };++ // Query cooperative matrix properties+ if capabilities.supports_extension(khr::cooperative_matrix::NAME) {+ let coop_matrix =+ khr::cooperative_matrix::Instance::new(&self.entry, &self.raw);+ capabilities.cooperative_matrix_properties =+ query_cooperative_matrix_properties(&coop_matrix, phd);+ }++ // Suppress some capabilities to avoid known problems+ if is_intel_igpu_outdated_for_robustness2(&capabilities) {+ capabilities+ .supported_extensions+ .retain(|&x| x.extension_name_as_c_str() != Ok(ext::robustness2::NAME));+ capabilities.robustness2 = None;+ }++ // Due to https://gitlab.freedesktop.org/mesa/mesa/-/work_items/15725+ // TODO(https://github.com/gfx-rs/wgpu/issues/9742): enable on+ // fixed driver versions, when available+ if capabilities.is_driver(vk::DriverId::MESA_RADV) {+ capabilities+ .supported_extensions+ .retain(|&x| x.extension_name_as_c_str() != Ok(ext::memory_budget::NAME));+ }+ };+ capabilities+ };++ let mut features = PhysicalDeviceFeatures::default();+ features.core = if let Some(ref get_device_properties) = self.get_physical_device_properties+ {+ let core = vk::PhysicalDeviceFeatures::default();+ let mut features2 = vk::PhysicalDeviceFeatures2KHR::default().features(core);++ // `VK_KHR_multiview` is promoted to 1.1+ if capabilities.device_api_version >= vk::API_VERSION_1_1+ || capabilities.supports_extension(khr::multiview::NAME)+ {+ let next = features+ .multiview+ .insert(vk::PhysicalDeviceMultiviewFeatures::default());+ features2 = features2.push_next(next);+ }++ // `VK_KHR_sampler_ycbcr_conversion` is promoted to 1.1+ if capabilities.device_api_version >= vk::API_VERSION_1_1+ || capabilities.supports_extension(khr::sampler_ycbcr_conversion::NAME)+ {+ let next = features+ .sampler_ycbcr_conversion+ .insert(vk::PhysicalDeviceSamplerYcbcrConversionFeatures::default());+ features2 = features2.push_next(next);+ }++ if capabilities.supports_extension(ext::descriptor_indexing::NAME) {+ let next = features+ .descriptor_indexing+ .insert(vk::PhysicalDeviceDescriptorIndexingFeaturesEXT::default());+ features2 = features2.push_next(next);+ }++ // `VK_KHR_timeline_semaphore` is promoted to 1.2, but has no+ // changes, so we can keep using the extension unconditionally.+ if capabilities.supports_extension(khr::timeline_semaphore::NAME) {+ let next = features+ .timeline_semaphore+ .insert(vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR::default());+ features2 = features2.push_next(next);+ }++ // `VK_KHR_shader_atomic_int64` is promoted to 1.2, but has no+ // changes, so we can keep using the extension unconditionally.+ if capabilities.device_api_version >= vk::API_VERSION_1_2+ || capabilities.supports_extension(khr::shader_atomic_int64::NAME)+ {+ let next = features+ .shader_atomic_int64+ .insert(vk::PhysicalDeviceShaderAtomicInt64Features::default());+ features2 = features2.push_next(next);+ }++ if capabilities.supports_extension(ext::shader_image_atomic_int64::NAME) {+ let next = features+ .shader_image_atomic_int64+ .insert(vk::PhysicalDeviceShaderImageAtomicInt64FeaturesEXT::default());+ features2 = features2.push_next(next);+ }+ if capabilities.supports_extension(ext::shader_atomic_float::NAME) {+ let next = features+ .shader_atomic_float+ .insert(vk::PhysicalDeviceShaderAtomicFloatFeaturesEXT::default());+ features2 = features2.push_next(next);+ }+ if capabilities.supports_extension(ext::image_robustness::NAME) {+ let next = features+ .image_robustness+ .insert(vk::PhysicalDeviceImageRobustnessFeaturesEXT::default());+ features2 = features2.push_next(next);+ }+ if capabilities.supports_extension(ext::robustness2::NAME) {+ let next = features+ .robustness2+ .insert(vk::PhysicalDeviceRobustness2FeaturesEXT::default());+ features2 = features2.push_next(next);+ }+ if capabilities.supports_extension(ext::texture_compression_astc_hdr::NAME) {+ let next = features+ .astc_hdr+ .insert(vk::PhysicalDeviceTextureCompressionASTCHDRFeaturesEXT::default());+ features2 = features2.push_next(next);+ }++ // `VK_KHR_shader_float16_int8` is promoted to 1.2+ if capabilities.device_api_version >= vk::API_VERSION_1_2+ || capabilities.supports_extension(khr::shader_float16_int8::NAME)+ {+ let next = features+ .shader_float16_int8+ .insert(vk::PhysicalDeviceShaderFloat16Int8FeaturesKHR::default());+ features2 = features2.push_next(next);+ }++ if capabilities.supports_extension(khr::_16bit_storage::NAME) {+ let next = features+ ._16bit_storage+ .insert(vk::PhysicalDevice16BitStorageFeaturesKHR::default());+ features2 = features2.push_next(next);+ }+ if capabilities.supports_extension(khr::acceleration_structure::NAME) {+ let next = features+ .acceleration_structure+ .insert(vk::PhysicalDeviceAccelerationStructureFeaturesKHR::default());+ features2 = features2.push_next(next);+ }++ if capabilities.supports_extension(khr::ray_tracing_position_fetch::NAME) {+ let next = features+ .position_fetch+ .insert(vk::PhysicalDeviceRayTracingPositionFetchFeaturesKHR::default());+ features2 = features2.push_next(next);+ }++ // `VK_KHR_maintenance4` is promoted to 1.3+ if capabilities.device_api_version >= vk::API_VERSION_1_3+ || capabilities.supports_extension(khr::maintenance4::NAME)+ {+ let next = features+ .maintenance4+ .insert(vk::PhysicalDeviceMaintenance4Features::default());+ features2 = features2.push_next(next);+ }++ // `VK_KHR_zero_initialize_workgroup_memory` is promoted to 1.3+ if capabilities.device_api_version >= vk::API_VERSION_1_3+ || capabilities.supports_extension(khr::zero_initialize_workgroup_memory::NAME)+ {+ let next = features+ .zero_initialize_workgroup_memory+ .insert(vk::PhysicalDeviceZeroInitializeWorkgroupMemoryFeatures::default());+ features2 = features2.push_next(next);+ }++ // `VK_EXT_subgroup_size_control` is promoted to 1.3+ if capabilities.device_api_version >= vk::API_VERSION_1_3+ || capabilities.supports_extension(ext::subgroup_size_control::NAME)+ {+ let next = features+ .subgroup_size_control+ .insert(vk::PhysicalDeviceSubgroupSizeControlFeatures::default());+ features2 = features2.push_next(next);+ }++ if capabilities.supports_extension(ext::mesh_shader::NAME) {+ let next = features+ .mesh_shader+ .insert(vk::PhysicalDeviceMeshShaderFeaturesEXT::default());+ features2 = features2.push_next(next);+ }++ // `VK_KHR_shader_integer_dot_product` is promoted to 1.3+ if capabilities.device_api_version >= vk::API_VERSION_1_3+ || capabilities.supports_extension(khr::shader_integer_dot_product::NAME)+ {+ let next = features+ .shader_integer_dot_product+ .insert(vk::PhysicalDeviceShaderIntegerDotProductFeatures::default());+ features2 = features2.push_next(next);+ }++ if capabilities.supports_extension(khr::fragment_shader_barycentric::NAME) {+ let next = features+ .shader_barycentrics+ .insert(vk::PhysicalDeviceFragmentShaderBarycentricFeaturesKHR::default());+ features2 = features2.push_next(next);+ }++ if capabilities.supports_extension(khr::portability_subset::NAME) {+ let next = features+ .portability_subset+ .insert(vk::PhysicalDevicePortabilitySubsetFeaturesKHR::default());+ features2 = features2.push_next(next);+ }++ if capabilities.supports_extension(khr::cooperative_matrix::NAME) {+ let next = features+ .cooperative_matrix+ .insert(vk::PhysicalDeviceCooperativeMatrixFeaturesKHR::default());+ features2 = features2.push_next(next);+ }++ if capabilities.device_api_version >= vk::API_VERSION_1_2+ || capabilities.supports_extension(khr::vulkan_memory_model::NAME)+ {+ let next = features+ .vulkan_memory_model+ .insert(vk::PhysicalDeviceVulkanMemoryModelFeaturesKHR::default());+ features2 = features2.push_next(next);+ }++ if capabilities.device_api_version >= vk::API_VERSION_1_1 {+ let next = features+ .shader_draw_parameters+ .insert(vk::PhysicalDeviceShaderDrawParametersFeatures::default());+ features2 = features2.push_next(next);+ }++ unsafe { get_device_properties.get_physical_device_features2(phd, &mut features2) };+ features2.features+ } else {+ unsafe { self.raw.get_physical_device_features(phd) }+ };++ (capabilities, features)+ }+}++impl super::Instance {+ pub fn expose_adapter(+ &self,+ phd: vk::PhysicalDevice,+ ) -> Option<crate::ExposedAdapter<super::Api>> {+ use crate::auxil::db;++ let (phd_capabilities, phd_features) = self.shared.inspect(phd);++ let mem_properties = {+ profiling::scope!("vkGetPhysicalDeviceMemoryProperties");+ unsafe { self.shared.raw.get_physical_device_memory_properties(phd) }+ };+ let memory_types = &mem_properties.memory_types_as_slice();+ let supports_lazily_allocated = memory_types.iter().any(|mem| {+ mem.property_flags+ .contains(vk::MemoryPropertyFlags::LAZILY_ALLOCATED)+ });++ let device_type = match phd_capabilities.properties.device_type {+ vk::PhysicalDeviceType::OTHER => wgt::DeviceType::Other,+ vk::PhysicalDeviceType::INTEGRATED_GPU => wgt::DeviceType::IntegratedGpu,+ vk::PhysicalDeviceType::DISCRETE_GPU => wgt::DeviceType::DiscreteGpu,+ vk::PhysicalDeviceType::VIRTUAL_GPU => wgt::DeviceType::VirtualGpu,+ vk::PhysicalDeviceType::CPU => wgt::DeviceType::Cpu,+ _ => wgt::DeviceType::Other,+ };+ let info = wgt::AdapterInfo {+ name: {+ phd_capabilities+ .properties+ .device_name_as_c_str()+ .ok()+ .and_then(|name| name.to_str().ok())+ .unwrap_or("?")+ .to_owned()+ },+ vendor: phd_capabilities.properties.vendor_id,+ device: phd_capabilities.properties.device_id,+ device_pci_bus_id: phd_capabilities+ .pci_bus_info+ .filter(|info| info.pci_bus != 0 || info.pci_device != 0)+ .map(|info| {+ format!(+ "{:04x}:{:02x}:{:02x}.{}",+ info.pci_domain, info.pci_bus, info.pci_device, info.pci_function+ )+ })+ .unwrap_or_default(),+ driver: {+ phd_capabilities+ .driver+ .as_ref()+ .and_then(|driver| driver.driver_name_as_c_str().ok())+ .and_then(|name| name.to_str().ok())+ .unwrap_or("?")+ .to_owned()+ },+ driver_info: {+ phd_capabilities+ .driver+ .as_ref()+ .and_then(|driver| driver.driver_info_as_c_str().ok())+ .and_then(|name| name.to_str().ok())+ .unwrap_or("?")+ .to_owned()+ },+ subgroup_min_size: phd_capabilities+ .subgroup_size_control+ .map(|subgroup_size| subgroup_size.min_subgroup_size)+ .unwrap_or(wgt::MINIMUM_SUBGROUP_MIN_SIZE),+ subgroup_max_size: phd_capabilities+ .subgroup_size_control+ .map(|subgroup_size| subgroup_size.max_subgroup_size)+ .unwrap_or(wgt::MAXIMUM_SUBGROUP_MAX_SIZE),+ transient_saves_memory: Some(supports_lazily_allocated),+ ..wgt::AdapterInfo::new(device_type, wgt::Backend::Vulkan)+ };+ let mut workarounds = super::Workarounds::empty();+ {+ // TODO: only enable for particular devices+ workarounds |= super::Workarounds::SEPARATE_ENTRY_POINTS;+ workarounds.set(+ super::Workarounds::EMPTY_RESOLVE_ATTACHMENT_LISTS,+ phd_capabilities.properties.vendor_id == db::qualcomm::VENDOR,+ );+ workarounds.set(+ super::Workarounds::FORCE_FILL_BUFFER_WITH_SIZE_GREATER_4096_ALIGNED_OFFSET_16,+ phd_capabilities.properties.vendor_id == db::nvidia::VENDOR,+ );+ };++ if let Some(driver) = phd_capabilities.driver {+ if driver.conformance_version.major == 0 {+ if driver.driver_id == vk::DriverId::MOLTENVK {+ log::debug!("Adapter is not Vulkan compliant, but is MoltenVK, continuing");+ } else if self+ .shared+ .flags+ .contains(wgt::InstanceFlags::ALLOW_UNDERLYING_NONCOMPLIANT_ADAPTER)+ {+ log::debug!("Adapter is not Vulkan compliant: {}", info.name);+ } else {+ log::debug!(+ "Adapter is not Vulkan compliant, hiding adapter: {}",+ info.name+ );+ return None;+ }+ }+ }+ if phd_capabilities.device_api_version == vk::API_VERSION_1_0+ && !phd_capabilities.supports_extension(khr::storage_buffer_storage_class::NAME)+ {+ log::debug!(+ "SPIR-V storage buffer class is not supported, hiding adapter: {}",+ info.name+ );+ return None;+ }+ if !phd_capabilities.supports_extension(khr::maintenance1::NAME)+ && phd_capabilities.device_api_version < vk::API_VERSION_1_1+ {+ log::debug!(+ "VK_KHR_maintenance1 is not supported, hiding adapter: {}",+ info.name+ );+ return None;+ }++ let queue_families = unsafe {+ self.shared+ .raw+ .get_physical_device_queue_family_properties(phd)+ };+ let queue_family_properties = queue_families.first()?;+ let queue_flags = queue_family_properties.queue_flags;+ if !queue_flags.contains(vk::QueueFlags::GRAPHICS) {+ log::debug!("The first queue only exposes {queue_flags:?}");+ return None;+ }++ let (available_features, mut downlevel_flags) = phd_features.to_wgpu(+ &self.shared.raw,+ phd,+ &phd_capabilities,+ queue_family_properties,+ );++ if phd_capabilities.is_driver(vk::DriverId::MESA_LLVMPIPE) {+ // The `F16_IN_F32` instructions do not normally require native `F16` support, but on+ // llvmpipe, they do.+ downlevel_flags.set(+ wgt::DownlevelFlags::SHADER_F16_IN_F32,+ available_features.contains(wgt::Features::SHADER_F16),+ );+ }++ downlevel_flags.set(+ wgt::DownlevelFlags::TEXTURE_COMPRESSION,+ available_features.contains(wgt::Features::TEXTURE_COMPRESSION_BC)+ || available_features.contains(+ wgt::Features::TEXTURE_COMPRESSION_ETC2+ | wgt::Features::TEXTURE_COMPRESSION_ASTC,+ ),+ );++ let has_robust_buffer_access2 = phd_features+ .robustness2+ .as_ref()+ .map(|r| r.robust_buffer_access2 == 1)+ .unwrap_or_default();++ let alignments = phd_capabilities.to_hal_alignments(has_robust_buffer_access2);++ let private_caps = super::PrivateCapabilities {+ image_view_usage: phd_capabilities.device_api_version >= vk::API_VERSION_1_1+ || phd_capabilities.supports_extension(khr::maintenance2::NAME),+ timeline_semaphores: match phd_features.timeline_semaphore {+ Some(features) => features.timeline_semaphore == vk::TRUE,+ None => phd_features+ .timeline_semaphore+ .is_some_and(|ext| ext.timeline_semaphore != 0),+ },+ texture_d24: supports_format(+ &self.shared.raw,+ phd,+ vk::Format::X8_D24_UNORM_PACK32,+ vk::ImageTiling::OPTIMAL,+ depth_stencil_required_flags(),+ ),+ texture_d24_s8: supports_format(+ &self.shared.raw,+ phd,+ vk::Format::D24_UNORM_S8_UINT,+ vk::ImageTiling::OPTIMAL,+ depth_stencil_required_flags(),+ ),+ texture_s8: supports_format(+ &self.shared.raw,+ phd,+ vk::Format::S8_UINT,+ vk::ImageTiling::OPTIMAL,+ depth_stencil_required_flags(),+ ),+ multi_draw_indirect: phd_features.core.multi_draw_indirect != 0,+ max_draw_indirect_count: phd_capabilities.properties.limits.max_draw_indirect_count,+ non_coherent_map_mask: phd_capabilities.properties.limits.non_coherent_atom_size - 1,+ can_present: true,+ //TODO: make configurable+ robust_buffer_access: phd_features.core.robust_buffer_access != 0,+ robust_image_access: match phd_features.robustness2 {+ Some(ref f) => f.robust_image_access2 != 0,+ None => phd_features+ .image_robustness+ .is_some_and(|ext| ext.robust_image_access != 0),+ },+ robust_buffer_access2: has_robust_buffer_access2,+ robust_image_access2: phd_features+ .robustness2+ .as_ref()+ .map(|r| r.robust_image_access2 == 1)+ .unwrap_or_default(),+ zero_initialize_workgroup_memory: phd_features+ .zero_initialize_workgroup_memory+ .is_some_and(|ext| ext.shader_zero_initialize_workgroup_memory == vk::TRUE),+ image_format_list: phd_capabilities.device_api_version >= vk::API_VERSION_1_2+ || phd_capabilities.supports_extension(khr::image_format_list::NAME),+ maximum_samplers: phd_capabilities+ .properties+ .limits+ .max_sampler_allocation_count,+ shader_integer_dot_product: phd_features+ .shader_integer_dot_product+ .is_some_and(|ext| ext.shader_integer_dot_product != 0),+ shader_int8: phd_features+ .shader_float16_int8+ .is_some_and(|features| features.shader_int8 != 0),+ multiview_instance_index_limit: phd_capabilities+ .multiview+ .map(|a| a.max_multiview_instance_index)+ .unwrap_or(0),+ scratch_buffer_alignment: alignments.ray_tracing_scratch_buffer_alignment,+ ray_tracing_pipeline_group_data_size: alignments.ray_tracing_pipeline_group_data_size,+ };+ let capabilities = crate::Capabilities {+ limits: phd_capabilities.to_wgpu_limits(),+ alignments,+ downlevel: wgt::DownlevelCapabilities {+ flags: downlevel_flags,+ limits: wgt::DownlevelLimits {},+ shader_model: wgt::ShaderModel::Sm5, //TODO?+ },+ cooperative_matrix_properties: phd_capabilities.cooperative_matrix_properties.clone(),+ };++ let adapter = super::Adapter {+ raw: phd,+ instance: Arc::clone(&self.shared),+ //queue_families,+ known_memory_flags: vk::MemoryPropertyFlags::DEVICE_LOCAL+ | vk::MemoryPropertyFlags::HOST_VISIBLE+ | vk::MemoryPropertyFlags::HOST_COHERENT+ | vk::MemoryPropertyFlags::HOST_CACHED+ | vk::MemoryPropertyFlags::LAZILY_ALLOCATED,+ phd_capabilities,+ phd_features,+ downlevel_flags,+ private_caps,+ workarounds,+ };++ Some(crate::ExposedAdapter {+ adapter,+ info,+ features: available_features,+ capabilities,+ })+ }+}++impl super::Adapter {+ pub fn raw_physical_device(&self) -> vk::PhysicalDevice {+ self.raw+ }++ pub fn get_physical_device_features(&self) -> &PhysicalDeviceFeatures {+ &self.phd_features+ }++ pub fn physical_device_capabilities(&self) -> &PhysicalDeviceProperties {+ &self.phd_capabilities+ }++ pub fn shared_instance(&self) -> &super::InstanceShared {+ &self.instance+ }++ pub fn required_device_extensions(&self, features: wgt::Features) -> Vec<&'static CStr> {+ let (supported_extensions, unsupported_extensions) = self+ .phd_capabilities+ .get_required_extensions(features)+ .iter()+ .partition::<Vec<&CStr>, _>(|&&extension| {+ self.phd_capabilities.supports_extension(extension)+ });++ if !unsupported_extensions.is_empty() {+ log::debug!("Missing extensions: {unsupported_extensions:?}");+ }++ log::debug!("Supported extensions: {supported_extensions:?}");+ supported_extensions+ }++ /// Create a `PhysicalDeviceFeatures` for opening a logical device with+ /// `features` from this adapter.+ ///+ /// The given `enabled_extensions` set must include all the extensions+ /// selected by [`required_device_extensions`] when passed `features`.+ /// Otherwise, the `PhysicalDeviceFeatures` value may not be able to select+ /// all the Vulkan features needed to represent `features` and this+ /// adapter's characteristics.+ ///+ /// Typically, you'd simply call `required_device_extensions`, and then pass+ /// its return value and the feature set you gave it directly to this+ /// function. But it's fine to add more extensions to the list.+ ///+ /// [`required_device_extensions`]: Self::required_device_extensions+ pub fn physical_device_features(+ &self,+ enabled_extensions: &[&'static CStr],+ features: wgt::Features,+ ) -> PhysicalDeviceFeatures {+ PhysicalDeviceFeatures::from_extensions_and_requested_features(+ &self.phd_capabilities,+ &self.phd_features,+ enabled_extensions,+ features,+ self.downlevel_flags,+ &self.private_caps,+ )+ }++ /// # Safety+ ///+ /// - `raw_device` must be created from this adapter.+ /// - `raw_device` must be created using `family_index`, `enabled_extensions` and `physical_device_features()`+ /// - `enabled_extensions` must be a superset of `required_device_extensions()`.+ /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of `raw_device`. If+ /// `drop_callback` is [`Some`], `raw_device` must be valid until the callback is called.+ #[allow(clippy::too_many_arguments)]+ pub unsafe fn device_from_raw(+ &self,+ raw_device: ash::Device,+ drop_callback: Option<crate::DropCallback>,+ enabled_extensions: &[&'static CStr],+ features: wgt::Features,+ limits: &wgt::Limits,+ memory_hints: &wgt::MemoryHints,+ family_index: u32,+ queue_index: u32,+ ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> {+ let mem_properties = {+ profiling::scope!("vkGetPhysicalDeviceMemoryProperties");+ unsafe {+ self.instance+ .raw+ .get_physical_device_memory_properties(self.raw)+ }+ };+ let memory_types = &mem_properties.memory_types_as_slice();+ let valid_ash_memory_types = memory_types.iter().enumerate().fold(0, |u, (i, mem)| {+ if self.known_memory_flags.contains(mem.property_flags) {+ u | (1 << i)+ } else {+ u+ }+ });++ // Note that VK_EXT_debug_utils is an instance extension (enabled at the instance+ // level) but contains a few functions that can be loaded directly on the Device for a+ // dispatch-table-less pointer.+ let debug_utils_fn = if self.instance.extensions.contains(&ext::debug_utils::NAME) {+ Some(ext::debug_utils::Device::new(+ &self.instance.raw,+ &raw_device,+ ))+ } else {+ None+ };+ let indirect_count_fn = if enabled_extensions.contains(&khr::draw_indirect_count::NAME) {+ Some(khr::draw_indirect_count::Device::new(+ &self.instance.raw,+ &raw_device,+ ))+ } else {+ None+ };+ let timeline_semaphore_fn = if enabled_extensions.contains(&khr::timeline_semaphore::NAME) {+ Some(super::ExtensionFn::Extension(+ khr::timeline_semaphore::Device::new(&self.instance.raw, &raw_device),+ ))+ } else if self.phd_capabilities.device_api_version >= vk::API_VERSION_1_2 {+ Some(super::ExtensionFn::Promoted)+ } else {+ None+ };+ let ray_tracing_fns = if enabled_extensions.contains(&khr::acceleration_structure::NAME)+ && enabled_extensions.contains(&khr::buffer_device_address::NAME)+ {+ Some(super::RayTracingDeviceExtensionFunctions {+ acceleration_structure: khr::acceleration_structure::Device::new(+ &self.instance.raw,+ &raw_device,+ ),+ buffer_device_address: khr::buffer_device_address::Device::new(+ &self.instance.raw,+ &raw_device,+ ),+ })+ } else {+ None+ };+ let ray_tracing_pipeline_fns =+ if enabled_extensions.contains(&khr::ray_tracing_pipeline::NAME) {+ Some(khr::ray_tracing_pipeline::Device::new(+ &self.instance.raw,+ &raw_device,+ ))+ } else {+ None+ };+ let mesh_shading_fns = if enabled_extensions.contains(&ext::mesh_shader::NAME) {+ Some(ext::mesh_shader::Device::new(+ &self.instance.raw,+ &raw_device,+ ))+ } else {+ None+ };+ let external_memory_fd_fn = if enabled_extensions.contains(&khr::external_memory_fd::NAME) {+ Some(khr::external_memory_fd::Device::new(+ &self.instance.raw,+ &raw_device,+ ))+ } else {+ None+ };++ let naga_options = {+ use naga::back::spv;++ // The following capabilities are always available+ // see https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap52.html#spirvenv-capabilities+ let mut capabilities = vec![+ spv::Capability::Shader,+ spv::Capability::Matrix,+ spv::Capability::Sampled1D,+ spv::Capability::Image1D,+ spv::Capability::ImageQuery,+ spv::Capability::DerivativeControl,+ spv::Capability::StorageImageExtendedFormats,+ ];++ if self+ .downlevel_flags+ .contains(wgt::DownlevelFlags::CUBE_ARRAY_TEXTURES)+ {+ capabilities.push(spv::Capability::SampledCubeArray);+ }++ if self+ .downlevel_flags+ .contains(wgt::DownlevelFlags::MULTISAMPLED_SHADING)+ {+ capabilities.push(spv::Capability::SampleRateShading);+ }++ if features.contains(wgt::Features::MULTIVIEW) {+ capabilities.push(spv::Capability::MultiView);+ }++ if features.contains(wgt::Features::PRIMITIVE_INDEX) {+ capabilities.push(spv::Capability::Geometry);+ }++ if features.intersects(wgt::Features::SUBGROUP | wgt::Features::SUBGROUP_VERTEX) {+ capabilities.push(spv::Capability::GroupNonUniform);+ capabilities.push(spv::Capability::GroupNonUniformVote);+ capabilities.push(spv::Capability::GroupNonUniformArithmetic);+ capabilities.push(spv::Capability::GroupNonUniformBallot);+ capabilities.push(spv::Capability::GroupNonUniformShuffle);+ capabilities.push(spv::Capability::GroupNonUniformShuffleRelative);+ capabilities.push(spv::Capability::GroupNonUniformQuad);+ }++ if features.intersects(+ wgt::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING+ | wgt::Features::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING+ | wgt::Features::UNIFORM_BUFFER_BINDING_ARRAYS,+ ) {+ capabilities.push(spv::Capability::ShaderNonUniform);+ }+ if features.contains(wgt::Features::BGRA8UNORM_STORAGE) {+ capabilities.push(spv::Capability::StorageImageWriteWithoutFormat);+ }++ if features.contains(wgt::Features::EXPERIMENTAL_RAY_QUERY) {+ capabilities.push(spv::Capability::RayQueryKHR);+ }++ if features.contains(wgt::Features::SHADER_INT64) {+ capabilities.push(spv::Capability::Int64);+ }++ if features.contains(wgt::Features::SHADER_F16) {+ capabilities.push(spv::Capability::Float16);+ }++ if features.contains(wgt::Features::SHADER_I16) {+ capabilities.push(spv::Capability::Int16);+ }++ if features.intersects(+ wgt::Features::SHADER_INT64_ATOMIC_ALL_OPS+ | wgt::Features::SHADER_INT64_ATOMIC_MIN_MAX+ | wgt::Features::TEXTURE_INT64_ATOMIC,+ ) {+ capabilities.push(spv::Capability::Int64Atomics);+ }++ if features.intersects(wgt::Features::TEXTURE_INT64_ATOMIC) {+ capabilities.push(spv::Capability::Int64ImageEXT);+ }++ if features.contains(wgt::Features::SHADER_FLOAT32_ATOMIC) {+ capabilities.push(spv::Capability::AtomicFloat32AddEXT);+ }++ if features.contains(wgt::Features::CLIP_DISTANCES) {+ capabilities.push(spv::Capability::ClipDistance);+ }++ // Vulkan bundles both barycentrics and per-vertex attributes under the same feature.+ if features+ .intersects(wgt::Features::SHADER_BARYCENTRICS | wgt::Features::SHADER_PER_VERTEX)+ {+ capabilities.push(spv::Capability::FragmentBarycentricKHR);+ }++ if features.contains(wgt::Features::SHADER_DRAW_INDEX) {+ capabilities.push(spv::Capability::DrawParameters);+ }++ let mut flags = spv::WriterFlags::empty();+ flags.set(+ spv::WriterFlags::DEBUG,+ self.instance.flags.contains(wgt::InstanceFlags::DEBUG),+ );+ flags.set(+ spv::WriterFlags::LABEL_VARYINGS,+ self.phd_capabilities.properties.vendor_id != crate::auxil::db::qualcomm::VENDOR,+ );+ flags.set(+ spv::WriterFlags::FORCE_POINT_SIZE,+ //Note: we could technically disable this when we are compiling separate entry points,+ // and we know exactly that the primitive topology is not `PointList`.+ // But this requires cloning the `spv::Options` struct, which has heap allocations.+ true, // could check `super::Workarounds::SEPARATE_ENTRY_POINTS`+ );+ flags.set(+ spv::WriterFlags::PRINT_ON_RAY_QUERY_INITIALIZATION_FAIL+ | spv::WriterFlags::PRINT_ON_TRACE_RAYS_FAIL,+ self.instance.flags.contains(wgt::InstanceFlags::DEBUG)+ && (self.instance.instance_api_version >= vk::API_VERSION_1_3+ || enabled_extensions.contains(&khr::shader_non_semantic_info::NAME)),+ );+ if features.contains(wgt::Features::EXPERIMENTAL_RAY_QUERY) {+ capabilities.push(spv::Capability::RayQueryKHR);+ }+ if features.contains(wgt::Features::EXPERIMENTAL_RAY_TRACING_PIPELINES) {+ capabilities.push(spv::Capability::RayTracingKHR);+ }+ if features.contains(wgt::Features::EXPERIMENTAL_RAY_HIT_VERTEX_RETURN) {+ capabilities.push(spv::Capability::RayQueryPositionFetchKHR)+ }+ if features.contains(wgt::Features::EXPERIMENTAL_MESH_SHADER) {+ capabilities.push(spv::Capability::MeshShadingEXT);+ }+ if features.contains(wgt::Features::EXPERIMENTAL_COOPERATIVE_MATRIX) {+ capabilities.push(spv::Capability::CooperativeMatrixKHR);+ // TODO: expose this more generally+ capabilities.push(spv::Capability::VulkanMemoryModel);+ }+ if self.private_caps.shader_integer_dot_product {+ // See <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_shader_integer_dot_product.html#_new_spir_v_capabilities>.+ capabilities.extend(&[+ spv::Capability::DotProductInputAllKHR,+ spv::Capability::DotProductInput4x8BitKHR,+ spv::Capability::DotProductInput4x8BitPackedKHR,+ spv::Capability::DotProductKHR,+ ]);+ }+ if self.private_caps.shader_int8 {+ // See <https://registry.khronos.org/vulkan/specs/latest/man/html/VkPhysicalDeviceShaderFloat16Int8Features.html#extension-features-shaderInt8>.+ capabilities.extend(&[spv::Capability::Int8]);+ }+ spv::Options {+ lang_version: match self.phd_capabilities.device_api_version {+ // Use maximum supported SPIR-V version according to+ // <https://github.com/KhronosGroup/Vulkan-Docs/blob/19b7651/appendices/spirvenv.adoc?plain=1#L21-L40>.+ vk::API_VERSION_1_0..vk::API_VERSION_1_1 => (1, 0),+ vk::API_VERSION_1_1..vk::API_VERSION_1_2 => (1, 3),+ vk::API_VERSION_1_2..vk::API_VERSION_1_3 => (1, 5),+ vk::API_VERSION_1_3.. => (1, 6),+ _ => unreachable!(),+ },+ flags,+ capabilities: Some(capabilities.iter().cloned().collect()),+ bounds_check_policies: naga::proc::BoundsCheckPolicies {+ index: naga::proc::BoundsCheckPolicy::Restrict,+ buffer: if self.private_caps.robust_buffer_access2 {+ naga::proc::BoundsCheckPolicy::Unchecked+ } else {+ naga::proc::BoundsCheckPolicy::Restrict+ },+ image_load: if self.private_caps.robust_image_access {+ naga::proc::BoundsCheckPolicy::Unchecked+ } else {+ naga::proc::BoundsCheckPolicy::Restrict+ },+ // TODO: support bounds checks on binding arrays+ binding_array: naga::proc::BoundsCheckPolicy::Unchecked,+ },+ zero_initialize_workgroup_memory: if self+ .private_caps+ .zero_initialize_workgroup_memory+ {+ spv::ZeroInitializeWorkgroupMemoryMode::Native+ } else {+ spv::ZeroInitializeWorkgroupMemoryMode::Polyfill+ },+ force_loop_bounding: true,+ ray_query_initialization_tracking: true,+ use_storage_input_output_16: features.contains(wgt::Features::SHADER_F16)+ && self.phd_features.supports_storage_input_output_16(),+ fake_missing_bindings: false,+ // We need to build this separately for each invocation, so just default it out here+ binding_map: BTreeMap::default(),+ debug_info: None,+ task_dispatch_limits: Some(naga::back::TaskDispatchLimits {+ max_mesh_workgroups_per_dim: limits.max_mesh_workgroups_per_dimension,+ max_mesh_workgroups_total: limits.max_mesh_workgroup_total_count,+ }),+ mesh_shader_primitive_indices_clamp: true,+ trace_ray_argument_validation: true,+ emit_int_div_checks: true,+ }+ };++ let raw_queue = {+ profiling::scope!("vkGetDeviceQueue");+ unsafe { raw_device.get_device_queue(family_index, queue_index) }+ };++ let driver_version = self+ .phd_capabilities+ .properties+ .driver_version+ .to_be_bytes();+ #[rustfmt::skip]+ let pipeline_cache_validation_key = [+ driver_version[0], driver_version[1], driver_version[2], driver_version[3],+ 0, 0, 0, 0,+ 0, 0, 0, 0,+ 0, 0, 0, 0,+ ];++ let drop_guard = crate::DropGuard::from_option(drop_callback);++ let empty_descriptor_set_layout = unsafe {+ raw_device+ .create_descriptor_set_layout(&vk::DescriptorSetLayoutCreateInfo::default(), None)+ .map_err(super::map_host_device_oom_err)?+ };++ let shared = Arc::new(super::DeviceShared {+ raw: raw_device,+ family_index,+ queue_index,+ raw_queue,+ drop_guard,+ instance: Arc::clone(&self.instance),+ physical_device: self.raw,+ enabled_extensions: enabled_extensions.into(),+ extension_fns: super::DeviceExtensionFunctions {+ debug_utils: debug_utils_fn,+ draw_indirect_count: indirect_count_fn,+ timeline_semaphore: timeline_semaphore_fn,+ ray_tracing: ray_tracing_fns,+ ray_tracing_pipelines: ray_tracing_pipeline_fns,+ mesh_shading: mesh_shading_fns,+ external_memory_fd: external_memory_fd_fn,+ },+ pipeline_cache_validation_key,+ vendor_id: self.phd_capabilities.properties.vendor_id,+ timestamp_period: self.phd_capabilities.properties.limits.timestamp_period,+ private_caps: self.private_caps.clone(),+ features,+ workarounds: self.workarounds,+ render_passes: Mutex::new(Default::default()),+ sampler_cache: Mutex::new(super::sampler::SamplerCache::new(+ self.private_caps.maximum_samplers,+ )),+ memory_allocations_counter: Default::default(),++ texture_identity_factory: super::ResourceIdentityFactory::new(),+ texture_view_identity_factory: super::ResourceIdentityFactory::new(),+ empty_descriptor_set_layout,+ });++ let relay_semaphores = super::RelaySemaphores::new(&shared)?;++ let queue = super::Queue {+ raw: raw_queue,+ device: Arc::clone(&shared),+ family_index,+ relay_semaphores: Mutex::new(relay_semaphores),+ signal_semaphores: Mutex::new(SemaphoreList::new(SemaphoreListMode::Signal)),+ wait_semaphores: Mutex::new(SemaphoreList::new(SemaphoreListMode::Wait)),+ };++ let allocation_sizes = AllocationSizes::from_memory_hints(memory_hints).into();++ let buffer_device_address = enabled_extensions.contains(&khr::buffer_device_address::NAME);++ let mem_allocator =+ gpu_allocator::vulkan::Allocator::new(&gpu_allocator::vulkan::AllocatorCreateDesc {+ instance: self.instance.raw.clone(),+ device: shared.raw.clone(),+ physical_device: self.raw,+ debug_settings: Default::default(),+ buffer_device_address,+ allocation_sizes,+ })?;++ let desc_allocator = super::descriptor::DescriptorAllocator::new(+ if let Some(di) = self.phd_capabilities.descriptor_indexing {+ di.max_update_after_bind_descriptors_in_all_pools+ } else {+ 0+ },+ );++ let device = super::Device {+ shared,+ mem_allocator: Mutex::new(mem_allocator),+ desc_allocator: Mutex::new(desc_allocator),+ valid_ash_memory_types,+ naga_options,+ #[cfg(feature = "renderdoc")]+ render_doc: Default::default(),+ counters: Default::default(),+ };++ Ok(crate::OpenDevice { device, queue })+ }++ pub fn texture_format_as_raw(&self, texture_format: wgt::TextureFormat) -> vk::Format {+ self.private_caps.map_texture_format(texture_format)+ }++ /// # Safety:+ /// - Same as `open` plus+ /// - The callback may not change anything that the device does not support.+ /// - The callback may not remove features.+ pub unsafe fn open_with_callback<'a>(+ &self,+ features: wgt::Features,+ limits: &wgt::Limits,+ memory_hints: &wgt::MemoryHints,+ callback: Option<Box<super::CreateDeviceCallback<'a>>>,+ ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> {+ let mut enabled_extensions = self.required_device_extensions(features);+ let mut enabled_phd_features = self.physical_device_features(&enabled_extensions, features);++ let family_index = 0; //TODO+ let family_info = vk::DeviceQueueCreateInfo::default()+ .queue_family_index(family_index)+ .queue_priorities(&[1.0]);+ let mut family_infos = Vec::from([family_info]);++ let mut pre_info = vk::DeviceCreateInfo::default();++ if let Some(callback) = callback {+ callback(super::CreateDeviceCallbackArgs {+ extensions: &mut enabled_extensions,+ device_features: &mut enabled_phd_features,+ queue_create_infos: &mut family_infos,+ create_info: &mut pre_info,+ _phantom: PhantomData,+ })+ }++ let str_pointers = enabled_extensions+ .iter()+ .map(|&s| {+ // Safe because `enabled_extensions` entries have static lifetime.+ s.as_ptr()+ })+ .collect::<Vec<_>>();++ let pre_info = pre_info+ .queue_create_infos(&family_infos)+ .enabled_extension_names(&str_pointers);+ let info = enabled_phd_features.add_to_device_create(pre_info);+ let raw_device = {+ profiling::scope!("vkCreateDevice");+ unsafe {+ self.instance+ .raw+ .create_device(self.raw, &info, None)+ .map_err(map_err)?+ }+ };+ fn map_err(err: vk::Result) -> crate::DeviceError {+ match err {+ vk::Result::ERROR_TOO_MANY_OBJECTS => crate::DeviceError::OutOfMemory,+ vk::Result::ERROR_INITIALIZATION_FAILED => crate::DeviceError::Lost,+ vk::Result::ERROR_EXTENSION_NOT_PRESENT | vk::Result::ERROR_FEATURE_NOT_PRESENT => {+ crate::hal_usage_error(err)+ }+ other => super::map_host_device_oom_and_lost_err(other),+ }+ }++ unsafe {+ self.device_from_raw(+ raw_device,+ None,+ &enabled_extensions,+ features,+ limits,+ memory_hints,+ family_info.queue_family_index,+ 0,+ )+ }+ }+}++impl crate::Adapter for super::Adapter {+ type A = super::Api;++ unsafe fn open(+ &self,+ features: wgt::Features,+ limits: &wgt::Limits,+ memory_hints: &wgt::MemoryHints,+ ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> {+ unsafe { self.open_with_callback(features, limits, memory_hints, None) }+ }++ unsafe fn texture_format_capabilities(+ &self,+ format: wgt::TextureFormat,+ ) -> crate::TextureFormatCapabilities {+ use crate::TextureFormatCapabilities as Tfc;++ let vk_format = self.private_caps.map_texture_format(format);+ let properties = unsafe {+ self.instance+ .raw+ .get_physical_device_format_properties(self.raw, vk_format)+ };+ let features = properties.optimal_tiling_features;++ let mut flags = Tfc::empty();+ flags.set(+ Tfc::SAMPLED,+ features.contains(vk::FormatFeatureFlags::SAMPLED_IMAGE),+ );+ flags.set(+ Tfc::SAMPLED_LINEAR,+ features.contains(vk::FormatFeatureFlags::SAMPLED_IMAGE_FILTER_LINEAR),+ );+ // flags.set(+ // Tfc::SAMPLED_MINMAX,+ // features.contains(vk::FormatFeatureFlags::SAMPLED_IMAGE_FILTER_MINMAX),+ // );+ flags.set(+ Tfc::STORAGE_READ_WRITE+ | Tfc::STORAGE_WRITE_ONLY+ | Tfc::STORAGE_READ_ONLY+ | Tfc::STORAGE_ATOMIC,+ features.contains(vk::FormatFeatureFlags::STORAGE_IMAGE),+ );+ flags.set(+ Tfc::STORAGE_ATOMIC,+ features.contains(vk::FormatFeatureFlags::STORAGE_IMAGE_ATOMIC),+ );+ flags.set(+ Tfc::COLOR_ATTACHMENT,+ features.contains(vk::FormatFeatureFlags::COLOR_ATTACHMENT),+ );+ flags.set(+ Tfc::COLOR_ATTACHMENT_BLEND,+ features.contains(vk::FormatFeatureFlags::COLOR_ATTACHMENT_BLEND),+ );+ flags.set(+ Tfc::DEPTH_STENCIL_ATTACHMENT,+ features.contains(vk::FormatFeatureFlags::DEPTH_STENCIL_ATTACHMENT),+ );+ flags.set(+ Tfc::COPY_SRC,+ features.intersects(vk::FormatFeatureFlags::TRANSFER_SRC),+ );+ flags.set(+ Tfc::COPY_DST,+ features.intersects(vk::FormatFeatureFlags::TRANSFER_DST),+ );+ flags.set(+ Tfc::STORAGE_ATOMIC,+ features.intersects(vk::FormatFeatureFlags::STORAGE_IMAGE_ATOMIC),+ );+ // Vulkan is very permissive about MSAA+ flags.set(Tfc::MULTISAMPLE_RESOLVE, !format.is_compressed());++ // get the supported sample counts+ let format_aspect = crate::FormatAspects::from(format);+ let limits = self.phd_capabilities.properties.limits;++ let sample_flags = if format_aspect.contains(crate::FormatAspects::DEPTH) {+ limits+ .framebuffer_depth_sample_counts+ .min(limits.sampled_image_depth_sample_counts)+ } else if format_aspect.contains(crate::FormatAspects::STENCIL) {+ limits+ .framebuffer_stencil_sample_counts+ .min(limits.sampled_image_stencil_sample_counts)+ } else {+ let first_aspect = format_aspect+ .iter()+ .next()+ .expect("All texture should at least one aspect")+ .map();++ // We should never get depth or stencil out of this, due to the above.+ assert_ne!(first_aspect, wgt::TextureAspect::DepthOnly);+ assert_ne!(first_aspect, wgt::TextureAspect::StencilOnly);++ match format.sample_type(Some(first_aspect), None).unwrap() {+ wgt::TextureSampleType::Float { .. } => limits+ .framebuffer_color_sample_counts+ .min(limits.sampled_image_color_sample_counts),+ wgt::TextureSampleType::Sint | wgt::TextureSampleType::Uint => {+ limits.sampled_image_integer_sample_counts+ }+ _ => unreachable!(),+ }+ };++ flags.set(+ Tfc::MULTISAMPLE_X2,+ sample_flags.contains(vk::SampleCountFlags::TYPE_2),+ );+ flags.set(+ Tfc::MULTISAMPLE_X4,+ sample_flags.contains(vk::SampleCountFlags::TYPE_4),+ );+ flags.set(+ Tfc::MULTISAMPLE_X8,+ sample_flags.contains(vk::SampleCountFlags::TYPE_8),+ );+ flags.set(+ Tfc::MULTISAMPLE_X16,+ sample_flags.contains(vk::SampleCountFlags::TYPE_16),+ );++ flags+ }++ unsafe fn surface_capabilities(+ &self,+ surface: &super::Surface,+ ) -> Option<crate::SurfaceCapabilities> {+ surface.inner.surface_capabilities(self)+ }++ unsafe fn surface_display_hdr_info(+ &self,+ surface: &super::Surface,+ ) -> Option<wgt::DisplayHdrInfo> {+ // Vulkan has no portable luminance query; the Win32 surface reads it+ // through DXGI (see `dxgi::hdr`). Every other surface reports `None`.+ surface.inner.display_hdr_info()+ }++ unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {+ // VK_GOOGLE_display_timing is the only way to get presentation+ // timestamps on vulkan right now and it is only ever available+ // on android and linux. This includes mac, but there's no alternative+ // on mac, so this is fine.+ #[cfg(unix)]+ {+ let mut timespec = libc::timespec::default();+ unsafe {+ libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut timespec);+ }++ wgt::PresentationTimestamp(+ timespec.tv_sec as u128 * 1_000_000_000 + timespec.tv_nsec as u128,+ )+ }+ #[cfg(not(unix))]+ {+ wgt::PresentationTimestamp::INVALID_TIMESTAMP+ }+ }++ fn get_ordered_buffer_usages(&self) -> wgt::BufferUses {+ wgt::BufferUses::INCLUSIVE | wgt::BufferUses::MAP_WRITE+ }++ // Vulkan makes very few execution ordering guarantees+ // see https://registry.khronos.org/vulkan/specs/latest/html/vkspec.html#synchronization-implicit+ // We just don't want to insert barriers between inclusive uses+ // See https://github.com/gfx-rs/wgpu/issues/8853+ fn get_ordered_texture_usages(&self) -> wgt::TextureUses {+ wgt::TextureUses::INCLUSIVE+ }+}++fn is_format_16bit_norm_supported(instance: &ash::Instance, phd: vk::PhysicalDevice) -> bool {+ [+ vk::Format::R16_UNORM,+ vk::Format::R16_SNORM,+ vk::Format::R16G16_UNORM,+ vk::Format::R16G16_SNORM,+ vk::Format::R16G16B16A16_UNORM,+ vk::Format::R16G16B16A16_SNORM,+ ]+ .into_iter()+ .all(|format| {+ supports_format(+ instance,+ phd,+ format,+ vk::ImageTiling::OPTIMAL,+ vk::FormatFeatureFlags::SAMPLED_IMAGE+ | vk::FormatFeatureFlags::STORAGE_IMAGE+ | vk::FormatFeatureFlags::TRANSFER_SRC+ | vk::FormatFeatureFlags::TRANSFER_DST,+ )+ })+}++fn is_float32_filterable_supported(instance: &ash::Instance, phd: vk::PhysicalDevice) -> bool {+ [+ vk::Format::R32_SFLOAT,+ vk::Format::R32G32_SFLOAT,+ vk::Format::R32G32B32A32_SFLOAT,+ ]+ .into_iter()+ .all(|format| {+ supports_format(+ instance,+ phd,+ format,+ vk::ImageTiling::OPTIMAL,+ vk::FormatFeatureFlags::SAMPLED_IMAGE_FILTER_LINEAR,+ )+ })+}++fn is_float32_blendable_supported(instance: &ash::Instance, phd: vk::PhysicalDevice) -> bool {+ [+ vk::Format::R32_SFLOAT,+ vk::Format::R32G32_SFLOAT,+ vk::Format::R32G32B32A32_SFLOAT,+ ]+ .into_iter()+ .all(|format| {+ supports_format(+ instance,+ phd,+ format,+ vk::ImageTiling::OPTIMAL,+ vk::FormatFeatureFlags::COLOR_ATTACHMENT_BLEND,+ )+ })+}++fn supports_format(+ instance: &ash::Instance,+ phd: vk::PhysicalDevice,+ format: vk::Format,+ tiling: vk::ImageTiling,+ features: vk::FormatFeatureFlags,+) -> bool {+ let properties = unsafe { instance.get_physical_device_format_properties(phd, format) };+ match tiling {+ vk::ImageTiling::LINEAR => properties.linear_tiling_features.contains(features),+ vk::ImageTiling::OPTIMAL => properties.optimal_tiling_features.contains(features),+ _ => false,+ }+}++fn supports_astc_3d(instance: &ash::Instance, phd: vk::PhysicalDevice) -> bool {+ [+ vk::Format::ASTC_4X4_UNORM_BLOCK,+ vk::Format::ASTC_4X4_SRGB_BLOCK,+ vk::Format::ASTC_5X4_UNORM_BLOCK,+ vk::Format::ASTC_5X4_SRGB_BLOCK,+ vk::Format::ASTC_5X5_UNORM_BLOCK,+ vk::Format::ASTC_5X5_SRGB_BLOCK,+ vk::Format::ASTC_6X5_UNORM_BLOCK,+ vk::Format::ASTC_6X5_SRGB_BLOCK,+ vk::Format::ASTC_6X6_UNORM_BLOCK,+ vk::Format::ASTC_6X6_SRGB_BLOCK,+ vk::Format::ASTC_8X5_UNORM_BLOCK,+ vk::Format::ASTC_8X5_SRGB_BLOCK,+ vk::Format::ASTC_8X6_UNORM_BLOCK,+ vk::Format::ASTC_8X6_SRGB_BLOCK,+ vk::Format::ASTC_8X8_UNORM_BLOCK,+ vk::Format::ASTC_8X8_SRGB_BLOCK,+ vk::Format::ASTC_10X5_UNORM_BLOCK,+ vk::Format::ASTC_10X5_SRGB_BLOCK,+ vk::Format::ASTC_10X6_UNORM_BLOCK,+ vk::Format::ASTC_10X6_SRGB_BLOCK,+ vk::Format::ASTC_10X8_UNORM_BLOCK,+ vk::Format::ASTC_10X8_SRGB_BLOCK,+ vk::Format::ASTC_10X10_UNORM_BLOCK,+ vk::Format::ASTC_10X10_SRGB_BLOCK,+ vk::Format::ASTC_12X10_UNORM_BLOCK,+ vk::Format::ASTC_12X10_SRGB_BLOCK,+ vk::Format::ASTC_12X12_UNORM_BLOCK,+ vk::Format::ASTC_12X12_SRGB_BLOCK,+ ]+ .into_iter()+ .all(|format| {+ unsafe {+ instance.get_physical_device_image_format_properties(+ phd,+ format,+ vk::ImageType::TYPE_3D,+ vk::ImageTiling::OPTIMAL,+ vk::ImageUsageFlags::SAMPLED,+ vk::ImageCreateFlags::empty(),+ )+ }+ .is_ok()+ })+}++fn supports_bgra8unorm_storage(+ instance: &ash::Instance,+ phd: vk::PhysicalDevice,+ device_api_version: u32,+) -> bool {+ // See https://github.com/KhronosGroup/Vulkan-Docs/issues/2027#issuecomment-1380608011++ // This check gates the function call and structures used below.+ // TODO: check for (`VK_KHR_get_physical_device_properties2` or VK1.1) and (`VK_KHR_format_feature_flags2` or VK1.3).+ // Right now we only check for VK1.3.+ if device_api_version < vk::API_VERSION_1_3 {+ return false;+ }++ unsafe {+ let mut properties3 = vk::FormatProperties3::default();+ let mut properties2 = vk::FormatProperties2::default().push_next(&mut properties3);++ instance.get_physical_device_format_properties2(+ phd,+ vk::Format::B8G8R8A8_UNORM,+ &mut properties2,+ );++ let features2 = properties2.format_properties.optimal_tiling_features;+ let features3 = properties3.optimal_tiling_features;++ features2.contains(vk::FormatFeatureFlags::STORAGE_IMAGE)+ && features3.contains(vk::FormatFeatureFlags2::STORAGE_WRITE_WITHOUT_FORMAT)+ }+}++// For https://github.com/gfx-rs/wgpu/issues/4599+// Intel iGPUs with outdated drivers can break rendering if `VK_EXT_robustness2` is used.+// Driver version 31.0.101.2115 works, but there's probably an earlier functional version.+fn is_intel_igpu_outdated_for_robustness2(capabilities: &PhysicalDeviceProperties) -> bool {+ const DRIVER_VERSION_WORKING: u32 = (101 << 14) | 2115; // X.X.101.2115++ let props = &capabilities.properties;++ let is_outdated = props.vendor_id == crate::auxil::db::intel::VENDOR+ && props.device_type == vk::PhysicalDeviceType::INTEGRATED_GPU+ && props.driver_version < DRIVER_VERSION_WORKING+ && capabilities.is_driver(vk::DriverId::INTEL_PROPRIETARY_WINDOWS);++ if is_outdated {+ log::debug!(+ "Disabling robustBufferAccess2 and robustImageAccess2: IntegratedGpu Intel Driver is outdated. Found with version 0x{:X}, less than the known good version 0x{:X} (31.0.101.2115)",+ props.driver_version,+ DRIVER_VERSION_WORKING+ );+ }+ is_outdated+}++/// Convert Vulkan component type to wgt::CooperativeScalarType.+fn map_vk_component_type(ty: vk::ComponentTypeKHR) -> Option<wgt::CooperativeScalarType> {+ match ty {+ vk::ComponentTypeKHR::FLOAT16 => Some(wgt::CooperativeScalarType::F16),+ vk::ComponentTypeKHR::FLOAT32 => Some(wgt::CooperativeScalarType::F32),+ vk::ComponentTypeKHR::SINT32 => Some(wgt::CooperativeScalarType::I32),+ vk::ComponentTypeKHR::UINT32 => Some(wgt::CooperativeScalarType::U32),+ _ => None,+ }+}++/// Convert Vulkan matrix size.+fn map_vk_cooperative_size(size: u32) -> Option<u32> {+ match size {+ 8 | 16 => Some(size),+ _ => None,+ }+}++/// Query all supported cooperative matrix configurations from Vulkan.+fn query_cooperative_matrix_properties(+ coop_matrix: &khr::cooperative_matrix::Instance,+ phd: vk::PhysicalDevice,+) -> Vec<wgt::CooperativeMatrixProperties> {+ let vk_properties =+ match unsafe { coop_matrix.get_physical_device_cooperative_matrix_properties(phd) } {+ Ok(props) => props,+ Err(e) => {+ log::warn!("Failed to query cooperative matrix properties: {e:?}");+ return Vec::new();+ }+ };++ log::debug!(+ "Vulkan reports {} cooperative matrix configurations",+ vk_properties.len()+ );++ let mut result = Vec::new();+ for prop in &vk_properties {+ log::debug!(+ " Vulkan coop matrix: M={} N={} K={} A={:?} B={:?} C={:?} Result={:?} scope={:?} saturating={}",+ prop.m_size,+ prop.n_size,+ prop.k_size,+ prop.a_type,+ prop.b_type,+ prop.c_type,+ prop.result_type,+ prop.scope,+ prop.saturating_accumulation+ );++ // Only include subgroup-scoped operations (the only scope we support)+ if prop.scope != vk::ScopeKHR::SUBGROUP {+ log::debug!(" Skipped: scope is not SUBGROUP");+ continue;+ }++ // Map sizes - skip configurations with sizes we don't support+ let m_size = match map_vk_cooperative_size(prop.m_size) {+ Some(s) => s,+ None => {+ log::debug!(" Skipped: M size {} not supported", prop.m_size);+ continue;+ }+ };+ let n_size = match map_vk_cooperative_size(prop.n_size) {+ Some(s) => s,+ None => {+ log::debug!(" Skipped: N size {} not supported", prop.n_size);+ continue;+ }+ };+ let k_size = match map_vk_cooperative_size(prop.k_size) {+ Some(s) => s,+ None => {+ log::debug!(" Skipped: K size {} not supported", prop.k_size);+ continue;+ }+ };++ // Map the component types - A and B must match, C and Result must match+ let ab_type = match map_vk_component_type(prop.a_type) {+ Some(t) if Some(t) == map_vk_component_type(prop.b_type) => t,+ _ => {+ log::debug!(+ " Skipped: A/B types {:?}/{:?} not supported or don't match",+ prop.a_type,+ prop.b_type+ );+ continue;+ }+ };+ let cr_type = match map_vk_component_type(prop.c_type) {+ Some(t) if Some(t) == map_vk_component_type(prop.result_type) => t,+ _ => {+ log::debug!(+ " Skipped: C/Result types {:?}/{:?} not supported or don't match",+ prop.c_type,+ prop.result_type+ );+ continue;+ }+ };++ log::debug!(" Accepted!");+ result.push(wgt::CooperativeMatrixProperties {+ m_size,+ n_size,+ k_size,+ ab_type,+ cr_type,+ saturating_accumulation: prop.saturating_accumulation != 0,+ });+ }++ log::info!(+ "Found {} cooperative matrix configurations supported by wgpu",+ result.len()+ );+ result+}diff --git a/vendor/wgpu-hal/src/vulkan/command.rs b/vendor/wgpu-hal/src/vulkan/command.rsnew file mode 100644index 00000000..3ea853e6--- /dev/null+++ b/vendor/wgpu-hal/src/vulkan/command.rs@@ -0,0 +1,1495 @@+use super::conv;+use arrayvec::ArrayVec;+use ash::vk;+use core::{mem, ops::Range};+use hashbrown::hash_map::Entry;++const ALLOCATION_GRANULARITY: u32 = 16;+const DST_IMAGE_LAYOUT: vk::ImageLayout = vk::ImageLayout::TRANSFER_DST_OPTIMAL;++impl super::Texture {+ fn map_buffer_copies<T>(&self, regions: T) -> impl Iterator<Item = vk::BufferImageCopy>+ where+ T: Iterator<Item = crate::BufferTextureCopy>,+ {+ let (block_width, block_height) = self.format.block_dimensions();+ let format = self.format;+ let copy_size = self.copy_size;+ regions.map(move |r| {+ let extent = r.texture_base.max_copy_size(©_size).min(&r.size);+ let (image_subresource, image_offset) = conv::map_subresource_layers(&r.texture_base);+ vk::BufferImageCopy {+ buffer_offset: r.buffer_layout.offset,+ buffer_row_length: r.buffer_layout.bytes_per_row.map_or(0, |bpr| {+ let block_size = format+ .block_copy_size(Some(r.texture_base.aspect.map()))+ .unwrap();+ block_width * (bpr / block_size)+ }),+ buffer_image_height: r+ .buffer_layout+ .rows_per_image+ .map_or(0, |rpi| rpi * block_height),+ image_subresource,+ image_offset,+ image_extent: conv::map_copy_extent(&extent),+ }+ })+ }+}++impl super::CommandEncoder {+ fn write_pass_end_timestamp_if_requested(&mut self) {+ if let Some((query_set, index)) = self.end_of_pass_timer_query.take() {+ unsafe {+ self.device.raw.cmd_write_timestamp(+ self.active,+ vk::PipelineStageFlags::BOTTOM_OF_PIPE,+ query_set,+ index,+ );+ }+ }+ }++ fn make_framebuffer(+ &mut self,+ key: super::FramebufferKey,+ ) -> Result<vk::Framebuffer, crate::DeviceError> {+ Ok(match self.framebuffers.entry(key) {+ Entry::Occupied(e) => *e.get(),+ Entry::Vacant(e) => {+ let super::FramebufferKey {+ raw_pass,+ ref attachment_views,+ attachment_identities: _,+ extent,+ } = *e.key();++ let vk_info = vk::FramebufferCreateInfo::default()+ .render_pass(raw_pass)+ .width(extent.width)+ .height(extent.height)+ .layers(extent.depth_or_array_layers)+ .attachments(attachment_views);++ let raw = unsafe { self.device.raw.create_framebuffer(&vk_info, None).unwrap() };+ *e.insert(raw)+ }+ })+ }++ fn make_temp_texture_view(+ &mut self,+ key: super::TempTextureViewKey,+ ) -> Result<super::IdentifiedTextureView, crate::DeviceError> {+ Ok(match self.temp_texture_views.entry(key) {+ Entry::Occupied(e) => *e.get(),+ Entry::Vacant(e) => {+ let super::TempTextureViewKey {+ texture,+ texture_identity: _,+ format,+ mip_level,+ depth_slice,+ } = *e.key();++ let vk_info = vk::ImageViewCreateInfo::default()+ .image(texture)+ .view_type(vk::ImageViewType::TYPE_2D)+ .format(format)+ .subresource_range(vk::ImageSubresourceRange {+ aspect_mask: vk::ImageAspectFlags::COLOR,+ base_mip_level: mip_level,+ level_count: 1,+ base_array_layer: depth_slice,+ layer_count: 1,+ });+ let raw = unsafe { self.device.raw.create_image_view(&vk_info, None) }+ .map_err(super::map_host_device_oom_and_ioca_err)?;++ let identity = self.device.texture_view_identity_factory.next();++ *e.insert(super::IdentifiedTextureView { raw, identity })+ }+ })+ }+}++impl crate::CommandEncoder for super::CommandEncoder {+ type A = super::Api;++ unsafe fn begin_encoding(&mut self, label: crate::Label) -> Result<(), crate::DeviceError> {+ if self.free.is_empty() {+ let vk_info = vk::CommandBufferAllocateInfo::default()+ .command_pool(self.raw)+ .command_buffer_count(ALLOCATION_GRANULARITY);+ let cmd_buf_vec = unsafe {+ self.device+ .raw+ .allocate_command_buffers(&vk_info)+ .map_err(super::map_host_device_oom_err)?+ };+ self.free.extend(cmd_buf_vec);+ }+ let raw = self.free.pop().unwrap();++ // Set the name unconditionally, since there might be a+ // previous name assigned to this.+ unsafe { self.device.set_object_name(raw, label.unwrap_or_default()) };++ // Reset some state in case the last renderpass was never ended.+ self.rpass_debug_marker_active = false;+ self.end_of_pass_timer_query = None;++ let vk_info = vk::CommandBufferBeginInfo::default()+ .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT);+ unsafe { self.device.raw.begin_command_buffer(raw, &vk_info) }+ .map_err(super::map_host_device_oom_err)?;+ self.active = raw;++ Ok(())+ }++ unsafe fn end_encoding(&mut self) -> Result<super::CommandBuffer, crate::DeviceError> {+ let raw = self.active;+ self.active = vk::CommandBuffer::null();+ unsafe { self.device.raw.end_command_buffer(raw) }.map_err(map_err)?;+ fn map_err(err: vk::Result) -> crate::DeviceError {+ // We don't use VK_KHR_video_encode_queue+ // VK_ERROR_INVALID_VIDEO_STD_PARAMETERS_KHR+ super::map_host_device_oom_err(err)+ }+ Ok(super::CommandBuffer { raw })+ }++ unsafe fn discard_encoding(&mut self) {+ // Safe use requires this is not called in the "closed" state, so the buffer+ // shouldn't be null. Assert this to make sure we're not pushing null+ // buffers to the discard pile.+ assert_ne!(self.active, vk::CommandBuffer::null());++ self.discarded.push(self.active);+ self.active = vk::CommandBuffer::null();+ }++ unsafe fn reset_all<I>(&mut self, cmd_bufs: I)+ where+ I: Iterator<Item = super::CommandBuffer>,+ {+ self.temp.clear();+ self.free+ .extend(cmd_bufs.into_iter().map(|cmd_buf| cmd_buf.raw));+ self.free.append(&mut self.discarded);+ // Delete framebuffers from the framebuffer cache+ for (_, framebuffer) in self.framebuffers.drain() {+ unsafe { self.device.raw.destroy_framebuffer(framebuffer, None) };+ }+ let _ = unsafe {+ self.device+ .raw+ .reset_command_pool(self.raw, vk::CommandPoolResetFlags::default())+ };+ }++ unsafe fn transition_buffers<'a, T>(&mut self, barriers: T)+ where+ T: Iterator<Item = crate::BufferBarrier<'a, super::Buffer>>,+ {+ //Note: this is done so that we never end up with empty stage flags+ let mut src_stages = vk::PipelineStageFlags::TOP_OF_PIPE;+ let mut dst_stages = vk::PipelineStageFlags::BOTTOM_OF_PIPE;+ let vk_barriers = &mut self.temp.buffer_barriers;+ vk_barriers.clear();++ for bar in barriers {+ let (src_stage, src_access) = conv::map_buffer_usage_to_barrier(bar.usage.from);+ src_stages |= src_stage;+ let (dst_stage, dst_access) = conv::map_buffer_usage_to_barrier(bar.usage.to);+ dst_stages |= dst_stage;++ vk_barriers.push(+ vk::BufferMemoryBarrier::default()+ .buffer(bar.buffer.raw)+ .size(vk::WHOLE_SIZE)+ .src_access_mask(src_access)+ .dst_access_mask(dst_access),+ )+ }++ if !vk_barriers.is_empty() {+ unsafe {+ self.device.raw.cmd_pipeline_barrier(+ self.active,+ src_stages,+ dst_stages,+ vk::DependencyFlags::empty(),+ &[],+ vk_barriers,+ &[],+ )+ };+ }+ }++ unsafe fn transition_textures<'a, T>(&mut self, barriers: T)+ where+ T: Iterator<Item = crate::TextureBarrier<'a, super::Texture>>,+ {+ let mut src_stages = vk::PipelineStageFlags::empty();+ let mut dst_stages = vk::PipelineStageFlags::empty();+ let vk_barriers = &mut self.temp.image_barriers;+ vk_barriers.clear();++ for bar in barriers {+ let range = conv::map_subresource_range_combined_aspect(+ &bar.range,+ bar.texture.format,+ &self.device.private_caps,+ );+ let (src_stage, src_access) = conv::map_texture_usage_to_barrier(bar.usage.from);+ let src_layout = conv::derive_image_layout(bar.usage.from, bar.texture.format);+ src_stages |= src_stage;+ let (dst_stage, dst_access) = conv::map_texture_usage_to_barrier(bar.usage.to);+ let dst_layout = conv::derive_image_layout(bar.usage.to, bar.texture.format);+ dst_stages |= dst_stage;++ vk_barriers.push(+ vk::ImageMemoryBarrier::default()+ .image(bar.texture.raw)+ .subresource_range(range)+ .src_access_mask(src_access)+ .dst_access_mask(dst_access)+ .old_layout(src_layout)+ .new_layout(dst_layout),+ );+ }++ if !vk_barriers.is_empty() {+ unsafe {+ self.device.raw.cmd_pipeline_barrier(+ self.active,+ src_stages,+ dst_stages,+ vk::DependencyFlags::empty(),+ &[],+ &[],+ vk_barriers,+ )+ };+ }+ }++ unsafe fn clear_buffer(&mut self, buffer: &super::Buffer, range: crate::MemoryRange) {+ let range_size = range.end - range.start;+ if self.device.workarounds.contains(+ super::Workarounds::FORCE_FILL_BUFFER_WITH_SIZE_GREATER_4096_ALIGNED_OFFSET_16,+ ) && range_size >= 4096+ && !range.start.is_multiple_of(16)+ {+ let rounded_start = wgt::math::align_to(range.start, 16);+ let prefix_size = rounded_start - range.start;++ unsafe {+ self.device.raw.cmd_fill_buffer(+ self.active,+ buffer.raw,+ range.start,+ prefix_size,+ 0,+ )+ };++ // This will never be zero, as rounding can only add up to 12 bytes, and the total size is 4096.+ let suffix_size = range.end - rounded_start;++ unsafe {+ self.device.raw.cmd_fill_buffer(+ self.active,+ buffer.raw,+ rounded_start,+ suffix_size,+ 0,+ )+ };+ } else {+ unsafe {+ self.device+ .raw+ .cmd_fill_buffer(self.active, buffer.raw, range.start, range_size, 0)+ };+ }+ }++ unsafe fn copy_buffer_to_buffer<T>(+ &mut self,+ src: &super::Buffer,+ dst: &super::Buffer,+ regions: T,+ ) where+ T: Iterator<Item = crate::BufferCopy>,+ {+ let vk_regions_iter = regions.map(|r| vk::BufferCopy {+ src_offset: r.src_offset,+ dst_offset: r.dst_offset,+ size: r.size.get(),+ });++ unsafe {+ self.device.raw.cmd_copy_buffer(+ self.active,+ src.raw,+ dst.raw,+ &smallvec::SmallVec::<[vk::BufferCopy; 32]>::from_iter(vk_regions_iter),+ )+ };+ }++ unsafe fn copy_texture_to_texture<T>(+ &mut self,+ src: &super::Texture,+ src_usage: wgt::TextureUses,+ dst: &super::Texture,+ regions: T,+ ) where+ T: Iterator<Item = crate::TextureCopy>,+ {+ let src_layout = conv::derive_image_layout(src_usage, src.format);++ let vk_regions_iter = regions.map(|r| {+ let (src_subresource, src_offset) = conv::map_subresource_layers(&r.src_base);+ let (dst_subresource, dst_offset) = conv::map_subresource_layers(&r.dst_base);+ let extent = r+ .size+ .min(&r.src_base.max_copy_size(&src.copy_size))+ .min(&r.dst_base.max_copy_size(&dst.copy_size));+ vk::ImageCopy {+ src_subresource,+ src_offset,+ dst_subresource,+ dst_offset,+ extent: conv::map_copy_extent(&extent),+ }+ });++ unsafe {+ self.device.raw.cmd_copy_image(+ self.active,+ src.raw,+ src_layout,+ dst.raw,+ DST_IMAGE_LAYOUT,+ &smallvec::SmallVec::<[vk::ImageCopy; 32]>::from_iter(vk_regions_iter),+ )+ };+ }++ unsafe fn copy_buffer_to_texture<T>(+ &mut self,+ src: &super::Buffer,+ dst: &super::Texture,+ regions: T,+ ) where+ T: Iterator<Item = crate::BufferTextureCopy>,+ {+ let vk_regions_iter = dst.map_buffer_copies(regions);++ unsafe {+ self.device.raw.cmd_copy_buffer_to_image(+ self.active,+ src.raw,+ dst.raw,+ DST_IMAGE_LAYOUT,+ &smallvec::SmallVec::<[vk::BufferImageCopy; 32]>::from_iter(vk_regions_iter),+ )+ };+ }++ unsafe fn copy_texture_to_buffer<T>(+ &mut self,+ src: &super::Texture,+ src_usage: wgt::TextureUses,+ dst: &super::Buffer,+ regions: T,+ ) where+ T: Iterator<Item = crate::BufferTextureCopy>,+ {+ let src_layout = conv::derive_image_layout(src_usage, src.format);+ let vk_regions_iter = src.map_buffer_copies(regions);++ unsafe {+ self.device.raw.cmd_copy_image_to_buffer(+ self.active,+ src.raw,+ src_layout,+ dst.raw,+ &smallvec::SmallVec::<[vk::BufferImageCopy; 32]>::from_iter(vk_regions_iter),+ )+ };+ }++ unsafe fn begin_query(&mut self, set: &super::QuerySet, index: u32) {+ unsafe {+ self.device.raw.cmd_begin_query(+ self.active,+ set.raw,+ index,+ vk::QueryControlFlags::empty(),+ )+ };+ }+ unsafe fn end_query(&mut self, set: &super::QuerySet, index: u32) {+ unsafe { self.device.raw.cmd_end_query(self.active, set.raw, index) };+ }+ unsafe fn write_timestamp(&mut self, set: &super::QuerySet, index: u32) {+ unsafe {+ self.device.raw.cmd_write_timestamp(+ self.active,+ vk::PipelineStageFlags::BOTTOM_OF_PIPE,+ set.raw,+ index,+ )+ };+ }+ unsafe fn read_acceleration_structure_compact_size(+ &mut self,+ acceleration_structure: &super::AccelerationStructure,+ buffer: &super::Buffer,+ ) {+ let ray_tracing_functions = self+ .device+ .extension_fns+ .ray_tracing+ .as_ref()+ .expect("Feature `RAY_TRACING` not enabled");+ let query_pool = acceleration_structure+ .compacted_size_query+ .as_ref()+ .unwrap();+ unsafe {+ self.device+ .raw+ .cmd_reset_query_pool(self.active, *query_pool, 0, 1);+ ray_tracing_functions+ .acceleration_structure+ .cmd_write_acceleration_structures_properties(+ self.active,+ &[acceleration_structure.raw],+ vk::QueryType::ACCELERATION_STRUCTURE_COMPACTED_SIZE_KHR,+ *query_pool,+ 0,+ );+ self.device.raw.cmd_copy_query_pool_results(+ self.active,+ *query_pool,+ 0,+ 1,+ buffer.raw,+ 0,+ wgt::QUERY_SIZE as vk::DeviceSize,+ vk::QueryResultFlags::TYPE_64 | vk::QueryResultFlags::WAIT,+ )+ };+ }+ unsafe fn reset_queries(&mut self, set: &super::QuerySet, range: Range<u32>) {+ unsafe {+ self.device.raw.cmd_reset_query_pool(+ self.active,+ set.raw,+ range.start,+ range.end - range.start,+ )+ };+ }+ unsafe fn copy_query_results(+ &mut self,+ set: &super::QuerySet,+ range: Range<u32>,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ stride: wgt::BufferSize,+ ) {+ unsafe {+ self.device.raw.cmd_copy_query_pool_results(+ self.active,+ set.raw,+ range.start,+ range.end - range.start,+ buffer.raw,+ offset,+ stride.get(),+ vk::QueryResultFlags::TYPE_64 | vk::QueryResultFlags::WAIT,+ )+ };+ }++ unsafe fn build_acceleration_structures<'a, T>(&mut self, descriptor_count: u32, descriptors: T)+ where+ super::Api: 'a,+ T: IntoIterator<+ Item = crate::BuildAccelerationStructureDescriptor<+ 'a,+ super::Buffer,+ super::AccelerationStructure,+ >,+ >,+ {+ const CAPACITY_OUTER: usize = 8;+ const CAPACITY_INNER: usize = 1;+ let descriptor_count = descriptor_count as usize;++ let ray_tracing_functions = self+ .device+ .extension_fns+ .ray_tracing+ .as_ref()+ .expect("Feature `RAY_TRACING` not enabled");++ let get_device_address = |buffer: Option<&super::Buffer>| unsafe {+ match buffer {+ Some(buffer) => ray_tracing_functions+ .buffer_device_address+ .get_buffer_device_address(+ &vk::BufferDeviceAddressInfo::default().buffer(buffer.raw),+ ),+ None => panic!("Buffers are required to build acceleration structures"),+ }+ };++ // storage to all the data required for cmd_build_acceleration_structures+ let mut ranges_storage = smallvec::SmallVec::<+ [smallvec::SmallVec<[vk::AccelerationStructureBuildRangeInfoKHR; CAPACITY_INNER]>;+ CAPACITY_OUTER],+ >::with_capacity(descriptor_count);+ let mut geometries_storage = smallvec::SmallVec::<+ [smallvec::SmallVec<[vk::AccelerationStructureGeometryKHR; CAPACITY_INNER]>;+ CAPACITY_OUTER],+ >::with_capacity(descriptor_count);++ // pointers to all the data required for cmd_build_acceleration_structures+ let mut geometry_infos = smallvec::SmallVec::<+ [vk::AccelerationStructureBuildGeometryInfoKHR; CAPACITY_OUTER],+ >::with_capacity(descriptor_count);+ let mut ranges_ptrs = smallvec::SmallVec::<+ [&[vk::AccelerationStructureBuildRangeInfoKHR]; CAPACITY_OUTER],+ >::with_capacity(descriptor_count);++ for desc in descriptors {+ let (geometries, ranges) = match *desc.entries {+ crate::AccelerationStructureEntries::Instances(ref instances) => {+ let instance_data = vk::AccelerationStructureGeometryInstancesDataKHR::default(+ // TODO: Code is so large that rustfmt refuses to treat this... :(+ )+ .data(vk::DeviceOrHostAddressConstKHR {+ device_address: get_device_address(instances.buffer),+ });++ let geometry = vk::AccelerationStructureGeometryKHR::default()+ .geometry_type(vk::GeometryTypeKHR::INSTANCES)+ .geometry(vk::AccelerationStructureGeometryDataKHR {+ instances: instance_data,+ });++ let range = vk::AccelerationStructureBuildRangeInfoKHR::default()+ .primitive_count(instances.count)+ .primitive_offset(instances.offset);++ (smallvec::smallvec![geometry], smallvec::smallvec![range])+ }+ crate::AccelerationStructureEntries::Triangles(ref in_geometries) => {+ let mut ranges = smallvec::SmallVec::<+ [vk::AccelerationStructureBuildRangeInfoKHR; CAPACITY_INNER],+ >::with_capacity(in_geometries.len());+ let mut geometries = smallvec::SmallVec::<+ [vk::AccelerationStructureGeometryKHR; CAPACITY_INNER],+ >::with_capacity(in_geometries.len());+ for triangles in in_geometries {+ let mut triangle_data =+ vk::AccelerationStructureGeometryTrianglesDataKHR::default()+ // IndexType::NONE_KHR is not set by default (due to being provided by VK_KHR_acceleration_structure) but unless there is an+ // index buffer we need to have IndexType::NONE_KHR as our index type.+ .index_type(vk::IndexType::NONE_KHR)+ .vertex_data(vk::DeviceOrHostAddressConstKHR {+ device_address: get_device_address(triangles.vertex_buffer)+ + (triangles.first_vertex as u64 * triangles.vertex_stride),+ })+ .vertex_format(conv::map_vertex_format(triangles.vertex_format))+ .max_vertex(triangles.vertex_count)+ .vertex_stride(triangles.vertex_stride);++ let mut range = vk::AccelerationStructureBuildRangeInfoKHR::default();++ if let Some(ref indices) = triangles.indices {+ triangle_data = triangle_data+ .index_data(vk::DeviceOrHostAddressConstKHR {+ device_address: get_device_address(indices.buffer),+ })+ .index_type(conv::map_index_format(indices.format));++ range = range+ .primitive_count(indices.count / 3)+ .primitive_offset(indices.offset);+ } else {+ range = range.primitive_count(triangles.vertex_count / 3);+ }++ if let Some(ref transform) = triangles.transform {+ let transform_device_address = unsafe {+ ray_tracing_functions+ .buffer_device_address+ .get_buffer_device_address(+ &vk::BufferDeviceAddressInfo::default()+ .buffer(transform.buffer.raw),+ )+ };+ triangle_data =+ triangle_data.transform_data(vk::DeviceOrHostAddressConstKHR {+ device_address: transform_device_address,+ });++ range = range.transform_offset(transform.offset);+ }++ let geometry = vk::AccelerationStructureGeometryKHR::default()+ .geometry_type(vk::GeometryTypeKHR::TRIANGLES)+ .geometry(vk::AccelerationStructureGeometryDataKHR {+ triangles: triangle_data,+ })+ .flags(conv::map_acceleration_structure_geometry_flags(+ triangles.flags,+ ));++ geometries.push(geometry);+ ranges.push(range);+ }+ (geometries, ranges)+ }+ crate::AccelerationStructureEntries::AABBs(ref in_geometries) => {+ let mut ranges = smallvec::SmallVec::<+ [vk::AccelerationStructureBuildRangeInfoKHR; CAPACITY_INNER],+ >::with_capacity(in_geometries.len());+ let mut geometries = smallvec::SmallVec::<+ [vk::AccelerationStructureGeometryKHR; CAPACITY_INNER],+ >::with_capacity(in_geometries.len());+ for aabb in in_geometries {+ let aabbs_data = vk::AccelerationStructureGeometryAabbsDataKHR::default()+ .data(vk::DeviceOrHostAddressConstKHR {+ device_address: get_device_address(aabb.buffer),+ })+ .stride(aabb.stride);++ let range = vk::AccelerationStructureBuildRangeInfoKHR::default()+ .primitive_count(aabb.count)+ .primitive_offset(aabb.offset);++ let geometry = vk::AccelerationStructureGeometryKHR::default()+ .geometry_type(vk::GeometryTypeKHR::AABBS)+ .geometry(vk::AccelerationStructureGeometryDataKHR {+ aabbs: aabbs_data,+ })+ .flags(conv::map_acceleration_structure_geometry_flags(aabb.flags));++ geometries.push(geometry);+ ranges.push(range);+ }+ (geometries, ranges)+ }+ };++ ranges_storage.push(ranges);+ geometries_storage.push(geometries);++ let scratch_device_address = unsafe {+ ray_tracing_functions+ .buffer_device_address+ .get_buffer_device_address(+ &vk::BufferDeviceAddressInfo::default().buffer(desc.scratch_buffer.raw),+ )+ };+ let ty = match *desc.entries {+ crate::AccelerationStructureEntries::Instances(_) => {+ vk::AccelerationStructureTypeKHR::TOP_LEVEL+ }+ _ => vk::AccelerationStructureTypeKHR::BOTTOM_LEVEL,+ };+ let mut geometry_info = vk::AccelerationStructureBuildGeometryInfoKHR::default()+ .ty(ty)+ .mode(conv::map_acceleration_structure_build_mode(desc.mode))+ .flags(conv::map_acceleration_structure_flags(desc.flags))+ .dst_acceleration_structure(desc.destination_acceleration_structure.raw)+ .scratch_data(vk::DeviceOrHostAddressKHR {+ device_address: scratch_device_address + desc.scratch_buffer_offset,+ });++ if desc.mode == crate::AccelerationStructureBuildMode::Update {+ geometry_info.src_acceleration_structure = desc+ .source_acceleration_structure+ .unwrap_or(desc.destination_acceleration_structure)+ .raw;+ }++ geometry_infos.push(geometry_info);+ }++ for (i, geometry_info) in geometry_infos.iter_mut().enumerate() {+ geometry_info.geometry_count = geometries_storage[i].len() as u32;+ geometry_info.p_geometries = geometries_storage[i].as_ptr();+ ranges_ptrs.push(&ranges_storage[i]);+ }++ unsafe {+ ray_tracing_functions+ .acceleration_structure+ .cmd_build_acceleration_structures(self.active, &geometry_infos, &ranges_ptrs);+ }+ }++ unsafe fn place_acceleration_structure_barrier(+ &mut self,+ barrier: crate::AccelerationStructureBarrier,+ ) {+ let (src_stage, src_access) = conv::map_acceleration_structure_usage_to_barrier(+ barrier.usage.from,+ self.device.features,+ );+ let (dst_stage, dst_access) = conv::map_acceleration_structure_usage_to_barrier(+ barrier.usage.to,+ self.device.features,+ );++ unsafe {+ self.device.raw.cmd_pipeline_barrier(+ self.active,+ src_stage | vk::PipelineStageFlags::TOP_OF_PIPE,+ dst_stage | vk::PipelineStageFlags::BOTTOM_OF_PIPE,+ vk::DependencyFlags::empty(),+ &[vk::MemoryBarrier::default()+ .src_access_mask(src_access)+ .dst_access_mask(dst_access)],+ &[],+ &[],+ )+ };+ }++ unsafe fn set_acceleration_structure_dependencies(+ _command_buffers: &[&super::CommandBuffer],+ _dependencies: &[&super::AccelerationStructure],+ ) {+ }+ // render++ unsafe fn begin_render_pass(+ &mut self,+ desc: &crate::RenderPassDescriptor<super::QuerySet, super::TextureView>,+ ) -> Result<(), crate::DeviceError> {+ let mut vk_clear_values =+ ArrayVec::<vk::ClearValue, { super::MAX_TOTAL_ATTACHMENTS }>::new();+ let mut rp_key = super::RenderPassKey {+ colors: ArrayVec::default(),+ depth_stencil: None,+ sample_count: desc.sample_count,+ multiview_mask: desc.multiview_mask,+ };+ let mut fb_key = super::FramebufferKey {+ raw_pass: vk::RenderPass::null(),+ attachment_views: ArrayVec::default(),+ attachment_identities: ArrayVec::default(),+ extent: desc.extent,+ };++ for cat in desc.color_attachments {+ if let Some(cat) = cat.as_ref() {+ let color_view = if cat.target.view.dimension == wgt::TextureViewDimension::D3 {+ let key = super::TempTextureViewKey {+ texture: cat.target.view.raw_texture,+ texture_identity: cat.target.view.texture_identity,+ format: cat.target.view.raw_format,+ mip_level: cat.target.view.base_mip_level,+ depth_slice: cat.depth_slice.unwrap(),+ };+ self.make_temp_texture_view(key)?+ } else {+ cat.target.view.identified_raw_view()+ };++ vk_clear_values.push(vk::ClearValue {+ color: unsafe { cat.make_vk_clear_color() },+ });+ let color = super::ColorAttachmentKey {+ base: cat.target.make_attachment_key(cat.ops),+ resolve: cat.resolve_target.as_ref().map(|target| {+ target.make_attachment_key(+ crate::AttachmentOps::LOAD_CLEAR | crate::AttachmentOps::STORE,+ )+ }),+ };++ rp_key.colors.push(Some(color));+ fb_key.push_view(color_view);+ if let Some(ref at) = cat.resolve_target {+ vk_clear_values.push(unsafe { mem::zeroed() });+ fb_key.push_view(at.view.identified_raw_view());+ }+ } else {+ rp_key.colors.push(None);+ }+ }+ if let Some(ref ds) = desc.depth_stencil_attachment {+ vk_clear_values.push(vk::ClearValue {+ depth_stencil: vk::ClearDepthStencilValue {+ depth: ds.clear_value.0,+ stencil: ds.clear_value.1,+ },+ });+ rp_key.depth_stencil = Some(super::DepthStencilAttachmentKey {+ base: ds.target.make_attachment_key(ds.depth_ops),+ stencil_ops: ds.stencil_ops,+ });+ fb_key.push_view(ds.target.view.identified_raw_view());+ }++ let render_area = vk::Rect2D {+ offset: vk::Offset2D { x: 0, y: 0 },+ extent: vk::Extent2D {+ width: desc.extent.width,+ height: desc.extent.height,+ },+ };+ let vk_viewports = [vk::Viewport {+ x: 0.0,+ y: desc.extent.height as f32,+ width: desc.extent.width as f32,+ height: -(desc.extent.height as f32),+ min_depth: 0.0,+ max_depth: 1.0,+ }];++ let raw_pass = self.device.make_render_pass(rp_key).unwrap();+ fb_key.raw_pass = raw_pass;+ let raw_framebuffer = self.make_framebuffer(fb_key).unwrap();++ let vk_info = vk::RenderPassBeginInfo::default()+ .render_pass(raw_pass)+ .render_area(render_area)+ .clear_values(&vk_clear_values)+ .framebuffer(raw_framebuffer);++ if let Some(label) = desc.label {+ unsafe { self.begin_debug_marker(label) };+ self.rpass_debug_marker_active = true;+ }++ // Start timestamp if any (before all other commands but after debug marker)+ if let Some(timestamp_writes) = desc.timestamp_writes.as_ref() {+ if let Some(index) = timestamp_writes.beginning_of_pass_write_index {+ unsafe {+ self.write_timestamp(timestamp_writes.query_set, index);+ }+ }+ self.end_of_pass_timer_query = timestamp_writes+ .end_of_pass_write_index+ .map(|index| (timestamp_writes.query_set.raw, index));+ }++ unsafe {+ self.device+ .raw+ .cmd_set_viewport(self.active, 0, &vk_viewports);+ self.device+ .raw+ .cmd_set_scissor(self.active, 0, &[render_area]);+ self.device.raw.cmd_begin_render_pass(+ self.active,+ &vk_info,+ vk::SubpassContents::INLINE,+ );+ };++ self.bind_point = vk::PipelineBindPoint::GRAPHICS;++ Ok(())+ }+ unsafe fn end_render_pass(&mut self) {+ unsafe {+ self.device.raw.cmd_end_render_pass(self.active);+ }++ // After all other commands but before debug marker, so this is still seen as part of this pass.+ self.write_pass_end_timestamp_if_requested();++ if self.rpass_debug_marker_active {+ unsafe {+ self.end_debug_marker();+ }+ self.rpass_debug_marker_active = false;+ }+ }++ unsafe fn set_bind_group(+ &mut self,+ layout: &super::PipelineLayout,+ index: u32,+ group: &super::BindGroup,+ dynamic_offsets: &[wgt::DynamicOffset],+ ) {+ let sets = [group.set.raw()];+ unsafe {+ self.device.raw.cmd_bind_descriptor_sets(+ self.active,+ self.bind_point,+ layout.raw,+ index,+ &sets,+ dynamic_offsets,+ )+ };+ }+ unsafe fn set_immediates(+ &mut self,+ layout: &super::PipelineLayout,+ offset_bytes: u32,+ data: &[u32],+ ) {+ unsafe {+ self.device.raw.cmd_push_constants(+ self.active,+ layout.raw,+ vk::ShaderStageFlags::ALL,+ offset_bytes,+ bytemuck::cast_slice(data),+ )+ };+ }++ unsafe fn insert_debug_marker(&mut self, label: &str) {+ if let Some(ext) = self.device.extension_fns.debug_utils.as_ref() {+ let cstr = self.temp.make_c_str(label);+ let vk_label = vk::DebugUtilsLabelEXT::default().label_name(cstr);+ unsafe { ext.cmd_insert_debug_utils_label(self.active, &vk_label) };+ }+ }+ unsafe fn begin_debug_marker(&mut self, group_label: &str) {+ if let Some(ext) = self.device.extension_fns.debug_utils.as_ref() {+ let cstr = self.temp.make_c_str(group_label);+ let vk_label = vk::DebugUtilsLabelEXT::default().label_name(cstr);+ unsafe { ext.cmd_begin_debug_utils_label(self.active, &vk_label) };+ }+ }+ unsafe fn end_debug_marker(&mut self) {+ if let Some(ext) = self.device.extension_fns.debug_utils.as_ref() {+ unsafe { ext.cmd_end_debug_utils_label(self.active) };+ }+ }++ unsafe fn set_render_pipeline(&mut self, pipeline: &super::RenderPipeline) {+ unsafe {+ self.current_pipeline_is_multiview = pipeline.is_multiview;+ self.device.raw.cmd_bind_pipeline(+ self.active,+ vk::PipelineBindPoint::GRAPHICS,+ pipeline.raw,+ )+ };+ }++ unsafe fn set_index_buffer<'a>(+ &mut self,+ binding: crate::BufferBinding<'a, super::Buffer>,+ format: wgt::IndexFormat,+ ) {+ unsafe {+ self.device.raw.cmd_bind_index_buffer(+ self.active,+ binding.buffer.raw,+ binding.offset,+ conv::map_index_format(format),+ )+ };+ }+ unsafe fn set_vertex_buffer<'a>(+ &mut self,+ index: u32,+ binding: crate::BufferBinding<'a, super::Buffer>,+ ) {+ let vk_buffers = [binding.buffer.raw];+ let vk_offsets = [binding.offset];+ unsafe {+ self.device+ .raw+ .cmd_bind_vertex_buffers(self.active, index, &vk_buffers, &vk_offsets)+ };+ }+ unsafe fn set_viewport(&mut self, rect: &crate::Rect<f32>, depth_range: Range<f32>) {+ let vk_viewports = [vk::Viewport {+ x: rect.x,+ y: rect.y + rect.h,+ width: rect.w,+ height: -rect.h, // flip Y+ min_depth: depth_range.start,+ max_depth: depth_range.end,+ }];+ unsafe {+ self.device+ .raw+ .cmd_set_viewport(self.active, 0, &vk_viewports)+ };+ }+ unsafe fn set_scissor_rect(&mut self, rect: &crate::Rect<u32>) {+ let vk_scissors = [vk::Rect2D {+ offset: vk::Offset2D {+ x: rect.x as i32,+ y: rect.y as i32,+ },+ extent: vk::Extent2D {+ width: rect.w,+ height: rect.h,+ },+ }];+ unsafe {+ self.device+ .raw+ .cmd_set_scissor(self.active, 0, &vk_scissors)+ };+ }+ unsafe fn set_stencil_reference(&mut self, value: u32) {+ unsafe {+ self.device.raw.cmd_set_stencil_reference(+ self.active,+ vk::StencilFaceFlags::FRONT_AND_BACK,+ value,+ )+ };+ }+ unsafe fn set_blend_constants(&mut self, color: &[f32; 4]) {+ unsafe { self.device.raw.cmd_set_blend_constants(self.active, color) };+ }++ unsafe fn draw(+ &mut self,+ first_vertex: u32,+ vertex_count: u32,+ first_instance: u32,+ instance_count: u32,+ ) {+ if self.current_pipeline_is_multiview+ && (first_instance as u64 + instance_count as u64 - 1)+ > self.device.private_caps.multiview_instance_index_limit as u64+ {+ panic!("This vulkan device is affected by [#8333](https://github.com/gfx-rs/wgpu/issues/8333)");+ }+ unsafe {+ self.device.raw.cmd_draw(+ self.active,+ vertex_count,+ instance_count,+ first_vertex,+ first_instance,+ )+ };+ }+ unsafe fn draw_indexed(+ &mut self,+ first_index: u32,+ index_count: u32,+ base_vertex: i32,+ first_instance: u32,+ instance_count: u32,+ ) {+ if self.current_pipeline_is_multiview+ && (first_instance as u64 + instance_count as u64 - 1)+ > self.device.private_caps.multiview_instance_index_limit as u64+ {+ panic!("This vulkan device is affected by [#8333](https://github.com/gfx-rs/wgpu/issues/8333)");+ }+ unsafe {+ self.device.raw.cmd_draw_indexed(+ self.active,+ index_count,+ instance_count,+ first_index,+ base_vertex,+ first_instance,+ )+ };+ }+ unsafe fn draw_mesh_tasks(+ &mut self,+ group_count_x: u32,+ group_count_y: u32,+ group_count_z: u32,+ ) {+ if let Some(ref t) = self.device.extension_fns.mesh_shading {+ unsafe {+ t.cmd_draw_mesh_tasks(self.active, group_count_x, group_count_y, group_count_z);+ };+ } else {+ panic!("Feature `MESH_SHADING` not enabled");+ }+ }+ unsafe fn draw_indirect(+ &mut self,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ ) {+ if draw_count >= 1+ && self.device.private_caps.multi_draw_indirect+ && draw_count <= self.device.private_caps.max_draw_indirect_count+ {+ unsafe {+ self.device.raw.cmd_draw_indirect(+ self.active,+ buffer.raw,+ offset,+ draw_count,+ size_of::<wgt::DrawIndirectArgs>() as u32,+ )+ };+ } else {+ for i in 0..draw_count {+ let indirect_offset = offset+ + i as wgt::BufferAddress+ * size_of::<wgt::DrawIndirectArgs>() as wgt::BufferAddress;+ unsafe {+ self.device.raw.cmd_draw_indirect(+ self.active,+ buffer.raw,+ indirect_offset,+ 1,+ size_of::<wgt::DrawIndirectArgs>() as u32,+ )+ };+ }+ }+ }+ unsafe fn draw_indexed_indirect(+ &mut self,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ ) {+ if draw_count >= 1+ && self.device.private_caps.multi_draw_indirect+ && draw_count <= self.device.private_caps.max_draw_indirect_count+ {+ unsafe {+ self.device.raw.cmd_draw_indexed_indirect(+ self.active,+ buffer.raw,+ offset,+ draw_count,+ size_of::<wgt::DrawIndexedIndirectArgs>() as u32,+ )+ };+ } else {+ for i in 0..draw_count {+ let indirect_offset = offset+ + i as wgt::BufferAddress+ * size_of::<wgt::DrawIndexedIndirectArgs>() as wgt::BufferAddress;+ unsafe {+ self.device.raw.cmd_draw_indexed_indirect(+ self.active,+ buffer.raw,+ indirect_offset,+ 1,+ size_of::<wgt::DrawIndexedIndirectArgs>() as u32,+ )+ };+ }+ }+ }+ unsafe fn draw_mesh_tasks_indirect(+ &mut self,+ buffer: &<Self::A as crate::Api>::Buffer,+ offset: wgt::BufferAddress,+ draw_count: u32,+ ) {+ if let Some(ref t) = self.device.extension_fns.mesh_shading {+ unsafe {+ t.cmd_draw_mesh_tasks_indirect(+ self.active,+ buffer.raw,+ offset,+ draw_count,+ size_of::<wgt::DispatchIndirectArgs>() as u32,+ );+ };+ } else {+ panic!("Feature `MESH_SHADING` not enabled");+ }+ }+ unsafe fn draw_indirect_count(+ &mut self,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ count_buffer: &super::Buffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ ) {+ let stride = size_of::<wgt::DrawIndirectArgs>() as u32;+ match self.device.extension_fns.draw_indirect_count {+ Some(ref t) => {+ unsafe {+ t.cmd_draw_indirect_count(+ self.active,+ buffer.raw,+ offset,+ count_buffer.raw,+ count_offset,+ max_count,+ stride,+ )+ };+ }+ None => panic!("Feature `DRAW_INDIRECT_COUNT` not enabled"),+ }+ }+ unsafe fn draw_indexed_indirect_count(+ &mut self,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ count_buffer: &super::Buffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ ) {+ let stride = size_of::<wgt::DrawIndexedIndirectArgs>() as u32;+ match self.device.extension_fns.draw_indirect_count {+ Some(ref t) => {+ unsafe {+ t.cmd_draw_indexed_indirect_count(+ self.active,+ buffer.raw,+ offset,+ count_buffer.raw,+ count_offset,+ max_count,+ stride,+ )+ };+ }+ None => panic!("Feature `DRAW_INDIRECT_COUNT` not enabled"),+ }+ }+ unsafe fn draw_mesh_tasks_indirect_count(+ &mut self,+ buffer: &<Self::A as crate::Api>::Buffer,+ offset: wgt::BufferAddress,+ count_buffer: &super::Buffer,+ count_offset: wgt::BufferAddress,+ max_count: u32,+ ) {+ if self.device.extension_fns.draw_indirect_count.is_none() {+ panic!("Feature `DRAW_INDIRECT_COUNT` not enabled");+ }+ if let Some(ref t) = self.device.extension_fns.mesh_shading {+ unsafe {+ t.cmd_draw_mesh_tasks_indirect_count(+ self.active,+ buffer.raw,+ offset,+ count_buffer.raw,+ count_offset,+ max_count,+ size_of::<wgt::DispatchIndirectArgs>() as u32,+ );+ };+ } else {+ panic!("Feature `MESH_SHADING` not enabled");+ }+ }++ // compute++ unsafe fn begin_compute_pass(+ &mut self,+ desc: &crate::ComputePassDescriptor<'_, super::QuerySet>,+ ) {+ self.bind_point = vk::PipelineBindPoint::COMPUTE;+ if let Some(label) = desc.label {+ unsafe { self.begin_debug_marker(label) };+ self.rpass_debug_marker_active = true;+ }++ if let Some(timestamp_writes) = desc.timestamp_writes.as_ref() {+ if let Some(index) = timestamp_writes.beginning_of_pass_write_index {+ unsafe {+ self.write_timestamp(timestamp_writes.query_set, index);+ }+ }+ self.end_of_pass_timer_query = timestamp_writes+ .end_of_pass_write_index+ .map(|index| (timestamp_writes.query_set.raw, index));+ }+ }+ unsafe fn end_compute_pass(&mut self) {+ self.write_pass_end_timestamp_if_requested();++ if self.rpass_debug_marker_active {+ unsafe { self.end_debug_marker() };+ self.rpass_debug_marker_active = false+ }+ }++ unsafe fn set_compute_pipeline(&mut self, pipeline: &super::ComputePipeline) {+ unsafe {+ self.device.raw.cmd_bind_pipeline(+ self.active,+ vk::PipelineBindPoint::COMPUTE,+ pipeline.raw,+ )+ };+ }++ unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]) {+ unsafe {+ self.device+ .raw+ .cmd_dispatch(self.active, count[0], count[1], count[2])+ };+ }+ unsafe fn dispatch_workgroups_indirect(+ &mut self,+ buffer: &super::Buffer,+ offset: wgt::BufferAddress,+ ) {+ unsafe {+ self.device+ .raw+ .cmd_dispatch_indirect(self.active, buffer.raw, offset)+ }+ }++ // ray tracing++ unsafe fn begin_ray_tracing_pass(&mut self, desc: &crate::RayTracingPassDescriptor<'_>) {+ self.bind_point = vk::PipelineBindPoint::RAY_TRACING_KHR;+ if let Some(label) = desc.label {+ unsafe { self.begin_debug_marker(label) };+ self.rpass_debug_marker_active = true;+ }+ }+ unsafe fn end_ray_tracing_pass(&mut self) {+ if self.rpass_debug_marker_active {+ unsafe { self.end_debug_marker() };+ self.rpass_debug_marker_active = false+ }+ }++ unsafe fn trace_rays(+ &mut self,+ count: [u32; 3],+ ray_generation_group_data: crate::PipelineGroupData<super::Buffer>,+ miss_group_data: crate::PipelineGroupData<super::Buffer>,+ intersection_group_data: crate::PipelineGroupData<super::Buffer>,+ ) {+ let ray_tracing_functions = self+ .device+ .extension_fns+ .ray_tracing+ .as_ref()+ .expect("Feature `EXPERIMENTAL_RAY_TRACING` not enabled");++ let ray_tracing_pipeline_functions = self+ .device+ .extension_fns+ .ray_tracing_pipelines+ .as_ref()+ .expect("Feature `EXPERIMENTAL_RAY_TRACING_PIPELINES` not enabled");++ let get_device_address = |buffer: &super::Buffer| unsafe {+ ray_tracing_functions+ .buffer_device_address+ .get_buffer_device_address(+ &vk::BufferDeviceAddressInfo::default().buffer(buffer.raw),+ )+ };++ unsafe {+ ray_tracing_pipeline_functions.cmd_trace_rays(+ self.raw_handle(),+ &vk::StridedDeviceAddressRegionKHR {+ device_address: get_device_address(ray_generation_group_data.buffer)+ + ray_generation_group_data.offset,+ stride: ray_generation_group_data.stride,+ size: ray_generation_group_data.stride /* no need for multiplying by count, vulkan requires the ray gen sbt to be just one group */,+ },+ &vk::StridedDeviceAddressRegionKHR {+ device_address: get_device_address(miss_group_data.buffer)+ + miss_group_data.offset,+ stride: miss_group_data.stride,+ size: miss_group_data.stride * miss_group_data.count,+ },+ &vk::StridedDeviceAddressRegionKHR {+ device_address: get_device_address(intersection_group_data.buffer)+ + intersection_group_data.offset,+ stride: intersection_group_data.stride,+ size: intersection_group_data.stride * intersection_group_data.count,+ },+ &vk::StridedDeviceAddressRegionKHR {+ device_address: 0,+ stride: 0,+ size: 0,+ },+ count[0],+ count[1],+ count[2],+ )+ };+ }++ unsafe fn set_ray_tracing_pipeline(&mut self, pipeline: &super::RayTracingPipeline) {+ unsafe {+ self.device.raw.cmd_bind_pipeline(+ self.active,+ vk::PipelineBindPoint::RAY_TRACING_KHR,+ pipeline.raw,+ )+ };+ }++ unsafe fn copy_acceleration_structure_to_acceleration_structure(+ &mut self,+ src: &super::AccelerationStructure,+ dst: &super::AccelerationStructure,+ copy: wgt::AccelerationStructureCopy,+ ) {+ let ray_tracing_functions = self+ .device+ .extension_fns+ .ray_tracing+ .as_ref()+ .expect("Feature `RAY_TRACING` not enabled");++ let mode = match copy {+ wgt::AccelerationStructureCopy::Clone => vk::CopyAccelerationStructureModeKHR::CLONE,+ wgt::AccelerationStructureCopy::Compact => {+ vk::CopyAccelerationStructureModeKHR::COMPACT+ }+ };++ unsafe {+ ray_tracing_functions+ .acceleration_structure+ .cmd_copy_acceleration_structure(+ self.active,+ &vk::CopyAccelerationStructureInfoKHR {+ s_type: vk::StructureType::COPY_ACCELERATION_STRUCTURE_INFO_KHR,+ p_next: core::ptr::null(),+ src: src.raw,+ dst: dst.raw,+ mode,+ _marker: Default::default(),+ },+ );+ }+ }+}++#[test]+fn check_dst_image_layout() {+ assert_eq!(+ conv::derive_image_layout(wgt::TextureUses::COPY_DST, wgt::TextureFormat::Rgba8Unorm),+ DST_IMAGE_LAYOUT+ );+}diff --git a/vendor/wgpu-hal/src/vulkan/conv.rs b/vendor/wgpu-hal/src/vulkan/conv.rsnew file mode 100644index 00000000..47bdf495--- /dev/null+++ b/vendor/wgpu-hal/src/vulkan/conv.rs@@ -0,0 +1,1136 @@+use alloc::vec::Vec;++use ash::vk;++impl super::PrivateCapabilities {+ pub fn map_texture_format(&self, format: wgt::TextureFormat) -> vk::Format {+ use ash::vk::Format as F;+ use wgt::TextureFormat as Tf;+ use wgt::{AstcBlock, AstcChannel};+ match format {+ Tf::R8Unorm => F::R8_UNORM,+ Tf::R8Snorm => F::R8_SNORM,+ Tf::R8Uint => F::R8_UINT,+ Tf::R8Sint => F::R8_SINT,+ Tf::R16Uint => F::R16_UINT,+ Tf::R16Sint => F::R16_SINT,+ Tf::R16Unorm => F::R16_UNORM,+ Tf::R16Snorm => F::R16_SNORM,+ Tf::R16Float => F::R16_SFLOAT,+ Tf::Rg8Unorm => F::R8G8_UNORM,+ Tf::Rg8Snorm => F::R8G8_SNORM,+ Tf::Rg8Uint => F::R8G8_UINT,+ Tf::Rg8Sint => F::R8G8_SINT,+ Tf::Rg16Unorm => F::R16G16_UNORM,+ Tf::Rg16Snorm => F::R16G16_SNORM,+ Tf::R32Uint => F::R32_UINT,+ Tf::R32Sint => F::R32_SINT,+ Tf::R32Float => F::R32_SFLOAT,+ Tf::Rg16Uint => F::R16G16_UINT,+ Tf::Rg16Sint => F::R16G16_SINT,+ Tf::Rg16Float => F::R16G16_SFLOAT,+ Tf::Rgba8Unorm => F::R8G8B8A8_UNORM,+ Tf::Rgba8UnormSrgb => F::R8G8B8A8_SRGB,+ Tf::Bgra8UnormSrgb => F::B8G8R8A8_SRGB,+ Tf::Rgba8Snorm => F::R8G8B8A8_SNORM,+ Tf::Bgra8Unorm => F::B8G8R8A8_UNORM,+ Tf::Rgba8Uint => F::R8G8B8A8_UINT,+ Tf::Rgba8Sint => F::R8G8B8A8_SINT,+ Tf::Rgb10a2Uint => F::A2B10G10R10_UINT_PACK32,+ Tf::Rgb10a2Unorm => F::A2B10G10R10_UNORM_PACK32,+ Tf::Rg11b10Ufloat => F::B10G11R11_UFLOAT_PACK32,+ Tf::R64Uint => F::R64_UINT,+ Tf::Rg32Uint => F::R32G32_UINT,+ Tf::Rg32Sint => F::R32G32_SINT,+ Tf::Rg32Float => F::R32G32_SFLOAT,+ Tf::Rgba16Uint => F::R16G16B16A16_UINT,+ Tf::Rgba16Sint => F::R16G16B16A16_SINT,+ Tf::Rgba16Unorm => F::R16G16B16A16_UNORM,+ Tf::Rgba16Snorm => F::R16G16B16A16_SNORM,+ Tf::Rgba16Float => F::R16G16B16A16_SFLOAT,+ Tf::Rgba32Uint => F::R32G32B32A32_UINT,+ Tf::Rgba32Sint => F::R32G32B32A32_SINT,+ Tf::Rgba32Float => F::R32G32B32A32_SFLOAT,+ Tf::Depth32Float => F::D32_SFLOAT,+ Tf::Depth32FloatStencil8 => F::D32_SFLOAT_S8_UINT,+ Tf::Depth24Plus => {+ if self.texture_d24 {+ F::X8_D24_UNORM_PACK32+ } else {+ F::D32_SFLOAT+ }+ }+ Tf::Depth24PlusStencil8 => {+ if self.texture_d24_s8 {+ F::D24_UNORM_S8_UINT+ } else {+ F::D32_SFLOAT_S8_UINT+ }+ }+ Tf::Stencil8 => {+ if self.texture_s8 {+ F::S8_UINT+ } else if self.texture_d24_s8 {+ F::D24_UNORM_S8_UINT+ } else {+ F::D32_SFLOAT_S8_UINT+ }+ }+ Tf::Depth16Unorm => F::D16_UNORM,+ Tf::NV12 => F::G8_B8R8_2PLANE_420_UNORM,+ Tf::P010 => F::G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16,+ Tf::Rgb9e5Ufloat => F::E5B9G9R9_UFLOAT_PACK32,+ Tf::Bc1RgbaUnorm => F::BC1_RGBA_UNORM_BLOCK,+ Tf::Bc1RgbaUnormSrgb => F::BC1_RGBA_SRGB_BLOCK,+ Tf::Bc2RgbaUnorm => F::BC2_UNORM_BLOCK,+ Tf::Bc2RgbaUnormSrgb => F::BC2_SRGB_BLOCK,+ Tf::Bc3RgbaUnorm => F::BC3_UNORM_BLOCK,+ Tf::Bc3RgbaUnormSrgb => F::BC3_SRGB_BLOCK,+ Tf::Bc4RUnorm => F::BC4_UNORM_BLOCK,+ Tf::Bc4RSnorm => F::BC4_SNORM_BLOCK,+ Tf::Bc5RgUnorm => F::BC5_UNORM_BLOCK,+ Tf::Bc5RgSnorm => F::BC5_SNORM_BLOCK,+ Tf::Bc6hRgbUfloat => F::BC6H_UFLOAT_BLOCK,+ Tf::Bc6hRgbFloat => F::BC6H_SFLOAT_BLOCK,+ Tf::Bc7RgbaUnorm => F::BC7_UNORM_BLOCK,+ Tf::Bc7RgbaUnormSrgb => F::BC7_SRGB_BLOCK,+ Tf::Etc2Rgb8Unorm => F::ETC2_R8G8B8_UNORM_BLOCK,+ Tf::Etc2Rgb8UnormSrgb => F::ETC2_R8G8B8_SRGB_BLOCK,+ Tf::Etc2Rgb8A1Unorm => F::ETC2_R8G8B8A1_UNORM_BLOCK,+ Tf::Etc2Rgb8A1UnormSrgb => F::ETC2_R8G8B8A1_SRGB_BLOCK,+ Tf::Etc2Rgba8Unorm => F::ETC2_R8G8B8A8_UNORM_BLOCK,+ Tf::Etc2Rgba8UnormSrgb => F::ETC2_R8G8B8A8_SRGB_BLOCK,+ Tf::EacR11Unorm => F::EAC_R11_UNORM_BLOCK,+ Tf::EacR11Snorm => F::EAC_R11_SNORM_BLOCK,+ Tf::EacRg11Unorm => F::EAC_R11G11_UNORM_BLOCK,+ Tf::EacRg11Snorm => F::EAC_R11G11_SNORM_BLOCK,+ Tf::Astc { block, channel } => match channel {+ AstcChannel::Unorm => match block {+ AstcBlock::B4x4 => F::ASTC_4X4_UNORM_BLOCK,+ AstcBlock::B5x4 => F::ASTC_5X4_UNORM_BLOCK,+ AstcBlock::B5x5 => F::ASTC_5X5_UNORM_BLOCK,+ AstcBlock::B6x5 => F::ASTC_6X5_UNORM_BLOCK,+ AstcBlock::B6x6 => F::ASTC_6X6_UNORM_BLOCK,+ AstcBlock::B8x5 => F::ASTC_8X5_UNORM_BLOCK,+ AstcBlock::B8x6 => F::ASTC_8X6_UNORM_BLOCK,+ AstcBlock::B8x8 => F::ASTC_8X8_UNORM_BLOCK,+ AstcBlock::B10x5 => F::ASTC_10X5_UNORM_BLOCK,+ AstcBlock::B10x6 => F::ASTC_10X6_UNORM_BLOCK,+ AstcBlock::B10x8 => F::ASTC_10X8_UNORM_BLOCK,+ AstcBlock::B10x10 => F::ASTC_10X10_UNORM_BLOCK,+ AstcBlock::B12x10 => F::ASTC_12X10_UNORM_BLOCK,+ AstcBlock::B12x12 => F::ASTC_12X12_UNORM_BLOCK,+ },+ AstcChannel::UnormSrgb => match block {+ AstcBlock::B4x4 => F::ASTC_4X4_SRGB_BLOCK,+ AstcBlock::B5x4 => F::ASTC_5X4_SRGB_BLOCK,+ AstcBlock::B5x5 => F::ASTC_5X5_SRGB_BLOCK,+ AstcBlock::B6x5 => F::ASTC_6X5_SRGB_BLOCK,+ AstcBlock::B6x6 => F::ASTC_6X6_SRGB_BLOCK,+ AstcBlock::B8x5 => F::ASTC_8X5_SRGB_BLOCK,+ AstcBlock::B8x6 => F::ASTC_8X6_SRGB_BLOCK,+ AstcBlock::B8x8 => F::ASTC_8X8_SRGB_BLOCK,+ AstcBlock::B10x5 => F::ASTC_10X5_SRGB_BLOCK,+ AstcBlock::B10x6 => F::ASTC_10X6_SRGB_BLOCK,+ AstcBlock::B10x8 => F::ASTC_10X8_SRGB_BLOCK,+ AstcBlock::B10x10 => F::ASTC_10X10_SRGB_BLOCK,+ AstcBlock::B12x10 => F::ASTC_12X10_SRGB_BLOCK,+ AstcBlock::B12x12 => F::ASTC_12X12_SRGB_BLOCK,+ },+ AstcChannel::Hdr => match block {+ AstcBlock::B4x4 => F::ASTC_4X4_SFLOAT_BLOCK_EXT,+ AstcBlock::B5x4 => F::ASTC_5X4_SFLOAT_BLOCK_EXT,+ AstcBlock::B5x5 => F::ASTC_5X5_SFLOAT_BLOCK_EXT,+ AstcBlock::B6x5 => F::ASTC_6X5_SFLOAT_BLOCK_EXT,+ AstcBlock::B6x6 => F::ASTC_6X6_SFLOAT_BLOCK_EXT,+ AstcBlock::B8x5 => F::ASTC_8X5_SFLOAT_BLOCK_EXT,+ AstcBlock::B8x6 => F::ASTC_8X6_SFLOAT_BLOCK_EXT,+ AstcBlock::B8x8 => F::ASTC_8X8_SFLOAT_BLOCK_EXT,+ AstcBlock::B10x5 => F::ASTC_10X5_SFLOAT_BLOCK_EXT,+ AstcBlock::B10x6 => F::ASTC_10X6_SFLOAT_BLOCK_EXT,+ AstcBlock::B10x8 => F::ASTC_10X8_SFLOAT_BLOCK_EXT,+ AstcBlock::B10x10 => F::ASTC_10X10_SFLOAT_BLOCK_EXT,+ AstcBlock::B12x10 => F::ASTC_12X10_SFLOAT_BLOCK_EXT,+ AstcBlock::B12x12 => F::ASTC_12X12_SFLOAT_BLOCK_EXT,+ },+ },+ }+ }+}++pub fn map_vk_surface_formats(+ sf: vk::SurfaceFormatKHR,+) -> Option<(wgt::TextureFormat, wgt::SurfaceColorSpace)> {+ use ash::vk::Format as F;+ use wgt::TextureFormat as Tf;+ // Format list we care about pulled from https://vulkan.gpuinfo.org/listsurfaceformats.php.+ let format = match sf.format {+ F::B8G8R8A8_UNORM => Tf::Bgra8Unorm,+ F::B8G8R8A8_SRGB => Tf::Bgra8UnormSrgb,+ F::R8G8B8A8_SNORM => Tf::Rgba8Snorm,+ F::R8G8B8A8_UNORM => Tf::Rgba8Unorm,+ F::R8G8B8A8_SRGB => Tf::Rgba8UnormSrgb,+ F::R16G16B16A16_SNORM => Tf::Rgba16Snorm,+ F::R16G16B16A16_UNORM => Tf::Rgba16Unorm,+ F::R16G16B16A16_SFLOAT => Tf::Rgba16Float,+ F::A2B10G10R10_UNORM_PACK32 => Tf::Rgb10a2Unorm,+ _ => return None,+ };+ let color_space = map_vk_color_space(sf.color_space)?;+ Some((format, color_space))+}++pub fn map_vk_color_space(color_space: vk::ColorSpaceKHR) -> Option<wgt::SurfaceColorSpace> {+ use wgt::SurfaceColorSpace as Scs;+ Some(match color_space {+ vk::ColorSpaceKHR::SRGB_NONLINEAR => Scs::Srgb,+ vk::ColorSpaceKHR::EXTENDED_SRGB_LINEAR_EXT => Scs::ExtendedSrgbLinear,+ vk::ColorSpaceKHR::EXTENDED_SRGB_NONLINEAR_EXT => Scs::ExtendedSrgb,+ vk::ColorSpaceKHR::DISPLAY_P3_NONLINEAR_EXT => Scs::DisplayP3,+ vk::ColorSpaceKHR::HDR10_ST2084_EXT => Scs::Bt2100Pq,+ vk::ColorSpaceKHR::HDR10_HLG_EXT => Scs::Bt2100Hlg,+ _ => return None,+ })+}++pub fn map_surface_color_space(color_space: wgt::SurfaceColorSpace) -> vk::ColorSpaceKHR {+ use wgt::SurfaceColorSpace as Scs;+ match color_space {+ Scs::Auto => unreachable!("wgpu-core resolves `Auto` before configuring the surface"),+ Scs::Srgb => vk::ColorSpaceKHR::SRGB_NONLINEAR,+ Scs::ExtendedSrgbLinear => vk::ColorSpaceKHR::EXTENDED_SRGB_LINEAR_EXT,+ Scs::ExtendedSrgb => vk::ColorSpaceKHR::EXTENDED_SRGB_NONLINEAR_EXT,+ Scs::DisplayP3 => vk::ColorSpaceKHR::DISPLAY_P3_NONLINEAR_EXT,+ Scs::Bt2100Pq => vk::ColorSpaceKHR::HDR10_ST2084_EXT,+ Scs::Bt2100Hlg => vk::ColorSpaceKHR::HDR10_HLG_EXT,+ Scs::ExtendedDisplayP3 => {+ unreachable!("`ExtendedDisplayP3` is never reported in the Vulkan surface capabilities")+ }+ }+}++impl crate::Attachment<'_, super::TextureView> {+ pub(super) fn make_attachment_key(&self, ops: crate::AttachmentOps) -> super::AttachmentKey {+ super::AttachmentKey {+ format: self.view.raw_format,+ layout: derive_image_layout(self.usage, self.view.format),+ ops,+ }+ }+}++impl crate::ColorAttachment<'_, super::TextureView> {+ pub(super) unsafe fn make_vk_clear_color(&self) -> vk::ClearColorValue {+ let cv = &self.clear_value;+ match self.target.view.format.sample_type(None, None).unwrap() {+ wgt::TextureSampleType::Float { .. } => vk::ClearColorValue {+ float32: [cv.r as f32, cv.g as f32, cv.b as f32, cv.a as f32],+ },+ wgt::TextureSampleType::Sint => vk::ClearColorValue {+ int32: [cv.r as i32, cv.g as i32, cv.b as i32, cv.a as i32],+ },+ wgt::TextureSampleType::Uint => vk::ClearColorValue {+ uint32: [cv.r as u32, cv.g as u32, cv.b as u32, cv.a as u32],+ },+ wgt::TextureSampleType::Depth => unreachable!(),+ }+ }+}++pub fn derive_image_layout(usage: wgt::TextureUses, format: wgt::TextureFormat) -> vk::ImageLayout {+ // Note: depth textures are always sampled with RODS layout+ let is_color = !format.is_depth_stencil_format();+ match usage {+ wgt::TextureUses::UNINITIALIZED => vk::ImageLayout::UNDEFINED,+ wgt::TextureUses::COPY_SRC => vk::ImageLayout::TRANSFER_SRC_OPTIMAL,+ wgt::TextureUses::COPY_DST => vk::ImageLayout::TRANSFER_DST_OPTIMAL,+ wgt::TextureUses::RESOURCE if is_color => vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL,+ wgt::TextureUses::COLOR_TARGET => vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL,+ wgt::TextureUses::DEPTH_STENCIL_WRITE => vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL,+ _ => {+ if usage == wgt::TextureUses::PRESENT {+ vk::ImageLayout::PRESENT_SRC_KHR+ } else if is_color {+ vk::ImageLayout::GENERAL+ } else {+ vk::ImageLayout::DEPTH_STENCIL_READ_ONLY_OPTIMAL+ }+ }+ }+}++pub fn map_texture_usage(usage: wgt::TextureUses) -> vk::ImageUsageFlags {+ let mut flags = vk::ImageUsageFlags::empty();+ if usage.contains(wgt::TextureUses::COPY_SRC) {+ flags |= vk::ImageUsageFlags::TRANSFER_SRC;+ }+ if usage.contains(wgt::TextureUses::COPY_DST) {+ flags |= vk::ImageUsageFlags::TRANSFER_DST;+ }+ if usage.contains(wgt::TextureUses::RESOURCE) {+ flags |= vk::ImageUsageFlags::SAMPLED;+ }+ if usage.contains(wgt::TextureUses::COLOR_TARGET) {+ flags |= vk::ImageUsageFlags::COLOR_ATTACHMENT;+ }+ if usage+ .intersects(wgt::TextureUses::DEPTH_STENCIL_READ | wgt::TextureUses::DEPTH_STENCIL_WRITE)+ {+ flags |= vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT;+ }+ if usage.intersects(+ wgt::TextureUses::STORAGE_READ_ONLY+ | wgt::TextureUses::STORAGE_WRITE_ONLY+ | wgt::TextureUses::STORAGE_READ_WRITE+ | wgt::TextureUses::STORAGE_ATOMIC,+ ) {+ flags |= vk::ImageUsageFlags::STORAGE;+ }+ if usage.contains(wgt::TextureUses::TRANSIENT) {+ flags |= vk::ImageUsageFlags::TRANSIENT_ATTACHMENT;+ }+ flags+}++pub fn map_texture_usage_to_barrier(+ usage: wgt::TextureUses,+) -> (vk::PipelineStageFlags, vk::AccessFlags) {+ let mut stages = vk::PipelineStageFlags::empty();+ let mut access = vk::AccessFlags::empty();+ let shader_stages = vk::PipelineStageFlags::VERTEX_SHADER+ | vk::PipelineStageFlags::FRAGMENT_SHADER+ | vk::PipelineStageFlags::COMPUTE_SHADER;++ if usage.contains(wgt::TextureUses::COPY_SRC) {+ stages |= vk::PipelineStageFlags::TRANSFER;+ access |= vk::AccessFlags::TRANSFER_READ;+ }+ if usage.contains(wgt::TextureUses::COPY_DST) {+ stages |= vk::PipelineStageFlags::TRANSFER;+ access |= vk::AccessFlags::TRANSFER_WRITE;+ }+ if usage.contains(wgt::TextureUses::RESOURCE) {+ stages |= shader_stages;+ access |= vk::AccessFlags::SHADER_READ;+ }+ if usage.contains(wgt::TextureUses::COLOR_TARGET) {+ stages |= vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT;+ access |= vk::AccessFlags::COLOR_ATTACHMENT_READ | vk::AccessFlags::COLOR_ATTACHMENT_WRITE;+ }+ if usage.intersects(wgt::TextureUses::DEPTH_STENCIL_READ) {+ stages |= vk::PipelineStageFlags::EARLY_FRAGMENT_TESTS+ | vk::PipelineStageFlags::LATE_FRAGMENT_TESTS;+ access |= vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_READ;+ }+ if usage.intersects(wgt::TextureUses::DEPTH_STENCIL_WRITE) {+ stages |= vk::PipelineStageFlags::EARLY_FRAGMENT_TESTS+ | vk::PipelineStageFlags::LATE_FRAGMENT_TESTS;+ access |= vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_READ+ | vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_WRITE;+ }+ if usage.intersects(+ wgt::TextureUses::STORAGE_READ_ONLY+ | wgt::TextureUses::STORAGE_READ_WRITE+ | wgt::TextureUses::STORAGE_ATOMIC,+ ) {+ stages |= shader_stages;+ access |= vk::AccessFlags::SHADER_READ;+ }+ if usage.intersects(+ wgt::TextureUses::STORAGE_WRITE_ONLY+ | wgt::TextureUses::STORAGE_READ_WRITE+ | wgt::TextureUses::STORAGE_ATOMIC,+ ) {+ stages |= shader_stages;+ access |= vk::AccessFlags::SHADER_WRITE;+ }++ if usage == wgt::TextureUses::UNINITIALIZED || usage == wgt::TextureUses::PRESENT {+ (+ vk::PipelineStageFlags::TOP_OF_PIPE,+ vk::AccessFlags::empty(),+ )+ } else {+ (stages, access)+ }+}++pub fn map_vk_image_usage(usage: vk::ImageUsageFlags) -> wgt::TextureUses {+ let mut bits = wgt::TextureUses::empty();+ if usage.contains(vk::ImageUsageFlags::TRANSFER_SRC) {+ bits |= wgt::TextureUses::COPY_SRC;+ }+ if usage.contains(vk::ImageUsageFlags::TRANSFER_DST) {+ bits |= wgt::TextureUses::COPY_DST;+ }+ if usage.contains(vk::ImageUsageFlags::SAMPLED) {+ bits |= wgt::TextureUses::RESOURCE;+ }+ if usage.contains(vk::ImageUsageFlags::COLOR_ATTACHMENT) {+ bits |= wgt::TextureUses::COLOR_TARGET;+ }+ if usage.contains(vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT) {+ bits |= wgt::TextureUses::DEPTH_STENCIL_READ | wgt::TextureUses::DEPTH_STENCIL_WRITE;+ }+ if usage.contains(vk::ImageUsageFlags::STORAGE) {+ bits |= wgt::TextureUses::STORAGE_READ_ONLY+ | wgt::TextureUses::STORAGE_WRITE_ONLY+ | wgt::TextureUses::STORAGE_READ_WRITE+ | wgt::TextureUses::STORAGE_ATOMIC;+ }+ if usage.contains(vk::ImageUsageFlags::TRANSIENT_ATTACHMENT) {+ bits |= wgt::TextureUses::TRANSIENT;+ }+ bits+}++pub fn map_texture_dimension(dim: wgt::TextureDimension) -> vk::ImageType {+ match dim {+ wgt::TextureDimension::D1 => vk::ImageType::TYPE_1D,+ wgt::TextureDimension::D2 => vk::ImageType::TYPE_2D,+ wgt::TextureDimension::D3 => vk::ImageType::TYPE_3D,+ }+}++pub fn map_index_format(index_format: wgt::IndexFormat) -> vk::IndexType {+ match index_format {+ wgt::IndexFormat::Uint16 => vk::IndexType::UINT16,+ wgt::IndexFormat::Uint32 => vk::IndexType::UINT32,+ }+}++pub fn map_vertex_format(vertex_format: wgt::VertexFormat) -> vk::Format {+ use wgt::VertexFormat as Vf;+ match vertex_format {+ Vf::Uint8 => vk::Format::R8_UINT,+ Vf::Uint8x2 => vk::Format::R8G8_UINT,+ Vf::Uint8x4 => vk::Format::R8G8B8A8_UINT,+ Vf::Sint8 => vk::Format::R8_SINT,+ Vf::Sint8x2 => vk::Format::R8G8_SINT,+ Vf::Sint8x4 => vk::Format::R8G8B8A8_SINT,+ Vf::Unorm8 => vk::Format::R8_UNORM,+ Vf::Unorm8x2 => vk::Format::R8G8_UNORM,+ Vf::Unorm8x4 => vk::Format::R8G8B8A8_UNORM,+ Vf::Snorm8 => vk::Format::R8_SNORM,+ Vf::Snorm8x2 => vk::Format::R8G8_SNORM,+ Vf::Snorm8x4 => vk::Format::R8G8B8A8_SNORM,+ Vf::Uint16 => vk::Format::R16_UINT,+ Vf::Uint16x2 => vk::Format::R16G16_UINT,+ Vf::Uint16x4 => vk::Format::R16G16B16A16_UINT,+ Vf::Sint16 => vk::Format::R16_SINT,+ Vf::Sint16x2 => vk::Format::R16G16_SINT,+ Vf::Sint16x4 => vk::Format::R16G16B16A16_SINT,+ Vf::Unorm16 => vk::Format::R16_UNORM,+ Vf::Unorm16x2 => vk::Format::R16G16_UNORM,+ Vf::Unorm16x4 => vk::Format::R16G16B16A16_UNORM,+ Vf::Snorm16 => vk::Format::R16_SNORM,+ Vf::Snorm16x2 => vk::Format::R16G16_SNORM,+ Vf::Snorm16x4 => vk::Format::R16G16B16A16_SNORM,+ Vf::Float16 => vk::Format::R16_SFLOAT,+ Vf::Float16x2 => vk::Format::R16G16_SFLOAT,+ Vf::Float16x4 => vk::Format::R16G16B16A16_SFLOAT,+ Vf::Float32 => vk::Format::R32_SFLOAT,+ Vf::Float32x2 => vk::Format::R32G32_SFLOAT,+ Vf::Float32x3 => vk::Format::R32G32B32_SFLOAT,+ Vf::Float32x4 => vk::Format::R32G32B32A32_SFLOAT,+ Vf::Uint32 => vk::Format::R32_UINT,+ Vf::Uint32x2 => vk::Format::R32G32_UINT,+ Vf::Uint32x3 => vk::Format::R32G32B32_UINT,+ Vf::Uint32x4 => vk::Format::R32G32B32A32_UINT,+ Vf::Sint32 => vk::Format::R32_SINT,+ Vf::Sint32x2 => vk::Format::R32G32_SINT,+ Vf::Sint32x3 => vk::Format::R32G32B32_SINT,+ Vf::Sint32x4 => vk::Format::R32G32B32A32_SINT,+ Vf::Float64 => vk::Format::R64_SFLOAT,+ Vf::Float64x2 => vk::Format::R64G64_SFLOAT,+ Vf::Float64x3 => vk::Format::R64G64B64_SFLOAT,+ Vf::Float64x4 => vk::Format::R64G64B64A64_SFLOAT,+ Vf::Unorm10_10_10_2 => vk::Format::A2B10G10R10_UNORM_PACK32,+ Vf::Unorm8x4Bgra => vk::Format::B8G8R8A8_UNORM,+ }+}++pub fn map_aspects(aspects: crate::FormatAspects) -> vk::ImageAspectFlags {+ let mut flags = vk::ImageAspectFlags::empty();+ if aspects.contains(crate::FormatAspects::COLOR) {+ flags |= vk::ImageAspectFlags::COLOR;+ }+ if aspects.contains(crate::FormatAspects::DEPTH) {+ flags |= vk::ImageAspectFlags::DEPTH;+ }+ if aspects.contains(crate::FormatAspects::STENCIL) {+ flags |= vk::ImageAspectFlags::STENCIL;+ }+ if aspects.contains(crate::FormatAspects::PLANE_0) {+ flags |= vk::ImageAspectFlags::PLANE_0;+ }+ if aspects.contains(crate::FormatAspects::PLANE_1) {+ flags |= vk::ImageAspectFlags::PLANE_1;+ }+ if aspects.contains(crate::FormatAspects::PLANE_2) {+ flags |= vk::ImageAspectFlags::PLANE_2;+ }+ flags+}++pub fn map_attachment_ops(+ op: crate::AttachmentOps,+) -> (vk::AttachmentLoadOp, vk::AttachmentStoreOp) {+ let load_op = if op.contains(crate::AttachmentOps::LOAD) {+ vk::AttachmentLoadOp::LOAD+ } else if op.contains(crate::AttachmentOps::LOAD_DONT_CARE) {+ vk::AttachmentLoadOp::DONT_CARE+ } else if op.contains(crate::AttachmentOps::LOAD_CLEAR) {+ vk::AttachmentLoadOp::CLEAR+ } else {+ unreachable!()+ };+ let store_op = if op.contains(crate::AttachmentOps::STORE) {+ vk::AttachmentStoreOp::STORE+ } else if op.contains(crate::AttachmentOps::STORE_DISCARD) {+ vk::AttachmentStoreOp::DONT_CARE+ } else {+ unreachable!()+ };+ (load_op, store_op)+}++pub fn map_present_mode(mode: wgt::PresentMode) -> vk::PresentModeKHR {+ match mode {+ wgt::PresentMode::Immediate => vk::PresentModeKHR::IMMEDIATE,+ wgt::PresentMode::Mailbox => vk::PresentModeKHR::MAILBOX,+ wgt::PresentMode::Fifo => vk::PresentModeKHR::FIFO,+ wgt::PresentMode::FifoRelaxed => vk::PresentModeKHR::FIFO_RELAXED,+ wgt::PresentMode::AutoNoVsync | wgt::PresentMode::AutoVsync => {+ unreachable!("Cannot create swapchain with Auto PresentationMode")+ }+ }+}++pub fn map_vk_present_mode(mode: vk::PresentModeKHR) -> Option<wgt::PresentMode> {+ // Not exposed in Ash yet.+ const FIFO_LATEST_READY: vk::PresentModeKHR = vk::PresentModeKHR::from_raw(1_000_361_000);++ // See https://registry.khronos.org/vulkan/specs/latest/man/html/VkPresentModeKHR.html+ match mode {+ vk::PresentModeKHR::IMMEDIATE => Some(wgt::PresentMode::Immediate),+ vk::PresentModeKHR::MAILBOX => Some(wgt::PresentMode::Mailbox),+ vk::PresentModeKHR::FIFO => Some(wgt::PresentMode::Fifo),+ vk::PresentModeKHR::FIFO_RELAXED => Some(wgt::PresentMode::FifoRelaxed),++ // Modes that aren't exposed yet.+ vk::PresentModeKHR::SHARED_DEMAND_REFRESH => None,+ vk::PresentModeKHR::SHARED_CONTINUOUS_REFRESH => None,+ FIFO_LATEST_READY => None,++ _ => {+ log::debug!("Unrecognized present mode {mode:?}");+ None+ }+ }+}++pub fn map_composite_alpha_mode(mode: wgt::CompositeAlphaMode) -> vk::CompositeAlphaFlagsKHR {+ match mode {+ wgt::CompositeAlphaMode::Opaque => vk::CompositeAlphaFlagsKHR::OPAQUE,+ wgt::CompositeAlphaMode::PreMultiplied => vk::CompositeAlphaFlagsKHR::PRE_MULTIPLIED,+ wgt::CompositeAlphaMode::PostMultiplied => vk::CompositeAlphaFlagsKHR::POST_MULTIPLIED,+ wgt::CompositeAlphaMode::Inherit => vk::CompositeAlphaFlagsKHR::INHERIT,+ wgt::CompositeAlphaMode::Auto => unreachable!(),+ }+}++pub fn map_vk_composite_alpha(flags: vk::CompositeAlphaFlagsKHR) -> Vec<wgt::CompositeAlphaMode> {+ let mut modes = Vec::new();+ if flags.contains(vk::CompositeAlphaFlagsKHR::OPAQUE) {+ modes.push(wgt::CompositeAlphaMode::Opaque);+ }+ if flags.contains(vk::CompositeAlphaFlagsKHR::PRE_MULTIPLIED) {+ modes.push(wgt::CompositeAlphaMode::PreMultiplied);+ }+ if flags.contains(vk::CompositeAlphaFlagsKHR::POST_MULTIPLIED) {+ modes.push(wgt::CompositeAlphaMode::PostMultiplied);+ }+ if flags.contains(vk::CompositeAlphaFlagsKHR::INHERIT) {+ modes.push(wgt::CompositeAlphaMode::Inherit);+ }+ modes+}++pub fn map_buffer_usage(usage: wgt::BufferUses) -> vk::BufferUsageFlags {+ let mut flags = vk::BufferUsageFlags::empty();+ if usage.contains(wgt::BufferUses::COPY_SRC) {+ flags |= vk::BufferUsageFlags::TRANSFER_SRC;+ }+ if usage.contains(wgt::BufferUses::COPY_DST) {+ flags |= vk::BufferUsageFlags::TRANSFER_DST;+ }+ if usage.contains(wgt::BufferUses::UNIFORM) {+ flags |= vk::BufferUsageFlags::UNIFORM_BUFFER;+ }+ if usage.intersects(wgt::BufferUses::STORAGE_READ_ONLY | wgt::BufferUses::STORAGE_READ_WRITE) {+ flags |= vk::BufferUsageFlags::STORAGE_BUFFER;+ }+ if usage.contains(wgt::BufferUses::INDEX) {+ flags |= vk::BufferUsageFlags::INDEX_BUFFER;+ }+ if usage.contains(wgt::BufferUses::VERTEX) {+ flags |= vk::BufferUsageFlags::VERTEX_BUFFER;+ }+ if usage.contains(wgt::BufferUses::INDIRECT) {+ flags |= vk::BufferUsageFlags::INDIRECT_BUFFER;+ }+ if usage.contains(wgt::BufferUses::ACCELERATION_STRUCTURE_SCRATCH) {+ flags |= vk::BufferUsageFlags::STORAGE_BUFFER | vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS;+ }+ if usage.intersects(+ wgt::BufferUses::BOTTOM_LEVEL_ACCELERATION_STRUCTURE_INPUT+ | wgt::BufferUses::TOP_LEVEL_ACCELERATION_STRUCTURE_INPUT,+ ) {+ flags |= vk::BufferUsageFlags::ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_KHR+ | vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS;+ }+ if usage.intersects(wgt::BufferUses::ACCELERATION_STRUCTURE_QUERY) {+ flags |= vk::BufferUsageFlags::TRANSFER_DST;+ }+ if usage.intersects(wgt::BufferUses::RAY_TRACING_PIPELINE_SHADER_DATA) {+ flags |= vk::BufferUsageFlags::SHADER_BINDING_TABLE_KHR+ | vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS;+ }+ flags+}++pub fn map_buffer_usage_to_barrier(+ usage: wgt::BufferUses,+) -> (vk::PipelineStageFlags, vk::AccessFlags) {+ let mut stages = vk::PipelineStageFlags::empty();+ let mut access = vk::AccessFlags::empty();+ let shader_stages = vk::PipelineStageFlags::VERTEX_SHADER+ | vk::PipelineStageFlags::FRAGMENT_SHADER+ | vk::PipelineStageFlags::COMPUTE_SHADER;++ if usage.contains(wgt::BufferUses::MAP_READ) {+ stages |= vk::PipelineStageFlags::HOST;+ access |= vk::AccessFlags::HOST_READ;+ }+ if usage.contains(wgt::BufferUses::MAP_WRITE) {+ stages |= vk::PipelineStageFlags::HOST;+ access |= vk::AccessFlags::HOST_WRITE;+ }+ if usage.contains(wgt::BufferUses::COPY_SRC) {+ stages |= vk::PipelineStageFlags::TRANSFER;+ access |= vk::AccessFlags::TRANSFER_READ;+ }+ if usage.contains(wgt::BufferUses::COPY_DST) {+ stages |= vk::PipelineStageFlags::TRANSFER;+ access |= vk::AccessFlags::TRANSFER_WRITE;+ }+ if usage.contains(wgt::BufferUses::UNIFORM) {+ stages |= shader_stages;+ access |= vk::AccessFlags::UNIFORM_READ;+ }+ if usage.intersects(wgt::BufferUses::STORAGE_READ_ONLY) {+ stages |= shader_stages;+ access |= vk::AccessFlags::SHADER_READ;+ }+ if usage.intersects(wgt::BufferUses::STORAGE_READ_WRITE) {+ stages |= shader_stages;+ access |= vk::AccessFlags::SHADER_READ | vk::AccessFlags::SHADER_WRITE;+ }+ if usage.contains(wgt::BufferUses::INDEX) {+ stages |= vk::PipelineStageFlags::VERTEX_INPUT;+ access |= vk::AccessFlags::INDEX_READ;+ }+ if usage.contains(wgt::BufferUses::VERTEX) {+ stages |= vk::PipelineStageFlags::VERTEX_INPUT;+ access |= vk::AccessFlags::VERTEX_ATTRIBUTE_READ;+ }+ if usage.contains(wgt::BufferUses::INDIRECT) {+ stages |= vk::PipelineStageFlags::DRAW_INDIRECT;+ access |= vk::AccessFlags::INDIRECT_COMMAND_READ;+ }+ if usage.intersects(+ wgt::BufferUses::BOTTOM_LEVEL_ACCELERATION_STRUCTURE_INPUT+ | wgt::BufferUses::TOP_LEVEL_ACCELERATION_STRUCTURE_INPUT+ | wgt::BufferUses::ACCELERATION_STRUCTURE_SCRATCH,+ ) {+ stages |= vk::PipelineStageFlags::ACCELERATION_STRUCTURE_BUILD_KHR;+ access |= vk::AccessFlags::ACCELERATION_STRUCTURE_READ_KHR+ | vk::AccessFlags::ACCELERATION_STRUCTURE_WRITE_KHR+ | vk::AccessFlags::SHADER_READ;+ }+ if usage.contains(wgt::BufferUses::ACCELERATION_STRUCTURE_QUERY) {+ stages |= vk::PipelineStageFlags::TRANSFER;+ access |= vk::AccessFlags::TRANSFER_WRITE;+ }++ (stages, access)+}++pub fn map_view_dimension(dim: wgt::TextureViewDimension) -> vk::ImageViewType {+ match dim {+ wgt::TextureViewDimension::D1 => vk::ImageViewType::TYPE_1D,+ wgt::TextureViewDimension::D2 => vk::ImageViewType::TYPE_2D,+ wgt::TextureViewDimension::D2Array => vk::ImageViewType::TYPE_2D_ARRAY,+ wgt::TextureViewDimension::Cube => vk::ImageViewType::CUBE,+ wgt::TextureViewDimension::CubeArray => vk::ImageViewType::CUBE_ARRAY,+ wgt::TextureViewDimension::D3 => vk::ImageViewType::TYPE_3D,+ }+}++pub fn map_copy_extent(extent: &crate::CopyExtent) -> vk::Extent3D {+ vk::Extent3D {+ width: extent.width,+ height: extent.height,+ depth: extent.depth,+ }+}++pub fn map_subresource_range(+ range: &wgt::ImageSubresourceRange,+ format: wgt::TextureFormat,+) -> vk::ImageSubresourceRange {+ vk::ImageSubresourceRange {+ aspect_mask: map_aspects(crate::FormatAspects::new(format, range.aspect)),+ base_mip_level: range.base_mip_level,+ level_count: range.mip_level_count.unwrap_or(vk::REMAINING_MIP_LEVELS),+ base_array_layer: range.base_array_layer,+ layer_count: range+ .array_layer_count+ .unwrap_or(vk::REMAINING_ARRAY_LAYERS),+ }+}++// Special subresource range mapping for dealing with barriers+// so that we account for the "hidden" depth aspect in emulated Stencil8.+pub(super) fn map_subresource_range_combined_aspect(+ range: &wgt::ImageSubresourceRange,+ format: wgt::TextureFormat,+ private_caps: &super::PrivateCapabilities,+) -> vk::ImageSubresourceRange {+ let mut range = map_subresource_range(range, format);+ if !private_caps.texture_s8 && format == wgt::TextureFormat::Stencil8 {+ range.aspect_mask |= vk::ImageAspectFlags::DEPTH;+ }+ range+}++pub fn map_subresource_layers(+ base: &crate::TextureCopyBase,+) -> (vk::ImageSubresourceLayers, vk::Offset3D) {+ let offset = vk::Offset3D {+ x: base.origin.x as i32,+ y: base.origin.y as i32,+ z: base.origin.z as i32,+ };+ let subresource = vk::ImageSubresourceLayers {+ aspect_mask: map_aspects(base.aspect),+ mip_level: base.mip_level,+ base_array_layer: base.array_layer,+ layer_count: 1,+ };+ (subresource, offset)+}++pub fn map_filter_mode(mode: wgt::FilterMode) -> vk::Filter {+ match mode {+ wgt::FilterMode::Nearest => vk::Filter::NEAREST,+ wgt::FilterMode::Linear => vk::Filter::LINEAR,+ }+}++pub fn map_mip_filter_mode(mode: wgt::MipmapFilterMode) -> vk::SamplerMipmapMode {+ match mode {+ wgt::MipmapFilterMode::Nearest => vk::SamplerMipmapMode::NEAREST,+ wgt::MipmapFilterMode::Linear => vk::SamplerMipmapMode::LINEAR,+ }+}++pub fn map_address_mode(mode: wgt::AddressMode) -> vk::SamplerAddressMode {+ match mode {+ wgt::AddressMode::ClampToEdge => vk::SamplerAddressMode::CLAMP_TO_EDGE,+ wgt::AddressMode::Repeat => vk::SamplerAddressMode::REPEAT,+ wgt::AddressMode::MirrorRepeat => vk::SamplerAddressMode::MIRRORED_REPEAT,+ wgt::AddressMode::ClampToBorder => vk::SamplerAddressMode::CLAMP_TO_BORDER,+ // wgt::AddressMode::MirrorClamp => vk::SamplerAddressMode::MIRROR_CLAMP_TO_EDGE,+ }+}++pub fn map_border_color(border_color: wgt::SamplerBorderColor) -> vk::BorderColor {+ match border_color {+ wgt::SamplerBorderColor::TransparentBlack | wgt::SamplerBorderColor::Zero => {+ vk::BorderColor::FLOAT_TRANSPARENT_BLACK+ }+ wgt::SamplerBorderColor::OpaqueBlack => vk::BorderColor::FLOAT_OPAQUE_BLACK,+ wgt::SamplerBorderColor::OpaqueWhite => vk::BorderColor::FLOAT_OPAQUE_WHITE,+ }+}++pub fn map_comparison(fun: wgt::CompareFunction) -> vk::CompareOp {+ use wgt::CompareFunction as Cf;+ match fun {+ Cf::Never => vk::CompareOp::NEVER,+ Cf::Less => vk::CompareOp::LESS,+ Cf::LessEqual => vk::CompareOp::LESS_OR_EQUAL,+ Cf::Equal => vk::CompareOp::EQUAL,+ Cf::GreaterEqual => vk::CompareOp::GREATER_OR_EQUAL,+ Cf::Greater => vk::CompareOp::GREATER,+ Cf::NotEqual => vk::CompareOp::NOT_EQUAL,+ Cf::Always => vk::CompareOp::ALWAYS,+ }+}++pub fn map_shader_stage(stage: wgt::ShaderStages) -> vk::ShaderStageFlags {+ let mut flags = vk::ShaderStageFlags::empty();+ if stage.contains(wgt::ShaderStages::VERTEX) {+ flags |= vk::ShaderStageFlags::VERTEX;+ }+ if stage.contains(wgt::ShaderStages::FRAGMENT) {+ flags |= vk::ShaderStageFlags::FRAGMENT;+ }+ if stage.contains(wgt::ShaderStages::COMPUTE) {+ flags |= vk::ShaderStageFlags::COMPUTE;+ }+ if stage.contains(wgt::ShaderStages::TASK) {+ flags |= vk::ShaderStageFlags::TASK_EXT;+ }+ if stage.contains(wgt::ShaderStages::MESH) {+ flags |= vk::ShaderStageFlags::MESH_EXT;+ }+ if stage.contains(wgt::ShaderStages::RAY_GENERATION) {+ flags |= vk::ShaderStageFlags::RAYGEN_KHR;+ }+ if stage.contains(wgt::ShaderStages::MISS) {+ flags |= vk::ShaderStageFlags::MISS_KHR;+ }+ if stage.contains(wgt::ShaderStages::ANY_HIT) {+ flags |= vk::ShaderStageFlags::ANY_HIT_KHR;+ }+ if stage.contains(wgt::ShaderStages::CLOSEST_HIT) {+ flags |= vk::ShaderStageFlags::CLOSEST_HIT_KHR;+ }+ flags+}++pub fn map_binding_type(ty: wgt::BindingType) -> vk::DescriptorType {+ match ty {+ wgt::BindingType::Buffer {+ ty,+ has_dynamic_offset,+ ..+ } => match ty {+ wgt::BufferBindingType::Storage { .. } => match has_dynamic_offset {+ true => vk::DescriptorType::STORAGE_BUFFER_DYNAMIC,+ false => vk::DescriptorType::STORAGE_BUFFER,+ },+ wgt::BufferBindingType::Uniform => match has_dynamic_offset {+ true => vk::DescriptorType::UNIFORM_BUFFER_DYNAMIC,+ false => vk::DescriptorType::UNIFORM_BUFFER,+ },+ },+ wgt::BindingType::Sampler { .. } => vk::DescriptorType::SAMPLER,+ wgt::BindingType::Texture { .. } => vk::DescriptorType::SAMPLED_IMAGE,+ wgt::BindingType::StorageTexture { .. } => vk::DescriptorType::STORAGE_IMAGE,+ wgt::BindingType::AccelerationStructure { .. } => {+ vk::DescriptorType::ACCELERATION_STRUCTURE_KHR+ }+ wgt::BindingType::ExternalTexture => unimplemented!(),+ }+}++pub fn map_topology(topology: wgt::PrimitiveTopology) -> vk::PrimitiveTopology {+ use wgt::PrimitiveTopology as Pt;+ match topology {+ Pt::PointList => vk::PrimitiveTopology::POINT_LIST,+ Pt::LineList => vk::PrimitiveTopology::LINE_LIST,+ Pt::LineStrip => vk::PrimitiveTopology::LINE_STRIP,+ Pt::TriangleList => vk::PrimitiveTopology::TRIANGLE_LIST,+ Pt::TriangleStrip => vk::PrimitiveTopology::TRIANGLE_STRIP,+ }+}++pub fn map_polygon_mode(mode: wgt::PolygonMode) -> vk::PolygonMode {+ match mode {+ wgt::PolygonMode::Fill => vk::PolygonMode::FILL,+ wgt::PolygonMode::Line => vk::PolygonMode::LINE,+ wgt::PolygonMode::Point => vk::PolygonMode::POINT,+ }+}++pub fn map_front_face(front_face: wgt::FrontFace) -> vk::FrontFace {+ match front_face {+ wgt::FrontFace::Cw => vk::FrontFace::CLOCKWISE,+ wgt::FrontFace::Ccw => vk::FrontFace::COUNTER_CLOCKWISE,+ }+}++pub fn map_cull_face(face: wgt::Face) -> vk::CullModeFlags {+ match face {+ wgt::Face::Front => vk::CullModeFlags::FRONT,+ wgt::Face::Back => vk::CullModeFlags::BACK,+ }+}++pub fn map_stencil_op(op: wgt::StencilOperation) -> vk::StencilOp {+ use wgt::StencilOperation as So;+ match op {+ So::Keep => vk::StencilOp::KEEP,+ So::Zero => vk::StencilOp::ZERO,+ So::Replace => vk::StencilOp::REPLACE,+ So::Invert => vk::StencilOp::INVERT,+ So::IncrementClamp => vk::StencilOp::INCREMENT_AND_CLAMP,+ So::IncrementWrap => vk::StencilOp::INCREMENT_AND_WRAP,+ So::DecrementClamp => vk::StencilOp::DECREMENT_AND_CLAMP,+ So::DecrementWrap => vk::StencilOp::DECREMENT_AND_WRAP,+ }+}++pub fn map_stencil_face(+ face: &wgt::StencilFaceState,+ compare_mask: u32,+ write_mask: u32,+) -> vk::StencilOpState {+ vk::StencilOpState {+ fail_op: map_stencil_op(face.fail_op),+ pass_op: map_stencil_op(face.pass_op),+ depth_fail_op: map_stencil_op(face.depth_fail_op),+ compare_op: map_comparison(face.compare),+ compare_mask,+ write_mask,+ reference: 0,+ }+}++fn map_blend_factor(factor: wgt::BlendFactor) -> vk::BlendFactor {+ use wgt::BlendFactor as Bf;+ match factor {+ Bf::Zero => vk::BlendFactor::ZERO,+ Bf::One => vk::BlendFactor::ONE,+ Bf::Src => vk::BlendFactor::SRC_COLOR,+ Bf::OneMinusSrc => vk::BlendFactor::ONE_MINUS_SRC_COLOR,+ Bf::SrcAlpha => vk::BlendFactor::SRC_ALPHA,+ Bf::OneMinusSrcAlpha => vk::BlendFactor::ONE_MINUS_SRC_ALPHA,+ Bf::Dst => vk::BlendFactor::DST_COLOR,+ Bf::OneMinusDst => vk::BlendFactor::ONE_MINUS_DST_COLOR,+ Bf::DstAlpha => vk::BlendFactor::DST_ALPHA,+ Bf::OneMinusDstAlpha => vk::BlendFactor::ONE_MINUS_DST_ALPHA,+ Bf::SrcAlphaSaturated => vk::BlendFactor::SRC_ALPHA_SATURATE,+ Bf::Constant => vk::BlendFactor::CONSTANT_COLOR,+ Bf::OneMinusConstant => vk::BlendFactor::ONE_MINUS_CONSTANT_COLOR,+ Bf::Src1 => vk::BlendFactor::SRC1_COLOR,+ Bf::OneMinusSrc1 => vk::BlendFactor::ONE_MINUS_SRC1_COLOR,+ Bf::Src1Alpha => vk::BlendFactor::SRC1_ALPHA,+ Bf::OneMinusSrc1Alpha => vk::BlendFactor::ONE_MINUS_SRC1_ALPHA,+ }+}++fn map_blend_op(operation: wgt::BlendOperation) -> vk::BlendOp {+ use wgt::BlendOperation as Bo;+ match operation {+ Bo::Add => vk::BlendOp::ADD,+ Bo::Subtract => vk::BlendOp::SUBTRACT,+ Bo::ReverseSubtract => vk::BlendOp::REVERSE_SUBTRACT,+ Bo::Min => vk::BlendOp::MIN,+ Bo::Max => vk::BlendOp::MAX,+ }+}++pub fn map_blend_component(+ component: &wgt::BlendComponent,+) -> (vk::BlendOp, vk::BlendFactor, vk::BlendFactor) {+ let op = map_blend_op(component.operation);+ let src = map_blend_factor(component.src_factor);+ let dst = map_blend_factor(component.dst_factor);+ (op, src, dst)+}++pub fn map_pipeline_statistics(+ types: wgt::PipelineStatisticsTypes,+) -> vk::QueryPipelineStatisticFlags {+ use wgt::PipelineStatisticsTypes as Pst;+ let mut flags = vk::QueryPipelineStatisticFlags::empty();+ if types.contains(Pst::VERTEX_SHADER_INVOCATIONS) {+ flags |= vk::QueryPipelineStatisticFlags::VERTEX_SHADER_INVOCATIONS;+ }+ if types.contains(Pst::CLIPPER_INVOCATIONS) {+ flags |= vk::QueryPipelineStatisticFlags::CLIPPING_INVOCATIONS;+ }+ if types.contains(Pst::CLIPPER_PRIMITIVES_OUT) {+ flags |= vk::QueryPipelineStatisticFlags::CLIPPING_PRIMITIVES;+ }+ if types.contains(Pst::FRAGMENT_SHADER_INVOCATIONS) {+ flags |= vk::QueryPipelineStatisticFlags::FRAGMENT_SHADER_INVOCATIONS;+ }+ if types.contains(Pst::COMPUTE_SHADER_INVOCATIONS) {+ flags |= vk::QueryPipelineStatisticFlags::COMPUTE_SHADER_INVOCATIONS;+ }+ flags+}++pub fn map_acceleration_structure_format(+ format: crate::AccelerationStructureFormat,+) -> vk::AccelerationStructureTypeKHR {+ match format {+ crate::AccelerationStructureFormat::TopLevel => vk::AccelerationStructureTypeKHR::TOP_LEVEL,+ crate::AccelerationStructureFormat::BottomLevel => {+ vk::AccelerationStructureTypeKHR::BOTTOM_LEVEL+ }+ }+}++pub fn map_acceleration_structure_build_mode(+ format: crate::AccelerationStructureBuildMode,+) -> vk::BuildAccelerationStructureModeKHR {+ match format {+ crate::AccelerationStructureBuildMode::Build => {+ vk::BuildAccelerationStructureModeKHR::BUILD+ }+ crate::AccelerationStructureBuildMode::Update => {+ vk::BuildAccelerationStructureModeKHR::UPDATE+ }+ }+}++pub fn map_acceleration_structure_flags(+ flags: crate::AccelerationStructureBuildFlags,+) -> vk::BuildAccelerationStructureFlagsKHR {+ let mut vk_flags = vk::BuildAccelerationStructureFlagsKHR::empty();++ if flags.contains(crate::AccelerationStructureBuildFlags::PREFER_FAST_TRACE) {+ vk_flags |= vk::BuildAccelerationStructureFlagsKHR::PREFER_FAST_TRACE;+ }++ if flags.contains(crate::AccelerationStructureBuildFlags::PREFER_FAST_BUILD) {+ vk_flags |= vk::BuildAccelerationStructureFlagsKHR::PREFER_FAST_BUILD;+ }++ if flags.contains(crate::AccelerationStructureBuildFlags::ALLOW_UPDATE) {+ vk_flags |= vk::BuildAccelerationStructureFlagsKHR::ALLOW_UPDATE;+ }++ if flags.contains(crate::AccelerationStructureBuildFlags::LOW_MEMORY) {+ vk_flags |= vk::BuildAccelerationStructureFlagsKHR::LOW_MEMORY;+ }++ if flags.contains(crate::AccelerationStructureBuildFlags::ALLOW_COMPACTION) {+ vk_flags |= vk::BuildAccelerationStructureFlagsKHR::ALLOW_COMPACTION+ }++ if flags.contains(crate::AccelerationStructureBuildFlags::ALLOW_RAY_HIT_VERTEX_RETURN) {+ vk_flags |= vk::BuildAccelerationStructureFlagsKHR::ALLOW_DATA_ACCESS+ }++ vk_flags+}++pub fn map_acceleration_structure_geometry_flags(+ flags: crate::AccelerationStructureGeometryFlags,+) -> vk::GeometryFlagsKHR {+ let mut vk_flags = vk::GeometryFlagsKHR::empty();++ if flags.contains(crate::AccelerationStructureGeometryFlags::OPAQUE) {+ vk_flags |= vk::GeometryFlagsKHR::OPAQUE;+ }++ if flags.contains(crate::AccelerationStructureGeometryFlags::NO_DUPLICATE_ANY_HIT_INVOCATION) {+ vk_flags |= vk::GeometryFlagsKHR::NO_DUPLICATE_ANY_HIT_INVOCATION;+ }++ vk_flags+}++pub fn map_acceleration_structure_usage_to_barrier(+ usage: crate::AccelerationStructureUses,+ features: wgt::Features,+) -> (vk::PipelineStageFlags, vk::AccessFlags) {+ let mut stages = vk::PipelineStageFlags::empty();+ let mut access = vk::AccessFlags::empty();++ if usage.contains(crate::AccelerationStructureUses::BUILD_INPUT) {+ stages |= vk::PipelineStageFlags::ACCELERATION_STRUCTURE_BUILD_KHR;+ access |= vk::AccessFlags::ACCELERATION_STRUCTURE_READ_KHR;+ }+ if usage.contains(crate::AccelerationStructureUses::QUERY_INPUT) {+ stages |= vk::PipelineStageFlags::ACCELERATION_STRUCTURE_BUILD_KHR;+ access |= vk::AccessFlags::ACCELERATION_STRUCTURE_READ_KHR;+ }+ if usage.contains(crate::AccelerationStructureUses::BUILD_OUTPUT) {+ stages |= vk::PipelineStageFlags::ACCELERATION_STRUCTURE_BUILD_KHR;+ access |= vk::AccessFlags::ACCELERATION_STRUCTURE_WRITE_KHR;+ }+ if usage.contains(crate::AccelerationStructureUses::SHADER_INPUT)+ && features.contains(wgt::Features::EXPERIMENTAL_RAY_QUERY)+ {+ stages |= vk::PipelineStageFlags::VERTEX_SHADER+ | vk::PipelineStageFlags::FRAGMENT_SHADER+ | vk::PipelineStageFlags::COMPUTE_SHADER;+ access |= vk::AccessFlags::ACCELERATION_STRUCTURE_READ_KHR;+ }+ if usage.contains(crate::AccelerationStructureUses::SHADER_INPUT)+ && features.contains(wgt::Features::EXPERIMENTAL_RAY_TRACING_PIPELINES)+ {+ stages |= vk::PipelineStageFlags::RAY_TRACING_SHADER_KHR;+ access |= vk::AccessFlags::ACCELERATION_STRUCTURE_READ_KHR;+ }+ if usage.contains(crate::AccelerationStructureUses::COPY_SRC) {+ stages |= vk::PipelineStageFlags::ACCELERATION_STRUCTURE_BUILD_KHR;+ access |= vk::AccessFlags::ACCELERATION_STRUCTURE_READ_KHR;+ }+ if usage.contains(crate::AccelerationStructureUses::COPY_DST) {+ stages |= vk::PipelineStageFlags::ACCELERATION_STRUCTURE_BUILD_KHR;+ access |= vk::AccessFlags::ACCELERATION_STRUCTURE_WRITE_KHR;+ }++ (stages, access)+}++#[cfg(test)]+mod tests {+ use super::*;++ /// `map_vk_color_space` and `map_surface_color_space` must stay mutually+ /// inverse so that a color space reported in the surface capabilities is+ /// exactly what the swapchain is created with.+ #[test]+ fn color_space_round_trip() {+ for vk_color_space in [+ vk::ColorSpaceKHR::SRGB_NONLINEAR,+ vk::ColorSpaceKHR::EXTENDED_SRGB_LINEAR_EXT,+ vk::ColorSpaceKHR::EXTENDED_SRGB_NONLINEAR_EXT,+ vk::ColorSpaceKHR::DISPLAY_P3_NONLINEAR_EXT,+ vk::ColorSpaceKHR::HDR10_ST2084_EXT,+ vk::ColorSpaceKHR::HDR10_HLG_EXT,+ ] {+ let mapped = map_vk_color_space(vk_color_space).unwrap();+ assert_eq!(map_surface_color_space(mapped), vk_color_space);+ }+ }++ #[test]+ fn unknown_color_spaces_are_dropped() {+ for vk_color_space in [+ vk::ColorSpaceKHR::BT2020_LINEAR_EXT,+ vk::ColorSpaceKHR::DOLBYVISION_EXT,+ vk::ColorSpaceKHR::PASS_THROUGH_EXT,+ vk::ColorSpaceKHR::ADOBERGB_NONLINEAR_EXT,+ ] {+ assert!(map_vk_color_space(vk_color_space).is_none());+ assert!(map_vk_surface_formats(vk::SurfaceFormatKHR {+ format: vk::Format::B8G8R8A8_UNORM,+ color_space: vk_color_space,+ })+ .is_none());+ }+ }++ #[test]+ fn hdr10_surface_format_maps() {+ let (format, color_space) = map_vk_surface_formats(vk::SurfaceFormatKHR {+ format: vk::Format::A2B10G10R10_UNORM_PACK32,+ color_space: vk::ColorSpaceKHR::HDR10_ST2084_EXT,+ })+ .unwrap();+ assert_eq!(format, wgt::TextureFormat::Rgb10a2Unorm);+ assert_eq!(color_space, wgt::SurfaceColorSpace::Bt2100Pq);+ }+}diff --git a/vendor/wgpu-hal/src/vulkan/descriptor.rs b/vendor/wgpu-hal/src/vulkan/descriptor.rsnew file mode 100644index 00000000..5961c3c5--- /dev/null+++ b/vendor/wgpu-hal/src/vulkan/descriptor.rs@@ -0,0 +1,339 @@+use alloc::vec::Vec;+use arrayvec::ArrayVec;+use ash::vk;+use hashbrown::{HashMap, HashSet};++const POOL_MIN_SETS: u32 = 64;+const POOL_MAX_SETS: u32 = 512;++/// The dynamic counts are tracked separately since dynamic textures/buffers+/// have distinct descriptor types compared to their non-dynamic counterparts.+#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]+pub struct DescriptorCounts {+ pub sampler: u32,+ pub sampled_image: u32,+ pub storage_image: u32,+ pub uniform_buffer: u32,+ pub uniform_buffer_dynamic: u32,+ pub storage_buffer: u32,+ pub storage_buffer_dynamic: u32,+ pub acceleration_structure: u32,+}++impl DescriptorCounts {+ fn total(&self) -> u32 {+ self.sampler+ + self.sampled_image+ + self.storage_image+ + self.uniform_buffer+ + self.uniform_buffer_dynamic+ + self.storage_buffer+ + self.storage_buffer_dynamic+ + self.acceleration_structure+ }+}++#[derive(Debug)]+pub struct DescriptorSet {+ raw: vk::DescriptorSet,+ bucket_key: BucketKey,+ pool_index: usize,+}++impl DescriptorSet {+ pub fn raw(&self) -> vk::DescriptorSet {+ self.raw+ }+}++#[derive(Debug, PartialEq, Eq, Hash)]+struct BucketKey {+ counts: DescriptorCounts,+ update_after_bind: bool,+}++struct Pool {+ raw: vk::DescriptorPool,+ capacity: u32,+ available: u32,+}++/// Keeps track of all pools created with this bucket's [`BucketKey`].+#[derive(Default)]+struct Bucket {+ /// We keep track of all descriptor set layouts that might use this bucket.+ /// If the set becomes empty, the bucket is destroyed.+ layouts: HashSet<vk::DescriptorSetLayout>,+ pools: Vec<Pool>,+ available_sets: u32,+ allocated_sets: u32,+}++impl Bucket {+ fn create_pool(+ &mut self,+ device: &ash::Device,+ key: &BucketKey,+ capacity_hint: u32,+ ) -> Result<(usize, &mut Pool), crate::DeviceError> {+ let index = self.pools.len();+ let pool = create_descriptor_pool(device, key, capacity_hint)?;+ self.available_sets += pool.capacity;+ self.pools.push(pool);+ Ok((index, self.pools.last_mut().unwrap()))+ }+}++pub struct DescriptorAllocator {+ buckets: HashMap<BucketKey, Bucket>,+ max_update_after_bind_descriptors_in_all_pools: u32,+ update_after_bind_descriptors_in_all_pools: u32,+}++impl super::BindGroupLayout {+ fn bucket_key(&self) -> BucketKey {+ let update_after_bind = self.contains_binding_arrays;+ let counts = self.desc_count.clone();+ BucketKey {+ counts,+ update_after_bind,+ }+ }+}++impl DescriptorAllocator {+ pub fn new(max_update_after_bind_descriptors_in_all_pools: u32) -> Self {+ DescriptorAllocator {+ buckets: HashMap::default(),+ max_update_after_bind_descriptors_in_all_pools,+ update_after_bind_descriptors_in_all_pools: 0,+ }+ }++ pub fn register_layout(+ &mut self,+ device: &ash::Device,+ layout: &super::BindGroupLayout,+ ) -> Result<(), crate::DeviceError> {+ let key = layout.bucket_key();+ let bucket = match self.buckets.entry(key) {+ hashbrown::hash_map::Entry::Occupied(occupied_entry) => occupied_entry.into_mut(),+ hashbrown::hash_map::Entry::Vacant(vacant_entry) => {+ let mut bucket = Bucket::default();+ // Create at least 1 pool upfront instead of doing this when+ // creating the first bind group.+ bucket.create_pool(device, vacant_entry.key(), POOL_MIN_SETS)?;+ vacant_entry.insert(bucket)+ }+ };++ assert!(bucket.layouts.insert(layout.raw));++ Ok(())+ }++ pub fn unregister_layout(&mut self, device: &ash::Device, layout: &super::BindGroupLayout) {+ let key = layout.bucket_key();+ let bucket = self.buckets.get_mut(&key).unwrap();++ assert!(bucket.layouts.remove(&layout.raw));++ if bucket.layouts.is_empty() {+ // Remove the bucket and destroy any remaining pools.+ let bucket = self.buckets.remove(&key).unwrap();+ for pool in bucket.pools {+ assert_eq!(+ pool.available, pool.capacity,+ "pool is not empty, at least one DescriptorSet has not been freed"+ );+ unsafe { device.destroy_descriptor_pool(pool.raw, None) };+ }+ }+ }++ pub unsafe fn alloc(+ &mut self,+ device: &ash::Device,+ layout: &super::BindGroupLayout,+ ) -> Result<DescriptorSet, crate::DeviceError> {+ let update_after_bind = layout.contains_binding_arrays;+ let total_descriptors = layout.desc_count.total();++ if update_after_bind+ && self.max_update_after_bind_descriptors_in_all_pools+ - self.update_after_bind_descriptors_in_all_pools+ < total_descriptors+ {+ return Err(crate::DeviceError::OutOfMemory);+ }++ let key = layout.bucket_key();+ let bucket = self.buckets.get_mut(&key).unwrap();++ // Prefer smaller/older/fuller pools for new allocations to prevent+ // fragmentation and possibly fragmentation of hardware resources+ // (VK_ERROR_FRAGMENTATION)+ let pool = bucket+ .pools+ .iter_mut()+ .enumerate()+ .find(|(_, pool)| pool.available != 0);++ let (pool_index, pool) = if let Some(pool) = pool {+ pool+ } else {+ let capacity_hint = bucket.allocated_sets;+ bucket.create_pool(device, &key, capacity_hint)?+ };++ let vk_info = vk::DescriptorSetAllocateInfo::default()+ .descriptor_pool(pool.raw)+ .set_layouts(core::slice::from_ref(&layout.raw));++ let raw = match unsafe { device.allocate_descriptor_sets(&vk_info) } {+ Ok(sets) => Ok(sets[0]),+ // We make sure not to exceed the size of the pool.+ Err(vk::Result::ERROR_OUT_OF_POOL_MEMORY) => unreachable!(),+ // We only allocate from a pool if the nr and type of descriptors+ // used by a layout is the same as those specified at pool+ // creation time.+ //+ // > Additionally, if all sets allocated from the pool since it was+ // created or most recently reset use the same number of+ // descriptors (of each type) and the requested allocation also+ // uses that same number of descriptors (of each type),+ // then fragmentation must not cause an allocation failure.+ //+ // from https://docs.vulkan.org/refpages/latest/refpages/source/VkDescriptorPoolCreateInfo.html#_description+ Err(vk::Result::ERROR_FRAGMENTED_POOL) => unreachable!(),+ Err(err) => Err(super::map_host_device_oom_err(err)),+ }?;++ pool.available -= 1;+ bucket.available_sets -= 1;+ bucket.allocated_sets += 1;+ if update_after_bind {+ self.update_after_bind_descriptors_in_all_pools += total_descriptors;+ }++ Ok(DescriptorSet {+ raw,+ bucket_key: key,+ pool_index,+ })+ }++ pub unsafe fn free(&mut self, device: &ash::Device, set: DescriptorSet) {+ let bucket = self.buckets.get_mut(&set.bucket_key).unwrap();+ let pool = bucket.pools.get_mut(set.pool_index).unwrap();++ let result =+ unsafe { device.free_descriptor_sets(pool.raw, core::slice::from_ref(&set.raw())) };+ if let Err(err) = result {+ // vkFreeDescriptorSets is documented to return:+ // - VK_ERROR_UNKNOWN+ // - VK_ERROR_VALIDATION_FAILED (we shouldn't encounter this one)+ // wgpu-hal doesn't currently report errors in destroy functions.+ // Panic here for now. It might be ok to ignore the error but the+ // proper solution would probably be to lose the device.+ panic!("vkFreeDescriptorSets error: {err}, please report this error");+ }++ pool.available += 1;+ bucket.available_sets += 1;+ bucket.allocated_sets -= 1;+ if set.bucket_key.update_after_bind {+ self.update_after_bind_descriptors_in_all_pools -= set.bucket_key.counts.total();+ }++ // Do not immediately destroy empty pools since they might have+ // been recently created.+ // Destroy a pool only if it's empty and we have 1/4th its capacity+ // in other pools.+ // Note that this logic will never destroy the last pool.+ let pool = bucket.pools.last().unwrap();+ if pool.available == pool.capacity+ && bucket.available_sets - pool.capacity > pool.capacity / 4+ {+ let pool = bucket.pools.pop().unwrap();+ unsafe { device.destroy_descriptor_pool(pool.raw, None) };+ bucket.available_sets -= pool.capacity;+ }+ }+}++impl Drop for DescriptorAllocator {+ fn drop(&mut self) {+ if !std::thread::panicking() {+ assert!(+ self.buckets.is_empty(),+ "buckets are not empty, at least one BGL has not been unregistered"+ )+ }+ }+}++fn create_descriptor_pool(+ device: &ash::Device,+ key: &BucketKey,+ capacity_hint: u32,+) -> Result<Pool, crate::DeviceError> {+ let counts = &key.counts;++ const NR_OF_DESCRIPTOR_TYPES: usize = 8;++ use vk::DescriptorType as Dt;+ let counts: [_; NR_OF_DESCRIPTOR_TYPES] = [+ (Dt::SAMPLER, counts.sampler),+ (Dt::SAMPLED_IMAGE, counts.sampled_image),+ (Dt::STORAGE_IMAGE, counts.storage_image),+ (Dt::UNIFORM_BUFFER, counts.uniform_buffer),+ (Dt::UNIFORM_BUFFER_DYNAMIC, counts.uniform_buffer_dynamic),+ (Dt::STORAGE_BUFFER, counts.storage_buffer),+ (Dt::STORAGE_BUFFER_DYNAMIC, counts.storage_buffer_dynamic),+ (+ Dt::ACCELERATION_STRUCTURE_KHR,+ counts.acceleration_structure,+ ),+ ];++ // bounded doubling growth strategy+ let mut capacity = capacity_hint+ .clamp(POOL_MIN_SETS, POOL_MAX_SETS)+ .next_power_of_two();++ // Lower capacity to avoid overflowing `count * capacity` calculations+ // below. The resulting `capacity` might not be a power of 2 but that's+ // ok.+ for (_, count) in counts {+ capacity = (u32::MAX / count.max(1)).min(capacity);+ }++ let pool_sizes = counts+ .into_iter()+ .filter(|&(_, count)| count != 0)+ .map(|(ty, count)| vk::DescriptorPoolSize {+ ty,+ descriptor_count: count * capacity,+ })+ .collect::<ArrayVec<_, NR_OF_DESCRIPTOR_TYPES>>();++ let mut flags = vk::DescriptorPoolCreateFlags::FREE_DESCRIPTOR_SET;+ if key.update_after_bind {+ flags |= vk::DescriptorPoolCreateFlags::UPDATE_AFTER_BIND;+ };++ let vk_info = vk::DescriptorPoolCreateInfo::default()+ .flags(flags)+ .max_sets(capacity)+ .pool_sizes(&pool_sizes);++ let raw = unsafe { device.create_descriptor_pool(&vk_info, None) }+ .map_err(super::map_host_device_oom_and_fragmentation_err)?;++ Ok(Pool {+ raw,+ capacity,+ available: capacity,+ })+}diff --git a/vendor/wgpu-hal/src/vulkan/device.rs b/vendor/wgpu-hal/src/vulkan/device.rsnew file mode 100644index 00000000..948f77a5--- /dev/null+++ b/vendor/wgpu-hal/src/vulkan/device.rs@@ -0,0 +1,3116 @@+use alloc::{borrow::ToOwned as _, collections::BTreeMap, ffi::CString, sync::Arc, vec::Vec};+use core::{+ ffi::CStr,+ mem::{self, MaybeUninit},+ num::NonZeroU32,+ ptr,+ time::Duration,+};++use arrayvec::ArrayVec;+use ash::{ext, vk};+use hashbrown::hash_map::Entry;+use parking_lot::{Mutex, RwLock};++use super::{conv, descriptor::DescriptorCounts, RawTlasInstance};+use crate::TlasInstance;++impl super::DeviceShared {+ /// Set the name of `object` to `name`.+ ///+ /// If `name` contains an interior null byte, then the name set will be truncated to that byte.+ ///+ /// # Safety+ ///+ /// This method inherits the safety contract from [`vkSetDebugUtilsObjectName`]. In particular:+ ///+ /// - `object` must be a valid handle for one of the following:+ /// - An instance-level object from the same instance as this device.+ /// - A physical-device-level object that descends from the same physical device as this+ /// device.+ /// - A device-level object that descends from this device.+ /// - `object` must be externally synchronized—only the calling thread should access it during+ /// this call.+ ///+ /// [`vkSetDebugUtilsObjectName`]: https://registry.khronos.org/vulkan/specs/latest/man/html/vkSetDebugUtilsObjectNameEXT.html+ pub(super) unsafe fn set_object_name(&self, object: impl vk::Handle, name: &str) {+ let Some(extension) = self.extension_fns.debug_utils.as_ref() else {+ return;+ };++ // Keep variables outside the if-else block to ensure they do not+ // go out of scope while we hold a pointer to them+ let mut buffer: [u8; 64] = [0u8; 64];+ let buffer_vec: Vec<u8>;++ // Append a null terminator to the string+ let name_bytes = if name.len() < buffer.len() {+ // Common case, string is very small. Allocate a copy on the stack.+ buffer[..name.len()].copy_from_slice(name.as_bytes());+ // Add null terminator+ buffer[name.len()] = 0;+ &buffer[..name.len() + 1]+ } else {+ // Less common case, the string is large.+ // This requires a heap allocation.+ buffer_vec = name+ .as_bytes()+ .iter()+ .cloned()+ .chain(core::iter::once(0))+ .collect();+ &buffer_vec+ };++ let name = CStr::from_bytes_until_nul(name_bytes).expect("We have added a null byte");++ let _result = unsafe {+ extension.set_debug_utils_object_name(+ &vk::DebugUtilsObjectNameInfoEXT::default()+ .object_handle(object)+ .object_name(name),+ )+ };+ }++ pub fn make_render_pass(+ &self,+ key: super::RenderPassKey,+ ) -> Result<vk::RenderPass, crate::DeviceError> {+ Ok(match self.render_passes.lock().entry(key) {+ Entry::Occupied(e) => *e.get(),+ Entry::Vacant(e) => {+ let super::RenderPassKey {+ ref colors,+ ref depth_stencil,+ sample_count,+ multiview_mask,+ } = *e.key();++ let mut vk_attachments = Vec::new();+ let mut color_refs = Vec::with_capacity(colors.len());+ let mut resolve_refs = Vec::with_capacity(color_refs.capacity());+ let mut ds_ref = None;+ let samples = vk::SampleCountFlags::from_raw(sample_count);+ let unused = vk::AttachmentReference {+ attachment: vk::ATTACHMENT_UNUSED,+ layout: vk::ImageLayout::UNDEFINED,+ };+ for cat in colors.iter() {+ let (color_ref, resolve_ref) =+ if let Some(super::ColorAttachmentKey { base, resolve }) = cat {+ let super::AttachmentKey {+ format,+ layout,+ ops,+ } = *base;++ let color_ref = vk::AttachmentReference {+ attachment: vk_attachments.len() as u32,+ layout,+ };+ vk_attachments.push({+ let (load_op, store_op) = conv::map_attachment_ops(ops);+ vk::AttachmentDescription::default()+ .format(format)+ .samples(samples)+ .load_op(load_op)+ .store_op(store_op)+ .initial_layout(layout)+ .final_layout(layout)+ });+ let resolve_ref = if let Some(rat) = resolve {+ let super::AttachmentKey {+ format,+ layout,+ ops,+ } = *rat;++ let (load_op, store_op) = conv::map_attachment_ops(ops);+ let vk_attachment = vk::AttachmentDescription::default()+ .format(format)+ .samples(vk::SampleCountFlags::TYPE_1)+ .load_op(load_op)+ .store_op(store_op)+ .initial_layout(layout)+ .final_layout(layout);+ vk_attachments.push(vk_attachment);++ vk::AttachmentReference {+ attachment: vk_attachments.len() as u32 - 1,+ layout,+ }+ } else {+ unused+ };++ (color_ref, resolve_ref)+ } else {+ (unused, unused)+ };++ color_refs.push(color_ref);+ resolve_refs.push(resolve_ref);+ }++ if let Some(ds) = depth_stencil {+ let super::DepthStencilAttachmentKey {+ ref base,+ stencil_ops,+ } = *ds;++ let super::AttachmentKey {+ format,+ layout,+ ops,+ } = *base;++ ds_ref = Some(vk::AttachmentReference {+ attachment: vk_attachments.len() as u32,+ layout,+ });+ let (load_op, store_op) = conv::map_attachment_ops(ops);+ let (stencil_load_op, stencil_store_op) = conv::map_attachment_ops(stencil_ops);+ let vk_attachment = vk::AttachmentDescription::default()+ .format(format)+ .samples(samples)+ .load_op(load_op)+ .store_op(store_op)+ .stencil_load_op(stencil_load_op)+ .stencil_store_op(stencil_store_op)+ .initial_layout(layout)+ .final_layout(layout);+ vk_attachments.push(vk_attachment);+ }++ let vk_subpasses = [{+ let mut vk_subpass = vk::SubpassDescription::default()+ .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS)+ .color_attachments(&color_refs)+ .resolve_attachments(&resolve_refs);++ if self+ .workarounds+ .contains(super::Workarounds::EMPTY_RESOLVE_ATTACHMENT_LISTS)+ && resolve_refs.is_empty()+ {+ vk_subpass.p_resolve_attachments = ptr::null();+ }++ if let Some(ref reference) = ds_ref {+ vk_subpass = vk_subpass.depth_stencil_attachment(reference)+ }+ vk_subpass+ }];++ let mut vk_info = vk::RenderPassCreateInfo::default()+ .attachments(&vk_attachments)+ .subpasses(&vk_subpasses);++ let mut multiview_info;+ let mask;+ if let Some(multiview_mask) = multiview_mask {+ mask = [multiview_mask.get()];++ // On Vulkan 1.1 or later, this is an alias for core functionality+ multiview_info = vk::RenderPassMultiviewCreateInfoKHR::default()+ .view_masks(&mask)+ .correlation_masks(&mask);+ vk_info = vk_info.push_next(&mut multiview_info);+ }++ let raw = unsafe {+ self.raw+ .create_render_pass(&vk_info, None)+ .map_err(super::map_host_device_oom_err)?+ };++ *e.insert(raw)+ }+ })+ }++ fn make_memory_ranges<'a, I: 'a + Iterator<Item = crate::MemoryRange>>(+ &self,+ buffer: &'a super::Buffer,+ ranges: I,+ ) -> Option<impl 'a + Iterator<Item = vk::MappedMemoryRange<'a>>> {+ let super::BufferOwnership::Managed(ref allocation) = buffer.ownership else {+ return None;+ };+ let allocation = allocation.lock();+ let mask = self.private_caps.non_coherent_map_mask;+ Some(ranges.map(move |range| {+ vk::MappedMemoryRange::default()+ .memory(allocation.memory())+ .offset((allocation.offset() + range.start) & !mask)+ .size((range.end - range.start + mask) & !mask)+ }))+ }+}++struct CompiledStage {+ create_info: vk::PipelineShaderStageCreateInfo<'static>,+ _entry_point: CString,+ temp_raw_module: Option<vk::ShaderModule>,+}++struct MemoryProperties {+ base: vk::PhysicalDeviceMemoryProperties,+ heap_budget: ArrayVec<vk::DeviceSize, { vk::MAX_MEMORY_HEAPS }>,+ heap_usage: ArrayVec<vk::DeviceSize, { vk::MAX_MEMORY_HEAPS }>,+}++impl MemoryProperties {+ fn types(&self) -> &[vk::MemoryType] {+ let count = self.base.memory_type_count as usize;+ &self.base.memory_types[0..count]+ }++ fn heaps(&self) -> &[vk::MemoryHeap] {+ let count = self.base.memory_heap_count as usize;+ &self.base.memory_heaps[0..count]+ }++ fn heap_budget(&self) -> &[vk::DeviceSize] {+ let count = self.base.memory_heap_count as usize;+ &self.heap_budget[0..count]+ }++ fn heap_usage(&self) -> &[vk::DeviceSize] {+ let count = self.base.memory_heap_count as usize;+ &self.heap_usage[0..count]+ }+}++impl super::Device {+ /// # Safety+ ///+ /// - `vk_image` must be created respecting `desc`+ /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of `vk_image`. If+ /// `drop_callback` is [`Some`], `vk_image` must be valid until the callback is called.+ /// - If the `ImageCreateFlags` does not contain `MUTABLE_FORMAT`, the `view_formats` of `desc` must be empty.+ /// - If `memory` is not [`super::TextureMemory::External`], wgpu-hal will take ownership of the+ /// memory (which is presumed to back `vk_image`). Otherwise, the memory must remain valid until+ /// `drop_callback` is called.+ pub unsafe fn texture_from_raw(+ &self,+ vk_image: vk::Image,+ desc: &crate::TextureDescriptor,+ drop_callback: Option<crate::DropCallback>,+ memory: super::TextureMemory,+ ) -> super::Texture {+ let identity = self.shared.texture_identity_factory.next();+ let drop_guard = crate::DropGuard::from_option(drop_callback);++ if let Some(label) = desc.label {+ unsafe { self.shared.set_object_name(vk_image, label) };+ }++ super::Texture {+ raw: vk_image,+ drop_guard,+ memory,+ format: desc.format,+ copy_size: desc.copy_extent(),+ identity,+ }+ }++ fn find_memory_type_index(+ &self,+ type_bits_req: u32,+ flags_req: vk::MemoryPropertyFlags,+ ) -> Option<usize> {+ let mem_properties = unsafe {+ self.shared+ .instance+ .raw+ .get_physical_device_memory_properties(self.shared.physical_device)+ };++ // https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkPhysicalDeviceMemoryProperties.html+ for (i, mem_ty) in mem_properties.memory_types_as_slice().iter().enumerate() {+ let types_bits = 1 << i;+ let is_required_memory_type = type_bits_req & types_bits != 0;+ let has_required_properties = mem_ty.property_flags & flags_req == flags_req;+ if is_required_memory_type && has_required_properties {+ return Some(i);+ }+ }++ None+ }++ fn create_image_without_memory(+ &self,+ desc: &crate::TextureDescriptor,+ external_memory_image_create_info: Option<&mut vk::ExternalMemoryImageCreateInfo>,+ ) -> Result<ImageWithoutMemory, crate::DeviceError> {+ self.create_image_without_memory_with_tiling(+ desc,+ vk::ImageTiling::OPTIMAL,+ external_memory_image_create_info,+ None,+ )+ }++ fn create_image_without_memory_with_tiling(+ &self,+ desc: &crate::TextureDescriptor,+ tiling: vk::ImageTiling,+ external_memory_image_create_info: Option<&mut vk::ExternalMemoryImageCreateInfo>,+ drm_modifier_info: Option<&mut vk::ImageDrmFormatModifierExplicitCreateInfoEXT>,+ ) -> Result<ImageWithoutMemory, crate::DeviceError> {+ let copy_size = desc.copy_extent();++ let mut raw_flags = vk::ImageCreateFlags::empty();+ if desc.dimension == wgt::TextureDimension::D3+ && desc.usage.contains(wgt::TextureUses::COLOR_TARGET)+ {+ raw_flags |= vk::ImageCreateFlags::TYPE_2D_ARRAY_COMPATIBLE;+ }+ if desc.is_cube_compatible() {+ raw_flags |= vk::ImageCreateFlags::CUBE_COMPATIBLE;+ }++ let original_format = self.shared.private_caps.map_texture_format(desc.format);+ let mut vk_view_formats = vec![];+ if !desc.view_formats.is_empty() {+ raw_flags |= vk::ImageCreateFlags::MUTABLE_FORMAT;++ if self.shared.private_caps.image_format_list {+ vk_view_formats = desc+ .view_formats+ .iter()+ .map(|f| self.shared.private_caps.map_texture_format(*f))+ .collect();+ vk_view_formats.push(original_format)+ }+ }+ if desc.format.is_multi_planar_format() {+ raw_flags |=+ vk::ImageCreateFlags::MUTABLE_FORMAT | vk::ImageCreateFlags::EXTENDED_USAGE;+ }++ let mut vk_info = vk::ImageCreateInfo::default()+ .flags(raw_flags)+ .image_type(conv::map_texture_dimension(desc.dimension))+ .format(original_format)+ .extent(conv::map_copy_extent(©_size))+ .mip_levels(desc.mip_level_count)+ .array_layers(desc.array_layer_count())+ .samples(vk::SampleCountFlags::from_raw(desc.sample_count))+ .tiling(tiling)+ .usage(conv::map_texture_usage(desc.usage))+ .sharing_mode(vk::SharingMode::EXCLUSIVE)+ .initial_layout(vk::ImageLayout::UNDEFINED);++ let mut format_list_info = vk::ImageFormatListCreateInfo::default();+ if !vk_view_formats.is_empty() {+ format_list_info = format_list_info.view_formats(&vk_view_formats);+ vk_info = vk_info.push_next(&mut format_list_info);+ }++ if let Some(ext_info) = external_memory_image_create_info {+ vk_info = vk_info.push_next(ext_info);+ }++ if let Some(drm_info) = drm_modifier_info {+ vk_info = vk_info.push_next(drm_info);+ }++ let raw = unsafe { self.shared.raw.create_image(&vk_info, None) }.map_err(map_err)?;+ fn map_err(err: vk::Result) -> crate::DeviceError {+ // We don't use VK_EXT_image_compression_control+ // VK_ERROR_COMPRESSION_EXHAUSTED_EXT+ super::map_host_device_oom_and_ioca_err(err)+ }+ let mut req = unsafe { self.shared.raw.get_image_memory_requirements(raw) };++ if desc.usage.contains(wgt::TextureUses::TRANSIENT) {+ let mem_type_index = self.find_memory_type_index(+ req.memory_type_bits,+ vk::MemoryPropertyFlags::LAZILY_ALLOCATED,+ );+ if let Some(mem_type_index) = mem_type_index {+ req.memory_type_bits = 1 << mem_type_index;+ }+ }++ Ok(ImageWithoutMemory {+ raw,+ requirements: req,+ })+ }++ /// # Safety+ ///+ /// - Vulkan (with VK_KHR_external_memory_win32)+ /// - The `d3d11_shared_handle` must be valid and respecting `desc`+ /// - `VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT` flag is used because we need to hold a reference to the handle+ #[cfg(windows)]+ pub unsafe fn texture_from_d3d11_shared_handle(+ &self,+ d3d11_shared_handle: windows::Win32::Foundation::HANDLE,+ desc: &crate::TextureDescriptor,+ ) -> Result<super::Texture, crate::DeviceError> {+ if !self+ .shared+ .features+ .contains(wgt::Features::VULKAN_EXTERNAL_MEMORY_WIN32)+ {+ log::error!("Vulkan driver does not support VK_KHR_external_memory_win32");+ return Err(crate::DeviceError::Unexpected);+ }++ let mut external_memory_image_info = vk::ExternalMemoryImageCreateInfo::default()+ .handle_types(vk::ExternalMemoryHandleTypeFlags::D3D11_TEXTURE);++ let image =+ self.create_image_without_memory(desc, Some(&mut external_memory_image_info))?;++ // Some external memory types require dedicated allocation+ // https://docs.vulkan.org/guide/latest/extensions/external.html#_importing_memory+ let mut dedicated_allocate_info =+ vk::MemoryDedicatedAllocateInfo::default().image(image.raw);++ let mut import_memory_info = vk::ImportMemoryWin32HandleInfoKHR::default()+ .handle_type(vk::ExternalMemoryHandleTypeFlags::D3D11_TEXTURE)+ .handle(d3d11_shared_handle.0 as _);+ // TODO: We should use `push_next` instead, but currently ash does not provide this method for the `ImportMemoryWin32HandleInfoKHR` type.+ #[allow(clippy::unnecessary_mut_passed)]+ {+ import_memory_info.p_next = <*const _>::cast(&mut dedicated_allocate_info);+ }++ let mem_type_index = self+ .find_memory_type_index(+ image.requirements.memory_type_bits,+ vk::MemoryPropertyFlags::DEVICE_LOCAL,+ )+ .ok_or(crate::DeviceError::Unexpected)?;++ let memory_allocate_info = vk::MemoryAllocateInfo::default()+ .allocation_size(image.requirements.size)+ .memory_type_index(mem_type_index as _)+ .push_next(&mut import_memory_info);+ let memory = unsafe { self.shared.raw.allocate_memory(&memory_allocate_info, None) }+ .map_err(super::map_host_device_oom_err)?;++ unsafe { self.shared.raw.bind_image_memory(image.raw, memory, 0) }+ .map_err(super::map_host_device_oom_err)?;++ Ok(unsafe {+ self.texture_from_raw(+ image.raw,+ desc,+ None,+ super::TextureMemory::Dedicated(memory),+ )+ })+ }++ /// Import a DMA-buf as a texture. Currently only supports single-plane DMA-bufs.+ ///+ /// # Safety+ ///+ /// - Requires `VULKAN_EXTERNAL_MEMORY_DMA_BUF` feature (implies VK_EXT_external_memory_dma_buf+ /// and VK_EXT_image_drm_format_modifier)+ /// - The `fd` must be a valid DMA-buf file descriptor matching `desc`+ /// - On success, Vulkan takes ownership of the file descriptor. On failure,+ /// the file descriptor is closed.+ /// - The `drm_modifier`, `stride`, and `offset` must match the DMA-buf layout+ #[cfg(unix)]+ pub unsafe fn texture_from_dmabuf_fd(+ &self,+ fd: std::os::unix::io::OwnedFd,+ desc: &crate::TextureDescriptor,+ drm_modifier: u64,+ stride: u64,+ offset: u64,+ ) -> Result<super::Texture, crate::DeviceError> {+ use std::os::unix::io::IntoRawFd;++ if !self+ .shared+ .features+ .contains(wgt::Features::VULKAN_EXTERNAL_MEMORY_DMA_BUF)+ {+ log::error!(+ "Vulkan driver does not support VK_EXT_external_memory_dma_buf \+ or VK_EXT_image_drm_format_modifier"+ );+ return Err(crate::DeviceError::Unexpected);+ }++ let external_memory_fd_fn = self+ .shared+ .extension_fns+ .external_memory_fd+ .as_ref()+ .ok_or_else(|| {+ log::error!("VK_KHR_external_memory_fd extension not loaded");+ crate::DeviceError::Unexpected+ })?;++ let mut external_memory_image_info = vk::ExternalMemoryImageCreateInfo::default()+ .handle_types(vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT);++ let plane_layout = vk::SubresourceLayout {+ offset,+ row_pitch: stride,+ size: 0,+ array_pitch: 0,+ depth_pitch: 0,+ };+ let mut drm_modifier_info = vk::ImageDrmFormatModifierExplicitCreateInfoEXT::default()+ .drm_format_modifier(drm_modifier)+ .plane_layouts(core::slice::from_ref(&plane_layout));++ let image = self.create_image_without_memory_with_tiling(+ desc,+ vk::ImageTiling::DRM_FORMAT_MODIFIER_EXT,+ Some(&mut external_memory_image_info),+ Some(&mut drm_modifier_info),+ )?;++ // Convert to raw fd. We must close it ourselves if any operation below+ // fails, since Vulkan only takes ownership on successful vkAllocateMemory.+ let fd_raw = fd.into_raw_fd();++ let result = self.import_dmabuf_memory(+ external_memory_fd_fn,+ fd_raw,+ image.raw,+ &image.requirements,+ );++ match result {+ Ok(memory) => Ok(unsafe {+ self.texture_from_raw(+ image.raw,+ desc,+ None,+ super::TextureMemory::Dedicated(memory),+ )+ }),+ Err(e) => {+ // Clean up the VkImage on failure.+ unsafe { self.shared.raw.destroy_image(image.raw, None) };+ Err(e)+ }+ }+ }++ /// Import DMA-buf memory and bind it to the image.+ ///+ /// On failure, the raw fd is closed (if not yet consumed by Vulkan) and the+ /// caller is responsible for destroying the VkImage.+ #[cfg(unix)]+ fn import_dmabuf_memory(+ &self,+ external_memory_fd_fn: &ash::khr::external_memory_fd::Device,+ fd_raw: i32,+ image: vk::Image,+ requirements: &vk::MemoryRequirements,+ ) -> Result<vk::DeviceMemory, crate::DeviceError> {+ let mut fd_props = vk::MemoryFdPropertiesKHR::default();+ unsafe {+ external_memory_fd_fn.get_memory_fd_properties(+ vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT,+ fd_raw,+ &mut fd_props,+ )+ }+ .map_err(|e| {+ unsafe { libc::close(fd_raw) };+ super::map_host_device_oom_err(e)+ })?;++ let mem_type_index = self+ .find_memory_type_index(+ requirements.memory_type_bits & fd_props.memory_type_bits,+ vk::MemoryPropertyFlags::empty(),+ )+ .ok_or_else(|| {+ unsafe { libc::close(fd_raw) };+ crate::DeviceError::Unexpected+ })?;++ let mut dedicated_allocate_info = vk::MemoryDedicatedAllocateInfo::default().image(image);++ let mut import_memory_info = vk::ImportMemoryFdInfoKHR::default()+ .handle_type(vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT)+ .fd(fd_raw);++ let memory_allocate_info = vk::MemoryAllocateInfo::default()+ .allocation_size(requirements.size)+ .memory_type_index(mem_type_index as _)+ .push_next(&mut import_memory_info)+ .push_next(&mut dedicated_allocate_info);++ // vkAllocateMemory takes ownership of the fd on success.+ // On failure, the fd is NOT consumed and we must close it.+ let memory = unsafe { self.shared.raw.allocate_memory(&memory_allocate_info, None) }+ .map_err(|e| {+ unsafe { libc::close(fd_raw) };+ super::map_host_device_oom_err(e)+ })?;++ // From this point, the fd is consumed. Only VkDeviceMemory needs cleanup on error.+ unsafe { self.shared.raw.bind_image_memory(image, memory, 0) }.map_err(|e| {+ unsafe { self.shared.raw.free_memory(memory, None) };+ super::map_host_device_oom_err(e)+ })?;++ Ok(memory)+ }++ fn create_shader_module_impl(+ &self,+ spv: &[u32],+ label: &crate::Label<'_>,+ ) -> Result<vk::ShaderModule, crate::DeviceError> {+ let vk_info = vk::ShaderModuleCreateInfo::default()+ .flags(vk::ShaderModuleCreateFlags::empty())+ .code(spv);++ let raw = unsafe {+ profiling::scope!("vkCreateShaderModule");+ self.shared+ .raw+ .create_shader_module(&vk_info, None)+ .map_err(map_err)?+ };+ fn map_err(err: vk::Result) -> crate::DeviceError {+ // We don't use VK_NV_glsl_shader+ // VK_ERROR_INVALID_SHADER_NV+ super::map_host_device_oom_err(err)+ }++ if let Some(label) = label {+ unsafe { self.shared.set_object_name(raw, label) };+ }++ Ok(raw)+ }++ fn compile_stage(+ &self,+ stage: &crate::ProgrammableStage<super::ShaderModule>,+ naga_stage: naga::ShaderStage,+ binding_map: &naga::back::spv::BindingMap,+ ) -> Result<CompiledStage, crate::PipelineError> {+ let stage_flags = crate::auxil::map_naga_stage(naga_stage);+ let vk_module = match *stage.module {+ super::ShaderModule::Raw(raw) => raw,+ super::ShaderModule::Intermediate {+ ref naga_shader,+ runtime_checks,+ } => {+ let pipeline_options = naga::back::spv::PipelineOptions {+ entry_point: stage.entry_point.to_owned(),+ shader_stage: naga_stage,+ };+ let needs_temp_options = !runtime_checks.bounds_checks+ || !runtime_checks.force_loop_bounding+ || !runtime_checks.ray_query_initialization_tracking+ || !binding_map.is_empty()+ || naga_shader.debug_source.is_some()+ || !stage.zero_initialize_workgroup_memory+ || !runtime_checks.task_shader_dispatch_tracking+ || !runtime_checks.mesh_shader_primitive_indices_clamp+ || !runtime_checks.int_div_checks;++ let mut temp_options;+ let options = if needs_temp_options {+ temp_options = self.naga_options.clone();+ if !runtime_checks.bounds_checks {+ temp_options.bounds_check_policies = naga::proc::BoundsCheckPolicies {+ index: naga::proc::BoundsCheckPolicy::Unchecked,+ buffer: naga::proc::BoundsCheckPolicy::Unchecked,+ image_load: naga::proc::BoundsCheckPolicy::Unchecked,+ binding_array: naga::proc::BoundsCheckPolicy::Unchecked,+ };+ }+ if !runtime_checks.force_loop_bounding {+ temp_options.force_loop_bounding = false;+ }+ if !runtime_checks.ray_query_initialization_tracking {+ temp_options.ray_query_initialization_tracking = false;+ }+ if !binding_map.is_empty() {+ temp_options.binding_map = binding_map.clone();+ }++ if let Some(ref debug) = naga_shader.debug_source {+ temp_options.debug_info = Some(naga::back::spv::DebugInfo {+ source_code: &debug.source_code,+ file_name: debug.file_name.as_ref(),+ language: naga::back::spv::SourceLanguage::WGSL,+ })+ }+ if !stage.zero_initialize_workgroup_memory {+ temp_options.zero_initialize_workgroup_memory =+ naga::back::spv::ZeroInitializeWorkgroupMemoryMode::None;+ }+ if !runtime_checks.task_shader_dispatch_tracking {+ temp_options.task_dispatch_limits = None;+ }+ temp_options.mesh_shader_primitive_indices_clamp =+ runtime_checks.mesh_shader_primitive_indices_clamp;+ temp_options.emit_int_div_checks = runtime_checks.int_div_checks;++ &temp_options+ } else {+ &self.naga_options+ };++ let (module, info) = naga::back::pipeline_constants::process_overrides(+ &naga_shader.module,+ &naga_shader.info,+ Some((naga_stage, stage.entry_point)),+ stage.constants,+ )+ .map_err(|e| {+ crate::PipelineError::PipelineConstants(stage_flags, format!("{e}"))+ })?;++ let spv = {+ profiling::scope!("naga::spv::write_vec");+ naga::back::spv::write_vec(&module, &info, options, Some(&pipeline_options))+ }+ .map_err(|e| crate::PipelineError::Linkage(stage_flags, format!("{e}")))?;+ self.create_shader_module_impl(&spv, &None)?+ }+ };++ let mut flags = vk::PipelineShaderStageCreateFlags::empty();+ if self.shared.features.contains(wgt::Features::SUBGROUP) {+ flags |= vk::PipelineShaderStageCreateFlags::ALLOW_VARYING_SUBGROUP_SIZE+ }++ let entry_point = CString::new(stage.entry_point).unwrap();+ let mut create_info = vk::PipelineShaderStageCreateInfo::default()+ .flags(flags)+ .stage(conv::map_shader_stage(stage_flags))+ .module(vk_module);++ // Circumvent struct lifetime check because of a self-reference inside CompiledStage+ create_info.p_name = entry_point.as_ptr();++ Ok(CompiledStage {+ create_info,+ _entry_point: entry_point,+ temp_raw_module: match *stage.module {+ super::ShaderModule::Raw(_) => None,+ super::ShaderModule::Intermediate { .. } => Some(vk_module),+ },+ })+ }++ /// Returns the queue family index of the device's internal queue.+ ///+ /// This is useful for constructing memory barriers needed for queue family ownership transfer when+ /// external memory is involved (from/to `VK_QUEUE_FAMILY_EXTERNAL_KHR` and `VK_QUEUE_FAMILY_FOREIGN_EXT`+ /// for example).+ pub fn queue_family_index(&self) -> u32 {+ self.shared.family_index+ }++ pub fn queue_index(&self) -> u32 {+ self.shared.queue_index+ }++ pub fn raw_device(&self) -> &ash::Device {+ &self.shared.raw+ }++ pub fn raw_physical_device(&self) -> vk::PhysicalDevice {+ self.shared.physical_device+ }++ pub fn raw_queue(&self) -> vk::Queue {+ self.shared.raw_queue+ }++ pub fn enabled_device_extensions(&self) -> &[&'static CStr] {+ &self.shared.enabled_extensions+ }++ pub fn shared_instance(&self) -> &super::InstanceShared {+ &self.shared.instance+ }++ fn get_memory_properties(&self) -> Option<MemoryProperties> {+ if !self+ .shared+ .enabled_extensions+ .contains(&ext::memory_budget::NAME)+ {+ return None;+ }++ let get_physical_device_properties = self+ .shared+ .instance+ .get_physical_device_properties+ .as_ref()+ .unwrap();++ let mut budget = vk::PhysicalDeviceMemoryBudgetPropertiesEXT::default();+ let mut props = vk::PhysicalDeviceMemoryProperties2::default().push_next(&mut budget);++ unsafe {+ get_physical_device_properties+ .get_physical_device_memory_properties2(self.shared.physical_device, &mut props);+ }++ let vk::PhysicalDeviceMemoryProperties2 {+ memory_properties, ..+ } = props;++ let mut heap_budget = ArrayVec::from(budget.heap_budget);+ let mut heap_usage = ArrayVec::from(budget.heap_usage);+ heap_budget.truncate(memory_properties.memory_heap_count as usize);+ heap_usage.truncate(memory_properties.memory_heap_count as usize);++ Some(MemoryProperties {+ base: memory_properties,+ heap_budget,+ heap_usage,+ })+ }++ /// Predict whether a proposed allocation will result in an OOM condition.+ ///+ /// If so, returns `Err(crate::DeviceError::OutOfMemory)`. If not, returns+ /// `Ok(())`.+ ///+ /// The prediction quality depends on accurately selecting the heap that+ /// [`gpu_allocator`] will use for the allocation, and is subject to+ /// deteriorate if the logic in [`gpu_allocator`] changes.+ fn error_if_would_oom_on_resource_allocation(+ &self,+ location: gpu_allocator::MemoryLocation,+ requirements: &vk::MemoryRequirements,+ ) -> Result<(), crate::DeviceError> {+ use gpu_allocator::MemoryLocation;++ let Some(threshold) = self+ .shared+ .instance+ .memory_budget_thresholds+ .for_resource_creation+ else {+ return Ok(());+ };++ let Some(memory_properties) = self.get_memory_properties() else {+ return Ok(());+ };++ // memory types not used by gpu-allocator+ let invalid_flags =+ vk::MemoryPropertyFlags::LAZILY_ALLOCATED | vk::MemoryPropertyFlags::PROTECTED;++ let preferred_flags = match location {+ MemoryLocation::GpuOnly => vk::MemoryPropertyFlags::DEVICE_LOCAL,+ MemoryLocation::CpuToGpu => {+ vk::MemoryPropertyFlags::HOST_VISIBLE+ | vk::MemoryPropertyFlags::HOST_COHERENT+ | vk::MemoryPropertyFlags::DEVICE_LOCAL+ }+ MemoryLocation::GpuToCpu => {+ vk::MemoryPropertyFlags::HOST_VISIBLE+ | vk::MemoryPropertyFlags::HOST_COHERENT+ | vk::MemoryPropertyFlags::HOST_CACHED+ }+ MemoryLocation::Unknown => vk::MemoryPropertyFlags::empty(),+ };++ let mut selected_heap = memory_properties+ .types()+ .iter()+ .enumerate()+ .find(|(i, ty)| {+ (1 << i) & requirements.memory_type_bits != 0+ && ty.property_flags.contains(preferred_flags)+ && !ty.property_flags.intersects(invalid_flags)+ });++ if selected_heap.is_none() {+ let required_flags = match location {+ MemoryLocation::GpuOnly => vk::MemoryPropertyFlags::DEVICE_LOCAL,+ MemoryLocation::CpuToGpu | MemoryLocation::GpuToCpu => {+ vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT+ }+ MemoryLocation::Unknown => vk::MemoryPropertyFlags::empty(),+ };+ selected_heap = memory_properties+ .types()+ .iter()+ .enumerate()+ .find(|(i, ty)| {+ (1 << i) & requirements.memory_type_bits != 0+ && ty.property_flags.contains(required_flags)+ && !ty.property_flags.intersects(invalid_flags)+ });+ }++ if let Some((_, ty)) = selected_heap {+ let i = ty.heap_index as usize;+ let heap_usage = memory_properties.heap_usage()[i];+ let heap_budget = memory_properties.heap_budget()[i];+ if heap_usage + requirements.size < heap_budget / 100 * threshold as u64 {+ Ok(())+ } else {+ log::warn!(+ "Allocation would result in an OOM condition\n\+ Request: {requirements:?}\n\+ Heap {index} had {heap_usage}B used of {heap_budget}B total before this request.",+ index = ty.heap_index,+ );+ Err(crate::DeviceError::OutOfMemory)+ }+ } else {+ log::warn!("Failed to find a suitable heap for {requirements:?}");+ Err(crate::DeviceError::OutOfMemory)+ }+ }+}++impl crate::Device for super::Device {+ type A = super::Api;++ unsafe fn create_buffer(+ &self,+ desc: &crate::BufferDescriptor,+ ) -> Result<super::Buffer, crate::DeviceError> {+ let vk_info = vk::BufferCreateInfo::default()+ .size(desc.size)+ .usage(conv::map_buffer_usage(desc.usage))+ .sharing_mode(vk::SharingMode::EXCLUSIVE);++ let raw = unsafe {+ self.shared+ .raw+ .create_buffer(&vk_info, None)+ .map_err(super::map_host_device_oom_and_ioca_err)?+ };++ let mut requirements = unsafe { self.shared.raw.get_buffer_memory_requirements(raw) };++ let is_cpu_read = desc.usage.contains(wgt::BufferUses::MAP_READ);+ let is_cpu_write = desc.usage.contains(wgt::BufferUses::MAP_WRITE);++ let location = match (is_cpu_read, is_cpu_write) {+ (true, true) => gpu_allocator::MemoryLocation::CpuToGpu,+ (true, false) => gpu_allocator::MemoryLocation::GpuToCpu,+ (false, true) => gpu_allocator::MemoryLocation::CpuToGpu,+ (false, false) => gpu_allocator::MemoryLocation::GpuOnly,+ };++ self.error_if_would_oom_on_resource_allocation(location, &requirements)+ .inspect_err(|_| {+ unsafe { self.shared.raw.destroy_buffer(raw, None) };+ })?;++ let name = desc.label.unwrap_or("Unlabeled buffer");++ if desc+ .usage+ .contains(wgt::BufferUses::ACCELERATION_STRUCTURE_SCRATCH)+ {+ // There is no way to specify this usage to Vulkan so we must make sure the alignment requirement is large enough.+ requirements.alignment = requirements+ .alignment+ .max(self.shared.private_caps.scratch_buffer_alignment as u64);+ }++ let allocation = self+ .mem_allocator+ .lock()+ .allocate(&gpu_allocator::vulkan::AllocationCreateDesc {+ name,+ requirements: vk::MemoryRequirements {+ memory_type_bits: requirements.memory_type_bits & self.valid_ash_memory_types,+ ..requirements+ },+ location,+ linear: true, // Buffers are always linear+ allocation_scheme: gpu_allocator::vulkan::AllocationScheme::GpuAllocatorManaged,+ })+ .inspect_err(|_| {+ unsafe { self.shared.raw.destroy_buffer(raw, None) };+ })?;++ unsafe {+ self.shared+ .raw+ .bind_buffer_memory(raw, allocation.memory(), allocation.offset())+ }+ .map_err(super::map_host_device_oom_and_ioca_err)+ .inspect_err(|_| {+ unsafe { self.shared.raw.destroy_buffer(raw, None) };+ })?;++ if let Some(label) = desc.label {+ unsafe { self.shared.set_object_name(raw, label) };+ }++ self.counters.buffer_memory.add(allocation.size() as isize);+ self.counters.buffers.add(1);++ Ok(super::Buffer {+ raw,+ ownership: super::BufferOwnership::Managed(Mutex::new(+ super::BufferMemoryBacking::Managed(allocation),+ )),+ })+ }+ unsafe fn destroy_buffer(&self, buffer: super::Buffer) {+ match buffer.ownership {+ super::BufferOwnership::Managed(allocation) => {+ unsafe { self.shared.raw.destroy_buffer(buffer.raw, None) };+ let allocation = allocation.into_inner();+ self.counters.buffer_memory.sub(allocation.size() as isize);+ match allocation {+ super::BufferMemoryBacking::Managed(allocation) => {+ let result = self.mem_allocator.lock().free(allocation);+ if let Err(err) = result {+ log::warn!("Failed to free buffer allocation: {err}");+ }+ }+ super::BufferMemoryBacking::VulkanMemory { memory, .. } => unsafe {+ self.shared.raw.free_memory(memory, None);+ },+ }+ }+ super::BufferOwnership::RawHandle => {+ unsafe { self.shared.raw.destroy_buffer(buffer.raw, None) };+ }+ super::BufferOwnership::External(_drop_guard) => {+ // The caller owns the `vk::Buffer` and its memory. Dropping+ // `_drop_guard` at the end of this arm runs the cleanup callback.+ }+ }++ self.counters.buffers.sub(1);+ }++ unsafe fn add_raw_buffer(&self, _buffer: &super::Buffer) {+ self.counters.buffers.add(1);+ }++ unsafe fn map_buffer(+ &self,+ buffer: &super::Buffer,+ range: crate::MemoryRange,+ ) -> Result<crate::BufferMapping, crate::DeviceError> {+ let super::BufferOwnership::Managed(ref allocation) = buffer.ownership else {+ crate::hal_usage_error("tried to map external buffer")+ };+ let mut allocation = allocation.lock();+ let super::BufferMemoryBacking::Managed(ref mut allocation) = *allocation else {+ crate::hal_usage_error("tried to map externally created buffer")+ };+ let is_coherent = allocation+ .memory_properties()+ .contains(vk::MemoryPropertyFlags::HOST_COHERENT);+ Ok(crate::BufferMapping {+ ptr: unsafe {+ allocation+ .mapped_ptr()+ .unwrap()+ .cast()+ .offset(range.start as isize)+ },+ is_coherent,+ })+ }++ unsafe fn unmap_buffer(&self, buffer: &super::Buffer) {+ match buffer.ownership {+ super::BufferOwnership::Managed(_) => {+ // gpu-allocator maps the buffer when allocated and unmaps it when free'd+ }+ super::BufferOwnership::RawHandle | super::BufferOwnership::External(_) => {+ crate::hal_usage_error("tried to unmap external buffer")+ }+ }+ }++ unsafe fn flush_mapped_ranges<I>(&self, buffer: &super::Buffer, ranges: I)+ where+ I: Iterator<Item = crate::MemoryRange>,+ {+ if let Some(vk_ranges) = self.shared.make_memory_ranges(buffer, ranges) {+ unsafe {+ self.shared+ .raw+ .flush_mapped_memory_ranges(+ &smallvec::SmallVec::<[vk::MappedMemoryRange; 32]>::from_iter(vk_ranges),+ )+ }+ .unwrap();+ }+ }+ unsafe fn invalidate_mapped_ranges<I>(&self, buffer: &super::Buffer, ranges: I)+ where+ I: Iterator<Item = crate::MemoryRange>,+ {+ if let Some(vk_ranges) = self.shared.make_memory_ranges(buffer, ranges) {+ unsafe {+ self.shared+ .raw+ .invalidate_mapped_memory_ranges(&smallvec::SmallVec::<+ [vk::MappedMemoryRange; 32],+ >::from_iter(vk_ranges))+ }+ .unwrap();+ }+ }++ unsafe fn create_texture(+ &self,+ desc: &crate::TextureDescriptor,+ ) -> Result<super::Texture, crate::DeviceError> {+ let image = self.create_image_without_memory(desc, None)?;++ self.error_if_would_oom_on_resource_allocation(+ gpu_allocator::MemoryLocation::GpuOnly,+ &image.requirements,+ )+ .inspect_err(|_| {+ unsafe { self.shared.raw.destroy_image(image.raw, None) };+ })?;++ let name = desc.label.unwrap_or("Unlabeled texture");++ let allocation = self+ .mem_allocator+ .lock()+ .allocate(&gpu_allocator::vulkan::AllocationCreateDesc {+ name,+ requirements: vk::MemoryRequirements {+ memory_type_bits: image.requirements.memory_type_bits+ & self.valid_ash_memory_types,+ ..image.requirements+ },+ location: gpu_allocator::MemoryLocation::GpuOnly,+ linear: false,+ allocation_scheme: gpu_allocator::vulkan::AllocationScheme::GpuAllocatorManaged,+ })+ .inspect_err(|_| {+ unsafe { self.shared.raw.destroy_image(image.raw, None) };+ })?;++ self.counters.texture_memory.add(allocation.size() as isize);++ unsafe {+ self.shared+ .raw+ .bind_image_memory(image.raw, allocation.memory(), allocation.offset())+ }+ .map_err(super::map_host_device_oom_err)+ .inspect_err(|_| {+ unsafe { self.shared.raw.destroy_image(image.raw, None) };+ })?;++ Ok(unsafe {+ self.texture_from_raw(+ image.raw,+ desc,+ None,+ super::TextureMemory::Allocation(allocation),+ )+ })+ }++ unsafe fn destroy_texture(&self, texture: super::Texture) {+ if texture.drop_guard.is_none() {+ unsafe { self.shared.raw.destroy_image(texture.raw, None) };+ }++ match texture.memory {+ super::TextureMemory::Allocation(allocation) => {+ self.counters.texture_memory.sub(allocation.size() as isize);+ let result = self.mem_allocator.lock().free(allocation);+ if let Err(err) = result {+ log::warn!("Failed to free texture allocation: {err}");+ }+ }+ super::TextureMemory::Dedicated(memory) => unsafe {+ self.shared.raw.free_memory(memory, None);+ },+ super::TextureMemory::External => {}+ }++ self.counters.textures.sub(1);+ }++ unsafe fn add_raw_texture(&self, _texture: &super::Texture) {+ self.counters.textures.add(1);+ }++ unsafe fn create_texture_view(+ &self,+ texture: &super::Texture,+ desc: &crate::TextureViewDescriptor,+ ) -> Result<super::TextureView, crate::DeviceError> {+ let subresource_range = conv::map_subresource_range(&desc.range, texture.format);+ let raw_format = self.shared.private_caps.map_texture_format(desc.format);+ let mut vk_info = vk::ImageViewCreateInfo::default()+ .flags(vk::ImageViewCreateFlags::empty())+ .image(texture.raw)+ .view_type(conv::map_view_dimension(desc.dimension))+ .format(raw_format)+ .subresource_range(subresource_range);+ let layers =+ NonZeroU32::new(subresource_range.layer_count).expect("Unexpected zero layer count");++ let mut image_view_info;+ if self.shared.private_caps.image_view_usage && !desc.usage.is_empty() {+ image_view_info =+ vk::ImageViewUsageCreateInfo::default().usage(conv::map_texture_usage(desc.usage));+ vk_info = vk_info.push_next(&mut image_view_info);+ }++ let raw = unsafe { self.shared.raw.create_image_view(&vk_info, None) }+ .map_err(super::map_host_device_oom_and_ioca_err)?;++ if let Some(label) = desc.label {+ unsafe { self.shared.set_object_name(raw, label) };+ }++ let identity = self.shared.texture_view_identity_factory.next();++ self.counters.texture_views.add(1);++ Ok(super::TextureView {+ raw_texture: texture.raw,+ raw,+ _layers: layers,+ format: desc.format,+ raw_format,+ base_mip_level: desc.range.base_mip_level,+ dimension: desc.dimension,+ texture_identity: texture.identity,+ view_identity: identity,+ })+ }+ unsafe fn destroy_texture_view(&self, view: super::TextureView) {+ unsafe { self.shared.raw.destroy_image_view(view.raw, None) };++ self.counters.texture_views.sub(1);+ }++ unsafe fn create_sampler(+ &self,+ desc: &crate::SamplerDescriptor,+ ) -> Result<super::Sampler, crate::DeviceError> {+ let mut create_info = vk::SamplerCreateInfo::default()+ .flags(vk::SamplerCreateFlags::empty())+ .mag_filter(conv::map_filter_mode(desc.mag_filter))+ .min_filter(conv::map_filter_mode(desc.min_filter))+ .mipmap_mode(conv::map_mip_filter_mode(desc.mipmap_filter))+ .address_mode_u(conv::map_address_mode(desc.address_modes[0]))+ .address_mode_v(conv::map_address_mode(desc.address_modes[1]))+ .address_mode_w(conv::map_address_mode(desc.address_modes[2]))+ .min_lod(desc.lod_clamp.start)+ .max_lod(desc.lod_clamp.end);++ if let Some(fun) = desc.compare {+ create_info = create_info+ .compare_enable(true)+ .compare_op(conv::map_comparison(fun));+ }++ if desc.anisotropy_clamp != 1 {+ // We only enable anisotropy if it is supported, and wgpu-hal interface guarantees+ // the clamp is in the range [1, 16] which is always supported if anisotropy is.+ create_info = create_info+ .anisotropy_enable(true)+ .max_anisotropy(desc.anisotropy_clamp as f32);+ }++ if let Some(color) = desc.border_color {+ create_info = create_info.border_color(conv::map_border_color(color));+ }++ let mut sampler_cache_guard = self.shared.sampler_cache.lock();++ let raw = sampler_cache_guard.create_sampler(&self.shared.raw, create_info)?;++ // Note: Cached samplers will just continually overwrite the label+ //+ // https://github.com/gfx-rs/wgpu/issues/6867+ if let Some(label) = desc.label {+ // SAFETY: we are holding a lock on the sampler cache,+ // so we can only be setting the name from one thread.+ unsafe { self.shared.set_object_name(raw, label) };+ }++ drop(sampler_cache_guard);++ self.counters.samplers.add(1);++ Ok(super::Sampler { raw, create_info })+ }+ unsafe fn destroy_sampler(&self, sampler: super::Sampler) {+ self.shared.sampler_cache.lock().destroy_sampler(+ &self.shared.raw,+ sampler.create_info,+ sampler.raw,+ );++ self.counters.samplers.sub(1);+ }++ unsafe fn create_command_encoder(+ &self,+ desc: &crate::CommandEncoderDescriptor<super::Queue>,+ ) -> Result<super::CommandEncoder, crate::DeviceError> {+ let vk_info = vk::CommandPoolCreateInfo::default()+ .queue_family_index(desc.queue.family_index)+ .flags(vk::CommandPoolCreateFlags::TRANSIENT);++ let raw = unsafe {+ self.shared+ .raw+ .create_command_pool(&vk_info, None)+ .map_err(super::map_host_device_oom_err)?+ };++ self.counters.command_encoders.add(1);++ Ok(super::CommandEncoder {+ raw,+ device: Arc::clone(&self.shared),+ active: vk::CommandBuffer::null(),+ bind_point: vk::PipelineBindPoint::default(),+ temp: super::Temp::default(),+ free: Vec::new(),+ discarded: Vec::new(),+ rpass_debug_marker_active: false,+ end_of_pass_timer_query: None,+ framebuffers: Default::default(),+ temp_texture_views: Default::default(),+ counters: Arc::clone(&self.counters),+ current_pipeline_is_multiview: false,+ })+ }++ unsafe fn create_bind_group_layout(+ &self,+ desc: &crate::BindGroupLayoutDescriptor,+ ) -> Result<super::BindGroupLayout, crate::DeviceError> {+ // Iterate through the entries and accumulate our Vulkan+ // DescriptorSetLayoutBindings and DescriptorBindingFlags, as well as+ // our binding map and our descriptor counts.+ // Note: not bothering with on stack arrays here as it's low frequency+ let mut vk_bindings = Vec::new();+ let mut binding_flags = Vec::new();+ let mut binding_map = Vec::new();+ let mut next_binding = 0;+ let mut contains_binding_arrays = false;+ let mut desc_count = DescriptorCounts::default();+ for entry in desc.entries {+ if entry.count.is_some() {+ contains_binding_arrays = true;+ }++ let partially_bound = desc+ .flags+ .contains(crate::BindGroupLayoutFlags::PARTIALLY_BOUND);+ let mut flags = vk::DescriptorBindingFlags::empty();+ if partially_bound && entry.count.is_some() {+ flags |= vk::DescriptorBindingFlags::PARTIALLY_BOUND;+ }+ if entry.count.is_some() {+ flags |= vk::DescriptorBindingFlags::UPDATE_AFTER_BIND;+ }++ let count = entry.count.map_or(1, |c| c.get());+ match entry.ty {+ wgt::BindingType::ExternalTexture => unimplemented!(),+ _ => {+ vk_bindings.push(vk::DescriptorSetLayoutBinding {+ binding: next_binding,+ descriptor_type: conv::map_binding_type(entry.ty),+ descriptor_count: count,+ stage_flags: conv::map_shader_stage(entry.visibility),+ p_immutable_samplers: ptr::null(),+ _marker: Default::default(),+ });+ binding_flags.push(flags);+ binding_map.push((+ entry.binding,+ super::BindingInfo {+ binding: next_binding,+ binding_array_size: entry.count,+ },+ ));+ next_binding += 1;+ }+ }++ match entry.ty {+ wgt::BindingType::Buffer {+ ty,+ has_dynamic_offset,+ ..+ } => match ty {+ wgt::BufferBindingType::Uniform => {+ if has_dynamic_offset {+ desc_count.uniform_buffer_dynamic += count;+ } else {+ desc_count.uniform_buffer += count;+ }+ }+ wgt::BufferBindingType::Storage { .. } => {+ if has_dynamic_offset {+ desc_count.storage_buffer_dynamic += count;+ } else {+ desc_count.storage_buffer += count;+ }+ }+ },+ wgt::BindingType::Sampler { .. } => {+ desc_count.sampler += count;+ }+ wgt::BindingType::Texture { .. } => {+ desc_count.sampled_image += count;+ }+ wgt::BindingType::StorageTexture { .. } => {+ desc_count.storage_image += count;+ }+ wgt::BindingType::AccelerationStructure { .. } => {+ desc_count.acceleration_structure += count;+ }+ wgt::BindingType::ExternalTexture => unimplemented!(),+ }+ }++ let vk_info = vk::DescriptorSetLayoutCreateInfo::default()+ .bindings(&vk_bindings)+ .flags(if contains_binding_arrays {+ vk::DescriptorSetLayoutCreateFlags::UPDATE_AFTER_BIND_POOL+ } else {+ vk::DescriptorSetLayoutCreateFlags::empty()+ });++ let mut binding_flag_info =+ vk::DescriptorSetLayoutBindingFlagsCreateInfo::default().binding_flags(&binding_flags);++ let vk_info = vk_info.push_next(&mut binding_flag_info);++ let raw = unsafe {+ self.shared+ .raw+ .create_descriptor_set_layout(&vk_info, None)+ .map_err(super::map_host_device_oom_err)?+ };++ if let Some(label) = desc.label {+ unsafe { self.shared.set_object_name(raw, label) };+ }++ let layout = super::BindGroupLayout {+ raw,+ desc_count,+ entries: desc.entries.into(),+ binding_map,+ contains_binding_arrays,+ };++ let result = self+ .desc_allocator+ .lock()+ .register_layout(&self.shared.raw, &layout);+ if let Err(err) = result {+ unsafe {+ self.shared+ .raw+ .destroy_descriptor_set_layout(layout.raw, None)+ };+ return Err(err);+ }++ self.counters.bind_group_layouts.add(1);++ Ok(layout)+ }+ unsafe fn destroy_bind_group_layout(&self, bg_layout: super::BindGroupLayout) {+ self.desc_allocator+ .lock()+ .unregister_layout(&self.shared.raw, &bg_layout);++ unsafe {+ self.shared+ .raw+ .destroy_descriptor_set_layout(bg_layout.raw, None)+ };++ self.counters.bind_group_layouts.sub(1);+ }++ unsafe fn create_pipeline_layout(+ &self,+ desc: &crate::PipelineLayoutDescriptor<super::BindGroupLayout>,+ ) -> Result<super::PipelineLayout, crate::DeviceError> {+ //Note: not bothering with on stack array here as it's low frequency+ let vk_set_layouts = desc+ .bind_group_layouts+ .iter()+ .map(|bgl| match bgl {+ Some(bgl) => bgl.raw,+ None => {+ // `VUID-VkPipelineLayoutCreateInfo-pSetLayouts-parameter`+ // says `VK_NULL_HANDLE` is allowed but+ // `VUID-VkPipelineLayoutCreateInfo-graphicsPipelineLibrary-06753`+ // says it's not, unless the `graphicsPipelineLibrary`+ // feature is enabled.+ //+ // We use an empty descriptor set layout to work around this.+ self.shared.empty_descriptor_set_layout+ }+ })+ .collect::<Vec<_>>();+ let vk_immediates_ranges: Option<vk::PushConstantRange> = if desc.immediate_size != 0 {+ Some(vk::PushConstantRange {+ stage_flags: vk::ShaderStageFlags::ALL,+ offset: 0,+ size: desc.immediate_size,+ })+ } else {+ None+ };++ let vk_info = vk::PipelineLayoutCreateInfo::default()+ .flags(vk::PipelineLayoutCreateFlags::empty())+ .set_layouts(&vk_set_layouts)+ .push_constant_ranges(vk_immediates_ranges.as_slice());++ let raw = {+ profiling::scope!("vkCreatePipelineLayout");+ unsafe {+ self.shared+ .raw+ .create_pipeline_layout(&vk_info, None)+ .map_err(super::map_host_device_oom_err)?+ }+ };++ if let Some(label) = desc.label {+ unsafe { self.shared.set_object_name(raw, label) };+ }++ let mut binding_map = BTreeMap::new();+ for (group, layout) in desc.bind_group_layouts.iter().enumerate() {+ let Some(layout) = layout else {+ continue;+ };++ for &(binding, binding_info) in &layout.binding_map {+ binding_map.insert(+ naga::ResourceBinding {+ group: group as u32,+ binding,+ },+ naga::back::spv::BindingInfo {+ descriptor_set: group as u32,+ binding: binding_info.binding,+ binding_array_size: binding_info.binding_array_size.map(NonZeroU32::get),+ },+ );+ }+ }++ self.counters.pipeline_layouts.add(1);+ Ok(super::PipelineLayout { raw, binding_map })+ }+ unsafe fn destroy_pipeline_layout(&self, pipeline_layout: super::PipelineLayout) {+ unsafe {+ self.shared+ .raw+ .destroy_pipeline_layout(pipeline_layout.raw, None)+ };++ self.counters.pipeline_layouts.sub(1);+ }++ unsafe fn create_bind_group(+ &self,+ desc: &crate::BindGroupDescriptor<+ super::BindGroupLayout,+ super::Buffer,+ super::Sampler,+ super::TextureView,+ super::AccelerationStructure,+ >,+ ) -> Result<super::BindGroup, crate::DeviceError> {+ let set = unsafe {+ self.desc_allocator+ .lock()+ .alloc(&self.shared.raw, desc.layout)?+ };++ if let Some(label) = desc.label {+ unsafe { self.shared.set_object_name(set.raw(), label) };+ }++ /// Helper for splitting off and initializing a given number of elements on a pre-allocated+ /// stack, based on items returned from an [`ExactSizeIterator`]. Typically created from a+ /// [`MaybeUninit`] slice (see [`Vec::spare_capacity_mut()`]).+ /// The updated [`ExtensionStack`] of remaining uninitialized elements is returned, safely+ /// representing that the initialized and remaining elements are two independent mutable+ /// borrows.+ struct ExtendStack<'a, T> {+ remainder: &'a mut [MaybeUninit<T>],+ }++ impl<'a, T> ExtendStack<'a, T> {+ fn from_vec_capacity(vec: &'a mut Vec<T>) -> Self {+ Self {+ remainder: vec.spare_capacity_mut(),+ }+ }++ fn extend_one(self, value: T) -> (Self, &'a mut T) {+ let (to_init, remainder) = self.remainder.split_first_mut().unwrap();+ let init = to_init.write(value);+ (Self { remainder }, init)+ }++ fn extend(+ self,+ iter: impl IntoIterator<Item = T> + ExactSizeIterator,+ ) -> (Self, &'a mut [T]) {+ let (to_init, remainder) = self.remainder.split_at_mut(iter.len());++ for (value, to_init) in iter.into_iter().zip(to_init.iter_mut()) {+ to_init.write(value);+ }++ // we can't use the safe (yet unstable) MaybeUninit::write_slice() here because of having an iterator to write++ let init = {+ // SAFETY: The loop above has initialized exactly as many items as to_init is+ // long, so it is safe to cast away the MaybeUninit<T> wrapper into T.++ // Additional safety docs from unstable slice_assume_init_mut+ // SAFETY: similar to safety notes for `slice_get_ref`, but we have a+ // mutable reference which is also guaranteed to be valid for writes.+ unsafe { mem::transmute::<&mut [MaybeUninit<T>], &mut [T]>(to_init) }+ };+ (Self { remainder }, init)+ }+ }++ let mut writes = Vec::with_capacity(desc.entries.len());+ let mut buffer_infos = Vec::with_capacity(desc.buffers.len());+ let mut buffer_infos = ExtendStack::from_vec_capacity(&mut buffer_infos);+ let mut image_infos = Vec::with_capacity(desc.samplers.len() + desc.textures.len());+ let mut image_infos = ExtendStack::from_vec_capacity(&mut image_infos);+ // TODO: This length could be reduced to just the number of top-level acceleration+ // structure bindings, where multiple consecutive TLAS bindings that are set via+ // one `WriteDescriptorSet` count towards one "info" struct, not the total number of+ // acceleration structure bindings to write:+ let mut acceleration_structure_infos =+ Vec::with_capacity(desc.acceleration_structures.len());+ let mut acceleration_structure_infos =+ ExtendStack::from_vec_capacity(&mut acceleration_structure_infos);+ let mut raw_acceleration_structures =+ Vec::with_capacity(desc.acceleration_structures.len());+ let mut raw_acceleration_structures =+ ExtendStack::from_vec_capacity(&mut raw_acceleration_structures);++ let layout_and_entry_iter = desc.entries.iter().map(|entry| {+ let layout = desc+ .layout+ .entries+ .iter()+ .find(|layout_entry| layout_entry.binding == entry.binding)+ .expect("internal error: no layout entry found with binding slot");+ (layout, entry)+ });+ let mut next_binding = 0;+ for (layout, entry) in layout_and_entry_iter {+ let write = vk::WriteDescriptorSet::default().dst_set(set.raw());++ match layout.ty {+ wgt::BindingType::Sampler(_) => {+ let start = entry.resource_index;+ let end = start + entry.count;+ let local_image_infos;+ (image_infos, local_image_infos) =+ image_infos.extend(desc.samplers[start as usize..end as usize].iter().map(+ |sampler| vk::DescriptorImageInfo::default().sampler(sampler.raw),+ ));+ writes.push(+ write+ .dst_binding(next_binding)+ .descriptor_type(conv::map_binding_type(layout.ty))+ .image_info(local_image_infos),+ );+ next_binding += 1;+ }+ wgt::BindingType::Texture { .. } | wgt::BindingType::StorageTexture { .. } => {+ let start = entry.resource_index;+ let end = start + entry.count;+ let local_image_infos;+ (image_infos, local_image_infos) =+ image_infos.extend(desc.textures[start as usize..end as usize].iter().map(+ |binding| {+ let layout =+ conv::derive_image_layout(binding.usage, binding.view.format);+ vk::DescriptorImageInfo::default()+ .image_view(binding.view.raw)+ .image_layout(layout)+ },+ ));+ writes.push(+ write+ .dst_binding(next_binding)+ .descriptor_type(conv::map_binding_type(layout.ty))+ .image_info(local_image_infos),+ );+ next_binding += 1;+ }+ wgt::BindingType::Buffer { .. } => {+ let start = entry.resource_index;+ let end = start + entry.count;+ let local_buffer_infos;+ (buffer_infos, local_buffer_infos) =+ buffer_infos.extend(desc.buffers[start as usize..end as usize].iter().map(+ |binding| {+ vk::DescriptorBufferInfo::default()+ .buffer(binding.buffer.raw)+ .offset(binding.offset)+ .range(+ binding.size.map_or(vk::WHOLE_SIZE, wgt::BufferSize::get),+ )+ },+ ));+ writes.push(+ write+ .dst_binding(next_binding)+ .descriptor_type(conv::map_binding_type(layout.ty))+ .buffer_info(local_buffer_infos),+ );+ next_binding += 1;+ }+ wgt::BindingType::AccelerationStructure { .. } => {+ let start = entry.resource_index;+ let end = start + entry.count;++ let local_raw_acceleration_structures;+ (+ raw_acceleration_structures,+ local_raw_acceleration_structures,+ ) = raw_acceleration_structures.extend(+ desc.acceleration_structures[start as usize..end as usize]+ .iter()+ .map(|acceleration_structure| acceleration_structure.raw),+ );++ let local_acceleration_structure_infos;+ (+ acceleration_structure_infos,+ local_acceleration_structure_infos,+ ) = acceleration_structure_infos.extend_one(+ vk::WriteDescriptorSetAccelerationStructureKHR::default()+ .acceleration_structures(local_raw_acceleration_structures),+ );++ writes.push(+ write+ .dst_binding(next_binding)+ .descriptor_type(conv::map_binding_type(layout.ty))+ .descriptor_count(entry.count)+ .push_next(local_acceleration_structure_infos),+ );+ next_binding += 1;+ }+ wgt::BindingType::ExternalTexture => unimplemented!(),+ }+ }++ unsafe { self.shared.raw.update_descriptor_sets(&writes, &[]) };++ self.counters.bind_groups.add(1);++ Ok(super::BindGroup { set })+ }++ unsafe fn destroy_bind_group(&self, group: super::BindGroup) {+ unsafe { self.desc_allocator.lock().free(&self.shared.raw, group.set) };++ self.counters.bind_groups.sub(1);+ }++ unsafe fn create_shader_module(+ &self,+ desc: &crate::ShaderModuleDescriptor,+ shader: crate::ShaderInput,+ ) -> Result<super::ShaderModule, crate::ShaderError> {+ let shader_module = match shader {+ crate::ShaderInput::Naga(naga_shader)+ if self+ .shared+ .workarounds+ .contains(super::Workarounds::SEPARATE_ENTRY_POINTS)+ || !naga_shader.module.overrides.is_empty() =>+ {+ super::ShaderModule::Intermediate {+ naga_shader,+ runtime_checks: desc.runtime_checks,+ }+ }+ crate::ShaderInput::Naga(naga_shader) => {+ let mut naga_options = self.naga_options.clone();+ naga_options.debug_info =+ naga_shader+ .debug_source+ .as_ref()+ .map(|d| naga::back::spv::DebugInfo {+ source_code: d.source_code.as_ref(),+ file_name: d.file_name.as_ref(),+ language: naga::back::spv::SourceLanguage::WGSL,+ });+ if !desc.runtime_checks.bounds_checks {+ naga_options.bounds_check_policies = naga::proc::BoundsCheckPolicies {+ index: naga::proc::BoundsCheckPolicy::Unchecked,+ buffer: naga::proc::BoundsCheckPolicy::Unchecked,+ image_load: naga::proc::BoundsCheckPolicy::Unchecked,+ binding_array: naga::proc::BoundsCheckPolicy::Unchecked,+ };+ }+ let spv = naga::back::spv::write_vec(+ &naga_shader.module,+ &naga_shader.info,+ &naga_options,+ None,+ )+ .map_err(|e| crate::ShaderError::Compilation(format!("{e}")))?;+ super::ShaderModule::Raw(self.create_shader_module_impl(&spv, &desc.label)?)+ }+ crate::ShaderInput::SpirV(data) => {+ super::ShaderModule::Raw(self.create_shader_module_impl(data, &desc.label)?)+ }+ crate::ShaderInput::MetalLib { .. }+ | crate::ShaderInput::Msl { .. }+ | crate::ShaderInput::Dxil { .. }+ | crate::ShaderInput::Hlsl { .. }+ | crate::ShaderInput::Glsl { .. } => unreachable!(),+ };++ self.counters.shader_modules.add(1);++ Ok(shader_module)+ }++ unsafe fn destroy_shader_module(&self, module: super::ShaderModule) {+ match module {+ super::ShaderModule::Raw(raw) => {+ unsafe { self.shared.raw.destroy_shader_module(raw, None) };+ }+ super::ShaderModule::Intermediate { .. } => {}+ }++ self.counters.shader_modules.sub(1);+ }++ unsafe fn create_render_pipeline(+ &self,+ desc: &crate::RenderPipelineDescriptor<+ super::PipelineLayout,+ super::ShaderModule,+ super::PipelineCache,+ >,+ ) -> Result<super::RenderPipeline, crate::PipelineError> {+ let dynamic_states = [+ vk::DynamicState::VIEWPORT,+ vk::DynamicState::SCISSOR,+ vk::DynamicState::BLEND_CONSTANTS,+ vk::DynamicState::STENCIL_REFERENCE,+ ];+ let mut compatible_rp_key = super::RenderPassKey {+ sample_count: desc.multisample.count,+ multiview_mask: desc.multiview_mask,+ ..Default::default()+ };+ let mut stages = ArrayVec::<_, { crate::MAX_CONCURRENT_SHADER_STAGES }>::new();+ let mut vertex_buffers = Vec::new();+ let mut vertex_attributes = Vec::new();++ if let crate::VertexProcessor::Standard {+ vertex_buffers: desc_vertex_buffers,+ vertex_stage: _,+ } = &desc.vertex_processor+ {+ vertex_buffers = Vec::with_capacity(desc_vertex_buffers.len());+ for (i, vb) in desc_vertex_buffers.iter().enumerate() {+ let Some(vb) = vb else {+ continue;+ };+ vertex_buffers.push(vk::VertexInputBindingDescription {+ binding: i as u32,+ stride: vb.array_stride as u32,+ input_rate: match vb.step_mode {+ wgt::VertexStepMode::Vertex => vk::VertexInputRate::VERTEX,+ wgt::VertexStepMode::Instance => vk::VertexInputRate::INSTANCE,+ },+ });+ for at in vb.attributes {+ vertex_attributes.push(vk::VertexInputAttributeDescription {+ location: at.shader_location,+ binding: i as u32,+ format: conv::map_vertex_format(at.format),+ offset: at.offset as u32,+ });+ }+ }+ }++ let vk_vertex_input = vk::PipelineVertexInputStateCreateInfo::default()+ .vertex_binding_descriptions(&vertex_buffers)+ .vertex_attribute_descriptions(&vertex_attributes);++ let vk_input_assembly = vk::PipelineInputAssemblyStateCreateInfo::default()+ .topology(conv::map_topology(desc.primitive.topology))+ .primitive_restart_enable(desc.primitive.strip_index_format.is_some());++ let mut compiled_vs = None;+ let mut compiled_ms = None;+ let mut compiled_ts = None;+ match &desc.vertex_processor {+ crate::VertexProcessor::Standard {+ vertex_buffers: _,+ vertex_stage,+ } => {+ compiled_vs = Some(self.compile_stage(+ vertex_stage,+ naga::ShaderStage::Vertex,+ &desc.layout.binding_map,+ )?);+ stages.push(compiled_vs.as_ref().unwrap().create_info);+ }+ crate::VertexProcessor::Mesh {+ task_stage,+ mesh_stage,+ } => {+ if let Some(t) = task_stage.as_ref() {+ compiled_ts = Some(self.compile_stage(+ t,+ naga::ShaderStage::Task,+ &desc.layout.binding_map,+ )?);+ stages.push(compiled_ts.as_ref().unwrap().create_info);+ }+ compiled_ms = Some(self.compile_stage(+ mesh_stage,+ naga::ShaderStage::Mesh,+ &desc.layout.binding_map,+ )?);+ stages.push(compiled_ms.as_ref().unwrap().create_info);+ }+ }+ let compiled_fs = match desc.fragment_stage {+ Some(ref stage) => {+ let compiled = self.compile_stage(+ stage,+ naga::ShaderStage::Fragment,+ &desc.layout.binding_map,+ )?;+ stages.push(compiled.create_info);+ Some(compiled)+ }+ None => None,+ };++ let mut vk_rasterization = vk::PipelineRasterizationStateCreateInfo::default()+ .polygon_mode(conv::map_polygon_mode(desc.primitive.polygon_mode))+ .front_face(conv::map_front_face(desc.primitive.front_face))+ .line_width(1.0)+ .depth_clamp_enable(desc.primitive.unclipped_depth);+ if let Some(face) = desc.primitive.cull_mode {+ vk_rasterization = vk_rasterization.cull_mode(conv::map_cull_face(face))+ }+ let mut vk_rasterization_conservative_state =+ vk::PipelineRasterizationConservativeStateCreateInfoEXT::default()+ .conservative_rasterization_mode(+ vk::ConservativeRasterizationModeEXT::OVERESTIMATE,+ );+ if desc.primitive.conservative {+ vk_rasterization = vk_rasterization.push_next(&mut vk_rasterization_conservative_state);+ }++ let mut vk_depth_stencil = vk::PipelineDepthStencilStateCreateInfo::default();+ if let Some(ref ds) = desc.depth_stencil {+ let vk_format = self.shared.private_caps.map_texture_format(ds.format);+ let vk_layout = if ds.is_read_only(desc.primitive.cull_mode) {+ vk::ImageLayout::DEPTH_STENCIL_READ_ONLY_OPTIMAL+ } else {+ vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL+ };+ compatible_rp_key.depth_stencil = Some(super::DepthStencilAttachmentKey {+ base: super::AttachmentKey::compatible(vk_format, vk_layout),+ stencil_ops: crate::AttachmentOps::all(),+ });++ if ds.is_depth_enabled() {+ vk_depth_stencil = vk_depth_stencil+ .depth_test_enable(true)+ .depth_write_enable(ds.depth_write_enabled.unwrap_or_default())+ .depth_compare_op(conv::map_comparison(ds.depth_compare.unwrap_or_default()));+ }+ if ds.stencil.is_enabled() {+ let s = &ds.stencil;+ let front = conv::map_stencil_face(&s.front, s.read_mask, s.write_mask);+ let back = conv::map_stencil_face(&s.back, s.read_mask, s.write_mask);+ vk_depth_stencil = vk_depth_stencil+ .stencil_test_enable(true)+ .front(front)+ .back(back);+ }++ if ds.bias.is_enabled() {+ vk_rasterization = vk_rasterization+ .depth_bias_enable(true)+ .depth_bias_constant_factor(ds.bias.constant as f32)+ .depth_bias_clamp(ds.bias.clamp)+ .depth_bias_slope_factor(ds.bias.slope_scale);+ }+ }++ let vk_viewport = vk::PipelineViewportStateCreateInfo::default()+ .flags(vk::PipelineViewportStateCreateFlags::empty())+ .scissor_count(1)+ .viewport_count(1);++ let vk_sample_mask = [+ desc.multisample.mask as u32,+ (desc.multisample.mask >> 32) as u32,+ ];+ let vk_multisample = vk::PipelineMultisampleStateCreateInfo::default()+ .rasterization_samples(vk::SampleCountFlags::from_raw(desc.multisample.count))+ .alpha_to_coverage_enable(desc.multisample.alpha_to_coverage_enabled)+ .sample_mask(&vk_sample_mask);++ let mut vk_attachments = Vec::with_capacity(desc.color_targets.len());+ for cat in desc.color_targets {+ let (key, attarchment) = if let Some(cat) = cat.as_ref() {+ let mut vk_attachment = vk::PipelineColorBlendAttachmentState::default()+ .color_write_mask(vk::ColorComponentFlags::from_raw(cat.write_mask.bits()));+ if let Some(ref blend) = cat.blend {+ let (color_op, color_src, color_dst) = conv::map_blend_component(&blend.color);+ let (alpha_op, alpha_src, alpha_dst) = conv::map_blend_component(&blend.alpha);+ vk_attachment = vk_attachment+ .blend_enable(true)+ .color_blend_op(color_op)+ .src_color_blend_factor(color_src)+ .dst_color_blend_factor(color_dst)+ .alpha_blend_op(alpha_op)+ .src_alpha_blend_factor(alpha_src)+ .dst_alpha_blend_factor(alpha_dst);+ }++ let vk_format = self.shared.private_caps.map_texture_format(cat.format);+ (+ Some(super::ColorAttachmentKey {+ base: super::AttachmentKey::compatible(+ vk_format,+ vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL,+ ),+ resolve: None,+ }),+ vk_attachment,+ )+ } else {+ (None, vk::PipelineColorBlendAttachmentState::default())+ };++ compatible_rp_key.colors.push(key);+ vk_attachments.push(attarchment);+ }++ let vk_color_blend =+ vk::PipelineColorBlendStateCreateInfo::default().attachments(&vk_attachments);++ let vk_dynamic_state =+ vk::PipelineDynamicStateCreateInfo::default().dynamic_states(&dynamic_states);++ let raw_pass = self.shared.make_render_pass(compatible_rp_key)?;++ let vk_infos = [{+ vk::GraphicsPipelineCreateInfo::default()+ .layout(desc.layout.raw)+ .stages(&stages)+ .vertex_input_state(&vk_vertex_input)+ .input_assembly_state(&vk_input_assembly)+ .rasterization_state(&vk_rasterization)+ .viewport_state(&vk_viewport)+ .multisample_state(&vk_multisample)+ .depth_stencil_state(&vk_depth_stencil)+ .color_blend_state(&vk_color_blend)+ .dynamic_state(&vk_dynamic_state)+ .render_pass(raw_pass)+ }];++ let pipeline_cache = desc+ .cache+ .map(|it| it.raw)+ .unwrap_or(vk::PipelineCache::null());++ let mut raw_vec = {+ profiling::scope!("vkCreateGraphicsPipelines");+ unsafe {+ self.shared+ .raw+ .create_graphics_pipelines(pipeline_cache, &vk_infos, None)+ .map_err(|(_, e)| super::map_pipeline_err(e))+ }?+ };++ let raw = raw_vec.pop().unwrap();+ if let Some(label) = desc.label {+ unsafe { self.shared.set_object_name(raw, label) };+ }++ if let Some(CompiledStage {+ temp_raw_module: Some(raw_module),+ ..+ }) = compiled_vs+ {+ unsafe { self.shared.raw.destroy_shader_module(raw_module, None) };+ }+ if let Some(CompiledStage {+ temp_raw_module: Some(raw_module),+ ..+ }) = compiled_ts+ {+ unsafe { self.shared.raw.destroy_shader_module(raw_module, None) };+ }+ if let Some(CompiledStage {+ temp_raw_module: Some(raw_module),+ ..+ }) = compiled_ms+ {+ unsafe { self.shared.raw.destroy_shader_module(raw_module, None) };+ }+ if let Some(CompiledStage {+ temp_raw_module: Some(raw_module),+ ..+ }) = compiled_fs+ {+ unsafe { self.shared.raw.destroy_shader_module(raw_module, None) };+ }++ self.counters.render_pipelines.add(1);++ Ok(super::RenderPipeline {+ raw,+ is_multiview: desc.multiview_mask.is_some(),+ })+ }++ unsafe fn destroy_render_pipeline(&self, pipeline: super::RenderPipeline) {+ unsafe { self.shared.raw.destroy_pipeline(pipeline.raw, None) };++ self.counters.render_pipelines.sub(1);+ }++ unsafe fn create_compute_pipeline(+ &self,+ desc: &crate::ComputePipelineDescriptor<+ super::PipelineLayout,+ super::ShaderModule,+ super::PipelineCache,+ >,+ ) -> Result<super::ComputePipeline, crate::PipelineError> {+ let compiled = self.compile_stage(+ &desc.stage,+ naga::ShaderStage::Compute,+ &desc.layout.binding_map,+ )?;++ let vk_infos = [{+ vk::ComputePipelineCreateInfo::default()+ .layout(desc.layout.raw)+ .stage(compiled.create_info)+ }];++ let pipeline_cache = desc+ .cache+ .map(|it| it.raw)+ .unwrap_or(vk::PipelineCache::null());++ let mut raw_vec = {+ profiling::scope!("vkCreateComputePipelines");+ unsafe {+ self.shared+ .raw+ .create_compute_pipelines(pipeline_cache, &vk_infos, None)+ .map_err(|(_, e)| super::map_pipeline_err(e))+ }?+ };++ let raw = raw_vec.pop().unwrap();+ if let Some(label) = desc.label {+ unsafe { self.shared.set_object_name(raw, label) };+ }++ if let Some(raw_module) = compiled.temp_raw_module {+ unsafe { self.shared.raw.destroy_shader_module(raw_module, None) };+ }++ self.counters.compute_pipelines.add(1);++ Ok(super::ComputePipeline { raw })+ }++ unsafe fn destroy_compute_pipeline(&self, pipeline: super::ComputePipeline) {+ unsafe { self.shared.raw.destroy_pipeline(pipeline.raw, None) };++ self.counters.compute_pipelines.sub(1);+ }++ unsafe fn create_ray_tracing_pipeline(+ &self,+ desc: &crate::RayTracingPipelineDescriptor<+ super::PipelineLayout,+ super::ShaderModule,+ super::PipelineCache,+ >,+ ) -> Result<super::RayTracingPipeline, crate::PipelineError> {+ let mut stages = Vec::new();+ let mut groups = Vec::new();++ let compiled_ray_gen = self.compile_stage(+ &desc.ray_generation,+ naga::ShaderStage::RayGeneration,+ &desc.layout.binding_map,+ )?;++ groups.push(+ vk::RayTracingShaderGroupCreateInfoKHR::default()+ .closest_hit_shader(vk::SHADER_UNUSED_KHR)+ .any_hit_shader(vk::SHADER_UNUSED_KHR)+ .intersection_shader(vk::SHADER_UNUSED_KHR)+ .general_shader(0) // stages is empty so next index is 0.+ .ty(vk::RayTracingShaderGroupTypeKHR::GENERAL),+ );++ stages.push(compiled_ray_gen.create_info);++ let compiled_miss = self.compile_stage(+ &desc.miss,+ naga::ShaderStage::Miss,+ &desc.layout.binding_map,+ )?;++ groups.push(+ vk::RayTracingShaderGroupCreateInfoKHR::default()+ .closest_hit_shader(vk::SHADER_UNUSED_KHR)+ .any_hit_shader(vk::SHADER_UNUSED_KHR)+ .intersection_shader(vk::SHADER_UNUSED_KHR)+ .general_shader(1) // stages always has one element so next index is 1.+ .ty(vk::RayTracingShaderGroupTypeKHR::GENERAL),+ );++ stages.push(compiled_miss.create_info);++ // This is to keep alive the CStrings, as the ones in the loop would be deallocated+ // causing UB otherwise.+ let mut compiled_stages = Vec::new();++ for group in desc.intersection {+ let compiled_closest_hits = self.compile_stage(+ &group.closest_hit,+ naga::ShaderStage::ClosestHit,+ &desc.layout.binding_map,+ )?;++ let closest_idx = stages.len();++ stages.push(compiled_closest_hits.create_info);++ compiled_stages.push(compiled_closest_hits);++ let mut raw_hit: vk::RayTracingShaderGroupCreateInfoKHR<'_> =+ vk::RayTracingShaderGroupCreateInfoKHR::default()+ .closest_hit_shader(closest_idx as _)+ .any_hit_shader(vk::SHADER_UNUSED_KHR)+ .intersection_shader(vk::SHADER_UNUSED_KHR)+ .general_shader(vk::SHADER_UNUSED_KHR)+ .ty(vk::RayTracingShaderGroupTypeKHR::TRIANGLES_HIT_GROUP);++ if let Some(any_hit) = &group.any_hit {+ let compiled_any_hit = self.compile_stage(+ any_hit,+ naga::ShaderStage::AnyHit,+ &desc.layout.binding_map,+ )?;++ let any_idx = stages.len();++ stages.push(compiled_any_hit.create_info);++ compiled_stages.push(compiled_any_hit);++ raw_hit = raw_hit.any_hit_shader(any_idx as _);+ }++ groups.push(raw_hit);+ }++ let create_infos = [{+ vk::RayTracingPipelineCreateInfoKHR::default()+ .layout(desc.layout.raw)+ .max_pipeline_ray_recursion_depth(desc.max_recursion_depth)+ .stages(&stages)+ .groups(&groups)+ }];++ let pipeline_cache = desc+ .cache+ .map(|it| it.raw)+ .unwrap_or(vk::PipelineCache::null());++ let fns = self+ .shared+ .extension_fns+ .ray_tracing_pipelines+ .as_ref()+ .unwrap();+ let pipelines = unsafe {+ fns.create_ray_tracing_pipelines(+ vk::DeferredOperationKHR::null(),+ pipeline_cache,+ &create_infos,+ None,+ )+ .map_err(|(_, e)| super::map_pipeline_err(e))+ }?;++ if let Some(raw_module) = compiled_ray_gen.temp_raw_module {+ unsafe { self.shared.raw.destroy_shader_module(raw_module, None) };+ }++ if let Some(raw_module) = compiled_miss.temp_raw_module {+ unsafe { self.shared.raw.destroy_shader_module(raw_module, None) };+ }++ for raw_module in compiled_stages+ .into_iter()+ .flat_map(|stage| stage.temp_raw_module)+ {+ unsafe { self.shared.raw.destroy_shader_module(raw_module, None) };+ }++ self.counters.ray_tracing_pipelines.add(1);++ Ok(super::RayTracingPipeline { raw: pipelines[0] })+ }++ unsafe fn destroy_ray_tracing_pipeline(&self, pipeline: super::RayTracingPipeline) {+ unsafe { self.shared.raw.destroy_pipeline(pipeline.raw, None) };++ self.counters.ray_tracing_pipelines.sub(1);+ }++ unsafe fn get_raytracing_pipeline_group_data(+ &self,+ pipeline: &super::RayTracingPipeline,+ groups: core::ops::Range<u32>,+ ) -> Result<Vec<u8>, crate::DeviceError> {+ let fns = self+ .shared+ .extension_fns+ .ray_tracing_pipelines+ .as_ref()+ .unwrap();++ let num = groups.end - groups.start;++ unsafe {+ fns.get_ray_tracing_shader_group_handles(+ pipeline.raw,+ groups.start,+ num,+ (num * self+ .shared+ .private_caps+ .ray_tracing_pipeline_group_data_size) as usize,+ )+ }+ .map_err(super::map_host_device_oom_err)+ }++ unsafe fn create_pipeline_cache(+ &self,+ desc: &crate::PipelineCacheDescriptor<'_>,+ ) -> Result<super::PipelineCache, crate::PipelineCacheError> {+ let mut info = vk::PipelineCacheCreateInfo::default();+ if let Some(data) = desc.data {+ info = info.initial_data(data)+ }+ profiling::scope!("vkCreatePipelineCache");+ let raw = unsafe { self.shared.raw.create_pipeline_cache(&info, None) }+ .map_err(super::map_host_device_oom_err)?;++ Ok(super::PipelineCache { raw })+ }+ fn pipeline_cache_validation_key(&self) -> Option<[u8; 16]> {+ Some(self.shared.pipeline_cache_validation_key)+ }+ unsafe fn destroy_pipeline_cache(&self, cache: super::PipelineCache) {+ unsafe { self.shared.raw.destroy_pipeline_cache(cache.raw, None) }+ }+ unsafe fn create_query_set(+ &self,+ desc: &wgt::QuerySetDescriptor<crate::Label>,+ ) -> Result<super::QuerySet, crate::DeviceError> {+ // Assume each query is 256 bytes. This is the case for occlusion+ // queries on an AMD W6800 with driver version 32.0.12030.9. The+ // size and allocation policy may vary; this is an approximate+ // check only.+ self.error_if_would_oom_on_resource_allocation(+ gpu_allocator::MemoryLocation::GpuToCpu,+ &vk::MemoryRequirements {+ size: desc.count as u64 * 256,+ alignment: 256,+ memory_type_bits: self.valid_ash_memory_types,+ },+ )?;++ let (vk_type, pipeline_statistics) = match desc.ty {+ wgt::QueryType::Occlusion => (+ vk::QueryType::OCCLUSION,+ vk::QueryPipelineStatisticFlags::empty(),+ ),+ wgt::QueryType::PipelineStatistics(statistics) => (+ vk::QueryType::PIPELINE_STATISTICS,+ conv::map_pipeline_statistics(statistics),+ ),+ wgt::QueryType::Timestamp => (+ vk::QueryType::TIMESTAMP,+ vk::QueryPipelineStatisticFlags::empty(),+ ),+ };++ let vk_info = vk::QueryPoolCreateInfo::default()+ .query_type(vk_type)+ .query_count(desc.count)+ .pipeline_statistics(pipeline_statistics);++ let raw = unsafe { self.shared.raw.create_query_pool(&vk_info, None) }+ .map_err(super::map_host_device_oom_err)?;+ if let Some(label) = desc.label {+ unsafe { self.shared.set_object_name(raw, label) };+ }++ self.counters.query_sets.add(1);++ Ok(super::QuerySet { raw })+ }++ unsafe fn destroy_query_set(&self, set: super::QuerySet) {+ unsafe { self.shared.raw.destroy_query_pool(set.raw, None) };++ self.counters.query_sets.sub(1);+ }++ unsafe fn create_fence(&self) -> Result<super::Fence, crate::DeviceError> {+ self.counters.fences.add(1);++ Ok(if self.shared.private_caps.timeline_semaphores {+ let mut sem_type_info =+ vk::SemaphoreTypeCreateInfo::default().semaphore_type(vk::SemaphoreType::TIMELINE);+ let vk_info = vk::SemaphoreCreateInfo::default().push_next(&mut sem_type_info);+ let raw = unsafe { self.shared.raw.create_semaphore(&vk_info, None) }+ .map_err(super::map_host_device_oom_err)?;++ super::Fence::TimelineSemaphore(raw)+ } else {+ super::Fence::FencePool(RwLock::new(super::FencePool {+ last_completed: 0,+ active: Vec::new(),+ free: Vec::new(),+ }))+ })+ }+ unsafe fn destroy_fence(&self, fence: super::Fence) {+ match fence {+ super::Fence::TimelineSemaphore(raw) => {+ unsafe { self.shared.raw.destroy_semaphore(raw, None) };+ }+ super::Fence::FencePool(pool) => {+ let super::FencePool {+ active,+ free,+ last_completed: _,+ } = pool.into_inner();++ for (_, raw) in active {+ unsafe {+ self.shared.raw.destroy_fence(Arc::into_inner(raw).expect("Fence should have its reference count be one by the end of each function"), None)+ };+ }+ for raw in free {+ unsafe { self.shared.raw.destroy_fence(raw, None) };+ }+ }+ }++ self.counters.fences.sub(1);+ }+ unsafe fn get_fence_value(+ &self,+ fence: &super::Fence,+ ) -> Result<crate::FenceValue, crate::DeviceError> {+ fence.get_latest(+ &self.shared.raw,+ self.shared.extension_fns.timeline_semaphore.as_ref(),+ )+ }+ unsafe fn wait(+ &self,+ fence: &super::Fence,+ wait_value: crate::FenceValue,+ timeout: Option<Duration>,+ ) -> Result<bool, crate::DeviceError> {+ let timeout_ns = timeout+ .unwrap_or(Duration::MAX)+ .as_nanos()+ .min(u64::MAX as _) as u64;+ self.shared.wait_for_fence(fence, wait_value, timeout_ns)+ }++ unsafe fn start_graphics_debugger_capture(&self) -> bool {+ #[cfg(feature = "renderdoc")]+ {+ // Renderdoc requires us to give us the pointer that vkInstance _points to_.+ let raw_vk_instance =+ vk::Handle::as_raw(self.shared.instance.raw.handle()) as *mut *mut _;+ let raw_vk_instance_dispatch_table = unsafe { *raw_vk_instance };+ unsafe {+ self.render_doc+ .start_frame_capture(raw_vk_instance_dispatch_table, ptr::null_mut())+ }+ }+ #[cfg(not(feature = "renderdoc"))]+ false+ }+ unsafe fn stop_graphics_debugger_capture(&self) {+ #[cfg(feature = "renderdoc")]+ {+ // Renderdoc requires us to give us the pointer that vkInstance _points to_.+ let raw_vk_instance =+ vk::Handle::as_raw(self.shared.instance.raw.handle()) as *mut *mut _;+ let raw_vk_instance_dispatch_table = unsafe { *raw_vk_instance };++ unsafe {+ self.render_doc+ .end_frame_capture(raw_vk_instance_dispatch_table, ptr::null_mut())+ }+ }+ }++ unsafe fn pipeline_cache_get_data(&self, cache: &super::PipelineCache) -> Option<Vec<u8>> {+ let data = unsafe { self.raw_device().get_pipeline_cache_data(cache.raw) };+ data.ok()+ }++ unsafe fn get_acceleration_structure_build_sizes<'a>(+ &self,+ desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, super::Buffer>,+ ) -> crate::AccelerationStructureBuildSizes {+ const CAPACITY: usize = 8;++ let ray_tracing_functions = self+ .shared+ .extension_fns+ .ray_tracing+ .as_ref()+ .expect("Feature `RAY_TRACING` not enabled");++ let (geometries, primitive_counts) = match *desc.entries {+ crate::AccelerationStructureEntries::Instances(ref instances) => {+ let instance_data = vk::AccelerationStructureGeometryInstancesDataKHR::default();++ let geometry = vk::AccelerationStructureGeometryKHR::default()+ .geometry_type(vk::GeometryTypeKHR::INSTANCES)+ .geometry(vk::AccelerationStructureGeometryDataKHR {+ instances: instance_data,+ });++ (+ smallvec::smallvec![geometry],+ smallvec::smallvec![instances.count],+ )+ }+ crate::AccelerationStructureEntries::Triangles(ref in_geometries) => {+ let mut primitive_counts =+ smallvec::SmallVec::<[u32; CAPACITY]>::with_capacity(in_geometries.len());+ let mut geometries = smallvec::SmallVec::<+ [vk::AccelerationStructureGeometryKHR; CAPACITY],+ >::with_capacity(in_geometries.len());++ for triangles in in_geometries {+ let mut triangle_data =+ vk::AccelerationStructureGeometryTrianglesDataKHR::default()+ .index_type(vk::IndexType::NONE_KHR)+ .vertex_format(conv::map_vertex_format(triangles.vertex_format))+ .max_vertex(triangles.vertex_count)+ .vertex_stride(triangles.vertex_stride)+ // The vulkan spec suggests we could pass a non-zero invalid address here if fetching+ // the real address has significant overhead, but we pass the real one to be on the+ // safe side for now.+ // from https://registry.khronos.org/vulkan/specs/latest/man/html/vkGetAccelerationStructureBuildSizesKHR.html+ // > The srcAccelerationStructure, dstAccelerationStructure, and mode members+ // > of pBuildInfo are ignored. Any VkDeviceOrHostAddressKHR or VkDeviceOrHostAddressConstKHR+ // > members of pBuildInfo are ignored by this command, except that the hostAddress+ // > member of VkAccelerationStructureGeometryTrianglesDataKHR::transformData will+ // > be examined to check if it is NULL.+ .transform_data(vk::DeviceOrHostAddressConstKHR {+ device_address: if desc+ .flags+ .contains(wgt::AccelerationStructureFlags::USE_TRANSFORM)+ {+ unsafe {+ ray_tracing_functions+ .buffer_device_address+ .get_buffer_device_address(+ &vk::BufferDeviceAddressInfo::default().buffer(+ triangles+ .transform+ .as_ref()+ .unwrap()+ .buffer+ .raw,+ ),+ )+ }+ } else {+ 0+ },+ });++ let pritive_count = if let Some(ref indices) = triangles.indices {+ triangle_data =+ triangle_data.index_type(conv::map_index_format(indices.format));+ indices.count / 3+ } else {+ triangles.vertex_count / 3+ };++ let geometry = vk::AccelerationStructureGeometryKHR::default()+ .geometry_type(vk::GeometryTypeKHR::TRIANGLES)+ .geometry(vk::AccelerationStructureGeometryDataKHR {+ triangles: triangle_data,+ })+ .flags(conv::map_acceleration_structure_geometry_flags(+ triangles.flags,+ ));++ geometries.push(geometry);+ primitive_counts.push(pritive_count);+ }+ (geometries, primitive_counts)+ }+ crate::AccelerationStructureEntries::AABBs(ref in_geometries) => {+ let mut primitive_counts =+ smallvec::SmallVec::<[u32; CAPACITY]>::with_capacity(in_geometries.len());+ let mut geometries = smallvec::SmallVec::<+ [vk::AccelerationStructureGeometryKHR; CAPACITY],+ >::with_capacity(in_geometries.len());+ for aabb in in_geometries {+ let aabbs_data = vk::AccelerationStructureGeometryAabbsDataKHR::default()+ .stride(aabb.stride);++ let geometry = vk::AccelerationStructureGeometryKHR::default()+ .geometry_type(vk::GeometryTypeKHR::AABBS)+ .geometry(vk::AccelerationStructureGeometryDataKHR { aabbs: aabbs_data })+ .flags(conv::map_acceleration_structure_geometry_flags(aabb.flags));++ geometries.push(geometry);+ primitive_counts.push(aabb.count);+ }+ (geometries, primitive_counts)+ }+ };++ let ty = match *desc.entries {+ crate::AccelerationStructureEntries::Instances(_) => {+ vk::AccelerationStructureTypeKHR::TOP_LEVEL+ }+ _ => vk::AccelerationStructureTypeKHR::BOTTOM_LEVEL,+ };++ let geometry_info = vk::AccelerationStructureBuildGeometryInfoKHR::default()+ .ty(ty)+ .flags(conv::map_acceleration_structure_flags(desc.flags))+ .geometries(&geometries);++ let mut raw = Default::default();+ unsafe {+ ray_tracing_functions+ .acceleration_structure+ .get_acceleration_structure_build_sizes(+ vk::AccelerationStructureBuildTypeKHR::DEVICE,+ &geometry_info,+ &primitive_counts,+ &mut raw,+ )+ }++ crate::AccelerationStructureBuildSizes {+ acceleration_structure_size: raw.acceleration_structure_size,+ update_scratch_size: raw.update_scratch_size,+ build_scratch_size: raw.build_scratch_size,+ }+ }++ unsafe fn get_acceleration_structure_device_address(+ &self,+ acceleration_structure: &super::AccelerationStructure,+ ) -> wgt::BufferAddress {+ let ray_tracing_functions = self+ .shared+ .extension_fns+ .ray_tracing+ .as_ref()+ .expect("Feature `RAY_TRACING` not enabled");++ unsafe {+ ray_tracing_functions+ .acceleration_structure+ .get_acceleration_structure_device_address(+ &vk::AccelerationStructureDeviceAddressInfoKHR::default()+ .acceleration_structure(acceleration_structure.raw),+ )+ }+ }++ unsafe fn create_acceleration_structure(+ &self,+ desc: &crate::AccelerationStructureDescriptor,+ ) -> Result<super::AccelerationStructure, crate::DeviceError> {+ let ray_tracing_functions = self+ .shared+ .extension_fns+ .ray_tracing+ .as_ref()+ .expect("Feature `RAY_TRACING` not enabled");++ let vk_buffer_info = vk::BufferCreateInfo::default()+ .size(desc.size)+ .usage(+ vk::BufferUsageFlags::ACCELERATION_STRUCTURE_STORAGE_KHR+ | vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS,+ )+ .sharing_mode(vk::SharingMode::EXCLUSIVE);++ unsafe {+ let raw_buffer = self+ .shared+ .raw+ .create_buffer(&vk_buffer_info, None)+ .map_err(super::map_host_device_oom_and_ioca_err)?;++ let requirements = self.shared.raw.get_buffer_memory_requirements(raw_buffer);++ self.error_if_would_oom_on_resource_allocation(+ gpu_allocator::MemoryLocation::GpuOnly,+ &requirements,+ )+ .inspect_err(|_| {+ self.shared.raw.destroy_buffer(raw_buffer, None);+ })?;++ let name = desc+ .label+ .unwrap_or("Unlabeled acceleration structure buffer");++ let allocation = self+ .mem_allocator+ .lock()+ .allocate(&gpu_allocator::vulkan::AllocationCreateDesc {+ name,+ requirements,+ location: gpu_allocator::MemoryLocation::GpuOnly,+ linear: true, // Buffers are always linear+ allocation_scheme: gpu_allocator::vulkan::AllocationScheme::GpuAllocatorManaged,+ })+ .inspect_err(|_| {+ self.shared.raw.destroy_buffer(raw_buffer, None);+ })?;++ self.shared+ .raw+ .bind_buffer_memory(raw_buffer, allocation.memory(), allocation.offset())+ .map_err(super::map_host_device_oom_and_ioca_err)+ .inspect_err(|_| {+ self.shared.raw.destroy_buffer(raw_buffer, None);+ })?;++ if let Some(label) = desc.label {+ self.shared.set_object_name(raw_buffer, label);+ }++ let vk_info = vk::AccelerationStructureCreateInfoKHR::default()+ .buffer(raw_buffer)+ .offset(0)+ .size(desc.size)+ .ty(conv::map_acceleration_structure_format(desc.format));++ let raw_acceleration_structure = ray_tracing_functions+ .acceleration_structure+ .create_acceleration_structure(&vk_info, None)+ .map_err(super::map_host_oom_and_ioca_err)+ .inspect_err(|_| {+ self.shared.raw.destroy_buffer(raw_buffer, None);+ })?;++ if let Some(label) = desc.label {+ self.shared+ .set_object_name(raw_acceleration_structure, label);+ }++ let pool = if desc.allow_compaction {+ let vk_info = vk::QueryPoolCreateInfo::default()+ .query_type(vk::QueryType::ACCELERATION_STRUCTURE_COMPACTED_SIZE_KHR)+ .query_count(1);++ let raw = self+ .shared+ .raw+ .create_query_pool(&vk_info, None)+ .map_err(super::map_host_device_oom_err)+ .inspect_err(|_| {+ ray_tracing_functions+ .acceleration_structure+ .destroy_acceleration_structure(raw_acceleration_structure, None);+ self.shared.raw.destroy_buffer(raw_buffer, None);+ })?;+ Some(raw)+ } else {+ None+ };++ Ok(super::AccelerationStructure {+ raw: raw_acceleration_structure,+ buffer: raw_buffer,+ allocation,+ compacted_size_query: pool,+ })+ }+ }++ unsafe fn destroy_acceleration_structure(+ &self,+ acceleration_structure: super::AccelerationStructure,+ ) {+ let ray_tracing_functions = self+ .shared+ .extension_fns+ .ray_tracing+ .as_ref()+ .expect("Feature `RAY_TRACING` not enabled");++ unsafe {+ ray_tracing_functions+ .acceleration_structure+ .destroy_acceleration_structure(acceleration_structure.raw, None);+ self.shared+ .raw+ .destroy_buffer(acceleration_structure.buffer, None);+ let result = self+ .mem_allocator+ .lock()+ .free(acceleration_structure.allocation);+ if let Err(err) = result {+ log::warn!("Failed to free buffer acceleration structure: {err}");+ }+ if let Some(query) = acceleration_structure.compacted_size_query {+ self.shared.raw.destroy_query_pool(query, None)+ }+ }+ }++ fn get_internal_counters(&self) -> wgt::HalCounters {+ self.counters+ .memory_allocations+ .set(self.shared.memory_allocations_counter.read());++ self.counters.as_ref().clone()+ }++ fn generate_allocator_report(&self) -> Option<wgt::AllocatorReport> {+ let gpu_allocator::AllocatorReport {+ allocations,+ blocks,+ total_allocated_bytes,+ total_capacity_bytes,+ } = self.mem_allocator.lock().generate_report();++ let allocations = allocations+ .into_iter()+ .map(|alloc| wgt::AllocationReport {+ name: alloc.name,+ offset: alloc.offset,+ size: alloc.size,+ })+ .collect();++ let blocks = blocks+ .into_iter()+ .map(|block| wgt::MemoryBlockReport {+ size: block.size,+ allocations: block.allocations.clone(),+ })+ .collect();++ Some(wgt::AllocatorReport {+ allocations,+ blocks,+ total_allocated_bytes,+ total_reserved_bytes: total_capacity_bytes,+ })+ }++ fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8> {+ const MAX_U24: u32 = (1u32 << 24u32) - 1u32;+ let temp = RawTlasInstance {+ transform: instance.transform,+ custom_data_and_mask: (instance.custom_data & MAX_U24)+ | (u32::from(instance.mask) << 24),+ shader_binding_table_record_offset_and_flags: (instance+ .pipeline_intersection_data_offset+ & MAX_U24),+ acceleration_structure_reference: instance.blas_address,+ };+ bytemuck::bytes_of(&temp).to_vec()+ }++ fn check_if_oom(&self) -> Result<(), crate::DeviceError> {+ let Some(threshold) = self+ .shared+ .instance+ .memory_budget_thresholds+ .for_device_loss+ else {+ return Ok(());+ };++ let Some(memory_properties) = self.get_memory_properties() else {+ return Ok(());+ };++ for i in 0..memory_properties.heaps().len() {+ let heap_usage = memory_properties.heap_usage()[i];+ let heap_budget = memory_properties.heap_budget()[i];++ if heap_usage >= heap_budget / 100 * threshold as u64 {+ return Err(crate::DeviceError::OutOfMemory);+ }+ }++ Ok(())+ }+}++impl super::DeviceShared {+ pub(super) fn new_binary_semaphore(+ &self,+ name: &str,+ ) -> Result<vk::Semaphore, crate::DeviceError> {+ unsafe {+ let semaphore = self+ .raw+ .create_semaphore(&vk::SemaphoreCreateInfo::default(), None)+ .map_err(super::map_host_device_oom_err)?;++ self.set_object_name(semaphore, name);++ Ok(semaphore)+ }+ }++ pub(super) fn wait_for_fence(+ &self,+ fence: &super::Fence,+ wait_value: crate::FenceValue,+ timeout_ns: u64,+ ) -> Result<bool, crate::DeviceError> {+ profiling::scope!("Device::wait");+ match *fence {+ super::Fence::TimelineSemaphore(raw) => {+ let semaphores = [raw];+ let values = [wait_value];+ let vk_info = vk::SemaphoreWaitInfo::default()+ .semaphores(&semaphores)+ .values(&values);+ let result = match self.extension_fns.timeline_semaphore {+ Some(super::ExtensionFn::Extension(ref ext)) => unsafe {+ ext.wait_semaphores(&vk_info, timeout_ns)+ },+ Some(super::ExtensionFn::Promoted) => unsafe {+ self.raw.wait_semaphores(&vk_info, timeout_ns)+ },+ None => unreachable!(),+ };+ match result {+ Ok(()) => Ok(true),+ Err(vk::Result::TIMEOUT) => Ok(false),+ Err(other) => Err(super::map_host_device_oom_and_lost_err(other)),+ }+ }+ super::Fence::FencePool(ref pool) => {+ let pool = pool.read();+ let super::FencePool {+ last_completed,+ ref active,+ free: _,+ } = *pool;+ if wait_value <= last_completed {+ Ok(true)+ } else {+ match active.iter().find(|&&(value, _)| value >= wait_value) {+ Some((_, fence)) => {+ // clone to show we are using this fence while the pool is unlocked.+ let fence = fence.clone();+ drop(pool);+ match unsafe {+ self.raw.wait_for_fences(+ core::slice::from_ref(&fence),+ true,+ timeout_ns,+ )+ } {+ Ok(()) => Ok(true),+ Err(vk::Result::TIMEOUT) => Ok(false),+ Err(other) => Err(super::map_host_device_oom_and_lost_err(other)),+ }+ }+ None => {+ crate::hal_usage_error(format!(+ "no signals reached value {wait_value}"+ ));+ }+ }+ }+ }+ }+ }+}++struct ImageWithoutMemory {+ raw: vk::Image,+ requirements: vk::MemoryRequirements,+}diff --git a/vendor/wgpu-hal/src/vulkan/drm.rs b/vendor/wgpu-hal/src/vulkan/drm.rsnew file mode 100644index 00000000..9c03b041--- /dev/null+++ b/vendor/wgpu-hal/src/vulkan/drm.rs@@ -0,0 +1,252 @@+#![cfg(drm)]++use alloc::{string::ToString, vec::Vec};+use core::{mem::MaybeUninit, num::NonZeroU32};+use std::os::fd::{AsFd, BorrowedFd};++use ash::{ext, khr, vk};++use drm::{+ self,+ control::{self, connector, Device as _},+};++struct Card(i32);+impl AsFd for Card {+ fn as_fd(&self) -> BorrowedFd<'_> {+ unsafe { BorrowedFd::borrow_raw(self.0) }+ }+}+impl control::Device for Card {}+impl drm::Device for Card {}++macro_rules! to_u64 {+ ($expr:expr) => {{+ #[allow(trivial_numeric_casts)]+ let expr = $expr as u64;+ assert!(size_of_val(&expr) <= size_of::<u64>());+ expr+ }};+}++impl super::Instance {+ /// Creates a new surface from the given drm fd and plane, deriving the connector and mode.+ ///+ /// # Safety+ ///+ /// - All parameters must point to valid DRM values.+ pub fn create_surface_from_drm_plane(+ &self,+ fd: i32,+ plane: u32,+ ) -> Result<super::Surface, crate::InstanceError> {+ let card = Card(fd);+ let plane_info = card+ .get_plane(+ NonZeroU32::new(plane)+ .ok_or(crate::InstanceError::new("Invalid drm plane".to_string()))?+ .into(),+ )+ .map_err(|e| crate::InstanceError::with_source("drm plane not found".to_string(), e))?;+ let crtc_handle = plane_info.crtc().ok_or(crate::InstanceError::new(+ "No CRTC for drm plane".to_string(),+ ))?;+ let crtc_info = card+ .get_crtc(crtc_handle)+ .map_err(|e| crate::InstanceError::with_source("drm CRTC not found".to_string(), e))?;+ let mode = crtc_info.mode().ok_or(crate::InstanceError::new(+ "No mode for drm CRTC".to_string(),+ ))?;++ let mut connector_and_mode = None;+ let resources = card.resource_handles().map_err(|e| {+ crate::InstanceError::with_source("No drm resource handles found".to_string(), e)+ })?;+ for connector_handle in resources.connectors() {+ let Ok(connector_info) = card.get_connector(*connector_handle, false) else {+ continue;+ };+ if connector_info.state() != connector::State::Connected {+ continue;+ }++ if let Some(encoder_handle) = connector_info.current_encoder() {+ if let Ok(encoder_info) = card.get_encoder(encoder_handle) {+ if encoder_info.crtc() == Some(crtc_handle) {+ connector_and_mode = Some((*connector_handle, mode));+ break;+ }+ }+ }+ }++ let (connector_handle, mode) = connector_and_mode.ok_or(crate::InstanceError::new(+ "Failed to derive drm connector and mode for plane".to_string(),+ ))?;+ let (width, height) = mode.size();+ // Rate in millihertz+ let refresh_rate = (((mode.clock() as f64 * 1000.0)+ / (mode.hsync().2 as f64 * mode.vsync().2 as f64))+ * 1000.0)+ .round() as u32;+ unsafe {+ self.create_surface_from_drm(+ fd,+ plane,+ connector_handle.into(),+ width as u32,+ height as u32,+ refresh_rate,+ )+ }+ }++ /// Creates a new surface from the given drm configuration.+ ///+ /// # Safety+ ///+ /// - All parameters must point to valid DRM values.+ pub unsafe fn create_surface_from_drm(+ &self,+ fd: i32,+ plane: u32,+ connector_id: u32,+ width: u32,+ height: u32,+ refresh_rate: u32,+ ) -> Result<super::Surface, crate::InstanceError> {+ if !self+ .shared+ .extensions+ .contains(&ext::acquire_drm_display::NAME)+ {+ return Err(crate::InstanceError::new(+ "Vulkan driver does not support VK_EXT_acquire_drm_display".to_string(),+ ));+ }++ let drm_stat = {+ let mut stat = MaybeUninit::<libc::stat>::uninit();++ if unsafe { libc::fstat(fd, stat.as_mut_ptr()) } != 0 {+ return Err(crate::InstanceError::new(+ "Unable to fstat drm device".to_string(),+ ));+ }++ unsafe { stat.assume_init() }+ };++ let raw_devices = match unsafe { self.shared.raw.enumerate_physical_devices() } {+ Ok(devices) => devices,+ Err(err) => {+ log::error!("enumerate_adapters: {err}");+ Vec::new()+ }+ };++ let mut physical_device = None;++ for device in raw_devices {+ let properties2 = vk::PhysicalDeviceProperties2KHR::default();++ let mut drm_props = vk::PhysicalDeviceDrmPropertiesEXT::default();+ let mut properties2 = properties2.push_next(&mut drm_props);++ unsafe {+ self.shared+ .raw+ .get_physical_device_properties2(device, &mut properties2)+ };++ /*+ The makedev call is just bit manipulation to combine major and minor device numbers into a Unix device ID.+ It doesn't perform any filesystem operations, only bitshifting.+ See: https://github.com/rust-lang/libc/blob/268e1b3810ac07ed637d9005bc1a54e49218c958/src/unix/linux_like/linux/mod.rs#L6049+ We use the resulting device IDs to check if the Vulkan raw device from enumerate_physical_devices+ matches the DRM device referred to by our file descriptor.+ */++ let primary_devid =+ libc::makedev(drm_props.primary_major as _, drm_props.primary_minor as _);+ let render_devid =+ libc::makedev(drm_props.render_major as _, drm_props.render_minor as _);++ // On most platforms, both `*_devid`s and `st_rdev` are `dev_t`s (which is generally+ // observed to be an unsigned integral type no greater than 64 bits). However, on some+ // platforms, there divergences from this pattern:+ //+ // - `armv7-linux-androideabi`: `dev_t` is `c_ulong`, and `*_devid`s are `dev_t`, but+ // `st_rdev` is `c_ulonglong`. So, we can't just do a `==` comparison.+ // - OpenBSD has `dev_t` on both sides, but is `i32` (N.B., unsigned). Therefore, we+ // can't just use `u64::from`.+ #[allow(clippy::useless_conversion)]+ if [primary_devid, render_devid]+ .map(|devid| to_u64!(devid))+ .contains(&to_u64!(drm_stat.st_rdev))+ {+ physical_device = Some(device)+ }+ }++ let physical_device = physical_device.ok_or(crate::InstanceError::new(+ "Failed to find suitable drm device".to_string(),+ ))?;++ let acquire_drm_display_instance =+ ext::acquire_drm_display::Instance::new(&self.shared.entry, &self.shared.raw);++ let display = unsafe {+ acquire_drm_display_instance+ .get_drm_display(physical_device, fd, connector_id)+ .expect("Failed to get drm display")+ };++ unsafe {+ acquire_drm_display_instance+ .acquire_drm_display(physical_device, fd, display)+ .expect("Failed to acquire drm display")+ }++ let display_instance = khr::display::Instance::new(&self.shared.entry, &self.shared.raw);++ let modes = unsafe {+ display_instance+ .get_display_mode_properties(physical_device, display)+ .expect("Failed to get display modes")+ };++ let mut mode = None;++ for current_mode in modes {+ log::trace!(+ "Comparing mode {}x{}@{} with {width}x{height}@{refresh_rate}",+ current_mode.parameters.visible_region.width,+ current_mode.parameters.visible_region.height,+ current_mode.parameters.refresh_rate+ );+ if current_mode.parameters.refresh_rate == refresh_rate+ && current_mode.parameters.visible_region.width == width+ && current_mode.parameters.visible_region.height == height+ {+ mode = Some(current_mode)+ }+ }++ let mode = mode.ok_or(crate::InstanceError::new(+ "Failed to find suitable display mode".to_string(),+ ))?;++ let create_info = vk::DisplaySurfaceCreateInfoKHR::default()+ .display_mode(mode.display_mode)+ .image_extent(mode.parameters.visible_region)+ .transform(vk::SurfaceTransformFlagsKHR::IDENTITY)+ .alpha_mode(vk::DisplayPlaneAlphaFlagsKHR::OPAQUE)+ .plane_index(plane);++ let surface = unsafe { display_instance.create_display_plane_surface(&create_info, None) }+ .expect("Failed to create DRM surface");++ Ok(self.create_surface_from_vk_surface_khr(surface, None))+ }+}diff --git a/vendor/wgpu-hal/src/vulkan/instance.rs b/vendor/wgpu-hal/src/vulkan/instance.rsnew file mode 100644index 00000000..17758758--- /dev/null+++ b/vendor/wgpu-hal/src/vulkan/instance.rs@@ -0,0 +1,1051 @@+use alloc::{borrow::ToOwned as _, boxed::Box, ffi::CString, string::String, sync::Arc, vec::Vec};+use core::{+ ffi::{c_void, CStr},+ marker::PhantomData,+ slice,+ str::FromStr,+};+use std::thread;++use arrayvec::ArrayVec;+use ash::{ext, khr, vk};+use parking_lot::RwLock;++unsafe extern "system" fn debug_utils_messenger_callback(+ message_severity: vk::DebugUtilsMessageSeverityFlagsEXT,+ message_type: vk::DebugUtilsMessageTypeFlagsEXT,+ callback_data_ptr: *const vk::DebugUtilsMessengerCallbackDataEXT,+ user_data: *mut c_void,+) -> vk::Bool32 {+ use alloc::borrow::Cow;++ if thread::panicking() {+ return vk::FALSE;+ }++ let cd = unsafe { &*callback_data_ptr };+ let user_data = unsafe { &*user_data.cast::<super::DebugUtilsMessengerUserData>() };++ const VUID_VKCMDENDDEBUGUTILSLABELEXT_COMMANDBUFFER_01912: i32 = 0x56146426;+ if cd.message_id_number == VUID_VKCMDENDDEBUGUTILSLABELEXT_COMMANDBUFFER_01912 {+ // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/5671+ // Versions 1.3.240 through 1.3.250 return a spurious error here if+ // the debug range start and end appear in different command buffers.+ if let Some(layer_properties) = user_data.validation_layer_properties.as_ref() {+ if layer_properties.layer_description.as_ref() == c"Khronos Validation Layer"+ && layer_properties.layer_spec_version >= vk::make_api_version(0, 1, 3, 240)+ && layer_properties.layer_spec_version <= vk::make_api_version(0, 1, 3, 250)+ {+ return vk::FALSE;+ }+ }+ }++ // Silence Vulkan Validation error "VUID-VkSwapchainCreateInfoKHR-pNext-07781"+ // This happens when a surface is configured with a size outside the allowed extent.+ // It's a false positive due to the inherent racy-ness of surface resizing.+ const VUID_VKSWAPCHAINCREATEINFOKHR_PNEXT_07781: i32 = 0x4c8929c1;+ if cd.message_id_number == VUID_VKSWAPCHAINCREATEINFOKHR_PNEXT_07781 {+ return vk::FALSE;+ }++ // Silence Vulkan Validation error "VUID-VkRenderPassBeginInfo-framebuffer-04627"+ // if the OBS layer is enabled. This is a bug in the OBS layer. As the OBS layer+ // does not have a version number they increment, there is no way to qualify the+ // suppression of the error to a specific version of the OBS layer.+ //+ // See https://github.com/obsproject/obs-studio/issues/9353+ const VUID_VKRENDERPASSBEGININFO_FRAMEBUFFER_04627: i32 = 0x45125641;+ if cd.message_id_number == VUID_VKRENDERPASSBEGININFO_FRAMEBUFFER_04627+ && user_data.has_obs_layer+ {+ return vk::FALSE;+ }++ // Silence Vulkan Validation error "VUID-vkCmdCopyImageToBuffer-pRegions-00184".+ // While we aren't sure yet, we suspect this is probably a VVL issue.+ // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/9276+ const VUID_VKCMDCOPYIMAGETOBUFFER_PREGIONS_00184: i32 = 0x45ef177c;+ if cd.message_id_number == VUID_VKCMDCOPYIMAGETOBUFFER_PREGIONS_00184 {+ return vk::FALSE;+ }++ // Silence Vulkan Validation error "VUID-StandaloneSpirv-None-10684".+ //+ // This is a bug. To prevent massive noise in the tests, lets suppress it for now.+ // https://github.com/gfx-rs/wgpu/issues/7696+ const VUID_STANDALONESPIRV_NONE_10684: i32 = 0xb210f7c2_u32 as i32;+ if cd.message_id_number == VUID_STANDALONESPIRV_NONE_10684 {+ return vk::FALSE;+ }++ let level = match message_severity {+ // We intentionally suppress info messages down to debug+ // so that users are not innundated with info messages from the runtime.+ vk::DebugUtilsMessageSeverityFlagsEXT::VERBOSE => log::Level::Trace,+ vk::DebugUtilsMessageSeverityFlagsEXT::INFO => log::Level::Debug,+ vk::DebugUtilsMessageSeverityFlagsEXT::WARNING => log::Level::Warn,+ vk::DebugUtilsMessageSeverityFlagsEXT::ERROR => log::Level::Error,+ _ => log::Level::Warn,+ };++ let message_id_name =+ unsafe { cd.message_id_name_as_c_str() }.map_or(Cow::Borrowed(""), CStr::to_string_lossy);+ let message = unsafe { cd.message_as_c_str() }.map_or(Cow::Borrowed(""), CStr::to_string_lossy);++ let _ = std::panic::catch_unwind(|| {+ log::log!(+ level,+ "{:?} [{} (0x{:x})]\n\t{}",+ message_type,+ message_id_name,+ cd.message_id_number,+ message,+ );+ });++ if cd.queue_label_count != 0 {+ let labels =+ unsafe { slice::from_raw_parts(cd.p_queue_labels, cd.queue_label_count as usize) };+ let names = labels+ .iter()+ .flat_map(|dul_obj| unsafe { dul_obj.label_name_as_c_str() }.map(CStr::to_string_lossy))+ .collect::<Vec<_>>();++ let _ = std::panic::catch_unwind(|| {+ log::log!(level, "\tqueues: {}", names.join(", "));+ });+ }++ if cd.cmd_buf_label_count != 0 {+ let labels =+ unsafe { slice::from_raw_parts(cd.p_cmd_buf_labels, cd.cmd_buf_label_count as usize) };+ let names = labels+ .iter()+ .flat_map(|dul_obj| unsafe { dul_obj.label_name_as_c_str() }.map(CStr::to_string_lossy))+ .collect::<Vec<_>>();++ let _ = std::panic::catch_unwind(|| {+ log::log!(level, "\tcommand buffers: {}", names.join(", "));+ });+ }++ if cd.object_count != 0 {+ let labels = unsafe { slice::from_raw_parts(cd.p_objects, cd.object_count as usize) };+ //TODO: use color fields of `vk::DebugUtilsLabelExt`?+ let names = labels+ .iter()+ .map(|obj_info| {+ let name = unsafe { obj_info.object_name_as_c_str() }+ .map_or(Cow::Borrowed("?"), CStr::to_string_lossy);++ format!(+ "(type: {:?}, hndl: 0x{:x}, name: {})",+ obj_info.object_type, obj_info.object_handle, name+ )+ })+ .collect::<Vec<_>>();+ let _ = std::panic::catch_unwind(|| {+ log::log!(level, "\tobjects: {}", names.join(", "));+ });+ }++ #[cfg(feature = "validation_canary")]+ if cfg!(debug_assertions) && level == log::Level::Error {+ use alloc::string::ToString as _;++ // Set canary and continue+ crate::VALIDATION_CANARY.add(message.to_string());+ }++ vk::FALSE+}++impl super::DebugUtilsCreateInfo {+ fn to_vk_create_info(&self) -> vk::DebugUtilsMessengerCreateInfoEXT<'_> {+ let user_data_ptr: *const super::DebugUtilsMessengerUserData = &*self.callback_data;+ vk::DebugUtilsMessengerCreateInfoEXT::default()+ .message_severity(self.severity)+ .message_type(self.message_type)+ .user_data(user_data_ptr as *mut _)+ .pfn_user_callback(Some(debug_utils_messenger_callback))+ }+}++impl super::InstanceShared {+ pub fn entry(&self) -> &ash::Entry {+ &self.entry+ }++ pub fn raw_instance(&self) -> &ash::Instance {+ &self.raw+ }++ pub fn instance_api_version(&self) -> u32 {+ self.instance_api_version+ }++ pub fn extensions(&self) -> &[&'static CStr] {+ &self.extensions[..]+ }+}++impl super::Instance {+ pub fn shared_instance(&self) -> &super::InstanceShared {+ &self.shared+ }++ fn enumerate_instance_extension_properties(+ entry: &ash::Entry,+ layer_name: Option<&CStr>,+ ) -> Result<Vec<vk::ExtensionProperties>, crate::InstanceError> {+ let instance_extensions = {+ profiling::scope!("vkEnumerateInstanceExtensionProperties");+ unsafe { entry.enumerate_instance_extension_properties(layer_name) }+ };+ instance_extensions.map_err(|e| {+ crate::InstanceError::with_source(+ String::from("enumerate_instance_extension_properties() failed"),+ e,+ )+ })+ }++ /// Return the instance extension names wgpu would like to enable.+ ///+ /// Return a vector of the names of instance extensions actually available+ /// on `entry` that wgpu would like to enable.+ ///+ /// The `instance_api_version` argument should be the instance's Vulkan API+ /// version, as obtained from `vkEnumerateInstanceVersion`. This is the same+ /// space of values as the `VK_API_VERSION` constants.+ ///+ /// Note that wgpu can function without many of these extensions (for+ /// example, `VK_KHR_wayland_surface` is certainly not going to be available+ /// everywhere), but if one of these extensions is available at all, wgpu+ /// assumes that it has been enabled.+ pub fn desired_extensions(+ entry: &ash::Entry,+ _instance_api_version: u32,+ flags: wgt::InstanceFlags,+ ) -> Result<Vec<&'static CStr>, crate::InstanceError> {+ let instance_extensions = Self::enumerate_instance_extension_properties(entry, None)?;++ // Check our extensions against the available extensions+ let mut extensions: Vec<&'static CStr> = Vec::new();++ // VK_KHR_surface+ extensions.push(khr::surface::NAME);++ // Platform-specific WSI extensions+ if cfg!(all(+ unix,+ not(target_os = "android"),+ not(target_os = "macos")+ )) {+ // VK_KHR_xlib_surface+ extensions.push(khr::xlib_surface::NAME);+ // VK_KHR_xcb_surface+ extensions.push(khr::xcb_surface::NAME);+ // VK_KHR_wayland_surface+ extensions.push(khr::wayland_surface::NAME);+ }+ if cfg!(target_os = "android") {+ // VK_KHR_android_surface+ extensions.push(khr::android_surface::NAME);+ }+ if cfg!(target_os = "windows") {+ // VK_KHR_win32_surface+ extensions.push(khr::win32_surface::NAME);+ }+ if cfg!(target_os = "macos") {+ // VK_EXT_metal_surface+ extensions.push(ext::metal_surface::NAME);+ extensions.push(khr::portability_enumeration::NAME);+ }+ if cfg!(drm) {+ // VK_EXT_acquire_drm_display -> VK_EXT_direct_mode_display -> VK_KHR_display+ extensions.push(ext::acquire_drm_display::NAME);+ extensions.push(ext::direct_mode_display::NAME);+ extensions.push(khr::display::NAME);+ extensions.push(khr::get_physical_device_properties2::NAME);+ extensions.push(khr::get_display_properties2::NAME);+ }++ if flags.contains(wgt::InstanceFlags::DEBUG) {+ // VK_EXT_debug_utils+ extensions.push(ext::debug_utils::NAME);+ }++ // VK_EXT_swapchain_colorspace+ // Provides wide color gamut+ extensions.push(ext::swapchain_colorspace::NAME);++ // VK_KHR_get_physical_device_properties2+ // Even though the extension was promoted to Vulkan 1.1, we still require the extension+ // so that we don't have to conditionally use the functions provided by the 1.1 instance+ extensions.push(khr::get_physical_device_properties2::NAME);++ // Only keep available extensions.+ extensions.retain(|&ext| {+ if instance_extensions+ .iter()+ .any(|inst_ext| inst_ext.extension_name_as_c_str() == Ok(ext))+ {+ true+ } else {+ log::debug!("Unable to find extension: {}", ext.to_string_lossy());+ false+ }+ });+ Ok(extensions)+ }++ /// # Safety+ ///+ /// - `raw_instance` must be created from `entry`+ /// - `raw_instance` must be created respecting `instance_api_version`, `extensions` and `flags`+ /// - `extensions` must be a superset of `desired_extensions()` and must be created from the+ /// same entry, `instance_api_version`` and flags.+ /// - `android_sdk_version` is ignored and can be `0` for all platforms besides Android+ /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of `raw_instance`. If+ /// `drop_callback` is [`Some`], `raw_instance` must be valid until the callback is called.+ ///+ /// If `debug_utils_user_data` is `Some`, then the validation layer is+ /// available, so create a [`vk::DebugUtilsMessengerEXT`].+ #[allow(clippy::too_many_arguments)]+ pub unsafe fn from_raw(+ entry: ash::Entry,+ raw_instance: ash::Instance,+ instance_api_version: u32,+ android_sdk_version: u32,+ debug_utils_create_info: Option<super::DebugUtilsCreateInfo>,+ extensions: Vec<&'static CStr>,+ flags: wgt::InstanceFlags,+ memory_budget_thresholds: wgt::MemoryBudgetThresholds,+ has_nv_optimus: bool,+ drop_callback: Option<crate::DropCallback>,+ ) -> Result<Self, crate::InstanceError> {+ log::debug!("Instance version: 0x{instance_api_version:x}");++ let debug_utils = if let Some(debug_utils_create_info) = debug_utils_create_info {+ if extensions.contains(&ext::debug_utils::NAME) {+ log::debug!("Enabling debug utils");++ let extension = ext::debug_utils::Instance::new(&entry, &raw_instance);+ let vk_info = debug_utils_create_info.to_vk_create_info();+ let messenger =+ unsafe { extension.create_debug_utils_messenger(&vk_info, None) }.unwrap();++ Some(super::DebugUtils {+ extension,+ messenger,+ callback_data: debug_utils_create_info.callback_data,+ })+ } else {+ log::debug!("Debug utils not enabled: extension not listed");+ None+ }+ } else {+ log::debug!(+ "Debug utils not enabled: \+ debug_utils_user_data not passed to Instance::from_raw"+ );+ None+ };++ let get_physical_device_properties =+ if extensions.contains(&khr::get_physical_device_properties2::NAME) {+ log::debug!("Enabling device properties2");+ Some(khr::get_physical_device_properties2::Instance::new(+ &entry,+ &raw_instance,+ ))+ } else {+ None+ };++ let drop_guard = crate::DropGuard::from_option(drop_callback);++ Ok(Self {+ shared: Arc::new(super::InstanceShared {+ raw: raw_instance,+ extensions,+ drop_guard,+ flags,+ memory_budget_thresholds,+ debug_utils,+ get_physical_device_properties,+ entry,+ has_nv_optimus,+ instance_api_version,+ android_sdk_version,+ }),+ })+ }++ fn create_surface_from_xlib(+ &self,+ dpy: *mut vk::Display,+ window: vk::Window,+ ) -> Result<super::Surface, crate::InstanceError> {+ if !self.shared.extensions.contains(&khr::xlib_surface::NAME) {+ return Err(crate::InstanceError::new(String::from(+ "Vulkan driver does not support VK_KHR_xlib_surface",+ )));+ }++ let surface = {+ let xlib_loader =+ khr::xlib_surface::Instance::new(&self.shared.entry, &self.shared.raw);+ let info = vk::XlibSurfaceCreateInfoKHR::default()+ .flags(vk::XlibSurfaceCreateFlagsKHR::empty())+ .window(window)+ .dpy(dpy);++ unsafe { xlib_loader.create_xlib_surface(&info, None) }+ .expect("XlibSurface::create_xlib_surface() failed")+ };++ Ok(self.create_surface_from_vk_surface_khr(surface, None))+ }++ fn create_surface_from_xcb(+ &self,+ connection: *mut vk::xcb_connection_t,+ window: vk::xcb_window_t,+ ) -> Result<super::Surface, crate::InstanceError> {+ if !self.shared.extensions.contains(&khr::xcb_surface::NAME) {+ return Err(crate::InstanceError::new(String::from(+ "Vulkan driver does not support VK_KHR_xcb_surface",+ )));+ }++ let surface = {+ let xcb_loader = khr::xcb_surface::Instance::new(&self.shared.entry, &self.shared.raw);+ let info = vk::XcbSurfaceCreateInfoKHR::default()+ .flags(vk::XcbSurfaceCreateFlagsKHR::empty())+ .window(window)+ .connection(connection);++ unsafe { xcb_loader.create_xcb_surface(&info, None) }+ .expect("XcbSurface::create_xcb_surface() failed")+ };++ Ok(self.create_surface_from_vk_surface_khr(surface, None))+ }++ fn create_surface_from_wayland(+ &self,+ display: *mut vk::wl_display,+ surface: *mut vk::wl_surface,+ ) -> Result<super::Surface, crate::InstanceError> {+ if !self.shared.extensions.contains(&khr::wayland_surface::NAME) {+ return Err(crate::InstanceError::new(String::from(+ "Vulkan driver does not support VK_KHR_wayland_surface",+ )));+ }++ let surface = {+ let w_loader =+ khr::wayland_surface::Instance::new(&self.shared.entry, &self.shared.raw);+ let info = vk::WaylandSurfaceCreateInfoKHR::default()+ .flags(vk::WaylandSurfaceCreateFlagsKHR::empty())+ .display(display)+ .surface(surface);++ unsafe { w_loader.create_wayland_surface(&info, None) }.expect("WaylandSurface failed")+ };++ Ok(self.create_surface_from_vk_surface_khr(surface, None))+ }++ fn create_surface_android(+ &self,+ window: *mut vk::ANativeWindow,+ ) -> Result<super::Surface, crate::InstanceError> {+ if !self.shared.extensions.contains(&khr::android_surface::NAME) {+ return Err(crate::InstanceError::new(String::from(+ "Vulkan driver does not support VK_KHR_android_surface",+ )));+ }++ let surface = {+ let a_loader =+ khr::android_surface::Instance::new(&self.shared.entry, &self.shared.raw);+ let info = vk::AndroidSurfaceCreateInfoKHR::default()+ .flags(vk::AndroidSurfaceCreateFlagsKHR::empty())+ .window(window);++ unsafe { a_loader.create_android_surface(&info, None) }.expect("AndroidSurface failed")+ };++ Ok(self.create_surface_from_vk_surface_khr(surface, None))+ }++ fn create_surface_from_hwnd(+ &self,+ hinstance: vk::HINSTANCE,+ hwnd: vk::HWND,+ ) -> Result<super::Surface, crate::InstanceError> {+ if !self.shared.extensions.contains(&khr::win32_surface::NAME) {+ return Err(crate::InstanceError::new(String::from(+ "Vulkan driver does not support VK_KHR_win32_surface",+ )));+ }++ let surface = {+ let info = vk::Win32SurfaceCreateInfoKHR::default()+ .flags(vk::Win32SurfaceCreateFlagsKHR::empty())+ .hinstance(hinstance)+ .hwnd(hwnd);+ let win32_loader =+ khr::win32_surface::Instance::new(&self.shared.entry, &self.shared.raw);+ unsafe {+ win32_loader+ .create_win32_surface(&info, None)+ .expect("Unable to create Win32 surface")+ }+ };++ // Wrap ash's `isize` `HWND` in `WindowHandle`; on Windows the+ // `NativeSurface` builds its DXGI HDR source from it.+ #[cfg(windows)]+ let window_handle = Some(crate::vulkan::swapchain::WindowHandle(+ windows::Win32::Foundation::HWND(hwnd as *mut c_void),+ ));+ #[cfg(not(windows))]+ let window_handle: Option<crate::vulkan::swapchain::WindowHandle> = None;+ Ok(self.create_surface_from_vk_surface_khr(surface, window_handle))+ }++ #[cfg(target_vendor = "apple")]+ fn create_surface_from_layer(+ &self,+ layer: raw_window_metal::Layer,+ ) -> Result<super::Surface, crate::InstanceError> {+ if !self.shared.extensions.contains(&ext::metal_surface::NAME) {+ return Err(crate::InstanceError::new(String::from(+ "Vulkan driver does not support VK_EXT_metal_surface",+ )));+ }++ // NOTE: The layer is retained by Vulkan's `vkCreateMetalSurfaceEXT`,+ // so no need to retain it beyond the scope of this function.+ let surface = {+ let metal_loader =+ ext::metal_surface::Instance::new(&self.shared.entry, &self.shared.raw);+ let vk_info = vk::MetalSurfaceCreateInfoEXT::default()+ .flags(vk::MetalSurfaceCreateFlagsEXT::empty())+ .layer(layer.as_ptr().as_ptr());++ unsafe { metal_loader.create_metal_surface(&vk_info, None).unwrap() }+ };++ Ok(self.create_surface_from_vk_surface_khr(surface, None))+ }++ pub(super) fn create_surface_from_vk_surface_khr(+ &self,+ surface: vk::SurfaceKHR,+ hwnd: Option<crate::vulkan::swapchain::WindowHandle>,+ ) -> super::Surface {+ let native_surface =+ crate::vulkan::swapchain::NativeSurface::from_vk_surface_khr(self, surface, hwnd);++ super::Surface {+ swapchain: RwLock::new(None),+ inner: Box::new(native_surface),+ }+ }++ /// `Instance::init` but with a callback.+ /// If you want to add extensions, add the to the `Vec<'static CStr>` not the create info, otherwise+ /// it will be overwritten+ ///+ /// # Safety:+ /// Same as `init` but additionally+ /// - Callback must not remove features.+ /// - Callback must not change anything to what the instance does not support.+ pub unsafe fn init_with_callback(+ desc: &crate::InstanceDescriptor<'_>,+ callback: Option<Box<super::CreateInstanceCallback>>,+ ) -> Result<Self, crate::InstanceError> {+ profiling::scope!("Init Vulkan Backend");++ let entry = unsafe {+ profiling::scope!("Load vk library");+ // ohos support is already fixed on ash main, but it's unclear when+ // a new release can happen.+ #[cfg(target_env = "ohos")]+ let loaded = ash::Entry::load_from("libvulkan.so");+ #[cfg(not(target_env = "ohos"))]+ let loaded = ash::Entry::load();+ loaded+ }+ .map_err(|err| {+ crate::InstanceError::with_source(String::from("missing Vulkan entry points"), err)+ })?;+ let version = {+ profiling::scope!("vkEnumerateInstanceVersion");+ unsafe { entry.try_enumerate_instance_version() }+ };+ let instance_api_version = match version {+ // Vulkan 1.1++ Ok(Some(version)) => version,+ Ok(None) => vk::API_VERSION_1_0,+ Err(err) => {+ return Err(crate::InstanceError::with_source(+ String::from("try_enumerate_instance_version() failed"),+ err,+ ));+ }+ };++ let app_name = CString::new(desc.name).unwrap();+ let app_info = vk::ApplicationInfo::default()+ .application_name(app_name.as_c_str())+ .application_version(1)+ .engine_name(c"wgpu-hal")+ .engine_version(2)+ .api_version(+ // Vulkan 1.0 doesn't like anything but 1.0 passed in here...+ if instance_api_version < vk::API_VERSION_1_1 {+ vk::API_VERSION_1_0+ } else {+ // This is the max Vulkan API version supported by `wgpu-hal`.+ //+ // If we want to increment this, there are some things that must be done first:+ // - Audit the behavioral differences between the previous and new API versions.+ // - Audit all extensions used by this backend:+ // - If any were promoted in the new API version and the behavior has changed, we must handle the new behavior in addition to the old behavior.+ // - If any were obsoleted in the new API version, we must implement a fallback for the new API version+ // - If any are non-KHR-vendored, we must ensure the new behavior is still correct (since backwards-compatibility is not guaranteed).+ vk::API_VERSION_1_3+ },+ );++ let mut extensions = Self::desired_extensions(&entry, instance_api_version, desc.flags)?;+ let mut create_info = vk::InstanceCreateInfo::default();++ if let Some(callback) = callback {+ callback(super::CreateInstanceCallbackArgs {+ extensions: &mut extensions,+ create_info: &mut create_info,+ entry: &entry,+ _phantom: PhantomData,+ });+ }++ let instance_layers = {+ profiling::scope!("vkEnumerateInstanceLayerProperties");+ unsafe { entry.enumerate_instance_layer_properties() }+ };+ let instance_layers = instance_layers.map_err(|e| {+ log::debug!("enumerate_instance_layer_properties: {e:?}");+ crate::InstanceError::with_source(+ String::from("enumerate_instance_layer_properties() failed"),+ e,+ )+ })?;++ fn find_layer<'layers>(+ instance_layers: &'layers [vk::LayerProperties],+ name: &CStr,+ ) -> Option<&'layers vk::LayerProperties> {+ instance_layers+ .iter()+ .find(|inst_layer| inst_layer.layer_name_as_c_str() == Ok(name))+ }++ let validation_layer_name = c"VK_LAYER_KHRONOS_validation";+ let validation_layer_properties = find_layer(&instance_layers, validation_layer_name);++ // Determine if VK_EXT_validation_features is available, so we can enable+ // GPU assisted validation and synchronization validation.+ let validation_features_are_enabled = if validation_layer_properties.is_some() {+ // Get the all the instance extension properties.+ let exts =+ Self::enumerate_instance_extension_properties(&entry, Some(validation_layer_name))?;+ // Convert all the names of the extensions into an iterator of CStrs.+ let mut ext_names = exts+ .iter()+ .filter_map(|ext| ext.extension_name_as_c_str().ok());+ // Find the validation features extension.+ ext_names.any(|ext_name| ext_name == ext::validation_features::NAME)+ } else {+ false+ };++ let should_enable_gpu_based_validation = desc+ .flags+ .intersects(wgt::InstanceFlags::GPU_BASED_VALIDATION)+ && validation_features_are_enabled;++ let has_nv_optimus = find_layer(&instance_layers, c"VK_LAYER_NV_optimus").is_some();++ let has_obs_layer = find_layer(&instance_layers, c"VK_LAYER_OBS_HOOK").is_some();++ let mut layers: Vec<&'static CStr> = Vec::new();++ let has_debug_extension = extensions.contains(&ext::debug_utils::NAME);+ let mut debug_user_data = has_debug_extension.then(|| {+ // Put the callback data on the heap, to ensure it will never be+ // moved.+ Box::new(super::DebugUtilsMessengerUserData {+ validation_layer_properties: None,+ has_obs_layer,+ })+ });++ // Request validation layer if asked.+ if desc.flags.intersects(wgt::InstanceFlags::VALIDATION)+ || should_enable_gpu_based_validation+ {+ if let Some(layer_properties) = validation_layer_properties {+ layers.push(validation_layer_name);++ if let Some(debug_user_data) = debug_user_data.as_mut() {+ debug_user_data.validation_layer_properties =+ Some(super::ValidationLayerProperties {+ layer_description: layer_properties+ .description_as_c_str()+ .unwrap()+ .to_owned(),+ layer_spec_version: layer_properties.spec_version,+ });+ }+ } else {+ log::debug!(+ "InstanceFlags::VALIDATION requested, but unable to find layer: {}",+ validation_layer_name.to_string_lossy()+ );+ }+ }+ let mut debug_utils = if let Some(callback_data) = debug_user_data {+ // having ERROR unconditionally because Vk doesn't like empty flags+ let mut severity = vk::DebugUtilsMessageSeverityFlagsEXT::ERROR;+ if log::max_level() >= log::LevelFilter::Debug {+ severity |= vk::DebugUtilsMessageSeverityFlagsEXT::VERBOSE;+ }+ if log::max_level() >= log::LevelFilter::Info {+ severity |= vk::DebugUtilsMessageSeverityFlagsEXT::INFO;+ }+ if log::max_level() >= log::LevelFilter::Warn {+ severity |= vk::DebugUtilsMessageSeverityFlagsEXT::WARNING;+ }++ let message_type = vk::DebugUtilsMessageTypeFlagsEXT::GENERAL+ | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION+ | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE;++ let create_info = super::DebugUtilsCreateInfo {+ severity,+ message_type,+ callback_data,+ };++ Some(create_info)+ } else {+ None+ };++ #[cfg(target_os = "android")]+ let android_sdk_version = {+ let properties = android_system_properties::AndroidSystemProperties::new();+ // See: https://developer.android.com/reference/android/os/Build.VERSION_CODES+ if let Some(val) = properties.get("ro.build.version.sdk") {+ match val.parse::<u32>() {+ Ok(sdk_ver) => sdk_ver,+ Err(err) => {+ log::error!(+ concat!(+ "Couldn't parse Android's ",+ "ro.build.version.sdk system property ({}): {}",+ ),+ val,+ err,+ );+ 0+ }+ }+ } else {+ log::error!("Couldn't read Android's ro.build.version.sdk system property");+ 0+ }+ };+ #[cfg(not(target_os = "android"))]+ let android_sdk_version = 0;++ let mut flags = vk::InstanceCreateFlags::empty();++ // Avoid VUID-VkInstanceCreateInfo-flags-06559: Only ask the instance to+ // enumerate incomplete Vulkan implementations (which we need on Mac) if+ // we managed to find the extension that provides the flag.+ if extensions.contains(&khr::portability_enumeration::NAME) {+ flags |= vk::InstanceCreateFlags::ENUMERATE_PORTABILITY_KHR;+ }+ let vk_instance = {+ let str_pointers = layers+ .iter()+ .chain(extensions.iter())+ .map(|&s: &&'static _| {+ // Safe because `layers` and `extensions` entries have static lifetime.+ s.as_ptr()+ })+ .collect::<Vec<_>>();++ create_info = create_info+ .flags(flags)+ .application_info(&app_info)+ .enabled_layer_names(&str_pointers[..layers.len()])+ .enabled_extension_names(&str_pointers[layers.len()..]);++ let mut debug_utils_create_info = debug_utils+ .as_mut()+ .map(|create_info| create_info.to_vk_create_info());+ if let Some(debug_utils_create_info) = debug_utils_create_info.as_mut() {+ create_info = create_info.push_next(debug_utils_create_info);+ }++ // Enable explicit validation features if available+ let mut validation_features;+ let mut validation_feature_list: ArrayVec<_, 3>;+ if validation_features_are_enabled {+ validation_feature_list = ArrayVec::new();++ // Always enable synchronization validation+ validation_feature_list+ .push(vk::ValidationFeatureEnableEXT::SYNCHRONIZATION_VALIDATION);++ // Only enable GPU assisted validation if requested.+ if should_enable_gpu_based_validation {+ validation_feature_list.push(vk::ValidationFeatureEnableEXT::GPU_ASSISTED);+ validation_feature_list+ .push(vk::ValidationFeatureEnableEXT::GPU_ASSISTED_RESERVE_BINDING_SLOT);+ }++ validation_features = vk::ValidationFeaturesEXT::default()+ .enabled_validation_features(&validation_feature_list);+ create_info = create_info.push_next(&mut validation_features);+ }++ unsafe {+ profiling::scope!("vkCreateInstance");+ entry.create_instance(&create_info, None)+ }+ .map_err(|e| {+ crate::InstanceError::with_source(+ String::from("Entry::create_instance() failed"),+ e,+ )+ })?+ };++ unsafe {+ Self::from_raw(+ entry,+ vk_instance,+ instance_api_version,+ android_sdk_version,+ debug_utils,+ extensions,+ desc.flags,+ desc.memory_budget_thresholds,+ has_nv_optimus,+ None,+ )+ }+ }+}++impl Drop for super::InstanceShared {+ fn drop(&mut self) {+ unsafe {+ // Keep du alive since destroy_instance may also log+ let _du = self.debug_utils.take().inspect(|du| {+ du.extension+ .destroy_debug_utils_messenger(du.messenger, None);+ });+ if self.drop_guard.is_none() {+ self.raw.destroy_instance(None);+ }+ }+ }+}++impl crate::Instance for super::Instance {+ type A = super::Api;++ unsafe fn init(desc: &crate::InstanceDescriptor<'_>) -> Result<Self, crate::InstanceError> {+ unsafe { Self::init_with_callback(desc, None) }+ }++ unsafe fn create_surface(+ &self,+ display_handle: raw_window_handle::RawDisplayHandle,+ window_handle: raw_window_handle::RawWindowHandle,+ ) -> Result<super::Surface, crate::InstanceError> {+ use raw_window_handle::{RawDisplayHandle as Rdh, RawWindowHandle as Rwh};++ // TODO: Replace with ash-window, which also lazy-loads the extension based on handle type++ match (window_handle, display_handle) {+ (Rwh::Wayland(handle), Rdh::Wayland(display)) => {+ self.create_surface_from_wayland(display.display.as_ptr(), handle.surface.as_ptr())+ }+ (Rwh::Xlib(handle), Rdh::Xlib(display)) => {+ let display = display.display.expect("Display pointer is not set.");+ self.create_surface_from_xlib(display.as_ptr(), handle.window)+ }+ (Rwh::Xcb(handle), Rdh::Xcb(display)) => {+ let connection = display.connection.expect("Pointer to X-Server is not set.");+ self.create_surface_from_xcb(connection.as_ptr(), handle.window.get())+ }+ #[cfg(drm)]+ (Rwh::Drm(handle), Rdh::Drm(display)) => {+ self.create_surface_from_drm_plane(display.fd, handle.plane)+ }+ (Rwh::AndroidNdk(handle), _) => {+ self.create_surface_android(handle.a_native_window.as_ptr())+ }+ (Rwh::Win32(handle), _) => {+ let hinstance = handle.hinstance.ok_or_else(|| {+ crate::InstanceError::new(String::from(+ "Vulkan requires raw-window-handle's Win32::hinstance to be set",+ ))+ })?;+ self.create_surface_from_hwnd(hinstance.get(), handle.hwnd.get())+ }+ #[cfg(target_vendor = "apple")]+ (Rwh::AppKit(handle), _)+ if self.shared.extensions.contains(&ext::metal_surface::NAME) =>+ {+ let layer = unsafe { raw_window_metal::Layer::from_ns_view(handle.ns_view) };+ self.create_surface_from_layer(layer)+ }+ #[cfg(target_vendor = "apple")]+ (Rwh::UiKit(handle), _)+ if self.shared.extensions.contains(&ext::metal_surface::NAME) =>+ {+ let layer = unsafe { raw_window_metal::Layer::from_ui_view(handle.ui_view) };+ self.create_surface_from_layer(layer)+ }+ (_, _) => Err(crate::InstanceError::new(format!(+ "window handle {window_handle:?} is not a Vulkan-compatible handle"+ ))),+ }+ }++ unsafe fn enumerate_adapters(+ &self,+ _surface_hint: Option<&super::Surface>,+ ) -> Vec<crate::ExposedAdapter<super::Api>> {+ use crate::auxil::db;++ let raw_devices = match unsafe { self.shared.raw.enumerate_physical_devices() } {+ Ok(devices) => devices,+ Err(err) => {+ log::error!("enumerate_adapters: {err}");+ Vec::new()+ }+ };++ let mut exposed_adapters = raw_devices+ .into_iter()+ .flat_map(|device| self.expose_adapter(device))+ .collect::<Vec<_>>();++ // Detect if it's an Intel + NVidia configuration with Optimus+ let has_nvidia_dgpu = exposed_adapters.iter().any(|exposed| {+ exposed.info.device_type == wgt::DeviceType::DiscreteGpu+ && exposed.info.vendor == db::nvidia::VENDOR+ });+ if cfg!(target_os = "linux") && has_nvidia_dgpu && self.shared.has_nv_optimus {+ for exposed in exposed_adapters.iter_mut() {+ if exposed.info.device_type == wgt::DeviceType::IntegratedGpu+ && exposed.info.vendor == db::intel::VENDOR+ {+ // Check if mesa driver and version less than 21.2+ if let Some(version) = exposed.info.driver_info.split_once("Mesa ").map(|s| {+ let mut components = s.1.split('.');+ let major = components.next().and_then(|s| u8::from_str(s).ok());+ let minor = components.next().and_then(|s| u8::from_str(s).ok());+ if let (Some(major), Some(minor)) = (major, minor) {+ (major, minor)+ } else {+ (0, 0)+ }+ }) {+ if version < (21, 2) {+ // See https://gitlab.freedesktop.org/mesa/mesa/-/issues/4688+ log::debug!(+ concat!(+ "Disabling presentation on '{}' (id {:?}) ",+ "due to NV Optimus and Intel Mesa < v21.2"+ ),+ exposed.info.name,+ exposed.adapter.raw+ );+ exposed.adapter.private_caps.can_present = false;+ }+ }+ }+ }+ }++ exposed_adapters+ }+}++impl crate::Surface for super::Surface {+ type A = super::Api;++ unsafe fn configure(+ &self,+ device: &super::Device,+ config: &crate::SurfaceConfiguration,+ ) -> Result<(), crate::SurfaceError> {+ // SAFETY: `configure`'s contract guarantees there are no resources derived from the swapchain in use.+ let mut swap_chain = self.swapchain.write();++ let mut old = swap_chain.take();+ if let Some(ref mut old) = old {+ unsafe { old.release_resources(device) };+ }++ let swapchain = unsafe { self.inner.create_swapchain(device, config, old)? };+ *swap_chain = Some(swapchain);++ Ok(())+ }++ unsafe fn unconfigure(&self, device: &super::Device) {+ if let Some(mut sc) = self.swapchain.write().take() {+ // SAFETY: `unconfigure`'s contract guarantees there are no resources derived from the swapchain in use.+ unsafe { sc.release_resources(device) };+ }+ }++ unsafe fn acquire_texture(+ &self,+ timeout: Option<core::time::Duration>,+ fence: &super::Fence,+ ) -> Result<crate::AcquiredSurfaceTexture<super::Api>, crate::SurfaceError> {+ let mut swapchain = self.swapchain.write();+ let swapchain = swapchain.as_mut().unwrap();++ unsafe { swapchain.acquire(timeout, fence) }+ }++ unsafe fn discard_texture(&self, texture: super::SurfaceTexture) {+ unsafe {+ self.swapchain+ .write()+ .as_mut()+ .unwrap()+ .discard_texture(texture)+ .unwrap()+ };+ }+}diff --git a/vendor/wgpu-hal/src/vulkan/mod.rs b/vendor/wgpu-hal/src/vulkan/mod.rsnew file mode 100644index 00000000..51e7414b--- /dev/null+++ b/vendor/wgpu-hal/src/vulkan/mod.rs@@ -0,0 +1,1763 @@+/*!+# Vulkan API internals.++## Stack memory++Ash expects slices, which we don't generally have available.+We cope with this requirement by the combination of the following ways:+ - temporarily allocating `Vec` on heap, where overhead is permitted+ - growing temporary local storage++## Framebuffers and Render passes++Render passes are cached on the device and kept forever.++Framebuffers are also cached on the device, but they are removed when+any of the image views (they have) gets removed.+If Vulkan supports image-less framebuffers,+then the actual views are excluded from the framebuffer key.++## Fences++If timeline semaphores are available, they are used 1:1 with wgpu-hal fences.+Otherwise, we manage a pool of `VkFence` objects behind each `hal::Fence`.++!*/++mod adapter;+mod command;+pub mod conv;+mod descriptor;+mod device;+mod drm;+mod instance;+mod sampler;+mod semaphore_list;+mod swapchain;++pub use adapter::PhysicalDeviceFeatures;++use alloc::{boxed::Box, ffi::CString, sync::Arc, vec::Vec};+use core::{borrow::Borrow, ffi::CStr, fmt, marker::PhantomData, mem, num::NonZeroU32};++use arrayvec::ArrayVec;+use ash::{ext, khr, vk};+use bytemuck::{Pod, Zeroable};+use hashbrown::HashSet;+use parking_lot::{Mutex, RwLock};++use naga::FastHashMap;+use wgt::InternalCounter;++use semaphore_list::SemaphoreList;++use crate::vulkan::semaphore_list::{SemaphoreListMode, SemaphoreType};++const MAX_TOTAL_ATTACHMENTS: usize = crate::MAX_COLOR_ATTACHMENTS * 2 + 1;++#[derive(Clone, Debug)]+pub struct Api;++impl crate::Api for Api {+ const VARIANT: wgt::Backend = wgt::Backend::Vulkan;++ type Instance = Instance;+ type Surface = Surface;+ type Adapter = Adapter;+ type Device = Device;++ type Queue = Queue;+ type CommandEncoder = CommandEncoder;+ type CommandBuffer = CommandBuffer;++ type Buffer = Buffer;+ type Texture = Texture;+ type SurfaceTexture = SurfaceTexture;+ type TextureView = TextureView;+ type Sampler = Sampler;+ type QuerySet = QuerySet;+ type Fence = Fence;+ type AccelerationStructure = AccelerationStructure;+ type PipelineCache = PipelineCache;++ type BindGroupLayout = BindGroupLayout;+ type BindGroup = BindGroup;+ type PipelineLayout = PipelineLayout;+ type ShaderModule = ShaderModule;+ type RenderPipeline = RenderPipeline;+ type ComputePipeline = ComputePipeline;+ type RayTracingPipeline = RayTracingPipeline;+}++crate::impl_dyn_resource!(+ Adapter,+ AccelerationStructure,+ BindGroup,+ BindGroupLayout,+ Buffer,+ CommandBuffer,+ CommandEncoder,+ ComputePipeline,+ Device,+ Fence,+ Instance,+ PipelineCache,+ PipelineLayout,+ QuerySet,+ Queue,+ RenderPipeline,+ RayTracingPipeline,+ Sampler,+ ShaderModule,+ Surface,+ SurfaceTexture,+ Texture,+ TextureView+);++struct DebugUtils {+ extension: ext::debug_utils::Instance,+ messenger: vk::DebugUtilsMessengerEXT,++ /// Owning pointer to the debug messenger callback user data.+ ///+ /// `InstanceShared::drop` destroys the debug messenger before+ /// dropping this, so the callback should never receive a dangling+ /// user data pointer.+ #[allow(dead_code)]+ callback_data: Box<DebugUtilsMessengerUserData>,+}++#[derive(Debug)]+pub struct DebugUtilsCreateInfo {+ severity: vk::DebugUtilsMessageSeverityFlagsEXT,+ message_type: vk::DebugUtilsMessageTypeFlagsEXT,+ callback_data: Box<DebugUtilsMessengerUserData>,+}++#[derive(Debug)]+/// The properties related to the validation layer needed for the+/// DebugUtilsMessenger for their workarounds+struct ValidationLayerProperties {+ /// Validation layer description, from `vk::LayerProperties`.+ layer_description: CString,++ /// Validation layer specification version, from `vk::LayerProperties`.+ layer_spec_version: u32,+}++/// User data needed by `instance::debug_utils_messenger_callback`.+///+/// When we create the [`vk::DebugUtilsMessengerEXT`], the `pUserData`+/// pointer refers to one of these values.+#[derive(Debug)]+pub struct DebugUtilsMessengerUserData {+ /// The properties related to the validation layer, if present+ validation_layer_properties: Option<ValidationLayerProperties>,++ /// If the OBS layer is present. OBS never increments the version of their layer,+ /// so there's no reason to have the version.+ has_obs_layer: bool,+}++pub struct InstanceShared {+ raw: ash::Instance,+ extensions: Vec<&'static CStr>,+ flags: wgt::InstanceFlags,+ memory_budget_thresholds: wgt::MemoryBudgetThresholds,+ debug_utils: Option<DebugUtils>,+ get_physical_device_properties: Option<khr::get_physical_device_properties2::Instance>,+ entry: ash::Entry,+ has_nv_optimus: bool,+ android_sdk_version: u32,+ /// The instance API version.+ ///+ /// Which is the version of Vulkan supported for instance-level functionality.+ ///+ /// It is associated with a `VkInstance` and its children,+ /// except for a `VkPhysicalDevice` and its children.+ instance_api_version: u32,++ // The `drop_guard` field must be the last field of this struct so it is dropped last.+ // Do not add new fields after it.+ drop_guard: Option<crate::DropGuard>,+}++impl fmt::Debug for InstanceShared {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ let Self {+ raw: _,+ extensions,+ flags,+ memory_budget_thresholds,+ debug_utils: _,+ get_physical_device_properties: _,+ entry: _,+ has_nv_optimus,+ android_sdk_version,+ instance_api_version,+ drop_guard: _,+ } = self;+ f.debug_struct("InstanceShared")+ .field("extensions", extensions)+ .field("flags", flags)+ .field("memory_budget_thresholds", memory_budget_thresholds)+ .field("has_nv_optimus", has_nv_optimus)+ .field("android_sdk_version", android_sdk_version)+ .field("instance_api_version", instance_api_version)+ .finish_non_exhaustive()+ }+}++#[derive(Debug)]+pub struct Instance {+ shared: Arc<InstanceShared>,+}++#[expect(missing_debug_implementations, reason = "TODO?")]+pub struct Surface {+ swapchain: RwLock<Option<Box<dyn swapchain::Swapchain>>>,+ inner: Box<dyn swapchain::Surface>,+}++impl Surface {+ /// Returns the raw Vulkan surface handle.+ ///+ /// Returns `None` if the surface is a DXGI surface.+ pub unsafe fn raw_native_handle(&self) -> Option<vk::SurfaceKHR> {+ Some(+ self.inner+ .as_any()+ .downcast_ref::<swapchain::NativeSurface>()?+ .as_raw(),+ )+ }++ /// Get the raw Vulkan swapchain associated with this surface.+ ///+ /// Returns [`None`] if the surface is not configured or if the swapchain+ /// is a DXGI swapchain.+ pub fn raw_native_swapchain(&self) -> Option<vk::SwapchainKHR> {+ let read = self.swapchain.read();+ Some(+ read.as_ref()?+ .as_any()+ .downcast_ref::<swapchain::NativeSwapchain>()?+ .as_raw(),+ )+ }++ /// Set the present timing information which will be used for the next [presentation](crate::Queue::present()) of this surface,+ /// using [VK_GOOGLE_display_timing].+ ///+ /// This can be used to give an id to presentations, for future use of [`vk::PastPresentationTimingGOOGLE`].+ /// Note that `wgpu-hal` does *not* provide a way to use that API - you should manually access this through [`ash`].+ ///+ /// This can also be used to add a "not before" timestamp to the presentation.+ ///+ /// The exact semantics of the fields are also documented in the [specification](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkPresentTimeGOOGLE.html) for the extension.+ ///+ /// # Panics+ ///+ /// - If the surface hasn't been configured.+ /// - If the surface has been configured for a DXGI swapchain.+ /// - If the device doesn't [support present timing](wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING).+ ///+ /// [VK_GOOGLE_display_timing]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_GOOGLE_display_timing.html+ #[track_caller]+ pub fn set_next_present_time(&self, present_timing: vk::PresentTimeGOOGLE) {+ let mut swapchain = self.swapchain.write();+ swapchain+ .as_mut()+ .expect("Surface should have been configured")+ .as_any_mut()+ .downcast_mut::<swapchain::NativeSwapchain>()+ .expect("Surface should have a native Vulkan swapchain")+ .set_next_present_time(present_timing);+ }+}++#[derive(Debug)]+pub struct SurfaceTexture {+ index: u32,+ texture: Texture,+ metadata: Box<dyn swapchain::SurfaceTextureMetadata>,+}++impl crate::DynSurfaceTexture for SurfaceTexture {}++impl Borrow<Texture> for SurfaceTexture {+ fn borrow(&self) -> &Texture {+ &self.texture+ }+}++impl Borrow<dyn crate::DynTexture> for SurfaceTexture {+ fn borrow(&self) -> &dyn crate::DynTexture {+ &self.texture+ }+}++#[derive(Debug)]+pub struct Adapter {+ raw: vk::PhysicalDevice,+ instance: Arc<InstanceShared>,+ //queue_families: Vec<vk::QueueFamilyProperties>,+ known_memory_flags: vk::MemoryPropertyFlags,+ phd_capabilities: adapter::PhysicalDeviceProperties,+ phd_features: PhysicalDeviceFeatures,+ downlevel_flags: wgt::DownlevelFlags,+ private_caps: PrivateCapabilities,+ workarounds: Workarounds,+}++// TODO there's no reason why this can't be unified--the function pointers should all be the same--it's not clear how to do this with `ash`.+enum ExtensionFn<T> {+ /// The loaded function pointer struct for an extension.+ Extension(T),+ /// The extension was promoted to a core version of Vulkan and the functions on `ash`'s `DeviceV1_x` traits should be used.+ Promoted,+}++struct DeviceExtensionFunctions {+ debug_utils: Option<ext::debug_utils::Device>,+ draw_indirect_count: Option<khr::draw_indirect_count::Device>,+ timeline_semaphore: Option<ExtensionFn<khr::timeline_semaphore::Device>>,+ ray_tracing: Option<RayTracingDeviceExtensionFunctions>,+ ray_tracing_pipelines: Option<khr::ray_tracing_pipeline::Device>,+ mesh_shading: Option<ext::mesh_shader::Device>,+ #[cfg_attr(not(unix), allow(dead_code))]+ external_memory_fd: Option<khr::external_memory_fd::Device>,+}++struct RayTracingDeviceExtensionFunctions {+ acceleration_structure: khr::acceleration_structure::Device,+ buffer_device_address: khr::buffer_device_address::Device,+}++/// Set of internal capabilities, which don't show up in the exposed+/// device geometry, but affect the code paths taken internally.+#[derive(Clone, Debug)]+struct PrivateCapabilities {+ image_view_usage: bool,+ timeline_semaphores: bool,+ texture_d24: bool,+ texture_d24_s8: bool,+ texture_s8: bool,+ /// Ability to present contents to any screen. Only needed to work around broken platform configurations.+ can_present: bool,+ non_coherent_map_mask: wgt::BufferAddress,+ multi_draw_indirect: bool,+ max_draw_indirect_count: u32,++ /// True if this adapter advertises the [`robustBufferAccess`][vrba] feature.+ ///+ /// Note that Vulkan's `robustBufferAccess` is not sufficient to implement+ /// `wgpu_hal`'s guarantee that shaders will not access buffer contents via+ /// a given bindgroup binding outside that binding's [accessible+ /// region][ar]. Enabling `robustBufferAccess` does ensure that+ /// out-of-bounds reads and writes are not undefined behavior (that's good),+ /// but still permits out-of-bounds reads to return data from anywhere+ /// within the buffer, not just the accessible region.+ ///+ /// [ar]: ../struct.BufferBinding.html#accessible-region+ /// [vrba]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#features-robustBufferAccess+ robust_buffer_access: bool,++ robust_image_access: bool,++ /// True if this adapter supports the [`VK_EXT_robustness2`] extension's+ /// [`robustBufferAccess2`] feature.+ ///+ /// This is sufficient to implement `wgpu_hal`'s [required bounds-checking][ar] of+ /// shader accesses to buffer contents. If this feature is not available,+ /// this backend must have Naga inject bounds checks in the generated+ /// SPIR-V.+ ///+ /// [`VK_EXT_robustness2`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_EXT_robustness2.html+ /// [`robustBufferAccess2`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkPhysicalDeviceRobustness2FeaturesEXT.html#features-robustBufferAccess2+ /// [ar]: ../struct.BufferBinding.html#accessible-region+ robust_buffer_access2: bool,++ robust_image_access2: bool,+ zero_initialize_workgroup_memory: bool,+ image_format_list: bool,+ maximum_samplers: u32,++ /// True if this adapter supports the [`VK_KHR_shader_integer_dot_product`] extension+ /// (promoted to Vulkan 1.3).+ ///+ /// This is used to generate optimized code for WGSL's `dot4{I, U}8Packed`.+ ///+ /// [`VK_KHR_shader_integer_dot_product`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_shader_integer_dot_product.html+ shader_integer_dot_product: bool,++ /// True if this adapter supports 8-bit integers provided by the+ /// [`VK_KHR_shader_float16_int8`] extension (promoted to Vulkan 1.2).+ ///+ /// Allows shaders to declare the "Int8" capability. Note, however, that this+ /// feature alone allows the use of 8-bit integers "only in the `Private`,+ /// `Workgroup` (for non-Block variables), and `Function` storage classes"+ /// ([see spec]). To use 8-bit integers in the interface storage classes (e.g.,+ /// `StorageBuffer`), you also need to enable the corresponding feature in+ /// `VkPhysicalDevice8BitStorageFeatures` and declare the corresponding SPIR-V+ /// capability (e.g., `StorageBuffer8BitAccess`).+ ///+ /// [`VK_KHR_shader_float16_int8`]: https://registry.khronos.org/vulkan/specs/latest/man/html/VK_KHR_shader_float16_int8.html+ /// [see spec]: https://registry.khronos.org/vulkan/specs/latest/man/html/VkPhysicalDeviceShaderFloat16Int8Features.html#extension-features-shaderInt8+ shader_int8: bool,++ /// This is done to panic before undefined behavior, and is imperfect.+ /// Basically, to allow implementations to emulate mv using instancing, if you+ /// want to draw `n` instances to VR, you must draw `2n` instances, but you+ /// can never draw more than `u32::MAX` instances. Therefore, when drawing+ /// multiview on some vulkan implementations, it might restrict the instance+ /// count, which isn't usually a thing in webgpu. We don't expose this limit+ /// because its strange, i.e. only occurs on certain vulkan implementations+ /// if you are drawing more than 128 million instances. We still want to avoid+ /// undefined behavior in this situation, so we panic if the limit is violated.+ multiview_instance_index_limit: u32,++ /// BufferUsages::ACCELERATION_STRUCTURE_SCRATCH allows usage as a scratch buffer.+ /// Vulkan has no way to specify this as a usage, and it maps to other usages, but+ /// these usages do not have as high of an alignment requirement using the buffer as+ /// a scratch buffer when building acceleration structures.+ scratch_buffer_alignment: u32,++ /// `get_raytracing_pipeline_group_data` requires both a group count and a data size.+ /// The data size parameter is just this * the group count, so we store this to not+ /// require an unnecessary parameter.+ ray_tracing_pipeline_group_data_size: u32,+}++bitflags::bitflags!(+ /// Workaround flags.+ #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]+ pub struct Workarounds: u32 {+ /// Only generate SPIR-V for one entry point at a time.+ const SEPARATE_ENTRY_POINTS = 0x1;+ /// Qualcomm OOMs when there are zero color attachments but a non-null pointer+ /// to a subpass resolve attachment array. This nulls out that pointer in that case.+ const EMPTY_RESOLVE_ATTACHMENT_LISTS = 0x2;+ /// If the following code returns false, then nvidia will end up filling the wrong range.+ ///+ /// ```skip+ /// fn nvidia_succeeds() -> bool {+ /// # let (copy_length, start_offset) = (0, 0);+ /// if copy_length >= 4096 {+ /// if start_offset % 16 != 0 {+ /// if copy_length == 4096 {+ /// return true;+ /// }+ /// if copy_length % 16 == 0 {+ /// return false;+ /// }+ /// }+ /// }+ /// true+ /// }+ /// ```+ ///+ /// As such, we need to make sure all calls to vkCmdFillBuffer are aligned to 16 bytes+ /// if they cover a range of 4096 bytes or more.+ const FORCE_FILL_BUFFER_WITH_SIZE_GREATER_4096_ALIGNED_OFFSET_16 = 0x4;+ }+);++#[derive(Clone, Debug, Eq, Hash, PartialEq)]+struct AttachmentKey {+ format: vk::Format,+ layout: vk::ImageLayout,+ ops: crate::AttachmentOps,+}++impl AttachmentKey {+ /// Returns an attachment key for a compatible attachment.+ fn compatible(format: vk::Format, layout: vk::ImageLayout) -> Self {+ Self {+ format,+ layout,+ ops: crate::AttachmentOps::all(),+ }+ }+}++#[derive(Clone, Eq, Hash, PartialEq)]+struct ColorAttachmentKey {+ base: AttachmentKey,+ resolve: Option<AttachmentKey>,+}++#[derive(Clone, Eq, Hash, PartialEq)]+struct DepthStencilAttachmentKey {+ base: AttachmentKey,+ stencil_ops: crate::AttachmentOps,+}++#[derive(Clone, Eq, Default, Hash, PartialEq)]+struct RenderPassKey {+ colors: ArrayVec<Option<ColorAttachmentKey>, { crate::MAX_COLOR_ATTACHMENTS }>,+ depth_stencil: Option<DepthStencilAttachmentKey>,+ sample_count: u32,+ multiview_mask: Option<NonZeroU32>,+}++struct DeviceShared {+ raw: ash::Device,+ family_index: u32,+ queue_index: u32,+ raw_queue: vk::Queue,+ instance: Arc<InstanceShared>,+ physical_device: vk::PhysicalDevice,+ enabled_extensions: Vec<&'static CStr>,+ extension_fns: DeviceExtensionFunctions,+ vendor_id: u32,+ pipeline_cache_validation_key: [u8; 16],+ timestamp_period: f32,+ private_caps: PrivateCapabilities,+ workarounds: Workarounds,+ features: wgt::Features,+ render_passes: Mutex<FastHashMap<RenderPassKey, vk::RenderPass>>,+ sampler_cache: Mutex<sampler::SamplerCache>,+ memory_allocations_counter: InternalCounter,++ /// Because we have cached framebuffers which are not deleted from until+ /// the device is destroyed, if the implementation of vulkan re-uses handles+ /// we need some way to differentiate between the old handle and the new handle.+ /// This factory allows us to have a dedicated identity value for each texture.+ texture_identity_factory: ResourceIdentityFactory<vk::Image>,+ /// As above, for texture views.+ texture_view_identity_factory: ResourceIdentityFactory<vk::ImageView>,++ empty_descriptor_set_layout: vk::DescriptorSetLayout,++ // The `drop_guard` field must be the last field of this struct so it is dropped last.+ // Do not add new fields after it.+ drop_guard: Option<crate::DropGuard>,+}++impl Drop for DeviceShared {+ fn drop(&mut self) {+ for &raw in self.render_passes.lock().values() {+ unsafe { self.raw.destroy_render_pass(raw, None) };+ }+ unsafe {+ self.raw+ .destroy_descriptor_set_layout(self.empty_descriptor_set_layout, None)+ };+ if self.drop_guard.is_none() {+ unsafe { self.raw.destroy_device(None) };+ }+ }+}++#[expect(+ missing_debug_implementations,+ reason = "needs work to not be disastrously verbose"+)]+pub struct Device {+ mem_allocator: Mutex<gpu_allocator::vulkan::Allocator>,+ desc_allocator: Mutex<descriptor::DescriptorAllocator>,+ valid_ash_memory_types: u32,+ naga_options: naga::back::spv::Options<'static>,+ #[cfg(feature = "renderdoc")]+ render_doc: crate::auxil::renderdoc::RenderDoc,+ counters: Arc<wgt::HalCounters>,+ // Struct members are dropped from first to last, put the Device last to ensure that+ // all resources that depends on it are destroyed before it like the mem_allocator+ shared: Arc<DeviceShared>,+}++impl Drop for Device {+ fn drop(&mut self) {}+}++/// Semaphores for forcing queue submissions to run in order.+///+/// The [`wgpu_hal::Queue`] trait promises that if two calls to [`submit`] are+/// ordered, then the first submission will finish on the GPU before the second+/// submission begins. To get this behavior on Vulkan we need to pass semaphores+/// to [`vkQueueSubmit`] for the commands to wait on before beginning execution,+/// and to signal when their execution is done.+///+/// Normally this can be done with a single semaphore, waited on and then+/// signalled for each submission. At any given time there's exactly one+/// submission that would signal the semaphore, and exactly one waiting on it,+/// as Vulkan requires.+///+/// However, as of Oct 2021, bug [#5508] in the Mesa ANV drivers caused them to+/// hang if we use a single semaphore. The workaround is to alternate between+/// two semaphores. The bug has been fixed in Mesa, but we should probably keep+/// the workaround until, say, Oct 2026.+///+/// [`wgpu_hal::Queue`]: crate::Queue+/// [`submit`]: crate::Queue::submit+/// [`vkQueueSubmit`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkQueueSubmit+/// [#5508]: https://gitlab.freedesktop.org/mesa/mesa/-/issues/5508+#[derive(Clone)]+struct RelaySemaphores {+ /// The semaphore the next submission should wait on before beginning+ /// execution on the GPU. This is `None` for the first submission, which+ /// should not wait on anything at all.+ wait: Option<vk::Semaphore>,++ /// The semaphore the next submission should signal when it has finished+ /// execution on the GPU.+ signal: vk::Semaphore,+}++impl RelaySemaphores {+ fn new(device: &DeviceShared) -> Result<Self, crate::DeviceError> {+ Ok(Self {+ wait: None,+ signal: device.new_binary_semaphore("RelaySemaphores: 1")?,+ })+ }++ /// Advances the semaphores, returning the semaphores that should be used for a submission.+ fn advance(&mut self, device: &DeviceShared) -> Result<Self, crate::DeviceError> {+ let old = self.clone();++ // Build the state for the next submission.+ match self.wait {+ None => {+ // The `old` values describe the first submission to this queue.+ // The second submission should wait on `old.signal`, and then+ // signal a new semaphore which we'll create now.+ self.wait = Some(old.signal);+ self.signal = device.new_binary_semaphore("RelaySemaphores: 2")?;+ }+ Some(ref mut wait) => {+ // What this submission signals, the next should wait.+ mem::swap(wait, &mut self.signal);+ }+ };++ Ok(old)+ }++ /// Destroys the semaphores.+ unsafe fn destroy(&self, device: &ash::Device) {+ unsafe {+ if let Some(wait) = self.wait {+ device.destroy_semaphore(wait, None);+ }+ device.destroy_semaphore(self.signal, None);+ }+ }+}++pub struct Queue {+ raw: vk::Queue,+ device: Arc<DeviceShared>,+ family_index: u32,+ relay_semaphores: Mutex<RelaySemaphores>,+ signal_semaphores: Mutex<SemaphoreList>,+ wait_semaphores: Mutex<SemaphoreList>,+}++impl fmt::Debug for Queue {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ let Self {+ raw: _,+ device: _,+ family_index,+ relay_semaphores: _,+ signal_semaphores: _,+ wait_semaphores: _,+ } = self;+ f.debug_struct("Queue")+ .field("family_index", family_index)+ .finish_non_exhaustive()+ }+}++impl Queue {+ pub fn as_raw(&self) -> vk::Queue {+ self.raw+ }+}++impl Drop for Queue {+ fn drop(&mut self) {+ unsafe { self.relay_semaphores.lock().destroy(&self.device.raw) };+ }+}+#[derive(Debug)]+enum BufferMemoryBacking {+ Managed(gpu_allocator::vulkan::Allocation),+ VulkanMemory {+ memory: vk::DeviceMemory,+ offset: u64,+ size: u64,+ },+}+impl BufferMemoryBacking {+ fn memory(&self) -> vk::DeviceMemory {+ match self {+ Self::Managed(m) => unsafe { m.memory() },+ Self::VulkanMemory { memory, .. } => *memory,+ }+ }+ fn offset(&self) -> u64 {+ match self {+ Self::Managed(m) => m.offset(),+ Self::VulkanMemory { offset, .. } => *offset,+ }+ }+ fn size(&self) -> u64 {+ match self {+ Self::Managed(m) => m.size(),+ Self::VulkanMemory { size, .. } => *size,+ }+ }+}+/// Describes who owns a [`Buffer`]'s `vk::Buffer` handle and its backing memory,+/// and therefore what cleanup is required when the buffer is destroyed.+#[derive(Debug)]+enum BufferOwnership {+ /// wgpu-hal owns the `vk::Buffer` and its backing memory. On cleanup the buffer+ /// handle is destroyed and the memory is released.+ Managed(Mutex<BufferMemoryBacking>),+ /// wgpu-hal owns the `vk::Buffer` handle but the backing memory is kept alive+ /// by the caller. On cleanup only the buffer handle is destroyed.+ RawHandle,+ /// Caller owns the `vk::Buffer` and its backing memory. On cleanup the+ /// [`crate::DropGuard`] runs the caller's cleanup callback and wgpu-hal touches+ /// neither the handle nor the memory.+ External(crate::DropGuard),+}++#[derive(Debug)]+pub struct Buffer {+ raw: vk::Buffer,++ // This field must be last, because it may contain a `DropGuard` which needs to be dropped after all other fields.+ ownership: BufferOwnership,+}+impl Buffer {+ /// # Safety+ ///+ /// - `vk_buffer`'s memory must be managed by the caller+ /// - Externally imported buffers can't be mapped by `wgpu`+ pub unsafe fn from_raw(vk_buffer: vk::Buffer) -> Self {+ Self {+ raw: vk_buffer,+ ownership: BufferOwnership::RawHandle,+ }+ }++ /// # Safety+ /// - `vk_buffer` must outlive the returned `Buffer`.+ /// - wgpu-hal will NOT call `vkDestroyBuffer`; the caller remains responsible for the buffer handle's destruction.+ /// The `drop_callback` runs when the `Buffer` drops and may be used to release caller-side bookkeeping.+ /// - Externally imported buffers can't be mapped by `wgpu`.+ pub unsafe fn from_raw_externally_owned(+ vk_buffer: vk::Buffer,+ drop_callback: crate::DropCallback,+ ) -> Self {+ Self {+ raw: vk_buffer,+ ownership: BufferOwnership::External(crate::DropGuard::new(drop_callback)),+ }+ }++ /// # Safety+ /// - We will use this buffer and the buffer's backing memory range as if we have exclusive ownership over it, until the wgpu resource is dropped and the wgpu-hal object is cleaned up+ /// - Externally imported buffers can't be mapped by `wgpu`+ /// - `offset` and `size` must be valid with the allocation of `memory`+ pub unsafe fn from_raw_managed(+ vk_buffer: vk::Buffer,+ memory: vk::DeviceMemory,+ offset: u64,+ size: u64,+ ) -> Self {+ Self {+ raw: vk_buffer,+ ownership: BufferOwnership::Managed(Mutex::new(BufferMemoryBacking::VulkanMemory {+ memory,+ offset,+ size,+ })),+ }+ }++ /// # Safety+ /// - The buffer handle must not be manually destroyed+ pub unsafe fn raw_handle(&self) -> vk::Buffer {+ self.raw+ }+}++impl crate::DynBuffer for Buffer {}++#[derive(Debug)]+pub struct AccelerationStructure {+ raw: vk::AccelerationStructureKHR,+ buffer: vk::Buffer,+ allocation: gpu_allocator::vulkan::Allocation,+ compacted_size_query: Option<vk::QueryPool>,+}++impl crate::DynAccelerationStructure for AccelerationStructure {}++#[derive(Debug)]+pub enum TextureMemory {+ // shared memory in GPU allocator (owned by wgpu-hal)+ Allocation(gpu_allocator::vulkan::Allocation),++ // dedicated memory (owned by wgpu-hal)+ Dedicated(vk::DeviceMemory),++ // memory not owned by wgpu+ External,+}++#[derive(Debug)]+pub struct Texture {+ raw: vk::Image,+ memory: TextureMemory,+ format: wgt::TextureFormat,+ copy_size: crate::CopyExtent,+ identity: ResourceIdentity<vk::Image>,++ // The `drop_guard` field must be the last field of this struct so it is dropped last.+ // Do not add new fields after it.+ drop_guard: Option<crate::DropGuard>,+}++impl crate::DynTexture for Texture {}++impl Texture {+ /// # Safety+ ///+ /// - The image handle must not be manually destroyed+ pub unsafe fn raw_handle(&self) -> vk::Image {+ self.raw+ }++ /// # Safety+ ///+ /// - The caller must not free the `vk::DeviceMemory` or+ /// `gpu_alloc::MemoryBlock` in the returned `TextureMemory`.+ pub unsafe fn memory(&self) -> &TextureMemory {+ &self.memory+ }+}++#[derive(Debug)]+pub struct TextureView {+ raw_texture: vk::Image,+ raw: vk::ImageView,+ _layers: NonZeroU32,+ format: wgt::TextureFormat,+ raw_format: vk::Format,+ base_mip_level: u32,+ dimension: wgt::TextureViewDimension,+ texture_identity: ResourceIdentity<vk::Image>,+ view_identity: ResourceIdentity<vk::ImageView>,+}++impl crate::DynTextureView for TextureView {}++impl TextureView {+ /// # Safety+ ///+ /// - The image view handle must not be manually destroyed+ pub unsafe fn raw_handle(&self) -> vk::ImageView {+ self.raw+ }++ /// Returns the raw texture view, along with its identity.+ fn identified_raw_view(&self) -> IdentifiedTextureView {+ IdentifiedTextureView {+ raw: self.raw,+ identity: self.view_identity,+ }+ }+}++#[derive(Debug)]+pub struct Sampler {+ raw: vk::Sampler,+ create_info: vk::SamplerCreateInfo<'static>,+}++impl crate::DynSampler for Sampler {}++/// Information about a binding within a specific BindGroupLayout / BindGroup.+/// This will be used to construct a [`naga::back::spv::BindingInfo`], where+/// the descriptor set value will be taken from the index of the group.+#[derive(Copy, Clone, Debug)]+struct BindingInfo {+ binding: u32,+ binding_array_size: Option<NonZeroU32>,+}++#[derive(Debug)]+pub struct BindGroupLayout {+ raw: vk::DescriptorSetLayout,+ desc_count: descriptor::DescriptorCounts,+ /// Sorted list of entries.+ entries: Box<[wgt::BindGroupLayoutEntry]>,+ /// Map of original binding index to remapped binding index and optional+ /// array size.+ binding_map: Vec<(u32, BindingInfo)>,+ contains_binding_arrays: bool,+}++impl crate::DynBindGroupLayout for BindGroupLayout {}++#[derive(Debug)]+pub struct PipelineLayout {+ raw: vk::PipelineLayout,+ binding_map: naga::back::spv::BindingMap,+}++impl crate::DynPipelineLayout for PipelineLayout {}++#[derive(Debug)]+pub struct BindGroup {+ set: descriptor::DescriptorSet,+}++impl crate::DynBindGroup for BindGroup {}++/// Miscellaneous allocation recycling pool for `CommandAllocator`.+#[derive(Default)]+struct Temp {+ marker: Vec<u8>,+ buffer_barriers: Vec<vk::BufferMemoryBarrier<'static>>,+ image_barriers: Vec<vk::ImageMemoryBarrier<'static>>,+}++impl Temp {+ fn clear(&mut self) {+ self.marker.clear();+ self.buffer_barriers.clear();+ self.image_barriers.clear();+ }++ fn make_c_str(&mut self, name: &str) -> &CStr {+ self.marker.clear();+ self.marker.extend_from_slice(name.as_bytes());+ self.marker.push(0);+ unsafe { CStr::from_bytes_with_nul_unchecked(&self.marker) }+ }+}++/// Generates unique IDs for each resource of type `T`.+///+/// Because vk handles are not permanently unique, this+/// provides a way to generate unique IDs for each resource.+struct ResourceIdentityFactory<T> {+ #[cfg(not(target_has_atomic = "64"))]+ next_id: Mutex<u64>,+ #[cfg(target_has_atomic = "64")]+ next_id: core::sync::atomic::AtomicU64,+ _phantom: PhantomData<T>,+}++impl<T> ResourceIdentityFactory<T> {+ fn new() -> Self {+ Self {+ #[cfg(not(target_has_atomic = "64"))]+ next_id: Mutex::new(0),+ #[cfg(target_has_atomic = "64")]+ next_id: core::sync::atomic::AtomicU64::new(0),+ _phantom: PhantomData,+ }+ }++ /// Returns a new unique ID for a resource of type `T`.+ fn next(&self) -> ResourceIdentity<T> {+ #[cfg(not(target_has_atomic = "64"))]+ {+ let mut next_id = self.next_id.lock();+ let id = *next_id;+ *next_id += 1;+ ResourceIdentity {+ id,+ _phantom: PhantomData,+ }+ }++ #[cfg(target_has_atomic = "64")]+ ResourceIdentity {+ id: self+ .next_id+ .fetch_add(1, core::sync::atomic::Ordering::Relaxed),+ _phantom: PhantomData,+ }+ }+}++/// A unique identifier for a resource of type `T`.+///+/// This is used as a hashable key for resources, which+/// is permanently unique through the lifetime of the program.+#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]+struct ResourceIdentity<T> {+ id: u64,+ _phantom: PhantomData<T>,+}++#[derive(Clone, Eq, Hash, PartialEq)]+struct FramebufferKey {+ raw_pass: vk::RenderPass,+ /// Because this is used as a key in a hash map, we need to include the identity+ /// so that this hashes differently, even if the ImageView handles are the same+ /// between different views.+ attachment_identities: ArrayVec<ResourceIdentity<vk::ImageView>, { MAX_TOTAL_ATTACHMENTS }>,+ /// While this is redundant for calculating the hash, we need access to an array+ /// of all the raw ImageViews when we are creating the actual framebuffer,+ /// so we store this here.+ attachment_views: ArrayVec<vk::ImageView, { MAX_TOTAL_ATTACHMENTS }>,+ extent: wgt::Extent3d,+}++impl FramebufferKey {+ fn push_view(&mut self, view: IdentifiedTextureView) {+ self.attachment_identities.push(view.identity);+ self.attachment_views.push(view.raw);+ }+}++/// A texture view paired with its identity.+#[derive(Copy, Clone)]+struct IdentifiedTextureView {+ raw: vk::ImageView,+ identity: ResourceIdentity<vk::ImageView>,+}++#[derive(Clone, Eq, Hash, PartialEq)]+struct TempTextureViewKey {+ texture: vk::Image,+ /// As this is used in a hashmap, we need to+ /// include the identity so that this hashes differently,+ /// even if the Image handles are the same between different images.+ texture_identity: ResourceIdentity<vk::Image>,+ format: vk::Format,+ mip_level: u32,+ depth_slice: u32,+}++// Any state in this struct that may be dirty after an abandoned encoding must+// be reset for reused encoders in `begin_encoding`.+pub struct CommandEncoder {+ raw: vk::CommandPool,+ device: Arc<DeviceShared>,++ /// The current command buffer, if `self` is in the ["recording"]+ /// state.+ ///+ /// ["recording"]: crate::CommandEncoder+ ///+ /// If non-`null`, the buffer is in the Vulkan "recording" state.+ active: vk::CommandBuffer,++ /// What kind of pass we are currently within: compute or render.+ bind_point: vk::PipelineBindPoint,++ /// Allocation recycling pool for this encoder.+ temp: Temp,++ /// A pool of available command buffers.+ ///+ /// These are all in the Vulkan "initial" state.+ free: Vec<vk::CommandBuffer>,++ /// A pool of discarded command buffers.+ ///+ /// These could be in any Vulkan state except "pending".+ discarded: Vec<vk::CommandBuffer>,++ /// If this is true, the active renderpass enabled a debug span,+ /// and needs to be disabled on renderpass close.+ rpass_debug_marker_active: bool,++ /// If set, the end of the next render/compute pass will write a timestamp at+ /// the given pool & location.+ end_of_pass_timer_query: Option<(vk::QueryPool, u32)>,++ framebuffers: FastHashMap<FramebufferKey, vk::Framebuffer>,+ temp_texture_views: FastHashMap<TempTextureViewKey, IdentifiedTextureView>,++ counters: Arc<wgt::HalCounters>,++ current_pipeline_is_multiview: bool,+}++impl Drop for CommandEncoder {+ fn drop(&mut self) {+ // SAFETY:+ //+ // VUID-vkDestroyCommandPool-commandPool-00041: wgpu_hal requires that a+ // `CommandBuffer` must live until its execution is complete, and that a+ // `CommandBuffer` must not outlive the `CommandEncoder` that built it.+ // Thus, we know that none of our `CommandBuffers` are in the "pending"+ // state.+ //+ // The other VUIDs are pretty obvious.+ unsafe {+ // `vkDestroyCommandPool` also frees any command buffers allocated+ // from that pool, so there's no need to explicitly call+ // `vkFreeCommandBuffers` on `cmd_encoder`'s `free` and `discarded`+ // fields.+ self.device.raw.destroy_command_pool(self.raw, None);+ }++ for (_, fb) in self.framebuffers.drain() {+ unsafe { self.device.raw.destroy_framebuffer(fb, None) };+ }++ for (_, view) in self.temp_texture_views.drain() {+ unsafe { self.device.raw.destroy_image_view(view.raw, None) };+ }++ self.counters.command_encoders.sub(1);+ }+}++impl CommandEncoder {+ /// # Safety+ ///+ /// - The command buffer handle must not be manually destroyed+ pub unsafe fn raw_handle(&self) -> vk::CommandBuffer {+ self.active+ }+}++impl fmt::Debug for CommandEncoder {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ f.debug_struct("CommandEncoder")+ .field("raw", &self.raw)+ .finish()+ }+}++#[derive(Debug)]+pub struct CommandBuffer {+ raw: vk::CommandBuffer,+}++impl crate::DynCommandBuffer for CommandBuffer {}++#[derive(Debug)]+pub enum ShaderModule {+ Raw(vk::ShaderModule),+ Intermediate {+ naga_shader: crate::NagaShader,+ runtime_checks: wgt::ShaderRuntimeChecks,+ },+}++impl crate::DynShaderModule for ShaderModule {}++#[derive(Debug)]+pub struct RenderPipeline {+ raw: vk::Pipeline,+ is_multiview: bool,+}++impl crate::DynRenderPipeline for RenderPipeline {}++#[derive(Debug)]+pub struct ComputePipeline {+ raw: vk::Pipeline,+}++impl crate::DynComputePipeline for ComputePipeline {}++#[derive(Debug)]+pub struct RayTracingPipeline {+ raw: vk::Pipeline,+}++impl crate::DynRayTracingPipeline for RayTracingPipeline {}++#[derive(Debug)]+pub struct PipelineCache {+ raw: vk::PipelineCache,+}++impl crate::DynPipelineCache for PipelineCache {}++#[derive(Debug)]+pub struct QuerySet {+ raw: vk::QueryPool,+}++impl crate::DynQuerySet for QuerySet {}++/// The [`Api::Fence`] type for [`vulkan::Api`].+///+/// This is an `enum` because there are two possible implementations of+/// `wgpu-hal` fences on Vulkan: Vulkan fences, which work on any version of+/// Vulkan, and Vulkan timeline semaphores, which are easier and cheaper but+/// require non-1.0 features.+///+/// [`Device::create_fence`] returns a [`TimelineSemaphore`] if+/// [`VK_KHR_timeline_semaphore`] is available and enabled, and a [`FencePool`]+/// otherwise.+///+/// [`Api::Fence`]: crate::Api::Fence+/// [`vulkan::Api`]: Api+/// [`Device::create_fence`]: crate::Device::create_fence+/// [`TimelineSemaphore`]: Fence::TimelineSemaphore+/// [`VK_KHR_timeline_semaphore`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#VK_KHR_timeline_semaphore+/// [`FencePool`]: Fence::FencePool+#[derive(Debug)]+pub enum Fence {+ /// A Vulkan [timeline semaphore].+ ///+ /// These are simpler to use than Vulkan fences, since timeline semaphores+ /// work exactly the way [`wpgu_hal::Api::Fence`] is specified to work.+ ///+ /// [timeline semaphore]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#synchronization-semaphores+ /// [`wpgu_hal::Api::Fence`]: crate::Api::Fence+ TimelineSemaphore(vk::Semaphore),++ /// A collection of Vulkan [fence]s, each associated with a [`FenceValue`].+ ///+ /// The effective [`FenceValue`] of this variant is the greater of+ /// `last_completed` and the maximum value associated with a signalled fence+ /// in `active`.+ ///+ /// Fences are available in all versions of Vulkan, but since they only have+ /// two states, "signaled" and "unsignaled", we need to use a separate fence+ /// for each queue submission we might want to wait for, and remember which+ /// [`FenceValue`] each one represents.+ ///+ /// One should keep the fence pool read while there are any references to the+ /// fences inside of them. This ensures there are no race conditions when+ /// resetting the fences+ ///+ /// [fence]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#synchronization-fences+ /// [`FenceValue`]: crate::FenceValue+ FencePool(RwLock<FencePool>),+}++/// A shared fence type. The arc is expect to have a ref-count of one once a function has finished being called+///+/// A fence should have access synchronised as fence resetting might happen at any point. Resetting checks the ref-count+/// of the fence, so instead of copying the fence, it should have its `Arc` container cloned which shows not to reset+/// this fence as it is being used.+pub(super) type SynchronizedFence = Arc<vk::Fence>;++#[derive(Debug)]+pub struct FencePool {+ last_completed: crate::FenceValue,+ /// The pending fence values have to be ascending.+ active: Vec<(crate::FenceValue, SynchronizedFence)>,+ // Don't need extra synchronisation around the fences here, if they are used they should be put into active.+ free: Vec<vk::Fence>,+}++impl crate::DynFence for Fence {}++impl Fence {+ /// Return the highest [`FenceValue`] among the signalled fences in `active`.+ ///+ /// As an optimization, assume that we already know that the fence has+ /// reached `last_completed`, and don't bother checking fences whose values+ /// are less than that: those fences remain in the `active` array only+ /// because we haven't called `maintain` yet to clean them up.+ ///+ /// [`FenceValue`]: crate::FenceValue+ fn check_active(+ device: &ash::Device,+ mut last_completed: crate::FenceValue,+ active: &[(crate::FenceValue, SynchronizedFence)],+ ) -> Result<crate::FenceValue, crate::DeviceError> {+ for &(value, ref raw) in active.iter() {+ unsafe {+ if value > last_completed+ && device+ // Don't need to clone as active should be from a read or+ // write lock which means this is already synchronised.+ .get_fence_status(**raw)+ .map_err(map_host_device_oom_and_lost_err)?+ {+ last_completed = value;+ }+ }+ }+ Ok(last_completed)+ }++ /// Return the highest signalled [`FenceValue`] for `self`.+ ///+ /// [`FenceValue`]: crate::FenceValue+ fn get_latest(+ &self,+ device: &ash::Device,+ extension: Option<&ExtensionFn<khr::timeline_semaphore::Device>>,+ ) -> Result<crate::FenceValue, crate::DeviceError> {+ match *self {+ Self::TimelineSemaphore(raw) => unsafe {+ Ok(match *extension.unwrap() {+ ExtensionFn::Extension(ref ext) => ext+ .get_semaphore_counter_value(raw)+ .map_err(map_host_device_oom_and_lost_err)?,+ ExtensionFn::Promoted => device+ .get_semaphore_counter_value(raw)+ .map_err(map_host_device_oom_and_lost_err)?,+ })+ },+ Self::FencePool(ref pool) => {+ let FencePool {+ last_completed,+ ref active,+ free: _,+ } = *pool.read();+ Self::check_active(device, last_completed, active)+ }+ }+ }++ /// Trim the internal state of this [`Fence`].+ ///+ /// This function has no externally visible effect, but you should call it+ /// periodically to keep this fence's resource consumption under control.+ ///+ /// For fences using the [`FencePool`] implementation, this function+ /// recycles fences that have been signaled. If you don't call this,+ /// [`Queue::submit`] will just keep allocating a new Vulkan fence every+ /// time it's called.+ ///+ /// [`FencePool`]: Fence::FencePool+ /// [`Queue::submit`]: crate::Queue::submit+ fn maintain(&self, device: &ash::Device) -> Result<(), crate::DeviceError> {+ match *self {+ Self::TimelineSemaphore(_) => {}+ Self::FencePool(ref pool) => {+ let FencePool {+ ref mut last_completed,+ ref mut active,+ ref mut free,+ } = *pool.write();++ let base_free = free.len();+ let latest = Self::check_active(device, *last_completed, active)?;++ active.retain_mut(|&mut (value, ref mut fence)| {+ if value > latest {+ true+ } else if let Some(fence) = Arc::get_mut(fence) {+ // No other references to these, so we have exclusive access. Add them to free and reset them later,+ // but drop them from active immediately+ free.push(*fence);+ false+ } else {+ // some other function is using it. Although this shouldn't be to long,+ // maintain shouldn't block, and it should be cleared up by the next time it happens+ true+ }+ });++ if free.len() != base_free {+ unsafe { device.reset_fences(&free[base_free..]) }+ .map_err(map_device_oom_err)?+ }+ *last_completed = latest;+ }+ }+ Ok(())+ }+}++impl crate::Queue for Queue {+ type A = Api;++ unsafe fn submit(+ &self,+ command_buffers: &[&CommandBuffer],+ surface_textures: &[&SurfaceTexture],+ (signal_fence, signal_value): (&Fence, crate::FenceValue),+ ) -> Result<(), crate::DeviceError> {+ let mut fence_raw = vk::Fence::null();++ let mut wait_semaphores = SemaphoreList::new(SemaphoreListMode::Wait);+ let mut signal_semaphores = SemaphoreList::new(SemaphoreListMode::Signal);++ // Double check that the same swapchain image isn't being given to us multiple times,+ // as that will deadlock when we try to lock them all.+ debug_assert!(+ {+ let mut check = HashSet::with_capacity(surface_textures.len());+ // We compare the Box by pointer, as Eq isn't well defined for SurfaceSemaphores.+ for st in surface_textures {+ let ptr: *const () = <*const _>::cast(&*st.metadata);+ check.insert(ptr as usize);+ }+ check.len() == surface_textures.len()+ },+ "More than one surface texture is being used from the same swapchain. This will cause a deadlock in release."+ );++ let locked_swapchain_semaphores = surface_textures+ .iter()+ .map(|st| st.metadata.get_semaphore_guard())+ .collect::<Vec<_>>();++ for mut semaphores in locked_swapchain_semaphores {+ semaphores.set_used_fence_value(signal_value);++ // If we're the first submission to operate on this image, wait on+ // its acquire semaphore, to make sure the presentation engine is+ // done with it.+ if let Some(sem) = semaphores.get_acquire_wait_semaphore() {+ wait_semaphores.push_wait(sem, vk::PipelineStageFlags::TOP_OF_PIPE);+ }++ // Get a semaphore to signal when we're done writing to this surface+ // image. Presentation of this image will wait for this.+ let signal_semaphore = semaphores.get_submit_signal_semaphore(&self.device)?;+ signal_semaphores.push_signal(signal_semaphore);+ }++ let mut guard = self.signal_semaphores.lock();+ if !guard.is_empty() {+ signal_semaphores.append(&mut guard);+ }++ let mut wait_guard = self.wait_semaphores.lock();+ if !wait_guard.is_empty() {+ wait_semaphores.append(&mut wait_guard);+ }++ // In order for submissions to be strictly ordered, we encode a dependency between each submission+ // using a pair of semaphores. This adds a wait if it is needed, and signals the next semaphore.+ let semaphore_state = self.relay_semaphores.lock().advance(&self.device)?;++ if let Some(sem) = semaphore_state.wait {+ wait_semaphores.push_wait(+ SemaphoreType::Binary(sem),+ vk::PipelineStageFlags::TOP_OF_PIPE,+ );+ }++ signal_semaphores.push_signal(SemaphoreType::Binary(semaphore_state.signal));++ // We need to signal our wgpu::Fence if we have one, this adds it to the signal list.+ signal_fence.maintain(&self.device.raw)?;+ // Keeping the Arc around is probably unneeded - the fence should never be signaled as it was reset,+ // and newer submits should not happen until this submit is done. Therefore, it should be too high+ // to be reset.+ let shared_fence;+ match *signal_fence {+ Fence::TimelineSemaphore(raw) => {+ signal_semaphores.push_signal(SemaphoreType::Timeline(raw, signal_value));+ }+ Fence::FencePool(ref pool) => {+ let FencePool {+ ref mut active,+ ref mut free,+ ..+ } = *pool.write();+ shared_fence = match free.pop() {+ Some(raw) => Arc::new(raw),+ None => unsafe {+ let fence = self+ .device+ .raw+ .create_fence(&vk::FenceCreateInfo::default(), None)+ .map_err(map_host_device_oom_err)?;+ Arc::new(fence)+ },+ };+ fence_raw = *shared_fence;+ active.push((signal_value, shared_fence.clone()));+ }+ }++ let vk_cmd_buffers = command_buffers+ .iter()+ .map(|cmd| cmd.raw)+ .collect::<Vec<_>>();++ let mut vk_info = vk::SubmitInfo::default().command_buffers(&vk_cmd_buffers);+ let mut vk_timeline_info = mem::MaybeUninit::uninit();+ vk_info = SemaphoreList::add_to_submit(+ &mut wait_semaphores,+ &mut signal_semaphores,+ vk_info,+ &mut vk_timeline_info,+ );++ profiling::scope!("vkQueueSubmit");+ unsafe {+ self.device+ .raw+ .queue_submit(self.raw, &[vk_info], fence_raw)+ .map_err(map_host_device_oom_and_lost_err)?+ };+ Ok(())+ }++ unsafe fn present(+ &self,+ surface: &Surface,+ texture: SurfaceTexture,+ ) -> Result<(), crate::SurfaceError> {+ let mut swapchain = surface.swapchain.write();++ unsafe { swapchain.as_mut().unwrap().present(self, texture) }+ }++ unsafe fn get_timestamp_period(&self) -> f32 {+ self.device.timestamp_period+ }++ unsafe fn wait_for_idle(&self) -> Result<(), crate::DeviceError> {+ unsafe { self.device.raw.queue_wait_idle(self.raw) }+ .map_err(map_host_device_oom_and_lost_err)+ }+}++impl Queue {+ pub fn raw_device(&self) -> &ash::Device {+ &self.device.raw+ }++ pub fn add_signal_semaphore(&self, semaphore: vk::Semaphore, semaphore_value: Option<u64>) {+ let mut guard = self.signal_semaphores.lock();+ if let Some(value) = semaphore_value {+ guard.push_signal(SemaphoreType::Timeline(semaphore, value));+ } else {+ guard.push_signal(SemaphoreType::Binary(semaphore));+ }+ }++ /// Remove `semaphore` from the pending signal list if it is still present.+ ///+ /// Returns `true` if the semaphore was found and removed. If the submit+ /// already consumed it, this is a harmless no-op that returns `false`.+ pub fn remove_signal_semaphore(&self, semaphore: vk::Semaphore) -> bool {+ self.signal_semaphores.lock().remove(semaphore)+ }++ /// Stage a semaphore wait on the next [`crate::Queue::submit`] call.+ ///+ /// `semaphore_value` selects the kind of payload the wait targets:+ ///+ /// - `Some(value)` - wait until `semaphore` (a timeline semaphore) has been signalled to at least `value`.+ /// - `None` - wait on a binary semaphore signal.+ ///+ /// `stage` is the pipeline stage at which the wait blocks downstream+ /// work (e.g. `vk::PipelineStageFlags::TOP_OF_PIPE` to gate the+ /// entire submission, or a more specific stage when only that stage+ /// reads the synchronised resource).+ pub fn add_wait_semaphore(+ &self,+ semaphore: vk::Semaphore,+ semaphore_value: Option<u64>,+ stage: vk::PipelineStageFlags,+ ) {+ let mut guard = self.wait_semaphores.lock();+ if let Some(value) = semaphore_value {+ guard.push_wait(SemaphoreType::Timeline(semaphore, value), stage);+ } else {+ guard.push_wait(SemaphoreType::Binary(semaphore), stage);+ }+ }++ /// Remove `semaphore` from the pending wait list if it is still present.+ ///+ /// Returns `true` if the semaphore was found and removed. If the submit+ /// already consumed it, this is a no-op that returns `false`.+ pub fn remove_wait_semaphore(&self, semaphore: vk::Semaphore) -> bool {+ self.wait_semaphores.lock().remove(semaphore)+ }+}++/// Maps+///+/// - VK_ERROR_OUT_OF_HOST_MEMORY+/// - VK_ERROR_OUT_OF_DEVICE_MEMORY+fn map_host_device_oom_err(err: vk::Result) -> crate::DeviceError {+ match err {+ vk::Result::ERROR_OUT_OF_HOST_MEMORY | vk::Result::ERROR_OUT_OF_DEVICE_MEMORY => {+ get_oom_err(err)+ }+ e => get_unexpected_err(e),+ }+}++/// Maps+///+/// - VK_ERROR_OUT_OF_HOST_MEMORY+/// - VK_ERROR_OUT_OF_DEVICE_MEMORY+/// - VK_ERROR_DEVICE_LOST+fn map_host_device_oom_and_lost_err(err: vk::Result) -> crate::DeviceError {+ match err {+ vk::Result::ERROR_DEVICE_LOST => get_lost_err(),+ other => map_host_device_oom_err(other),+ }+}++/// Maps+///+/// - VK_ERROR_OUT_OF_HOST_MEMORY+/// - VK_ERROR_OUT_OF_DEVICE_MEMORY+/// - VK_ERROR_FRAGMENTATION+fn map_host_device_oom_and_fragmentation_err(err: vk::Result) -> crate::DeviceError {+ match err {+ vk::Result::ERROR_FRAGMENTATION => get_oom_err(err),+ other => map_host_device_oom_err(other),+ }+}++/// Maps+///+/// - VK_ERROR_OUT_OF_HOST_MEMORY+/// - VK_ERROR_OUT_OF_DEVICE_MEMORY+/// - VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR+fn map_host_device_oom_and_ioca_err(err: vk::Result) -> crate::DeviceError {+ // We don't use VK_KHR_buffer_device_address+ // VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR+ map_host_device_oom_err(err)+}++/// Maps+///+/// - VK_ERROR_OUT_OF_HOST_MEMORY+fn map_host_oom_err(err: vk::Result) -> crate::DeviceError {+ match err {+ vk::Result::ERROR_OUT_OF_HOST_MEMORY => get_oom_err(err),+ e => get_unexpected_err(e),+ }+}++/// Maps+///+/// - VK_ERROR_OUT_OF_DEVICE_MEMORY+fn map_device_oom_err(err: vk::Result) -> crate::DeviceError {+ match err {+ vk::Result::ERROR_OUT_OF_DEVICE_MEMORY => get_oom_err(err),+ e => get_unexpected_err(e),+ }+}++/// Maps+///+/// - VK_ERROR_OUT_OF_HOST_MEMORY+/// - VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR+fn map_host_oom_and_ioca_err(err: vk::Result) -> crate::DeviceError {+ // We don't use VK_KHR_buffer_device_address+ // VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR+ map_host_oom_err(err)+}++/// Maps+///+/// - VK_ERROR_OUT_OF_HOST_MEMORY+/// - VK_ERROR_OUT_OF_DEVICE_MEMORY+/// - VK_PIPELINE_COMPILE_REQUIRED_EXT+/// - VK_ERROR_INVALID_SHADER_NV+fn map_pipeline_err(err: vk::Result) -> crate::DeviceError {+ // We don't use VK_EXT_pipeline_creation_cache_control+ // VK_PIPELINE_COMPILE_REQUIRED_EXT+ // We don't use VK_NV_glsl_shader+ // VK_ERROR_INVALID_SHADER_NV+ map_host_device_oom_err(err)+}++/// Returns [`crate::DeviceError::Unexpected`] or panics if the `internal_error_panic`+/// feature flag is enabled.+fn get_unexpected_err(_err: vk::Result) -> crate::DeviceError {+ #[cfg(feature = "internal_error_panic")]+ panic!("Unexpected Vulkan error: {_err:?}");++ #[allow(unreachable_code)]+ crate::DeviceError::Unexpected+}++/// Returns [`crate::DeviceError::OutOfMemory`].+fn get_oom_err(_err: vk::Result) -> crate::DeviceError {+ crate::DeviceError::OutOfMemory+}++/// Returns [`crate::DeviceError::Lost`] or panics if the `device_lost_panic`+/// feature flag is enabled.+fn get_lost_err() -> crate::DeviceError {+ #[cfg(feature = "device_lost_panic")]+ panic!("Device lost");++ #[allow(unreachable_code)]+ crate::DeviceError::Lost+}++#[derive(Clone, Copy, Pod, Zeroable)]+#[repr(C)]+struct RawTlasInstance {+ transform: [f32; 12],+ custom_data_and_mask: u32,+ shader_binding_table_record_offset_and_flags: u32,+ acceleration_structure_reference: u64,+}++/// Arguments to the [`CreateDeviceCallback`].+#[derive(Debug)]+pub struct CreateDeviceCallbackArgs<'arg, 'pnext, 'this>+where+ 'this: 'pnext,+{+ /// The extensions to enable for the device. You must not remove anything from this list,+ /// but you may add to it.+ pub extensions: &'arg mut Vec<&'static CStr>,+ /// The physical device features to enable. You may enable features, but must not disable any.+ pub device_features: &'arg mut PhysicalDeviceFeatures,+ /// The queue create infos for the device. You may add or modify queue create infos as needed.+ pub queue_create_infos: &'arg mut Vec<vk::DeviceQueueCreateInfo<'pnext>>,+ /// The create info for the device. You may add or modify things in the pnext chain, but+ /// do not turn features off. Additionally, do not add things to the list of extensions,+ /// or to the feature set, as all changes to that member will be overwritten.+ pub create_info: &'arg mut vk::DeviceCreateInfo<'pnext>,+ /// We need to have `'this` in the struct, so we can declare that all lifetimes coming from+ /// captures in the closure will live longer (and hence satisfy) `'pnext`. However, we+ /// don't actually directly use `'this`+ _phantom: PhantomData<&'this ()>,+}++/// Callback to allow changing the vulkan device creation parameters.+///+/// # Safety:+/// - If you want to add extensions, add the to the `Vec<'static CStr>` not the create info,+/// as the create info value will be overwritten.+/// - Callback must not remove features.+/// - Callback must not change anything to what the instance does not support.+pub type CreateDeviceCallback<'this> =+ dyn for<'arg, 'pnext> FnOnce(CreateDeviceCallbackArgs<'arg, 'pnext, 'this>) + 'this;++/// Arguments to the [`CreateInstanceCallback`].+#[expect(missing_debug_implementations, reason = "TODO?")]+pub struct CreateInstanceCallbackArgs<'arg, 'pnext, 'this>+where+ 'this: 'pnext,+{+ /// The extensions to enable for the instance. You must not remove anything from this list,+ /// but you may add to it.+ pub extensions: &'arg mut Vec<&'static CStr>,+ /// The create info for the instance. You may add or modify things in the pnext chain, but+ /// do not turn features off. Additionally, do not add things to the list of extensions,+ /// all changes to that member will be overwritten.+ pub create_info: &'arg mut vk::InstanceCreateInfo<'pnext>,+ /// Vulkan entry point.+ pub entry: &'arg ash::Entry,+ /// We need to have `'this` in the struct, so we can declare that all lifetimes coming from+ /// captures in the closure will live longer (and hence satisfy) `'pnext`. However, we+ /// don't actually directly use `'this`+ _phantom: PhantomData<&'this ()>,+}++/// Callback to allow changing the vulkan instance creation parameters.+///+/// # Safety:+/// - If you want to add extensions, add the to the `Vec<'static CStr>` not the create info,+/// as the create info value will be overwritten.+/// - Callback must not remove features.+/// - Callback must not change anything to what the instance does not support.+pub type CreateInstanceCallback<'this> =+ dyn for<'arg, 'pnext> FnOnce(CreateInstanceCallbackArgs<'arg, 'pnext, 'this>) + 'this;diff --git a/vendor/wgpu-hal/src/vulkan/sampler.rs b/vendor/wgpu-hal/src/vulkan/sampler.rsnew file mode 100644index 00000000..b99341d2--- /dev/null+++ b/vendor/wgpu-hal/src/vulkan/sampler.rs@@ -0,0 +1,177 @@+//! Sampler cache for Vulkan backend.+//!+//! Nearly identical to the DX12 sampler cache, without descriptor heap management.++use ash::vk;+use hashbrown::{hash_map::Entry, HashMap};+use ordered_float::OrderedFloat;++/// If the allowed sampler count is above this value, the sampler cache is disabled.+const ENABLE_SAMPLER_CACHE_CUTOFF: u32 = 1 << 20;++/// [`vk::SamplerCreateInfo`] is not hashable, so we wrap it in a newtype that is.+///+/// We use [`OrderedFloat`] to allow for floating point values to be compared and+/// hashed in a defined way.+#[derive(Copy, Clone)]+struct HashableSamplerCreateInfo(vk::SamplerCreateInfo<'static>);++impl PartialEq for HashableSamplerCreateInfo {+ fn eq(&self, other: &Self) -> bool {+ self.0.flags == other.0.flags+ && self.0.mag_filter == other.0.mag_filter+ && self.0.min_filter == other.0.min_filter+ && self.0.mipmap_mode == other.0.mipmap_mode+ && self.0.address_mode_u == other.0.address_mode_u+ && self.0.address_mode_v == other.0.address_mode_v+ && self.0.address_mode_w == other.0.address_mode_w+ && OrderedFloat(self.0.mip_lod_bias) == OrderedFloat(other.0.mip_lod_bias)+ && self.0.anisotropy_enable == other.0.anisotropy_enable+ && OrderedFloat(self.0.max_anisotropy) == OrderedFloat(other.0.max_anisotropy)+ && self.0.compare_enable == other.0.compare_enable+ && self.0.compare_op == other.0.compare_op+ && OrderedFloat(self.0.min_lod) == OrderedFloat(other.0.min_lod)+ && OrderedFloat(self.0.max_lod) == OrderedFloat(other.0.max_lod)+ && self.0.border_color == other.0.border_color+ && self.0.unnormalized_coordinates == other.0.unnormalized_coordinates+ }+}++impl Eq for HashableSamplerCreateInfo {}++impl core::hash::Hash for HashableSamplerCreateInfo {+ fn hash<H: core::hash::Hasher>(&self, state: &mut H) {+ self.0.flags.hash(state);+ self.0.mag_filter.hash(state);+ self.0.min_filter.hash(state);+ self.0.mipmap_mode.hash(state);+ self.0.address_mode_u.hash(state);+ self.0.address_mode_v.hash(state);+ self.0.address_mode_w.hash(state);+ OrderedFloat(self.0.mip_lod_bias).hash(state);+ self.0.anisotropy_enable.hash(state);+ OrderedFloat(self.0.max_anisotropy).hash(state);+ self.0.compare_enable.hash(state);+ self.0.compare_op.hash(state);+ OrderedFloat(self.0.min_lod).hash(state);+ OrderedFloat(self.0.max_lod).hash(state);+ self.0.border_color.hash(state);+ self.0.unnormalized_coordinates.hash(state);+ }+}++/// Entry in the sampler cache.+struct CacheEntry {+ sampler: vk::Sampler,+ ref_count: u32,+}++/// Global sampler cache.+///+/// As some devices have a low limit (4000) on the number of unique samplers that can be created,+/// we need to cache samplers to avoid running out if people eagerly create duplicate samplers.+pub(crate) struct SamplerCache {+ /// Mapping from the sampler description to sampler and reference count.+ samplers: HashMap<HashableSamplerCreateInfo, CacheEntry>,+ /// Maximum number of unique samplers that can be created.+ total_capacity: u32,+ /// If true, the sampler cache is disabled and all samplers are created on demand.+ passthrough: bool,+}++impl SamplerCache {+ pub fn new(total_capacity: u32) -> Self {+ let passthrough = total_capacity >= ENABLE_SAMPLER_CACHE_CUTOFF;+ Self {+ samplers: HashMap::new(),+ total_capacity,+ passthrough,+ }+ }++ /// Create a sampler, or return an existing one if it already exists.+ ///+ /// If the sampler already exists, the reference count is incremented.+ ///+ /// If the sampler does not exist, a new sampler is created and inserted into the cache.+ ///+ /// If the cache is full, an error is returned.+ pub fn create_sampler(+ &mut self,+ device: &ash::Device,+ create_info: vk::SamplerCreateInfo<'static>,+ ) -> Result<vk::Sampler, crate::DeviceError> {+ if self.passthrough {+ return unsafe { device.create_sampler(&create_info, None) }+ .map_err(super::map_host_device_oom_and_ioca_err);+ };++ // Get the number of used samplers. Needs to be done before to appease the borrow checker.+ let used_samplers = self.samplers.len();++ match self.samplers.entry(HashableSamplerCreateInfo(create_info)) {+ Entry::Occupied(occupied_entry) => {+ // We have found a match, so increment the refcount and return the index.+ let value = occupied_entry.into_mut();+ value.ref_count += 1;+ Ok(value.sampler)+ }+ Entry::Vacant(vacant_entry) => {+ // We need to create a new sampler.++ // We need to check if we can create more samplers.+ if used_samplers >= self.total_capacity as usize {+ log::error!("There is no more room in the global sampler heap for more unique samplers. Your device supports a maximum of {} unique samplers.", self.samplers.len());+ return Err(crate::DeviceError::OutOfMemory);+ }++ // Create the sampler.+ let sampler = unsafe { device.create_sampler(&create_info, None) }+ .map_err(super::map_host_device_oom_and_ioca_err)?;++ // Insert the new sampler into the mapping.+ vacant_entry.insert(CacheEntry {+ sampler,+ ref_count: 1,+ });++ Ok(sampler)+ }+ }+ }++ /// Decrease the reference count of a sampler and destroy it if the reference count reaches 0.+ ///+ /// The provided sampler is checked against the sampler in the cache to ensure there is no clerical error.+ pub fn destroy_sampler(+ &mut self,+ device: &ash::Device,+ create_info: vk::SamplerCreateInfo<'static>,+ provided_sampler: vk::Sampler,+ ) {+ if self.passthrough {+ unsafe { device.destroy_sampler(provided_sampler, None) };+ return;+ };++ let Entry::Occupied(mut hash_map_entry) =+ self.samplers.entry(HashableSamplerCreateInfo(create_info))+ else {+ log::error!("Trying to destroy a sampler that does not exist.");+ return;+ };+ let cache_entry = hash_map_entry.get_mut();++ assert_eq!(+ cache_entry.sampler, provided_sampler,+ "Provided sampler does not match the sampler in the cache."+ );++ cache_entry.ref_count -= 1;++ if cache_entry.ref_count == 0 {+ unsafe { device.destroy_sampler(cache_entry.sampler, None) };+ hash_map_entry.remove();+ }+ }+}diff --git a/vendor/wgpu-hal/src/vulkan/semaphore_list.rs b/vendor/wgpu-hal/src/vulkan/semaphore_list.rsnew file mode 100644index 00000000..045bfe84--- /dev/null+++ b/vendor/wgpu-hal/src/vulkan/semaphore_list.rs@@ -0,0 +1,218 @@+//! Definition of the [`SemaphoreList`] type.++use alloc::vec::Vec;+use ash::vk;+use core::mem::MaybeUninit;++#[derive(Debug, PartialEq)]+pub enum SemaphoreListMode {+ Wait,+ Signal,+}++/// A list of Vulkan semaphores to wait for or signal.+///+/// This represents a list of binary or timeline semaphores, together+/// with values for the timeline semaphores, and stage masks, if these+/// are used for waiting.+///+/// This type ensures that the array of semaphores to be signaled+/// stays aligned with the array of values for timeline semaphores+/// appearing in that list. The [`add_to_submit`] method prepares the+/// `vkQueueSubmit` arguments appropriately for whatever semaphores we+/// actually have.+///+/// [`add_to_submit`]: SemaphoreList::add_to_submit+#[derive(Debug)]+pub struct SemaphoreList {+ /// Mode of the semaphore list. Used for validation.+ mode: SemaphoreListMode,++ /// Semaphores to use.+ ///+ /// This can be a mix of binary and timeline semaphores.+ semaphores: Vec<vk::Semaphore>,++ /// Values for the timeline semaphores.+ ///+ /// If no timeline semaphores are present in [`semaphores`], this+ /// is empty. If any timeline semaphores are present, then this+ /// has the same length as [`semaphores`], with dummy !0 values+ /// in the elements corresponding to binary semaphores, since+ /// Vulkan ignores these.+ ///+ /// [`semaphores`]: Self::semaphores+ values: Vec<u64>,++ /// Stage masks for wait semaphores.+ ///+ /// This is only used if `mode` is `Wait`.+ pub stage_masks: Vec<vk::PipelineStageFlags>,+}++impl SemaphoreList {+ pub fn new(mode: SemaphoreListMode) -> Self {+ Self {+ mode,+ semaphores: Vec::new(),+ values: Vec::new(),+ stage_masks: Vec::new(),+ }+ }++ pub fn is_empty(&self) -> bool {+ self.semaphores.is_empty()+ }++ /// Add this list to the semaphores to be signalled by a `vkQueueSubmit` call.+ ///+ /// - Set `submit_info`'s `pSignalSemaphores` list to this list's+ /// semaphores.+ ///+ /// - If this list contains any timeline semaphores, then initialize+ /// `timeline_info`, set its `pSignalSemaphoreValues` to this+ /// list's values, and add it to `submit_info`s extension chain.+ ///+ /// Return the revised `submit_info` value.+ pub fn add_to_submit<'info, 'semaphores: 'info>(+ wait_semaphores: &'semaphores mut Self,+ signal_semaphores: &'semaphores mut Self,+ submit_info: vk::SubmitInfo<'info>,+ timeline_info: &'info mut MaybeUninit<vk::TimelineSemaphoreSubmitInfo<'info>>,+ ) -> vk::SubmitInfo<'info> {+ wait_semaphores.check();+ signal_semaphores.check();++ assert!(matches!(wait_semaphores.mode, SemaphoreListMode::Wait));+ assert!(matches!(signal_semaphores.mode, SemaphoreListMode::Signal));++ let timeline_info = timeline_info.write(vk::TimelineSemaphoreSubmitInfo::default());++ let mut uses_timeline = false;++ if !wait_semaphores.values.is_empty() {+ *timeline_info = timeline_info.wait_semaphore_values(&wait_semaphores.values);+ uses_timeline = true;+ }++ if !signal_semaphores.values.is_empty() {+ *timeline_info = timeline_info.signal_semaphore_values(&signal_semaphores.values);+ uses_timeline = true;+ }++ let mut submit_info = submit_info+ .wait_semaphores(&wait_semaphores.semaphores)+ .wait_dst_stage_mask(&wait_semaphores.stage_masks)+ .signal_semaphores(&signal_semaphores.semaphores);++ if uses_timeline {+ submit_info = submit_info.push_next(timeline_info);+ }++ submit_info+ }++ /// Add a semaphore to be signaled. Panics if this is a list of semaphores to wait.+ pub fn push_signal(&mut self, semaphore: SemaphoreType) {+ assert!(matches!(self.mode, SemaphoreListMode::Signal));+ self.push_inner(semaphore);+ }++ /// Add a semaphore to be waited for. Panics if this is a list of semaphores to signal.+ pub fn push_wait(&mut self, semaphore: SemaphoreType, stage: vk::PipelineStageFlags) {+ assert!(matches!(self.mode, SemaphoreListMode::Wait));++ self.stage_masks.push(stage);+ self.push_inner(semaphore);+ }++ fn push_inner(&mut self, semaphore: SemaphoreType) {+ match semaphore {+ SemaphoreType::Binary(semaphore) => {+ self.semaphores.push(semaphore);+ // Push a dummy value if necessary.+ if !self.values.is_empty() {+ self.values.push(!0);+ }+ }+ SemaphoreType::Timeline(semaphore, value) => {+ // We may be the first timeline semaphore, ensure that the values+ // array is filled with dummy values for existing binary semaphores.+ self.pad_values();+ self.semaphores.push(semaphore);+ self.values.push(value);+ }+ }++ self.check();+ }++ /// Remove all entries matching `semaphore` from the list. Returns `true`+ /// if anything was removed.+ pub fn remove(&mut self, semaphore: vk::Semaphore) -> bool {+ let mut removed = false;+ let mut i = 0;+ while i < self.semaphores.len() {+ if self.semaphores[i] == semaphore {+ self.semaphores.swap_remove(i);+ if !self.values.is_empty() {+ self.values.swap_remove(i);+ }+ if !self.stage_masks.is_empty() {+ self.stage_masks.swap_remove(i);+ }+ removed = true;+ } else {+ i += 1;+ }+ }+ self.check();+ removed+ }++ /// Append `other` to `self`, leaving `other` empty.+ pub fn append(&mut self, other: &mut Self) {+ assert_eq!(self.mode, other.mode);++ // If we're about to receive values, ensure we're aligned first.+ if !other.values.is_empty() {+ self.pad_values();+ }+ self.semaphores.append(&mut other.semaphores);+ self.values.append(&mut other.values);+ // If we had values, but `other` did not, re-align.+ if !self.values.is_empty() {+ self.pad_values();+ }+ self.stage_masks.append(&mut other.stage_masks);+ self.check();+ }++ /// Pad `self.values` with dummy values for binary semaphores,+ /// in preparation for adding a timeline semaphore value.+ ///+ /// This is a no-op if we already have values.+ fn pad_values(&mut self) {+ self.values.resize(self.semaphores.len(), !0);+ }++ #[track_caller]+ fn check(&self) {+ debug_assert!(self.values.is_empty() || self.values.len() == self.semaphores.len());+ match self.mode {+ SemaphoreListMode::Wait => {+ debug_assert!(+ self.stage_masks.is_empty() || self.stage_masks.len() == self.semaphores.len()+ );+ }+ SemaphoreListMode::Signal => {+ debug_assert!(self.stage_masks.is_empty());+ }+ }+ }+}++pub enum SemaphoreType {+ Binary(vk::Semaphore),+ Timeline(vk::Semaphore, u64),+}diff --git a/vendor/wgpu-hal/src/vulkan/swapchain/mod.rs b/vendor/wgpu-hal/src/vulkan/swapchain/mod.rsnew file mode 100644index 00000000..9be6002e--- /dev/null+++ b/vendor/wgpu-hal/src/vulkan/swapchain/mod.rs@@ -0,0 +1,119 @@+use alloc::boxed::Box;+use core::{any::Any, fmt::Debug, time::Duration};++use crate::vulkan::{semaphore_list::SemaphoreType, DeviceShared};++pub(super) use native::*;++mod native;++/// Win32 `HWND`, handed to a Win32 [`NativeSurface`] at construction so it can+/// build its [`DxgiHdrSource`](crate::auxil::dxgi::hdr::DxgiHdrSource) for the+/// display-HDR query. Borrowed from the app's window.+///+/// A newtype rather than a bare `isize` so the cfg lives in one place: off Windows+/// it is an uninhabited enum, so the non-Windows surface paths can't construct one+/// and the constructor signatures stay cfg-free.+#[cfg(windows)]+#[derive(Clone, Copy)]+pub(crate) struct WindowHandle(pub(crate) windows::Win32::Foundation::HWND);++#[cfg(not(windows))]+#[derive(Clone, Copy)]+pub(crate) enum WindowHandle {}++pub(super) trait Surface: Send + Sync + 'static {+ /// Returns the surface capabilities for the given adapter.+ ///+ /// Returns `None` if the surface is not compatible with the adapter.+ fn surface_capabilities(&self, adapter: &super::Adapter) -> Option<crate::SurfaceCapabilities>;++ /// Creates a swapchain for the surface with the given configuration.+ ///+ /// If this is not the first swapchain created for the surface, the old swapchain+ /// must be provided. [`Swapchain::release_resources`] must be called on the old swapchain+ /// before calling this method.+ unsafe fn create_swapchain(+ &self,+ device: &super::Device,+ config: &crate::SurfaceConfiguration,+ provided_old_swapchain: Option<Box<dyn Swapchain>>,+ ) -> Result<Box<dyn Swapchain>, crate::SurfaceError>;++ /// This surface's current display HDR info, if it can report it.+ ///+ /// `Some` only for Win32 surfaces (read through DXGI); `None` otherwise+ /// (Wayland / X11 / Android / Metal), which is the default.+ fn display_hdr_info(&self) -> Option<wgt::DisplayHdrInfo> {+ None+ }++ /// Allows downcasting to the concrete type.+ fn as_any(&self) -> &dyn Any;+}++pub(super) trait Swapchain: Send + Sync + 'static {+ /// Releases all resources associated with the swapchain, without+ /// destroying the swapchain itself. Must be called before calling+ /// either [`Surface::create_swapchain`] or dropping the swapchain.+ ///+ /// The swapchain must not be in use when this is called.+ unsafe fn release_resources(&mut self, device: &super::Device);++ /// Acquires the next available surface texture for rendering.+ ///+ /// `timeout` specifies the maximum time to wait for an image to become available.+ /// If `None` is specified, this function will wait indefinitely.+ ///+ /// Returns `Err(SurfaceError::Timeout)` if the timeout elapsed before an image became available.+ unsafe fn acquire(+ &mut self,+ timeout: Option<Duration>,+ fence: &super::Fence,+ ) -> Result<crate::AcquiredSurfaceTexture<crate::api::Vulkan>, crate::SurfaceError>;++ /// Tries to discard the acquired texture without presenting it.+ ///+ /// In practice, this doesn't really work in the current implementations.+ unsafe fn discard_texture(+ &mut self,+ texture: super::SurfaceTexture,+ ) -> Result<(), crate::SurfaceError>;++ /// Presents the given surface texture using the queue.+ unsafe fn present(+ &mut self,+ queue: &super::Queue,+ texture: crate::vulkan::SurfaceTexture,+ ) -> Result<(), crate::SurfaceError>;++ /// Allows downcasting to the concrete type.+ fn as_any(&self) -> &dyn Any;++ /// Allows downcasting to the concrete type mutably.+ fn as_any_mut(&mut self) -> &mut dyn Any;+}++/// Swapchain specific metadata associated with a surface texture.+pub(super) trait SurfaceTextureMetadata: Debug + Send + Sync + 'static {+ /// Returns a guard which can yield the semaphores needed for submission using this swapchain texture.+ fn get_semaphore_guard(&self) -> Box<dyn SwapchainSubmissionSemaphoreGuard + '_>;++ /// Allows downcasting to the concrete type.+ fn as_any(&self) -> &dyn Any;+}++/// Guard type for managing swapchain submission semaphores.+pub(super) trait SwapchainSubmissionSemaphoreGuard {+ /// Sets the Fence value for this submission.+ fn set_used_fence_value(&mut self, value: u64);++ /// Gets semaphores to wait on before doing GPU work for this swapchain texture.+ fn get_acquire_wait_semaphore(&mut self) -> Option<SemaphoreType>;++ /// Gets the semaphore to signal when GPU work for this swapchain texture is complete.+ fn get_submit_signal_semaphore(+ &mut self,+ device: &DeviceShared,+ ) -> Result<SemaphoreType, crate::DeviceError>;+}diff --git a/vendor/wgpu-hal/src/vulkan/swapchain/native.rs b/vendor/wgpu-hal/src/vulkan/swapchain/native.rsnew file mode 100644index 00000000..218b9ef1--- /dev/null+++ b/vendor/wgpu-hal/src/vulkan/swapchain/native.rs@@ -0,0 +1,937 @@+//! Vulkan Surface and Swapchain implementation using native Vulkan surfaces.++use alloc::{boxed::Box, sync::Arc, vec::Vec};+use core::any::Any;++use ash::{khr, vk};+use parking_lot::{Mutex, MutexGuard};++use crate::vulkan::{+ conv, map_host_device_oom_and_lost_err,+ semaphore_list::SemaphoreType,+ swapchain::{+ Surface, SurfaceTextureMetadata, Swapchain, SwapchainSubmissionSemaphoreGuard, WindowHandle,+ },+ DeviceShared, InstanceShared,+};++pub(crate) struct NativeSurface {+ raw: vk::SurfaceKHR,+ functor: khr::surface::Instance,+ instance: Arc<InstanceShared>,+ /// Built from the window's `HWND` (Windows only) to answer the display-HDR+ /// query; `None` for non-Win32 surfaces.+ #[cfg(windows)]+ hdr_source: Option<crate::auxil::dxgi::hdr::DxgiHdrSource>,+}++impl NativeSurface {+ pub fn from_vk_surface_khr(+ instance: &crate::vulkan::Instance,+ raw: vk::SurfaceKHR,+ hwnd: Option<WindowHandle>,+ ) -> Self {+ #[cfg(not(windows))]+ let _ = hwnd;+ let functor = khr::surface::Instance::new(&instance.shared.entry, &instance.shared.raw);+ Self {+ raw,+ functor,+ instance: Arc::clone(&instance.shared),+ #[cfg(windows)]+ hdr_source: hwnd.map(|wh| crate::auxil::dxgi::hdr::DxgiHdrSource::new(wh.0)),+ }+ }++ pub fn as_raw(&self) -> vk::SurfaceKHR {+ self.raw+ }+}++impl Drop for NativeSurface {+ fn drop(&mut self) {+ unsafe {+ self.functor.destroy_surface(self.raw, None);+ }+ }+}++impl Surface for NativeSurface {+ fn surface_capabilities(+ &self,+ adapter: &crate::vulkan::Adapter,+ ) -> Option<crate::SurfaceCapabilities> {+ if !adapter.private_caps.can_present {+ return None;+ }+ let queue_family_index = 0; //TODO+ {+ profiling::scope!("vkGetPhysicalDeviceSurfaceSupportKHR");+ match unsafe {+ self.functor.get_physical_device_surface_support(+ adapter.raw,+ queue_family_index,+ self.raw,+ )+ } {+ Ok(true) => (),+ Ok(false) => return None,+ Err(e) => {+ log::error!("get_physical_device_surface_support: {e}");+ return None;+ }+ }+ }++ let caps = {+ profiling::scope!("vkGetPhysicalDeviceSurfaceCapabilitiesKHR");+ match unsafe {+ self.functor+ .get_physical_device_surface_capabilities(adapter.raw, self.raw)+ } {+ Ok(caps) => caps,+ Err(e) => {+ log::error!("get_physical_device_surface_capabilities: {e}");+ return None;+ }+ }+ };++ // If image count is 0, the support number of images is unlimited.+ let max_image_count = if caps.max_image_count == 0 {+ !0+ } else {+ caps.max_image_count+ };++ // `0xFFFFFFFF` indicates that the extent depends on the created swapchain.+ let current_extent = if caps.current_extent.width != !0 && caps.current_extent.height != !0+ {+ Some(wgt::Extent3d {+ width: caps.current_extent.width,+ height: caps.current_extent.height,+ depth_or_array_layers: 1,+ })+ } else {+ None+ };++ let raw_present_modes = {+ profiling::scope!("vkGetPhysicalDeviceSurfacePresentModesKHR");+ match unsafe {+ self.functor+ .get_physical_device_surface_present_modes(adapter.raw, self.raw)+ } {+ Ok(present_modes) => present_modes,+ Err(e) => {+ log::error!("get_physical_device_surface_present_modes: {e}");+ // Per definition of `SurfaceCapabilities`, there must be at least one present mode.+ return None;+ }+ }+ };++ let raw_surface_formats = {+ profiling::scope!("vkGetPhysicalDeviceSurfaceFormatsKHR");+ match unsafe {+ self.functor+ .get_physical_device_surface_formats(adapter.raw, self.raw)+ } {+ Ok(formats) => formats,+ Err(e) => {+ log::error!("get_physical_device_surface_formats: {e}");+ // Per definition of `SurfaceCapabilities`, there must be at least one present format.+ return None;+ }+ }+ };++ // Group the driver's (format, color space) pairs into one entry per+ // format, preserving the driver's format order.+ let mut formats: Vec<wgt::SurfaceFormatCapabilities> = Vec::new();+ for (format, color_space) in raw_surface_formats+ .into_iter()+ .filter_map(conv::map_vk_surface_formats)+ {+ let color_spaces = color_space.to_color_spaces().unwrap();+ match formats.iter_mut().find(|fc| fc.format == format) {+ Some(fc) => fc.color_spaces |= color_spaces,+ None => formats.push(wgt::SurfaceFormatCapabilities {+ format,+ color_spaces,+ }),+ }+ }+ Some(crate::SurfaceCapabilities {+ formats,+ // TODO: Right now we're always truncating the swap chain+ // (presumably - we're actually setting the min image count which isn't necessarily the swap chain size)+ // Instead, we should use extensions when available to wait in present.+ // See https://github.com/gfx-rs/wgpu/issues/2869+ maximum_frame_latency: (caps.min_image_count - 1)..=(max_image_count - 1), // Note this can't underflow since both `min_image_count` is at least one and we already patched `max_image_count`.+ current_extent,+ usage: conv::map_vk_image_usage(caps.supported_usage_flags),+ present_modes: raw_present_modes+ .into_iter()+ .flat_map(conv::map_vk_present_mode)+ .collect(),+ composite_alpha_modes: conv::map_vk_composite_alpha(caps.supported_composite_alpha),+ })+ }++ unsafe fn create_swapchain(+ &self,+ device: &crate::vulkan::Device,+ config: &crate::SurfaceConfiguration,+ provided_old_swapchain: Option<Box<dyn Swapchain>>,+ ) -> Result<Box<dyn Swapchain>, crate::SurfaceError> {+ profiling::scope!("Device::create_swapchain");+ let functor = khr::swapchain::Device::new(&self.instance.raw, &device.shared.raw);++ let old_swapchain = provided_old_swapchain+ .as_ref()+ .map(|osc| osc.as_any().downcast_ref::<NativeSwapchain>().unwrap().raw)+ .unwrap_or(vk::SwapchainKHR::null());++ let color_space = conv::map_surface_color_space(config.color_space);++ let original_format = device.shared.private_caps.map_texture_format(config.format);+ let mut raw_flags = vk::SwapchainCreateFlagsKHR::empty();+ let mut raw_view_formats: Vec<vk::Format> = vec![];+ if !config.view_formats.is_empty() {+ raw_flags |= vk::SwapchainCreateFlagsKHR::MUTABLE_FORMAT;+ raw_view_formats = config+ .view_formats+ .iter()+ .map(|f| device.shared.private_caps.map_texture_format(*f))+ .collect();+ raw_view_formats.push(original_format);+ }++ let mut info = vk::SwapchainCreateInfoKHR::default()+ .flags(raw_flags)+ .surface(self.raw)+ .min_image_count(config.maximum_frame_latency + 1) // TODO: https://github.com/gfx-rs/wgpu/issues/2869+ .image_format(original_format)+ .image_color_space(color_space)+ .image_extent(vk::Extent2D {+ width: config.extent.width,+ height: config.extent.height,+ })+ .image_array_layers(config.extent.depth_or_array_layers)+ .image_usage(conv::map_texture_usage(config.usage))+ .image_sharing_mode(vk::SharingMode::EXCLUSIVE)+ .pre_transform(vk::SurfaceTransformFlagsKHR::IDENTITY)+ .composite_alpha(conv::map_composite_alpha_mode(config.composite_alpha_mode))+ .present_mode(conv::map_present_mode(config.present_mode))+ .clipped(true)+ .old_swapchain(old_swapchain);++ let mut format_list_info = vk::ImageFormatListCreateInfo::default();+ if !raw_view_formats.is_empty() {+ format_list_info = format_list_info.view_formats(&raw_view_formats);+ info = info.push_next(&mut format_list_info);+ }++ let result = {+ profiling::scope!("vkCreateSwapchainKHR");+ unsafe { functor.create_swapchain(&info, None) }+ };++ let raw = match result {+ Ok(swapchain) => swapchain,+ Err(error) => {+ return Err(match error {+ vk::Result::ERROR_SURFACE_LOST_KHR+ | vk::Result::ERROR_INITIALIZATION_FAILED => crate::SurfaceError::Lost,+ vk::Result::ERROR_NATIVE_WINDOW_IN_USE_KHR => {+ crate::SurfaceError::Other("Native window is in use")+ }+ // We don't use VK_EXT_image_compression_control+ // VK_ERROR_COMPRESSION_EXHAUSTED_EXT+ other => map_host_device_oom_and_lost_err(other).into(),+ });+ }+ };++ let images = unsafe { functor.get_swapchain_images(raw) }+ .map_err(crate::vulkan::map_host_device_oom_err)?;++ // This fence is only used to throttle acquisition on Windows. It is very important to+ // avoid bad frame pacing when the Vulkan driver is using a DXGI swapchain. See+ // https://github.com/gfx-rs/wgpu/issues/8310 and+ // https://github.com/gfx-rs/wgpu/issues/8354 for more details.+ let fence = if cfg!(target_os = "windows") {+ let raw = unsafe {+ device+ .shared+ .raw+ .create_fence(&vk::FenceCreateInfo::default(), None)+ .map_err(crate::vulkan::map_host_device_oom_err)?+ };+ Some(raw)+ } else {+ None+ };++ // NOTE: It's important that we define the same number of acquire/present semaphores+ // as we will need to index into them with the image index.+ let acquire_semaphores = (0..images.len())+ .map(|i| {+ SwapchainAcquireSemaphore::new(&device.shared, i)+ .map(Mutex::new)+ .map(Arc::new)+ })+ .collect::<Result<Vec<_>, _>>()?;++ let present_semaphores = (0..images.len())+ .map(|i| Arc::new(Mutex::new(SwapchainPresentSemaphores::new(i))))+ .collect::<Vec<_>>();++ Ok(Box::new(NativeSwapchain {+ raw,+ functor,+ device: Arc::clone(&device.shared),+ images,+ fence,+ config: config.clone(),+ acquire_semaphores,+ next_acquire_index: 0,+ present_semaphores,+ next_present_time: None,+ }))+ }++ #[cfg(windows)]+ fn display_hdr_info(&self) -> Option<wgt::DisplayHdrInfo> {+ self.hdr_source.as_ref()?.display_hdr_info()+ }++ fn as_any(&self) -> &dyn Any {+ self+ }+}++pub(crate) struct NativeSwapchain {+ raw: vk::SwapchainKHR,+ functor: khr::swapchain::Device,+ device: Arc<DeviceShared>,+ images: Vec<vk::Image>,+ /// Fence used to wait on the acquired image.+ fence: Option<vk::Fence>,+ config: crate::SurfaceConfiguration,++ /// Semaphores used between image acquisition and the first submission+ /// that uses that image. This is indexed using [`next_acquire_index`].+ ///+ /// Because we need to provide this to [`vkAcquireNextImageKHR`], we haven't+ /// received the swapchain image index for the frame yet, so we cannot use+ /// that to index it.+ ///+ /// Before we pass this to [`vkAcquireNextImageKHR`], we ensure that we wait on+ /// the submission indicated by [`previously_used_submission_index`]. This ensures+ /// the semaphore is no longer in use before we use it.+ ///+ /// [`next_acquire_index`]: NativeSwapchain::next_acquire_index+ /// [`vkAcquireNextImageKHR`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkAcquireNextImageKHR+ /// [`previously_used_submission_index`]: SwapchainAcquireSemaphore::previously_used_submission_index+ acquire_semaphores: Vec<Arc<Mutex<SwapchainAcquireSemaphore>>>,+ /// The index of the next acquire semaphore to use.+ ///+ /// This is incremented each time we acquire a new image, and wraps around+ /// to 0 when it reaches the end of [`acquire_semaphores`].+ ///+ /// [`acquire_semaphores`]: NativeSwapchain::acquire_semaphores+ next_acquire_index: usize,++ /// Semaphore sets used between all submissions that write to an image and+ /// the presentation of that image.+ ///+ /// This is indexed by the swapchain image index returned by+ /// [`vkAcquireNextImageKHR`].+ ///+ /// We know it is safe to use these semaphores because use them+ /// _after_ the acquire semaphore. Because the acquire semaphore+ /// has been signaled, the previous presentation using that image+ /// is known-finished, so this semaphore is no longer in use.+ ///+ /// [`vkAcquireNextImageKHR`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkAcquireNextImageKHR+ present_semaphores: Vec<Arc<Mutex<SwapchainPresentSemaphores>>>,++ /// The present timing information which will be set in the next call to [`present()`](crate::Queue::present()).+ ///+ /// # Safety+ ///+ /// This must only be set if [`wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING`] is enabled, and+ /// so the VK_GOOGLE_display_timing extension is present.+ next_present_time: Option<vk::PresentTimeGOOGLE>,+}++impl Drop for NativeSwapchain {+ fn drop(&mut self) {+ unsafe {+ self.functor.destroy_swapchain(self.raw, None);+ }+ }+}++impl Swapchain for NativeSwapchain {+ unsafe fn release_resources(&mut self, device: &crate::vulkan::Device) {+ profiling::scope!("Swapchain::release_resources");+ {+ profiling::scope!("vkDeviceWaitIdle");+ // We need to also wait until all presentation work is done. Because there is no way to portably wait until+ // the presentation work is done, we are forced to wait until the device is idle.+ let _ = unsafe {+ device+ .shared+ .raw+ .device_wait_idle()+ .map_err(map_host_device_oom_and_lost_err)+ };+ };++ if let Some(fence) = self.fence {+ unsafe { device.shared.raw.destroy_fence(fence, None) }+ }++ // We cannot take this by value, as the function returns `self`.+ for semaphore in self.acquire_semaphores.drain(..) {+ let arc_removed = Arc::into_inner(semaphore).expect(+ "Trying to destroy a SwapchainAcquireSemaphore that is still in use by a SurfaceTexture",+ );+ let mutex_removed = arc_removed.into_inner();++ unsafe { mutex_removed.destroy(&device.shared.raw) };+ }++ for semaphore in self.present_semaphores.drain(..) {+ let arc_removed = Arc::into_inner(semaphore).expect(+ "Trying to destroy a SwapchainPresentSemaphores that is still in use by a SurfaceTexture",+ );+ let mutex_removed = arc_removed.into_inner();++ unsafe { mutex_removed.destroy(&device.shared.raw) };+ }+ }++ unsafe fn acquire(+ &mut self,+ timeout: Option<core::time::Duration>,+ fence: &crate::vulkan::Fence,+ ) -> Result<crate::AcquiredSurfaceTexture<crate::api::Vulkan>, crate::SurfaceError> {+ let mut timeout_ns = match timeout {+ Some(duration) => duration.as_nanos() as u64,+ None => u64::MAX,+ };++ // AcquireNextImageKHR on Android (prior to Android 11) doesn't support timeouts+ // and will also log verbose warnings if tying to use a timeout.+ //+ // Android 10 implementation for reference:+ // https://android.googlesource.com/platform/frameworks/native/+/refs/tags/android-mainline-10.0.0_r13/vulkan/libvulkan/swapchain.cpp#1426+ // Android 11 implementation for reference:+ // https://android.googlesource.com/platform/frameworks/native/+/refs/tags/android-mainline-11.0.0_r45/vulkan/libvulkan/swapchain.cpp#1438+ //+ // Android 11 corresponds to an SDK_INT/ro.build.version.sdk of 30+ if cfg!(target_os = "android") && self.device.instance.android_sdk_version < 30 {+ timeout_ns = u64::MAX;+ }++ let acquire_semaphore_arc = self.get_acquire_semaphore();+ // Nothing should be using this, so we don't block, but panic if we fail to lock.+ let acquire_semaphore_guard = acquire_semaphore_arc+ .try_lock()+ .expect("Failed to lock a SwapchainSemaphores.");++ // Wait for all commands writing to the previously acquired image to+ // complete.+ //+ // Almost all the steps in the usual acquire-draw-present flow are+ // asynchronous: they get something started on the presentation engine+ // or the GPU, but on the CPU, control returns immediately. Without some+ // sort of intervention, the CPU could crank out frames much faster than+ // the presentation engine can display them.+ //+ // This is the intervention: if any submissions drew on this image, and+ // thus waited for `locked_swapchain_semaphores.acquire`, wait for all+ // of them to finish, thus ensuring that it's okay to pass `acquire` to+ // `vkAcquireNextImageKHR` again.+ let completed = self.device.wait_for_fence(+ fence,+ acquire_semaphore_guard.previously_used_submission_index,+ timeout_ns,+ )?;+ if !completed {+ return Err(crate::SurfaceError::Timeout);+ }++ let acquire_fence = self.fence.unwrap_or_else(vk::Fence::null);++ // will block if no image is available+ let (index, suboptimal) = match unsafe {+ profiling::scope!("vkAcquireNextImageKHR");+ self.functor.acquire_next_image(+ self.raw,+ timeout_ns,+ acquire_semaphore_guard.acquire,+ acquire_fence,+ )+ } {+ // We treat `VK_SUBOPTIMAL_KHR` as `VK_SUCCESS` on Android.+ // See the comment in `Queue::present`.+ #[cfg(target_os = "android")]+ Ok((index, _)) => (index, false),+ #[cfg(not(target_os = "android"))]+ Ok(pair) => pair,+ Err(error) => {+ return match error {+ vk::Result::TIMEOUT => Err(crate::SurfaceError::Timeout),+ vk::Result::NOT_READY | vk::Result::ERROR_OUT_OF_DATE_KHR => {+ Err(crate::SurfaceError::Outdated)+ }+ vk::Result::ERROR_SURFACE_LOST_KHR => Err(crate::SurfaceError::Lost),+ // We don't use VK_EXT_full_screen_exclusive+ // VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT+ other => Err(map_host_device_oom_and_lost_err(other).into()),+ };+ }+ };++ if let Some(fence) = self.fence {+ unsafe {+ // The `wait_all` argument must be `true` to avoid crash on some Android devices. See https://github.com/gfx-rs/wgpu/pull/8769+ self.device+ .raw+ .wait_for_fences(&[fence], true, timeout_ns)+ .map_err(map_host_device_oom_and_lost_err)?;++ self.device+ .raw+ .reset_fences(&[fence])+ .map_err(map_host_device_oom_and_lost_err)?;+ }+ }++ drop(acquire_semaphore_guard);+ // We only advance the surface semaphores if we successfully acquired an image, otherwise+ // we should try to re-acquire using the same semaphores.+ self.advance_acquire_semaphore();++ let present_semaphore_arc = self.get_present_semaphores(index);++ // special case for Intel Vulkan returning bizarre values (ugh)+ if self.device.vendor_id == crate::auxil::db::intel::VENDOR && index > 0x100 {+ return Err(crate::SurfaceError::Outdated);+ }++ let identity = self.device.texture_identity_factory.next();++ let texture = crate::vulkan::SurfaceTexture {+ index,+ texture: crate::vulkan::Texture {+ raw: self.images[index as usize],+ drop_guard: None,+ memory: crate::vulkan::TextureMemory::External,+ format: self.config.format,+ copy_size: crate::CopyExtent {+ width: self.config.extent.width,+ height: self.config.extent.height,+ depth: 1,+ },+ identity,+ },+ metadata: Box::new(NativeSurfaceTextureMetadata {+ acquire_semaphores: acquire_semaphore_arc,+ present_semaphores: present_semaphore_arc,+ }),+ };+ Ok(crate::AcquiredSurfaceTexture {+ texture,+ suboptimal,+ })+ }++ unsafe fn discard_texture(+ &mut self,+ _texture: crate::vulkan::SurfaceTexture,+ ) -> Result<(), crate::SurfaceError> {+ // TODO: Current implementation no-ops+ Ok(())+ }++ unsafe fn present(+ &mut self,+ queue: &crate::vulkan::Queue,+ texture: crate::vulkan::SurfaceTexture,+ ) -> Result<(), crate::SurfaceError> {+ let metadata = texture+ .metadata+ .as_any()+ .downcast_ref::<NativeSurfaceTextureMetadata>()+ .unwrap();+ let mut acquire_semaphore = metadata.acquire_semaphores.lock();+ let mut present_semaphores = metadata.present_semaphores.lock();++ let wait_semaphores = present_semaphores.get_present_wait_semaphores();++ // Reset the acquire and present semaphores internal state+ // to be ready for the next frame.+ //+ // We do this before the actual call to present to ensure that+ // even if this method errors and early outs, we have reset+ // the state for next frame.+ acquire_semaphore.end_semaphore_usage();+ present_semaphores.end_semaphore_usage();++ drop(acquire_semaphore);++ let swapchains = [self.raw];+ let image_indices = [texture.index];+ let vk_info = vk::PresentInfoKHR::default()+ .swapchains(&swapchains)+ .image_indices(&image_indices)+ .wait_semaphores(&wait_semaphores);++ let mut display_timing;+ let present_times;+ let vk_info = if let Some(present_time) = self.next_present_time.take() {+ debug_assert!(+ self.device+ .features+ .contains(wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING),+ "`next_present_time` should only be set if `VULKAN_GOOGLE_DISPLAY_TIMING` is enabled"+ );+ present_times = [present_time];+ display_timing = vk::PresentTimesInfoGOOGLE::default().times(&present_times);+ // SAFETY: We know that VK_GOOGLE_display_timing is present because of the safety contract on `next_present_time`.+ vk_info.push_next(&mut display_timing)+ } else {+ vk_info+ };++ let suboptimal = {+ profiling::scope!("vkQueuePresentKHR");+ unsafe { self.functor.queue_present(queue.raw, &vk_info) }.map_err(|error| {+ match error {+ vk::Result::ERROR_OUT_OF_DATE_KHR => crate::SurfaceError::Outdated,+ vk::Result::ERROR_SURFACE_LOST_KHR => crate::SurfaceError::Lost,+ // We don't use VK_EXT_full_screen_exclusive+ // VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT+ _ => map_host_device_oom_and_lost_err(error).into(),+ }+ })?+ };+ if suboptimal {+ // We treat `VK_SUBOPTIMAL_KHR` as `VK_SUCCESS` on Android.+ // On Android 10+, libvulkan's `vkQueuePresentKHR` implementation returns `VK_SUBOPTIMAL_KHR` if not doing pre-rotation+ // (i.e `VkSwapchainCreateInfoKHR::preTransform` not being equal to the current device orientation).+ // This is always the case when the device orientation is anything other than the identity one, as we unconditionally use `VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR`.+ #[cfg(not(target_os = "android"))]+ log::debug!("Suboptimal present of frame {}", texture.index);+ }+ Ok(())+ }++ fn as_any(&self) -> &dyn Any {+ self+ }++ fn as_any_mut(&mut self) -> &mut dyn Any {+ self+ }+}++impl NativeSwapchain {+ pub(crate) fn as_raw(&self) -> vk::SwapchainKHR {+ self.raw+ }++ pub fn set_next_present_time(&mut self, present_timing: vk::PresentTimeGOOGLE) {+ let features = wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING;+ if self.device.features.contains(features) {+ self.next_present_time = Some(present_timing);+ } else {+ // Ideally we'd use something like `device.required_features` here, but that's in `wgpu-core`, which we are a dependency of+ panic!(+ concat!(+ "Tried to set display timing properties ",+ "without the corresponding feature ({:?}) enabled."+ ),+ features+ );+ }+ }++ /// Mark the current frame finished, advancing to the next acquire semaphore.+ fn advance_acquire_semaphore(&mut self) {+ let semaphore_count = self.acquire_semaphores.len();+ self.next_acquire_index = (self.next_acquire_index + 1) % semaphore_count;+ }++ /// Get the next acquire semaphore that should be used with this swapchain.+ fn get_acquire_semaphore(&self) -> Arc<Mutex<SwapchainAcquireSemaphore>> {+ self.acquire_semaphores[self.next_acquire_index].clone()+ }++ /// Get the set of present semaphores that should be used with the given image index.+ fn get_present_semaphores(&self, index: u32) -> Arc<Mutex<SwapchainPresentSemaphores>> {+ self.present_semaphores[index as usize].clone()+ }+}++/// Semaphore used to acquire a swapchain image.+#[derive(Debug)]+struct SwapchainAcquireSemaphore {+ /// A semaphore that is signaled when this image is safe for us to modify.+ ///+ /// When [`vkAcquireNextImageKHR`] returns the index of the next swapchain+ /// image that we should use, that image may actually still be in use by the+ /// presentation engine, and is not yet safe to modify. However, that+ /// function does accept a semaphore that it will signal when the image is+ /// indeed safe to begin messing with.+ ///+ /// This semaphore is:+ ///+ /// - waited for by the first queue submission to operate on this image+ /// since it was acquired, and+ ///+ /// - signaled by [`vkAcquireNextImageKHR`] when the acquired image is ready+ /// for us to use.+ ///+ /// [`vkAcquireNextImageKHR`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkAcquireNextImageKHR+ acquire: vk::Semaphore,++ /// True if the next command submission operating on this image should wait+ /// for [`acquire`].+ ///+ /// We must wait for `acquire` before drawing to this swapchain image, but+ /// because `wgpu-hal` queue submissions are always strongly ordered, only+ /// the first submission that works with a swapchain image actually needs to+ /// wait. We set this flag when this image is acquired, and clear it the+ /// first time it's passed to [`Queue::submit`] as a surface texture.+ ///+ /// Additionally, semaphores can only be waited on once, so we need to ensure+ /// that we only actually pass this semaphore to the first submission that+ /// uses that image.+ ///+ /// [`acquire`]: SwapchainAcquireSemaphore::acquire+ /// [`Queue::submit`]: crate::Queue::submit+ should_wait_for_acquire: bool,++ /// The fence value of the last command submission that wrote to this image.+ ///+ /// The next time we try to acquire this image, we'll block until+ /// this submission finishes, proving that [`acquire`] is ready to+ /// pass to `vkAcquireNextImageKHR` again.+ ///+ /// [`acquire`]: SwapchainAcquireSemaphore::acquire+ previously_used_submission_index: crate::FenceValue,+}++impl SwapchainAcquireSemaphore {+ fn new(device: &DeviceShared, index: usize) -> Result<Self, crate::DeviceError> {+ Ok(Self {+ acquire: device+ .new_binary_semaphore(&format!("SwapchainImageSemaphore: Index {index} acquire"))?,+ should_wait_for_acquire: true,+ previously_used_submission_index: 0,+ })+ }++ /// Sets the fence value which the next acquire will wait for. This prevents+ /// the semaphore from being used while the previous submission is still in flight.+ fn set_used_fence_value(&mut self, value: crate::FenceValue) {+ self.previously_used_submission_index = value;+ }++ /// Return the semaphore that commands drawing to this image should wait for, if any.+ ///+ /// This only returns `Some` once per acquisition; see+ /// [`SwapchainAcquireSemaphore::should_wait_for_acquire`] for details.+ fn get_acquire_wait_semaphore(&mut self) -> Option<vk::Semaphore> {+ if self.should_wait_for_acquire {+ self.should_wait_for_acquire = false;+ Some(self.acquire)+ } else {+ None+ }+ }++ /// Indicates the cpu-side usage of this semaphore has finished for the frame,+ /// so reset internal state to be ready for the next frame.+ fn end_semaphore_usage(&mut self) {+ // Reset the acquire semaphore, so that the next time we acquire this+ // image, we can wait for it again.+ self.should_wait_for_acquire = true;+ }++ unsafe fn destroy(&self, device: &ash::Device) {+ unsafe {+ device.destroy_semaphore(self.acquire, None);+ }+ }+}++#[derive(Debug)]+struct SwapchainPresentSemaphores {+ /// A pool of semaphores for ordering presentation after drawing.+ ///+ /// The first [`present_index`] semaphores in this vector are:+ ///+ /// - all waited on by the call to [`vkQueuePresentKHR`] that presents this+ /// image, and+ ///+ /// - each signaled by some [`vkQueueSubmit`] queue submission that draws to+ /// this image, when the submission finishes execution.+ ///+ /// This vector accumulates one semaphore per submission that writes to this+ /// image. This is awkward, but hard to avoid: [`vkQueuePresentKHR`]+ /// requires a semaphore to order it with respect to drawing commands, and+ /// we can't attach new completion semaphores to a command submission after+ /// it's been submitted. This means that, at submission time, we must create+ /// the semaphore we might need if the caller's next action is to enqueue a+ /// presentation of this image.+ ///+ /// An alternative strategy would be for presentation to enqueue an empty+ /// submit, ordered relative to other submits in the usual way, and+ /// signaling a single presentation semaphore. But we suspect that submits+ /// are usually expensive enough, and semaphores usually cheap enough, that+ /// performance-sensitive users will avoid making many submits, so that the+ /// cost of accumulated semaphores will usually be less than the cost of an+ /// additional submit.+ ///+ /// Only the first [`present_index`] semaphores in the vector are actually+ /// going to be signalled by submitted commands, and need to be waited for+ /// by the next present call. Any semaphores beyond that index were created+ /// for prior presents and are simply being retained for recycling.+ ///+ /// [`present_index`]: SwapchainPresentSemaphores::present_index+ /// [`vkQueuePresentKHR`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkQueuePresentKHR+ /// [`vkQueueSubmit`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkQueueSubmit+ present: Vec<vk::Semaphore>,++ /// The number of semaphores in [`present`] to be signalled for this submission.+ ///+ /// [`present`]: SwapchainPresentSemaphores::present+ present_index: usize,++ /// Which image this semaphore set is used for.+ frame_index: usize,+}++impl SwapchainPresentSemaphores {+ pub fn new(frame_index: usize) -> Self {+ Self {+ present: Vec::new(),+ present_index: 0,+ frame_index,+ }+ }++ /// Return the semaphore that the next submission that writes to this image should+ /// signal when it's done.+ ///+ /// See [`SwapchainPresentSemaphores::present`] for details.+ fn get_submit_signal_semaphore(+ &mut self,+ device: &DeviceShared,+ ) -> Result<vk::Semaphore, crate::DeviceError> {+ // Try to recycle a semaphore we created for a previous presentation.+ let sem = match self.present.get(self.present_index) {+ Some(sem) => *sem,+ None => {+ let sem = device.new_binary_semaphore(&format!(+ "SwapchainImageSemaphore: Image {} present semaphore {}",+ self.frame_index, self.present_index+ ))?;+ self.present.push(sem);+ sem+ }+ };++ self.present_index += 1;++ Ok(sem)+ }++ /// Indicates the cpu-side usage of this semaphore has finished for the frame,+ /// so reset internal state to be ready for the next frame.+ fn end_semaphore_usage(&mut self) {+ // Reset the index to 0, so that the next time we get a semaphore, we+ // start from the beginning of the list.+ self.present_index = 0;+ }++ /// Return the semaphores that a presentation of this image should wait on.+ ///+ /// Return a slice of semaphores that the call to [`vkQueueSubmit`] that+ /// ends this image's acquisition should wait for. See+ /// [`SwapchainPresentSemaphores::present`] for details.+ ///+ /// Reset `self` to be ready for the next acquisition cycle.+ ///+ /// [`vkQueueSubmit`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkQueueSubmit+ fn get_present_wait_semaphores(&mut self) -> Vec<vk::Semaphore> {+ self.present[0..self.present_index].to_vec()+ }++ unsafe fn destroy(&self, device: &ash::Device) {+ unsafe {+ for sem in &self.present {+ device.destroy_semaphore(*sem, None);+ }+ }+ }+}++#[derive(Debug)]+struct NativeSurfaceTextureMetadata {+ acquire_semaphores: Arc<Mutex<SwapchainAcquireSemaphore>>,+ present_semaphores: Arc<Mutex<SwapchainPresentSemaphores>>,+}++impl SurfaceTextureMetadata for NativeSurfaceTextureMetadata {+ fn get_semaphore_guard(&self) -> Box<dyn SwapchainSubmissionSemaphoreGuard + '_> {+ Box::new(NativeSwapchainSubmissionSemaphoreGuard {+ acquire_semaphore_guard: self+ .acquire_semaphores+ .try_lock()+ .expect("Failed to lock surface acquire semaphore"),+ present_semaphores_guard: self+ .present_semaphores+ .try_lock()+ .expect("Failed to lock surface present semaphores"),+ })+ }++ fn as_any(&self) -> &dyn Any {+ self+ }+}++struct NativeSwapchainSubmissionSemaphoreGuard<'a> {+ acquire_semaphore_guard: MutexGuard<'a, SwapchainAcquireSemaphore>,+ present_semaphores_guard: MutexGuard<'a, SwapchainPresentSemaphores>,+}++impl<'a> SwapchainSubmissionSemaphoreGuard for NativeSwapchainSubmissionSemaphoreGuard<'a> {+ fn set_used_fence_value(&mut self, value: u64) {+ self.acquire_semaphore_guard.set_used_fence_value(value);+ }++ fn get_acquire_wait_semaphore(&mut self) -> Option<SemaphoreType> {+ self.acquire_semaphore_guard+ .get_acquire_wait_semaphore()+ .map(SemaphoreType::Binary)+ }++ fn get_submit_signal_semaphore(+ &mut self,+ device: &DeviceShared,+ ) -> Result<SemaphoreType, crate::DeviceError> {+ self.present_semaphores_guard+ .get_submit_signal_semaphore(device)+ .map(SemaphoreType::Binary)+ }+}