gfx-rs / wgpu

A cross-platform, safe, pure-Rust graphics API.
https://wgpu.rs
Apache License 2.0
12.67k stars 926 forks source link

SegFault on WSL2/Windows 11 #2213

Closed parasyte closed 2 years ago

parasyte commented 2 years ago

Description

I was excited to finally try out WSLg on Windows 11, but it just crashes in the Vulkan driver.

Repro steps

Run the hello-triangle example (main branch) on Ubuntu 20.04, making sure to follow the instructions provided in the Microsoft article. (Also, winit requires the libxkbcommon-dev to workaround https://github.com/rust-windowing/winit/issues/1760)

The x11-apps do work: xcalc, xclock, xedit, xeyes, xlogo...

Expected vs observed behavior

Here's a backtrace from gdb:

Thread 1 "hello-triangle" received signal SIGSEGV, Segmentation fault.
0x00007ffff7c15d75 in wl_proxy_destroy () from /lib/x86_64-linux-gnu/libwayland-client.so.0
(gdb) bt
#0  0x00007ffff7c15d75 in wl_proxy_destroy () from /lib/x86_64-linux-gnu/libwayland-client.so.0
#1  0x00007ffff5ccdb37 in ?? () from /lib/x86_64-linux-gnu/libVkLayer_MESA_device_select.so
#2  0x00007ffff5cccff4 in ?? () from /lib/x86_64-linux-gnu/libVkLayer_MESA_device_select.so
#3  0x00007ffff7973bdb in ?? () from /lib/x86_64-linux-gnu/libvulkan.so.1
#4  0x00007ffff7976849 in vkEnumeratePhysicalDevices () from /lib/x86_64-linux-gnu/libvulkan.so.1
#5  0x0000555555d9e4c2 in ash::vk::features::InstanceFnV1_0::enumerate_physical_devices (self=0x555556935438, instance=..., p_physical_device_count=0x7fffffff8884, p_physical_devices=0x0) at /home/jay/.cargo/registry/src/github.com-1ecc6299db9ec823/ash-0.33.3+1.2.191/src/vk/features.rs:554
#6  0x0000555555d94087 in ash::instance::Instance::enumerate_physical_devices::{{closure}} (count=0x7fffffff8884, data=0x0) at /home/jay/.cargo/registry/src/github.com-1ecc6299db9ec823/ash-0.33.3+1.2.191/src/instance.rs:374
#7  0x0000555555d96311 in ash::prelude::read_into_uninitialized_vector (f=...) at /home/jay/.cargo/registry/src/github.com-1ecc6299db9ec823/ash-0.33.3+1.2.191/src/prelude.rs:42
#8  0x0000555555d94025 in ash::instance::Instance::enumerate_physical_devices (self=0x555556935430) at /home/jay/.cargo/registry/src/github.com-1ecc6299db9ec823/ash-0.33.3+1.2.191/src/instance.rs:373
#9  0x0000555555c03049 in wgpu_hal::vulkan::instance::<impl wgpu_hal::Instance<wgpu_hal::vulkan::Api> for wgpu_hal::vulkan::Instance>::enumerate_adapters (self=0x55555693a668) at wgpu-hal/src/vulkan/instance.rs:609
#10 0x0000555555ab6953 in wgpu_core::instance::<impl wgpu_core::hub::Global<G>>::request_adapter::gather (instance=..., inputs=0x7fffffffc028, compatible_surface=..., force_software=false, device_types=0x7fffffffa030) at /home/jay/other-projects/wgpu/wgpu-core/src/instance.rs:570
#11 0x0000555555a12c00 in wgpu_core::instance::<impl wgpu_core::hub::Global<G>>::request_adapter (self=0x55555693a650, desc=0x7fffffffc018, inputs=...) at /home/jay/other-projects/wgpu/wgpu-core/src/instance.rs:600
#12 0x0000555555a7dbdc in <wgpu::backend::direct::Context as wgpu::Context>::instance_request_adapter (self=0x55555693a650, options=0x55555688f7b8) at wgpu/src/backend/direct.rs:794
#13 0x0000555555a6d8bd in wgpu::Instance::request_adapter (self=0x55555688f770, options=0x55555688f7b8) at wgpu/src/lib.rs:1461
#14 0x000055555571510f in hello_triangle::run::{{closure}} () at wgpu/examples/hello-triangle/main.rs:12
#15 0x0000555555715de9 in <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll (self=..., cx=0x7fffffffd210) at /rustc/b416e3892d9526709f3a248f5ed3a43a970f795e/library/core/src/future/mod.rs:80
#16 0x000055555571e3e8 in pollster::block_on (fut=...) at /home/jay/.cargo/registry/src/github.com-1ecc6299db9ec823/pollster-0.2.4/src/lib.rs:132
#17 0x00005555557214e7 in hello_triangle::main () at wgpu/examples/hello-triangle/main.rs:142

Extra materials

N/A

Platform

GPU shows up in Ubuntu as:

$ sudo lspci -vvv

9a8c:00:00.0 3D controller: Microsoft Corporation Device 008e
        Physical Slot: 1407270391
        Control: I/O+ Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR- FastB2B- DisINTx-
        Status: Cap+ 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- <TAbort- <MAbort- >SERR- <PERR- INTx-
        Latency: 0
        Capabilities: [40] Null
        Kernel driver in use: dxgkrnl
parasyte commented 2 years ago

I just tried the ash triangle example, and it crashes in the same manner. (Had to install Vulkan Validation Layers to run this example.)

parasyte commented 2 years ago

I disabled the vulkan backend entirely with the patch below, and the gles backend works, mostly!

disable-vulkan.patch ```patch diff --git a/wgpu-core/build.rs b/wgpu-core/build.rs index 37cf80b7..7e540e61 100644 --- a/wgpu-core/build.rs +++ b/wgpu-core/build.rs @@ -7,7 +7,6 @@ fn main() { unix_wo_apple: {all(unix, not(apple))}, // Backends - vulkan: { all(not(wasm), any(windows, unix_wo_apple)) }, metal: { all(not(wasm), apple) }, dx12: { all(not(wasm), windows) }, dx11: { all(false, not(wasm), windows) }, diff --git a/wgpu-core/src/lib.rs b/wgpu-core/src/lib.rs index 2e1ca617..7417a2bf 100644 --- a/wgpu-core/src/lib.rs +++ b/wgpu-core/src/lib.rs @@ -203,8 +203,6 @@ macro_rules! gfx_select { // Note: For some reason the cfg aliases defined in build.rs don't succesfully apply in this // macro so we must specify their equivalents manually match $id.backend() { - #[cfg(all(not(target_arch = "wasm32"), not(target_os = "ios"), not(target_os = "macos")))] - wgt::Backend::Vulkan => $global.$method::<$crate::api::Vulkan>( $($param),* ), #[cfg(all(not(target_arch = "wasm32"), any(target_os = "ios", target_os = "macos")))] wgt::Backend::Metal => $global.$method::<$crate::api::Metal>( $($param),* ), #[cfg(all(not(target_arch = "wasm32"), windows))] ```

With this patch, wgpu-info prints:

Adapter 0:
        Backend:   Gl
        Name:      "D3D12 (NVIDIA GeForce RTX 3090)"
        VendorID:  0
        DeviceID:  0
        Type:      DiscreteGpu
        Compliant: false
        Features:
                DEPTH_CLIP_CONTROL:                                             true
                TEXTURE_COMPRESSION_BC:                                         false
                INDIRECT_FIRST_INSTANCE:                                        false
                TIMESTAMP_QUERY:                                                false
                PIPELINE_STATISTICS_QUERY:                                      false
                MAPPABLE_PRIMARY_BUFFERS:                                       false
                TEXTURE_BINDING_ARRAY:                                          false
                BUFFER_BINDING_ARRAY:                                           false
                STORAGE_RESOURCE_BINDING_ARRAY:                                 false
                SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING:  false
                UNIFORM_BUFFER_AND_STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING:  false
                PARTIALLY_BOUND_BINDING_ARRAY:                                  false
                UNSIZED_BINDING_ARRAY:                                          false
                MULTI_DRAW_INDIRECT:                                            false
                MULTI_DRAW_INDIRECT_COUNT:                                      false
                PUSH_CONSTANTS:                                                 false
                ADDRESS_MODE_CLAMP_TO_BORDER:                                   false
                POLYGON_MODE_LINE:                                              false
                POLYGON_MODE_POINT:                                             false
                TEXTURE_COMPRESSION_ETC2:                                       true
                TEXTURE_COMPRESSION_ASTC_LDR:                                   false
                TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES:                       true
                SHADER_FLOAT64:                                                 false
                VERTEX_ATTRIBUTE_64BIT:                                         false
                CONSERVATIVE_RASTERIZATION:                                     false
                VERTEX_WRITABLE_STORAGE:                                        false
                CLEAR_COMMANDS:                                                 true
                SPIRV_SHADER_PASSTHROUGH:                                       false
                SHADER_PRIMITIVE_INDEX:                                         false
                MULTIVIEW:                                                      false
        Limits:
                Max Texture Dimension 1d:                        16384
                Max Texture Dimension 2d:                        16384
                Max Texture Dimension 3d:                        1024
                Max Texture Array Layers:                        16384
                Max Bind Groups:                                 8
                Max Dynamic Uniform Buffers Per Pipeline Layout: 12
                Max Dynamic Storage Buffers Per Pipeline Layout: 0
                Max Sampled Textures Per Shader Stage:           16
                Max Samplers Per Shader Stage:                   16
                Max Storage Buffers Per Shader Stage:            0
                Max Storage Textures Per Shader Stage:           0
                Max Uniform Buffers Per Shader Stage:            12
                Max Uniform Buffer Binding Size:                 65536
                Max Storage Buffer Binding Size:                 0
                Max Vertex Buffers:                              16
                Max Vertex Attributes:                           16
                Max Vertex Buffer Array Stride:                  4294967295
                Max Push Constant Size:                          0
                Min Uniform Buffer Offset Alignment:             256
                Min Storage Buffer Offset Alignment:             256
                Max Compute Workgroup Size X:                    0
                Max Compute Workgroup Size Y:                    0
                Max Compute Workgroup Size Z:                    0
                Max Compute Workgroups Per Dimmension:           0
        Downlevel Properties:
                Shader Model:                        Sm5
                COMPUTE_SHADERS:                     false
                FRAGMENT_WRITABLE_STORAGE:           false
                INDIRECT_EXECUTION:                  false
                BASE_VERTEX:                         false
                READ_ONLY_DEPTH_STENCIL:             false
                DEVICE_LOCAL_IMAGE_COPIES:           true
                NON_POWER_OF_TWO_MIPMAPPED_TEXTURES: true
                CUBE_ARRAY_TEXTURES:                 true
                COMPARISON_SAMPLERS:                 true
                INDEPENDENT_BLENDING:                true
                VERTEX_STORAGE:                      false
                ANISOTROPIC_FILTERING:               false
                FRAGMENT_STORAGE:                    false
kvark commented 2 years ago

You should be able to disable Vulkan by just changing the flags passed to wgpu::Instance::new, without any code changes.

parasyte commented 2 years ago

Good thing to call out! I tried the WGPU_BACKEND env var without success but didn't realize that hello-triangle does not use the framework: https://github.com/gfx-rs/wgpu/blob/943145b4df9dd4c0bc236a76ef96acd13c46fead/wgpu/examples/framework.rs#L125-L127

https://github.com/gfx-rs/wgpu/blob/943145b4df9dd4c0bc236a76ef96acd13c46fead/wgpu/examples/hello-triangle/main.rs#L10

I do know that wgpu-info is hardcoded with all backends, though. https://github.com/gfx-rs/wgpu/blob/943145b4df9dd4c0bc236a76ef96acd13c46fead/wgpu-info/src/main.rs#L104

codeart1st commented 2 years ago

For me, need to install sudo apt-get install libxcursor-dev libegl1 to get rendering on x11 working. Wayland still not working for me because of a random error in winit.

Instance { context: Context { type: "Native" } }
AdapterInfo { name: "D3D12 (NVIDIA GeForce RTX 3050 Laptop GPU)", vendor: 0, device: 0, device_type: DiscreteGpu, backend: Gl }

Also installed kisak-mesa, but currently didn't really know if it was really needed. Update: Uninstalled kisak-mesa again and seems to be working as before with default packages. Update2: To get wayland working for me, I need to install libxkbcommon-dev libegl1 libwayland-dev.

parasyte commented 2 years ago

Updating Mesa fixes the segfault. In short:

$ sudo add-apt-repository ppa:kisak/kisak-mesa
$ sudo apt update
$ sudo apt upgrade

Not a wgpu bug! 🎉

jaredzhou commented 2 years ago

always got the following err 2022-03-09T14:16:35.695354Z ERROR wgpu_hal::vulkan::instance: GENERAL [Loader Message (0x0)] setupLoaderTermPhysDevs: Failed to detect any valid GPUs in the current config any idea how to fix it?