Hash :
605af42e
Author :
Date :
2020-03-04T21:52:53
Vulkan: Move ICD overrides to a common place We also need the bundled ICDs and selection logic for certain tests that create a vulkan instance. If we don't, the tests will use default vulkan even if ANGLE is using swiftshader-vulkan. Tests of driver specific handle types (e..g opaque fd) will fail in this case. Bug: angleproject:4092 Change-Id: Iff089c9341b8b533ee8634923d0177c5dbcd5cc8 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2088659 Commit-Queue: Michael Spang <spang@chromium.org> Reviewed-by: Tim Van Patten <timvp@google.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
//
// Copyright 2020 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// vulkan_icd.cpp : Helper for creating vulkan instances & selecting physical device.
#include "common/vulkan/vulkan_icd.h"
#include <functional>
#include <vector>
#include "common/bitset_utils.h"
#include "common/debug.h"
#include "common/system_utils.h"
namespace angle
{
namespace vk
{
namespace
{
// This function is unused on Android/Fuschia/GGP
#if !defined(ANGLE_PLATFORM_ANDROID) && !defined(ANGLE_PLATFORM_FUCHSIA) && \
!defined(ANGLE_PLATFORM_GGP)
const std::string WrapICDEnvironment(const char *icdEnvironment)
{
# if defined(ANGLE_PLATFORM_APPLE)
// On MacOS the libraries are bundled into the application directory
std::string ret = angle::GetHelperExecutableDir() + icdEnvironment;
return ret;
# endif // defined(ANGLE_PLATFORM_APPLE)
return icdEnvironment;
}
constexpr char kLoaderLayersPathEnv[] = "VK_LAYER_PATH";
#endif
constexpr char kLoaderICDFilenamesEnv[] = "VK_ICD_FILENAMES";
constexpr char kANGLEPreferredDeviceEnv[] = "ANGLE_PREFERRED_DEVICE";
constexpr uint32_t kMockVendorID = 0xba5eba11;
constexpr uint32_t kMockDeviceID = 0xf005ba11;
constexpr char kMockDeviceName[] = "Vulkan Mock Device";
constexpr uint32_t kGoogleVendorID = 0x1AE0;
constexpr uint32_t kSwiftShaderDeviceID = 0xC0DE;
constexpr char kSwiftShaderDeviceName[] = "SwiftShader Device";
using ICDFilterFunc = std::function<bool(const VkPhysicalDeviceProperties &)>;
ICDFilterFunc GetFilterForICD(vk::ICD preferredICD)
{
switch (preferredICD)
{
case vk::ICD::Mock:
return [](const VkPhysicalDeviceProperties &deviceProperties) {
return ((deviceProperties.vendorID == kMockVendorID) &&
(deviceProperties.deviceID == kMockDeviceID) &&
(strcmp(deviceProperties.deviceName, kMockDeviceName) == 0));
};
case vk::ICD::SwiftShader:
return [](const VkPhysicalDeviceProperties &deviceProperties) {
return ((deviceProperties.vendorID == kGoogleVendorID) &&
(deviceProperties.deviceID == kSwiftShaderDeviceID) &&
(strncmp(deviceProperties.deviceName, kSwiftShaderDeviceName,
strlen(kSwiftShaderDeviceName)) == 0));
};
default:
const std::string anglePreferredDevice =
angle::GetEnvironmentVar(kANGLEPreferredDeviceEnv);
return [anglePreferredDevice](const VkPhysicalDeviceProperties &deviceProperties) {
return (anglePreferredDevice.empty() ||
anglePreferredDevice == deviceProperties.deviceName);
};
}
}
} // namespace
// If we're loading the validation layers, we could be running from any random directory.
// Change to the executable directory so we can find the layers, then change back to the
// previous directory to be safe we don't disrupt the application.
ScopedVkLoaderEnvironment::ScopedVkLoaderEnvironment(bool enableValidationLayers, vk::ICD icd)
: mEnableValidationLayers(enableValidationLayers),
mICD(icd),
mChangedCWD(false),
mChangedICDEnv(false)
{
// Changing CWD and setting environment variables makes no sense on Android,
// since this code is a part of Java application there.
// Android Vulkan loader doesn't need this either.
#if !defined(ANGLE_PLATFORM_ANDROID) && !defined(ANGLE_PLATFORM_FUCHSIA) && \
!defined(ANGLE_PLATFORM_GGP)
if (icd == vk::ICD::Mock)
{
if (!setICDEnvironment(WrapICDEnvironment(ANGLE_VK_MOCK_ICD_JSON).c_str()))
{
ERR() << "Error setting environment for Mock/Null Driver.";
}
}
# if defined(ANGLE_VK_SWIFTSHADER_ICD_JSON)
else if (icd == vk::ICD::SwiftShader)
{
if (!setICDEnvironment(WrapICDEnvironment(ANGLE_VK_SWIFTSHADER_ICD_JSON).c_str()))
{
ERR() << "Error setting environment for SwiftShader.";
}
}
# endif // defined(ANGLE_VK_SWIFTSHADER_ICD_JSON)
if (mEnableValidationLayers || icd != vk::ICD::Default)
{
const auto &cwd = angle::GetCWD();
if (!cwd.valid())
{
ERR() << "Error getting CWD for Vulkan layers init.";
mEnableValidationLayers = false;
mICD = vk::ICD::Default;
}
else
{
mPreviousCWD = cwd.value();
std::string exeDir = angle::GetExecutableDirectory();
mChangedCWD = angle::SetCWD(exeDir.c_str());
if (!mChangedCWD)
{
ERR() << "Error setting CWD for Vulkan layers init.";
mEnableValidationLayers = false;
mICD = vk::ICD::Default;
}
}
}
// Override environment variable to use the ANGLE layers.
if (mEnableValidationLayers)
{
if (!angle::PrependPathToEnvironmentVar(kLoaderLayersPathEnv, ANGLE_VK_LAYERS_DIR))
{
ERR() << "Error setting environment for Vulkan layers init.";
mEnableValidationLayers = false;
}
}
#endif // !defined(ANGLE_PLATFORM_ANDROID)
}
ScopedVkLoaderEnvironment::~ScopedVkLoaderEnvironment()
{
if (mChangedCWD)
{
#if !defined(ANGLE_PLATFORM_ANDROID)
ASSERT(mPreviousCWD.valid());
angle::SetCWD(mPreviousCWD.value().c_str());
#endif // !defined(ANGLE_PLATFORM_ANDROID)
}
if (mChangedICDEnv)
{
if (mPreviousICDEnv.value().empty())
{
angle::UnsetEnvironmentVar(kLoaderICDFilenamesEnv);
}
else
{
angle::SetEnvironmentVar(kLoaderICDFilenamesEnv, mPreviousICDEnv.value().c_str());
}
}
}
bool ScopedVkLoaderEnvironment::setICDEnvironment(const char *icd)
{
// Override environment variable to use built Mock ICD
// ANGLE_VK_ICD_JSON gets set to the built mock ICD in BUILD.gn
mPreviousICDEnv = angle::GetEnvironmentVar(kLoaderICDFilenamesEnv);
mChangedICDEnv = angle::SetEnvironmentVar(kLoaderICDFilenamesEnv, icd);
if (!mChangedICDEnv)
{
mICD = vk::ICD::Default;
}
return mChangedICDEnv;
}
void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices,
vk::ICD preferredICD,
VkPhysicalDevice *physicalDeviceOut,
VkPhysicalDeviceProperties *physicalDevicePropertiesOut)
{
ASSERT(!physicalDevices.empty());
ICDFilterFunc filter = GetFilterForICD(preferredICD);
for (const VkPhysicalDevice &physicalDevice : physicalDevices)
{
vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut);
if (filter(*physicalDevicePropertiesOut))
{
*physicalDeviceOut = physicalDevice;
return;
}
}
WARN() << "Preferred device ICD not found. Using default physicalDevice instead.";
// Fall back to first device.
*physicalDeviceOut = physicalDevices[0];
vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut);
}
} // namespace vk
} // namespace angle