msys2 / MINGW-packages

Package scripts for MinGW-w64 targets to build under MSYS2.
https://packages.msys2.org
BSD 3-Clause "New" or "Revised" License
2.24k stars 1.21k forks source link

MESA/d3d12 driver not working #16474

Closed lb90 closed 1 year ago

lb90 commented 1 year ago

There seem to be severe issues with the MESA d3d12 driver. I have installed it and almost all GL programs do not work anymore:

I wonder if MESA/d3d12 supports mingw-w64 at all.

Also, I see some warnings like this:

../../src/gallium/drivers/d3d12/d3d12_video_screen.cpp:462:63: warning: format specifies type 'unsigned int' but the argument has type 'HRESULT' (aka 'long') [-Wformat]
      debug_printf("CheckFeatureSupport failed with HR %x\n", hr);
                                                       ~~     ^~
                                                       %lx
lb90 commented 1 year ago

When launching blender I get:

Error   : EXCEPTION_ACCESS_VIOLATION
Address : 0x00007FFE7A081BA0
Module  : libgallium_wgl.dll
Thread  : 00005aa4
Writing: D:\msys64\tmp\blender.crash.txt

I have then built the mesa package with options=(debug !strip). In that case we get a failed assertion:

D3D12: CreateGraphicsPipelineState failed!
Assertion failed: ctx->current_gfx_pso, file ../../src/gallium/drivers/d3d12/d3d12_draw.cpp, line 1046

Same with GTK4. Using gdb we can see that just before the assert the following code is reached:

