doitsujin / dxvk

Vulkan-based implementation of D3D9, D3D10 and D3D11 for Linux / Wine
zlib License
12.25k stars 783 forks source link

native: Ability to set a WSI programmatically #4093

Open smcv opened 5 days ago

smcv commented 5 days ago

When a program uses DXVK-Native, currently it must set the environment variable DXVK_WSI_DRIVER=SDL2 or similar, as documented in the README. This can be done by a wrapper script, or by calling setenv() or putenv() at the beginning of main(), for example something like this in a C++ executable that uses SDL2:

int main(int argc, char** argv)
{
#ifndef _WIN32
  if (::setenv("DXVK_WSI_DRIVER", "SDL2", 1) < 0) {                               
    std::cerr << "setenv:" << std::strerror(errno) << std::endl;                  
    return 1;                                                                     
  }                                                                               
#endif

  ...

However, setting environment variables like this inherits down through the process hierarchy: for example if a game launcher uses SDL2 and DXVK-Native, then every game launched by it would inherit DXVK_WSI_DRIVER=SDL2, even if the game has nothing to do with SDL2 and is actually using GLFW. This seems error-prone: the fact that the parent process (launcher) uses SDL2 for its HWND implementation is a fact about the parent process only, and generally not a true fact about its child processes.

One solution that might be suggested is to unset the environment variable after initializing DXVK, but that usually isn't safe, because environ is shared state with no specific locking, therefore it is unsafe to edit the environment in one thread while another thread might be calling getenv(). Many libraries call getenv() in functions that might be called from a non-main thread, for example anything that uses gettext() for localization, with the result that the only place that the environment can safely be altered is right at the beginning of main(), before the first non-main thread has been created.

Would it be possible to add a DXVK-specific way to set the WSI in use, as non-inheritable process-global state, analogous to SDL_SetHint()? Perhaps something like this?

int main (int argc, char **argv)
{
  HWND window;

#ifndef _WIN32
  dxvkInitWSI("SDL2");
#endif

  ... carry on with initialization ...

#ifdef _WIN32
  window = ... create a Win32 window ...
#else
  window = ... create a SDL2 window ...
#endif

  ... continue with application code ...
}

Or alternatively, a way to tell DXVK "my HWND is actually a SDL2 SDL_Window" via parameters to CreateDeviceEx or CreateSwapChainForHwnd() or equivalent, or by calling a DXVK-specific method on the Device, or something of that sort (whatever layer is the most sensible one), so that you could do something like this?

  Com<ID3D11Device> device;

  HRESULT status = D3D11CreateDevice(...);

#ifndef _WIN32
  device->dxvkSetWSI("SDL2");
#endif

  ... continue with application code ...

cc @flibitijibibo

flibitijibibo commented 5 days ago

At one point I thought about making it accessible via the DXGIFactory since we need to know which WSI to use the moment anything DXGI gets touched, but it's worth noting that a dxvk-native application must set the variable on startup, so if the application and its launcher both use it they're required to specify the WSI as if they were the base process (we treat "no value found" as a hard error to avoid apps depending on any kind of default value), so that would avoid the conflict in this particular example.

A real export wouldn't be so bad though, an example could be tried with this line in FNA3D: https://github.com/FNA-XNA/FNA3D/blob/master/src/FNA3D_Driver_D3D11.c#L5198