diff --git a/src/libANGLE/renderer/vulkan/CLContextVk.cpp b/src/libANGLE/renderer/vulkan/CLContextVk.cpp index af3062cc883..ace22a11007 100644 --- a/src/libANGLE/renderer/vulkan/CLContextVk.cpp +++ b/src/libANGLE/renderer/vulkan/CLContextVk.cpp @@ -10,7 +10,6 @@ #include "libANGLE/renderer/vulkan/CLEventVk.h" #include "libANGLE/renderer/vulkan/CLMemoryVk.h" #include "libANGLE/renderer/vulkan/CLProgramVk.h" -#include "libANGLE/renderer/vulkan/DisplayVk.h" #include "libANGLE/renderer/vulkan/vk_renderer.h" #include "libANGLE/renderer/vulkan/vk_utils.h" @@ -22,11 +21,9 @@ namespace rx { -CLContextVk::CLContextVk(const cl::Context &context, - const egl::Display *display, - const cl::DevicePtrs devicePtrs) +CLContextVk::CLContextVk(const cl::Context &context, const cl::DevicePtrs devicePtrs) : CLContextImpl(context), - vk::Context(GetImplAs(display)->getRenderer()), + vk::Context(getPlatform()->getRenderer()), mAssociatedDevices(devicePtrs) {} diff --git a/src/libANGLE/renderer/vulkan/CLContextVk.h b/src/libANGLE/renderer/vulkan/CLContextVk.h index 21856593689..a4d85a775c5 100644 --- a/src/libANGLE/renderer/vulkan/CLContextVk.h +++ b/src/libANGLE/renderer/vulkan/CLContextVk.h @@ -16,7 +16,6 @@ #include #include "libANGLE/CLDevice.h" -#include "libANGLE/Display.h" namespace rx { @@ -24,9 +23,7 @@ namespace rx class CLContextVk : public CLContextImpl, public vk::Context { public: - CLContextVk(const cl::Context &context, - const egl::Display *display, - const cl::DevicePtrs devicePtrs); + CLContextVk(const cl::Context &context, const cl::DevicePtrs devicePtrs); ~CLContextVk() override; diff --git a/src/libANGLE/renderer/vulkan/CLPlatformVk.cpp b/src/libANGLE/renderer/vulkan/CLPlatformVk.cpp index 15ee652af45..bb5ad5a723b 100644 --- a/src/libANGLE/renderer/vulkan/CLPlatformVk.cpp +++ b/src/libANGLE/renderer/vulkan/CLPlatformVk.cpp @@ -6,10 +6,10 @@ // CLPlatformVk.cpp: Implements the class methods for CLPlatformVk. #include "libANGLE/renderer/vulkan/CLPlatformVk.h" -#include "common/MemoryBuffer.h" +#include "common/vulkan/vulkan_icd.h" +#include "libANGLE/angletypes.h" #include "libANGLE/renderer/vulkan/CLContextVk.h" #include "libANGLE/renderer/vulkan/CLDeviceVk.h" -#include "libANGLE/renderer/vulkan/DisplayVk.h" #include "libANGLE/renderer/vulkan/vk_renderer.h" #include "libANGLE/CLPlatform.h" @@ -18,6 +18,7 @@ #include "anglebase/no_destructor.h" #include "common/angle_version_info.h" #include "libANGLE/renderer/vulkan/vk_utils.h" +#include "vulkan/vulkan_core.h" namespace rx { @@ -40,26 +41,24 @@ std::string CreateExtensionString(const NameVersionVector &extList) return extensions; } -angle::Result InitBackendRenderer(egl::Display *display) +} // namespace + +angle::Result CLPlatformVk::initBackendRenderer() { - // Initialize the backend vk::Renderer by initializing a dummy/default EGL display object - // TODO(aannestrand) Implement display-less vk::Renderer init - // http://anglebug.com/8515 - // TODO(aannestrand) Add CL and EGL context testing - // http://anglebug.com/8514 - if (display == nullptr || IsError(display->initialize())) - { - ERR() << "Failed to init renderer!"; - ANGLE_CL_RETURN_ERROR(CL_OUT_OF_HOST_MEMORY); - } + ASSERT(mRenderer != nullptr); + + ANGLE_TRY(mRenderer->initialize(this, this, angle::vk::ICD::Default, 0, 0, + vk::UseValidationLayers::YesIfAvailable, getWSIExtension(), + getWSILayer(), getWindowSystem(), angle::FeatureOverrides{})); + return angle::Result::Continue; } -} // namespace - CLPlatformVk::~CLPlatformVk() { - mDisplay->getImplementation()->terminate(); + ASSERT(mRenderer); + mRenderer->onDestroy(this); + delete mRenderer; } CLPlatformImpl::Info CLPlatformVk::createInfo() const @@ -81,14 +80,11 @@ CLPlatformImpl::Info CLPlatformVk::createInfo() const CLDeviceImpl::CreateDatas CLPlatformVk::createDevices() const { - ASSERT(mDisplay); - ASSERT(mDisplay->isInitialized()); - CLDeviceImpl::CreateDatas createDatas; // Convert Vk device type to CL equivalent cl_device_type type = CL_DEVICE_TYPE_DEFAULT; - switch (GetImplAs(mDisplay)->getRenderer()->getPhysicalDeviceProperties().deviceType) + switch (mRenderer->getPhysicalDeviceProperties().deviceType) { case VK_PHYSICAL_DEVICE_TYPE_CPU: type |= CL_DEVICE_TYPE_CPU; @@ -107,8 +103,7 @@ CLDeviceImpl::CreateDatas CLPlatformVk::createDevices() const } createDatas.emplace_back(type, [this](const cl::Device &device) { - return CLDeviceVk::Ptr( - new CLDeviceVk(device, GetImplAs(mDisplay)->getRenderer())); + return CLDeviceVk::Ptr(new CLDeviceVk(device, mRenderer)); }); return createDatas; } @@ -118,7 +113,7 @@ angle::Result CLPlatformVk::createContext(cl::Context &context, bool userSync, CLContextImpl::Ptr *contextOut) { - *contextOut = CLContextImpl::Ptr(new (std::nothrow) CLContextVk(context, mDisplay, devices)); + *contextOut = CLContextImpl::Ptr(new (std::nothrow) CLContextVk(context, devices)); if (*contextOut == nullptr) { ANGLE_CL_RETURN_ERROR(CL_OUT_OF_HOST_MEMORY); @@ -131,11 +126,8 @@ angle::Result CLPlatformVk::createContextFromType(cl::Context &context, bool userSync, CLContextImpl::Ptr *contextOut) { - ASSERT(mDisplay); - ASSERT(mDisplay->isInitialized()); - const VkPhysicalDeviceType &vkPhysicalDeviceType = - GetImplAs(mDisplay)->getRenderer()->getPhysicalDeviceProperties().deviceType; + getRenderer()->getPhysicalDeviceProperties().deviceType; if (deviceType.isSet(CL_DEVICE_TYPE_CPU) && vkPhysicalDeviceType != VK_PHYSICAL_DEVICE_TYPE_CPU) { @@ -168,7 +160,7 @@ angle::Result CLPlatformVk::createContextFromType(cl::Context &context, } } - *contextOut = CLContextImpl::Ptr(new (std::nothrow) CLContextVk(context, mDisplay, devices)); + *contextOut = CLContextImpl::Ptr(new (std::nothrow) CLContextVk(context, devices)); if (*contextOut == nullptr) { ANGLE_CL_RETURN_ERROR(CL_OUT_OF_HOST_MEMORY); @@ -183,8 +175,14 @@ angle::Result CLPlatformVk::unloadCompiler() void CLPlatformVk::Initialize(CreateFuncs &createFuncs) { - createFuncs.emplace_back( - [](const cl::Platform &platform) { return Ptr(new CLPlatformVk(platform)); }); + createFuncs.emplace_back([](const cl::Platform &platform) -> CLPlatformImpl::Ptr { + CLPlatformVk::Ptr platformVk = CLPlatformVk::Ptr(new (std::nothrow) CLPlatformVk(platform)); + if (platformVk == nullptr || IsError(platformVk->initBackendRenderer())) + { + return Ptr(nullptr); + } + return Ptr(std::move(platformVk)); + }); } const std::string &CLPlatformVk::GetVersionString() @@ -197,17 +195,76 @@ const std::string &CLPlatformVk::GetVersionString() } CLPlatformVk::CLPlatformVk(const cl::Platform &platform) - : CLPlatformImpl(platform), mBlobCache(1024 * 1024) + : CLPlatformImpl(platform), vk::Context(new vk::Renderer()), mBlobCache(1024 * 1024) +{} + +void CLPlatformVk::handleError(VkResult result, + const char *file, + const char *function, + unsigned int line) +{ + ASSERT(result != VK_SUCCESS); + + std::stringstream errorStream; + errorStream << "Internal Vulkan error (" << result << "): " << VulkanResultString(result) + << ", in " << file << ", " << function << ":" << line << "."; + std::string errorString = errorStream.str(); + + if (result == VK_ERROR_DEVICE_LOST) + { + WARN() << errorString; + mRenderer->notifyDeviceLost(); + } +} + +angle::NativeWindowSystem CLPlatformVk::getWindowSystem() { - // Select vulkan backend - const EGLint attribList[] = {EGL_PLATFORM_ANGLE_TYPE_ANGLE, - EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE, EGL_NONE}; - egl::AttributeMap attribMap = egl::AttributeMap::CreateFromIntArray(attribList); - attribMap.initializeWithoutValidation(); +#if defined(ANGLE_ENABLE_VULKAN) +# if defined(ANGLE_PLATFORM_LINUX) +# if defined(ANGLE_USE_GBM) + return angle::NativeWindowSystem::Gbm; +# elif defined(ANGLE_USE_X11) + return angle::NativeWindowSystem::X11; +# elif defined(ANGLE_USE_WAYLAND) + return angle::NativeWindowSystem::Wayland; +# else + handleError(VK_ERROR_INCOMPATIBLE_DRIVER, __FILE__, __func__, __LINE__); + return angle::NativeWindowSystem::Other; +# endif +# elif defined(ANGLE_PLATFORM_ANDROID) + return angle::NativeWindowSystem::Other; +# else + handleError(VK_ERROR_INCOMPATIBLE_DRIVER, __FILE__, __func__, __LINE__); + return angle::NativeWindowSystem::Other; +# endif +#elif + UNREACHABLE(); +#endif +} - mDisplay = egl::Display::GetDisplayFromNativeDisplay(EGL_PLATFORM_ANGLE_ANGLE, - EGL_DEFAULT_DISPLAY, attribMap); - ANGLE_CL_IMPL_TRY(InitBackendRenderer(mDisplay)); +const char *CLPlatformVk::getWSIExtension() +{ +#if defined(ANGLE_ENABLE_VULKAN) +# if defined(ANGLE_PLATFORM_LINUX) +# if defined(ANGLE_USE_GBM) + return nullptr; +# elif defined(ANGLE_USE_X11) + return VK_KHR_XCB_SURFACE_EXTENSION_NAME; +# elif defined(ANGLE_USE_WAYLAND) + return VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME; +# else + handleError(VK_ERROR_INCOMPATIBLE_DRIVER, __FILE__, __func__, __LINE__); + return nullptr; +# endif +# elif defined(ANGLE_PLATFORM_ANDROID) + return VK_KHR_ANDROID_SURFACE_EXTENSION_NAME; +# else + handleError(VK_ERROR_INCOMPATIBLE_DRIVER, __FILE__, __func__, __LINE__); + return nullptr; +# endif +#elif + UNREACHABLE(); +#endif } // vk::GlobalOps diff --git a/src/libANGLE/renderer/vulkan/CLPlatformVk.h b/src/libANGLE/renderer/vulkan/CLPlatformVk.h index d5ae672127f..1ce95f18115 100644 --- a/src/libANGLE/renderer/vulkan/CLPlatformVk.h +++ b/src/libANGLE/renderer/vulkan/CLPlatformVk.h @@ -9,6 +9,7 @@ #define LIBANGLE_RENDERER_VULKAN_CLPLATFORMVK_H_ #include "common/MemoryBuffer.h" +#include "libANGLE/angletypes.h" #include "libANGLE/renderer/CLPlatformImpl.h" #include "libANGLE/renderer/vulkan/vk_utils.h" @@ -19,9 +20,11 @@ namespace rx { -class CLPlatformVk : public CLPlatformImpl, vk::GlobalOps +class CLPlatformVk : public CLPlatformImpl, public vk::Context, public vk::GlobalOps { public: + using Ptr = std::unique_ptr; + ~CLPlatformVk() override; Info createInfo() const override; @@ -44,6 +47,14 @@ class CLPlatformVk : public CLPlatformImpl, vk::GlobalOps static constexpr cl_version GetVersion(); static const std::string &GetVersionString(); + angle::Result initBackendRenderer(); + + // vk::Context + void handleError(VkResult result, + const char *file, + const char *function, + unsigned int line) override; + // vk::GlobalOps void putBlob(const angle::BlobCacheKey &key, const angle::MemoryBuffer &value) override; bool getBlob(const angle::BlobCacheKey &key, angle::BlobCacheValue *valueOut) override; @@ -53,7 +64,10 @@ class CLPlatformVk : public CLPlatformImpl, vk::GlobalOps private: explicit CLPlatformVk(const cl::Platform &platform); - egl::Display *mDisplay; + + angle::NativeWindowSystem getWindowSystem(); + const char *getWSIExtension(); + const char *getWSILayer() { return nullptr; } mutable std::mutex mBlobCacheMutex; angle::SizedMRUCache mBlobCache;