Skip to content

Commit

Permalink
CL/VK: Setup platform as provider of vk::Context
Browse files Browse the repository at this point in the history
Setup the CLPlatformVk to be the implementer of `vk::Context`.
Initialize the Renderer instance during platform setup. This removes the
dependency on the DisplayVk instance and as a consequence any associated
egl state.

Bug: angleproject:8515
Change-Id: If1848b002d6309911305ca3cb93c316ed7f48965
Signed-off-by: Gowtham Tammana <[email protected]>
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5406612
Commit-Queue: Austin Annestrand <[email protected]>
Reviewed-by: Shahbaz Youssefi <[email protected]>
  • Loading branch information
gowtham-sarc authored and Angle LUCI CQ committed Apr 2, 2024
1 parent e4fe461 commit bb70f5d
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 50 deletions.
7 changes: 2 additions & 5 deletions src/libANGLE/renderer/vulkan/CLContextVk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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<DisplayVk>(display)->getRenderer()),
vk::Context(getPlatform()->getRenderer()),
mAssociatedDevices(devicePtrs)
{}

Expand Down
5 changes: 1 addition & 4 deletions src/libANGLE/renderer/vulkan/CLContextVk.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@

#include <libANGLE/CLContext.h>
#include "libANGLE/CLDevice.h"
#include "libANGLE/Display.h"

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;

Expand Down
135 changes: 96 additions & 39 deletions src/libANGLE/renderer/vulkan/CLPlatformVk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
{
Expand All @@ -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
Expand All @@ -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<DisplayVk>(mDisplay)->getRenderer()->getPhysicalDeviceProperties().deviceType)
switch (mRenderer->getPhysicalDeviceProperties().deviceType)
{
case VK_PHYSICAL_DEVICE_TYPE_CPU:
type |= CL_DEVICE_TYPE_CPU;
Expand All @@ -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<DisplayVk>(mDisplay)->getRenderer()));
return CLDeviceVk::Ptr(new CLDeviceVk(device, mRenderer));
});
return createDatas;
}
Expand All @@ -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);
Expand All @@ -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<DisplayVk>(mDisplay)->getRenderer()->getPhysicalDeviceProperties().deviceType;
getRenderer()->getPhysicalDeviceProperties().deviceType;

if (deviceType.isSet(CL_DEVICE_TYPE_CPU) && vkPhysicalDeviceType != VK_PHYSICAL_DEVICE_TYPE_CPU)
{
Expand Down Expand Up @@ -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);
Expand All @@ -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()
Expand All @@ -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
Expand Down
18 changes: 16 additions & 2 deletions src/libANGLE/renderer/vulkan/CLPlatformVk.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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>;

~CLPlatformVk() override;

Info createInfo() const override;
Expand All @@ -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;
Expand All @@ -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<angle::BlobCacheKey, angle::MemoryBuffer> mBlobCache;
Expand Down

0 comments on commit bb70f5d

Please sign in to comment.