Thread 34 hit Breakpoint 1, d3d12_draw_vbo (pctx=0x351039a0, dinfo=0x354c5c88, drawid_offset=0, indirect=0x0, draws=0x375fede8, num_draws=1) at ../../src/gallium/drivers/d3d12/d3d12_draw.cpp:1045
1045          ctx->current_gfx_pso = d3d12_get_gfx_pipeline_state(ctx);
(gdb) s
d3d12_get_gfx_pipeline_state (ctx=0x351039a0) at ../../src/gallium/drivers/d3d12/d3d12_pipeline_state.cpp:352
352        uint32_t hash = hash_gfx_pipeline_state(&ctx->gfx_pipeline_state);
(gdb) n
353        struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(ctx->pso_cache, hash,
(gdb)
354                                                                      &ctx->gfx_pipeline_state);
(gdb)
353        struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(ctx->pso_cache, hash,
(gdb)
355        if (!entry) {
(gdb)
356           struct d3d12_gfx_pso_entry *data = (struct d3d12_gfx_pso_entry *)MALLOC(sizeof(struct d3d12_gfx_pso_entry));
(gdb)
357           if (!data)
(gdb) 
360           data->key = ctx->gfx_pipeline_state;
(gdb)
361           data->pso = create_gfx_pipeline_state(ctx);
(gdb)
warning: D3D12 ERROR: ID3D12Device::CreateInputLayout: Input Signature in bytecode could not be parsed.  Data may be corrupt or in an unrecognizable format. [ STATE_CREATION ERROR #63: CREATEINPUTLAYOUT_UNPARSE
ABLEINPUTSIGNATURE]
warning: D3D12 ERROR: ID3D12Device::CreateVertexShader: Vertex Shader is corrupt or in an unrecognized format. [ STATE_CREATION ERROR #67: CREATEVERTEXSHADER_INVALIDSHADERBYTECODE]
warning: D3D12 ERROR: ID3D12Device::CreatePixelShader: Pixel Shader is corrupt or in an unrecognized format. [ STATE_CREATION ERROR #93: CREATEPIXELSHADER_INVALIDSHADERBYTECODE]
warning: D3D12: CreateGraphicsPipelineState failed!
362           if (!data->pso) {
(gdb)

GTK3 instead hangs with the following stack trace:

(gdb) thread 1
[Switching to thread 1 (Thread 23464.0x2754)]
#0  0x00007fff0f44d144 in ntdll!ZwWaitForSingleObject () from C:\WINDOWS\SYSTEM32\ntdll.dll
(gdb) bt
#0  0x00007fff0f44d144 in ntdll!ZwWaitForSingleObject () from C:\WINDOWS\SYSTEM32\ntdll.dll
#1  0x00007fff0cbb306e in WaitForSingleObjectEx () from C:\WINDOWS\System32\KernelBase.dll 
#2  0x00007ffe815c3139 in amdxc64!OpenShimInterface () from C:\WINDOWS\System32\DriverStore\FileRepository\u0384804.inf_amd64_41ca4ca6939f5e56\B384763\amdxc64.dll       
#3  0x00007ffe815c3094 in amdxc64!OpenShimInterface () from C:\WINDOWS\System32\DriverStore\FileRepository\u0384804.inf_amd64_41ca4ca6939f5e56\B384763\amdxc64.dll       
#4  0x00007ffe813eecec in amdxc64!AmdExtD3DCreateInterface () from C:\WINDOWS\System32\DriverStore\FileRepository\u0384804.inf_amd64_41ca4ca6939f5e56\B384763\amdxc64.dll
#5  0x00007ffe8141d3d1 in amdxc64!AmdExtD3DCreateInterface () from C:\WINDOWS\System32\DriverStore\FileRepository\u0384804.inf_amd64_41ca4ca6939f5e56\B384763\amdxc64.dll
#6  0x00007ffed8aeca26 in d3d12core!D3D12GetInterface () from C:\WINDOWS\SYSTEM32\D3D12Core.dll
#7  0x00007ffed8a7fbc2 in d3d12core!D3D12GetInterface () from C:\WINDOWS\SYSTEM32\D3D12Core.dll
#8  0x00007ffed8a5ad74 in ?? () from C:\WINDOWS\SYSTEM32\D3D12Core.dll
#9  0x00007ffef8ba455b in ?? () from C:\WINDOWS\SYSTEM32\D3D12.dll
#10 0x00007ffef8ba489f in d3d12!D3D12CoreRegisterLayers () from C:\WINDOWS\SYSTEM32\D3D12.dll
#11 0x00007ffe8b357bee in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb)
lb90 commented 1 year ago

Finally I have tested a very simple GL client: https://learn.microsoft.com/en-us/windows/win32/opengl/the-program-ported-to-win32

The code can be compiled with: cc -o gl-sample gl-sample.c -lopengl32 -lglu32 -lgdi32. It fails with "ChoosePixelFormat failed" when run with MESA's opengl32.dll

MehdiChinoune commented 1 year ago

similar to #7530

driver1998 commented 1 year ago
#2  0x00007ffe815c3139 in amdxc64!OpenShimInterface () from C:\WINDOWS\System32\DriverStore\FileRepository\u0384804.inf_amd64_41ca4ca6939f5e56\B384763\amdxc64.dll       
#3  0x00007ffe815c3094 in amdxc64!OpenShimInterface () from C:\WINDOWS\System32\DriverStore\FileRepository\u0384804.inf_amd64_41ca4ca6939f5e56\B384763\amdxc64.dll       
#4  0x00007ffe813eecec in amdxc64!AmdExtD3DCreateInterface () from C:\WINDOWS\System32\DriverStore\FileRepository\u0384804.inf_amd64_41ca4ca6939f5e56\B384763\amdxc64.dll
#5  0x00007ffe8141d3d1 in amdxc64!AmdExtD3DCreateInterface () from C:\WINDOWS\System32\DriverStore\FileRepository\u0384804.inf_amd64_41ca4ca6939f5e56\B384763\amdxc64.dll

Looks like it is crashing inside your AMD GPU driver. Try running mesa/d3d12 with WARP (Microsoft's reference Direct3D software renderer) with LIBGL_ALWAYS_SOFTWARE=true GALLIUM_DRIVER=d3d12

lb90 commented 1 year ago

Hi @driver1998!

Here are the results with LIBGL_ALWAYS_SOFTWARE=true GALLIUM_DRIVER=d3d12:

Could you test if blender works with mesa installed?

lazka commented 1 year ago

(for the record: I always got crashes/glitches with gtk4+mesa installed. with AMD)

lb90 commented 1 year ago

Yeah, indeed! I have now tested GTK applications with only LIBGL_ALWAYS_SOFTWARE=1 and they do not work...

driver1998 commented 1 year ago

With only LIBGL_ALWAYS_SOFTWARE=1 llvmpipe would be used instead of d3d12 IIRC.

lb90 commented 1 year ago

I managed to buld MESA w/ Gallium d3d12 on MSVC and I get the same issues: GTK3 hangs, GTK4 crashes. Looks like an upstream issue

Opened Opened https://gitlab.freedesktop.org/mesa/mesa/-/issues/8726

lb90 commented 1 year ago

Ah, that explains a lot: https://github.com/microsoft/DirectXShaderCompiler/issues/2550. See also

So, in the end, it's because we dont' have dxil.dll 🤷‍♂️

driver1998 commented 1 year ago

dxil.dll can be copied from Windows SDK, it is redistributable so I guess packaging it in MSYS2 won't hurt either.

lb90 commented 1 year ago

Yep 🙂 either that or make a package for https://github.com/microsoft/DirectXShaderCompiler

Relevant: https://learn.microsoft.com/en-us/archive/blogs/marcelolr/using-the-github-dxcompiler-dll

driver1998 commented 1 year ago

Enable D3D12ExperimentalShaderModels feature before creating a device according to the instructions here: https://docs.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-d3d12enableexperimentalfeatures

Looks like we'll need to patch mesa to enable experimental mode instead, if we don't want to use dxil.dll. Not sure if this is a good idea.

lb90 commented 1 year ago

I have done some research on this topic:

  1. dxil.dll is not open source. It's distributed as part of https://github.com/microsoft/DirectXShaderCompiler, but it's not really part of it. See https://github.com/microsoft/DirectXShaderCompiler/issues/1330#issuecomment-393605245 and https://github.com/microsoft/DirectXShaderCompiler/issues/4435#issuecomment-1120456695
  2. The dxil.dll from WinSDK is much older than the dxil.dll published on GitHub releases, generally. ATM dxil.dll on WinSDK is from December 2020, the latest dxil.dll on GitHub is from March 2023

The DLL exports only two simple functions that create COM objects:

>dumpbin /EXPORTS dxil.dll
Microsoft (R) COFF/PE Dumper Version 14.35.32216.1
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file dxil.dll

File Type: DLL

  Section contains the following exports for dxil.dll

    ordinal hint RVA      name

          1    0 0000140C DxcCreateInstance
          2    1 000014F4 DxcCreateInstance2

Those two functions are pretty much ABI-agnostic, meaning that they follow that standard ABI of Win32 system DLLs (there are no things like bitfields, C++ classes, etc. which may differ betweeen mingw-w64 compilers and MSVC).

However dependencies are a bit trickier! dxil.dll from WinSDK and dxil.dll from GitHub have different imports

Dependencies of WinSDK's dxil.dll

>dumpbin /IMPORTS dxil.dll
Microsoft (R) COFF/PE Dumper Version 14.35.32216.1
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file dxil.dll

File Type: DLL

  Section contains the following imports:

    api-ms-win-crt-runtime-l1-1-0.dll
             1800F0EB0 Import Address Table
             18011EB10 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                          36 _initterm
                          37 _initterm_e

    api-ms-win-crt-private-l1-1-0.dll
             1800F0D78 Import Address Table
             18011E9D8 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                          93 _o__cexit
                          A0 _o__configure_narrow_argv
                          A7 _o__crt_atexit
                          C0 _o__errno
                          C2 _o__execute_onexit_table
                          E9 _o__fpclass
                         112 _o__get_osfhandle
                         13C _o__initialize_narrow_environment
                         13D _o__initialize_onexit_table
                         140 _o__invalid_parameter_noinfo_noreturn
                         243 _o__purecall
                         251 _o__register_onexit_function
                         259 _o__seh_filter_dll
                         463 memmove
                         341 _o_abort
                         356 _o_atoi
                         35C _o_calloc
                         38C _o_free
                         39E _o_isalnum
                         39F _o_isalpha
                         3A2 _o_isdigit
                         3A6 _o_isprint
                         3DD _o_mbsrtowcs
                         3F9 _o_qsort
                         435 _o_terminate
                         43C _o_toupper
                           C __C_specific_handler
                           1 _CxxThrowException
                          6E _o___stdio_common_vsprintf_s
                          6C _o___stdio_common_vsprintf
                          6A _o___stdio_common_vsnprintf_s
                          60 _o___std_type_info_destroy_list
                          5F _o___std_exception_destroy
                          5E _o___std_exception_copy
                          12 __CxxFrameHandler3
                         460 memchr
                         461 memcmp
                         462 memcpy

    api-ms-win-crt-string-l1-1-0.dll
             1800F0EC8 Import Address Table
             18011EB28 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                          83 memset

    ADVAPI32.dll
             1800F0BE0 Import Address Table
             18011E840 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                          D6 EventUnregister
                          DA EventWriteTransfer
                          D4 EventRegister

    KERNEL32.dll
             1800F0C00 Import Address Table
             18011E860 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                         218 GetProcAddress
                          68 CreateEventW
                         4A8 WaitForSingleObjectEx
                         3C2 ResetEvent
                         40A SetEvent
                         2BD InitializeCriticalSectionAndSpinCount
                         13A FormatMessageW
                         2B7 InitOnceExecuteOnce
                          3A CloseHandle
                          B0 DeleteCriticalSection
                         2BC InitializeCriticalSection
                         30F LeaveCriticalSection
                          CC EnterCriticalSection
                         38D RaiseException
                         227 GetProcessTimes
                         17D GetConsoleMode
                         23A GetStdHandle
                         182 GetConsoleScreenBufferInfo
                         478 TlsFree
                         479 TlsGetValue
                         4CB WideCharToMultiByte
                         477 TlsAlloc
                         1D6 GetLastError
                         338 MultiByteToWideChar
                         47A TlsSetValue
                         473 TerminateProcess
                         196 GetCurrentProcess
                         1EC GetModuleHandleW
                         2D8 IsProcessorFeaturePresent
                         459 SetUnhandledExceptionFilter
                         484 UnhandledExceptionFilter
                         2D2 IsDebuggerPresent
                         3D8 RtlVirtualUnwind
                         3D1 RtlLookupFunctionEntry
                         3CC RtlCaptureContext
                         2C1 InitializeSListHead
                         250 GetSystemTimeAsFileTime
                         19B GetCurrentThreadId
                         197 GetCurrentProcessId
                         382 QueryPerformanceCounter
                         3F8 SetConsoleTextAttribute
                          C0 DisableThreadLibraryCalls

    OLE32.dll
             1800F0D58 Import Address Table
             18011E9B8 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                          49 CoGetMalloc

    api-ms-win-crt-math-l1-1-0.dll
             1800F0D68 Import Address Table
             18011E9C8 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                          7D ceilf

I.e only system modules and the UCRT. There's a _CxxThrowException, but I assume that exceptions thrown internally from dxil.dll are also catched internally. Otherwise it would be a problem to use dxil.dll from languages other than C++.

Dependencies of GitHub's dxil.dll (2023/03/01)

>dumpbin /IMPORTS dxil.dll
Microsoft (R) COFF/PE Dumper Version 14.35.32216.1
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file dxil.dll

File Type: DLL

  Section contains the following imports:

    KERNEL32.dll
             180105020 Import Address Table
             1801394C8 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                          97 CloseHandle
                         370 HeapAlloc
                         377 HeapReAlloc
                         374 HeapFree
                         379 HeapSize
                         2D8 GetProcessHeap
                         416 MultiByteToWideChar
                         63B WideCharToMultiByte
                         5DA TlsAlloc
                         5DC TlsGetValue
                         5DD TlsSetValue
                         126 DeleteCriticalSection
                         389 InitializeCriticalSection
                         14C EnterCriticalSection
                         3E4 LeaveCriticalSection
                         2F7 GetStdHandle
                         2E2 GetProcessTimes
                         235 GetCurrentProcess
                         219 GetConsoleMode
                         21F GetConsoleScreenBufferInfo
                         538 SetConsoleTextAttribute
                         30E GetSystemTimeAsFileTime
                         236 GetCurrentProcessId
                         38B InitializeCriticalSectionEx
                         281 GetLastError
                         5DB TlsFree
                         3A4 IsDebuggerPresent
                         43E OutputDebugStringW
                         23A GetCurrentThreadId
                         474 QueryPerformanceCounter
                         3AC IsProcessorFeaturePresent
                         5C8 TerminateProcess
                         5A8 SetUnhandledExceptionFilter
                         3F6 LocalFree
                         1C3 FormatMessageA
                         284 GetLocaleInfoEx
                         38A InitializeCriticalSectionAndSpinCount
                         54C SetEvent
                         4F0 ResetEvent
                         615 WaitForSingleObjectEx
                          D1 CreateEventW
                         299 GetModuleHandleW
                         2D1 GetProcAddress
                         4F9 RtlCaptureContext
                         501 RtlLookupFunctionEntry
                         508 RtlVirtualUnwind
                         5EA UnhandledExceptionFilter
                         38E InitializeSListHead

    ADVAPI32.dll
             180105000 Import Address Table
             1801394A8 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                         10E EventUnregister
                         10C EventRegister
                         114 EventWriteTransfer

    ole32.dll
             1801053B8 Import Address Table
             180139860 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                          51 CoGetMalloc

    MSVCP140.dll
             1801051A8 Import Address Table
             180139650 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                         28F ?_Xout_of_range@std@@YAXPEBD@Z
                         28E ?_Xlength_error@std@@YAXPEBD@Z
                         273 ?_Syserror_map@std@@YAPEBDH@Z
                         285 ?_Winerror_map@std@@YAHH@Z
                         591 _Query_perf_frequency
                         590 _Query_perf_counter
                         28C ?_Xbad_function_call@std@@YAXXZ

    VCRUNTIME140.dll
             1801051E8 Import Address Table
             180139690 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                          21 __std_exception_copy
                          25 __std_type_info_destroy_list
                          1C __current_exception_context
                          1B __current_exception
                           8 __C_specific_handler
                          3B memcmp
                          3A memchr
                          3D memmove
                          3C memcpy
                          36 _purecall
                          3E memset
                          22 __std_exception_destroy
                           1 _CxxThrowException
                          23 __std_terminate

    VCRUNTIME140_1.dll
             180105260 Import Address Table
             180139708 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                           0 __CxxFrameHandler4

    api-ms-win-crt-heap-l1-1-0.dll
             180105288 Import Address Table
             180139730 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                          18 free
                          17 calloc

    api-ms-win-crt-runtime-l1-1-0.dll
             1801052B8 Import Address Table
             180139760 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                          16 _cexit
                          67 terminate
                          36 _initterm
                          37 _initterm_e
                          22 _execute_onexit_table
                          3C _register_onexit_function
                          54 abort
                          34 _initialize_onexit_table
                          1E _crt_atexit
                          39 _invalid_parameter_noinfo_noreturn
                          33 _initialize_narrow_environment
                          18 _configure_narrow_argv
                          21 _errno
                          3F _seh_filter_dll

    api-ms-win-crt-stdio-l1-1-0.dll
             180105330 Import Address Table
             1801397D8 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                          26 _fileno
                           0 __acrt_iob_func
                           F __stdio_common_vsprintf_s
                          37 _get_osfhandle
                          8A fwrite
                           B __stdio_common_vsnprintf_s
                           D __stdio_common_vsprintf
                          57 _setmode

    api-ms-win-crt-string-l1-1-0.dll
             180105378 Import Address Table
             180139820 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                          64 isalnum
                          68 isdigit
                          65 isalpha
                          98 toupper
                          6C isprint

    api-ms-win-crt-math-l1-1-0.dll
             1801052A0 Import Address Table
             180139748 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                          7D ceilf
                          2B _fpclass

    api-ms-win-crt-utility-l1-1-0.dll
             1801053A8 Import Address Table
             180139850 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                          19 qsort

    api-ms-win-crt-convert-l1-1-0.dll
             180105270 Import Address Table
             180139718 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                          59 mbsrtowcs
                          50 atoi

This one depends on system modules, the UCRT and...the VC runtime! That can be a problem...

lb90 commented 1 year ago

So we should package the older dxil.dll which is distibuted as part of the Windows SDK, or maybe request DirectxShaderCompiler developers to provide a dxil.dll with the runtime linked statically

/cc @driver1998

lb90 commented 1 year ago

Opened #16643 as a way to address this issue. Comments welcome! 🙂

lb90 commented 1 year ago

Fixed with https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22141, let's close this!