libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
10.21k stars 1.86k forks source link

[GPU] SDL_CreateGPUDevice crashes on X11 with Vulkan backend when using `prime-run` #11552

Open Urist-McDeveloper opened 2 days ago

Urist-McDeveloper commented 2 days ago

Reproducible on both 3.1.6 and latest main branch. Here's CMake log just in case. Minimal example:

#include <SDL3/SDL.h>

int main(void) {
    SDL_Init(SDL_INIT_VIDEO);

    SDL_GPUDevice *device = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV, false, NULL);
    SDL_Log("OK!");

    SDL_DestroyGPUDevice(device);
    SDL_Quit();
    return 0;
}
$ cc test.c -o test -lSDL3
$ ./test
OK!
$ prime-run ./test
X Error of failed request:  BadMatch (invalid parameter attributes)
  Major opcode of failed request:  156 (NV-GLX)
  Minor opcode of failed request:  43 ()
  Serial number of failed request:  252
  Current serial number in output stream:  253

The crash happens when both __GLX_VENDOR_LIBRARY_NAME and __NV_PRIME_RENDER_OFFLOAD=1 are set. The exact call that causes the crash is vkGetPhysicalDeviceXlibPresentationSupportKHR at SDL_x11vulkan.c:283. The call stack is:

  1. SDL_Vulkan_GetPresentationSupport at SDL_video.c:5818;
  2. VULKAN_INTERNAL_IsDeviceSuitable at SDL_gpu_vulkan.c:11044;
  3. VULKAN_INTERNAL_DeterminePhysicalDevice at SDL_gpu_vulkan.c:11161;
  4. VULKAN_INTERNAL_PrepareVulkan at SDL_gpu_vulkan.c:11389;
  5. VULKAN_CreateDevice at SDL_gpu_vulkan.c:11440;
  6. SDL_CreateGPUDeviceWithProperties at SDL_gpu.c:529;
  7. SDL_CreateGPUDevice at SDL_gpu.c:507.

Here's the kicker: VULKAN_INTERNAL_PrepareVulkan is actually called before from VULKAN_PrepareDriver (SDL_gpu_vulkan.c:11413) with seemingly the exact same argument since both debugMode and preferLowPower are false, and it doesn't crash. There's probably some difference in global state but I couldn't find it. Also, I wasn't able to reproduce this crash at all when manually creating Vulkan instance and device and calling SDL_Vulkan_GetPresentationSupport.