zink: support using lavapipe

This is really nasty, and shouldn't really be needed, but we have a
problem where both Zink and Lavapipe checks $GALLIUM_DRIVER, meaning that
Zink tries to use Lavapipe, and Lavapipe tries to use Zink.

This patch side-steps that by temporarily setting $GALLIUM_DRIVER to
"llvmpipe", giving Lavapipe a chance to succeed.

This is not great at all. The most obvious problem is that this is super
thread-unsafe, effectively modifying global state without any care. In
reality, we'd only want the pipe-loader in the *same thread* to ignore
Zink, but it's not so obvious how to do that without introducing lots of
ugly zink-specific cruft.

People shouldn't be using Zink if they don't have a GPU, it's going to
be much better to use LLVMpipe in that case. So let's not worry too much
about this case, and instead guard this dangerous logic with an
ZINK_USE_LAVAPIPE environment variable. This means this behavior only
happens if people opt in to it.

With this in place, we can start using Zink + Lavapipe on CI.

In the longer run, it might be better to use Adam Jackson's copper
loader instead, and drop exposing Zink as a software rasterizer
entirely. But that's something for the great future where we have flying
cars and all.

Reviewed-by: Adam Jackson <ajax@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7881>
This commit is contained in:
Erik Faye-Lund 2020-12-02 17:26:26 +01:00 committed by Marge Bot
parent 3adf6da4c1
commit 3824c06aff
2 changed files with 38 additions and 1 deletions

View file

@ -71,12 +71,18 @@ zink_nir_algebraic_c = custom_target(
depend_files : nir_algebraic_py,
)
zink_c_args = []
if with_swrast_vk
zink_c_args += '-DZINK_WITH_SWRAST_VK'
endif
libzink = static_library(
'zink',
[files_libzink, zink_device_info, zink_instance, zink_nir_algebraic_c],
gnu_symbol_visibility : 'hidden',
include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux, inc_vulkan_wsi, inc_vulkan_util],
dependencies: [dep_vulkan, idep_nir_headers, idep_mesautil],
c_args: zink_c_args,
)
driver_zink = declare_dependency(

View file

@ -724,6 +724,17 @@ choose_pdev(const VkInstance instance)
for (i = 0; i < pdev_count; ++i) {
VkPhysicalDeviceProperties props;
vkGetPhysicalDeviceProperties(pdevs[i], &props);
#ifdef ZINK_WITH_SWRAST_VK
char *use_lavapipe = getenv("ZINK_USE_LAVAPIPE");
if (use_lavapipe) {
if (props.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
pdev = pdevs[i];
break;
} else
continue;
}
#endif
if (props.deviceType != VK_PHYSICAL_DEVICE_TYPE_CPU) {
pdev = pdevs[i];
break;
@ -1156,11 +1167,31 @@ fail:
struct pipe_screen *
zink_create_screen(struct sw_winsys *winsys)
{
struct zink_screen *ret = zink_internal_create_screen(NULL);
#ifdef ZINK_WITH_SWRAST_VK
char *use_lavapipe = getenv("ZINK_USE_LAVAPIPE"), *gallium_driver = NULL;
if (use_lavapipe) {
/**
* HACK: Temorarily unset $GALLIUM_DRIVER to prevent Lavapipe from
* recursively trying to use zink as the gallium driver.
*
* This is not thread-safe, so if an application creates another
* context in another thread at the same time, well, we're out of
* luck!
*/
gallium_driver = getenv("GALLIUM_DRIVER");
setenv("GALLIUM_DRIVER", "llvmpipe", 1);
}
#endif
struct zink_screen *ret = zink_internal_create_screen(NULL);
if (ret)
ret->winsys = winsys;
#ifdef ZINK_WITH_SWRAST_VK
if (gallium_driver)
setenv("GALLIUM_DRIVER", gallium_driver, 1);
#endif
return &ret->base;
}