Hash :
af56ca61
Author :
Date :
2024-01-11T13:14:32
OpenCL/Vulkan: Initial support for context Introduce OpenCL context object creation for Vulkan backend in ANGLE. Bug: angleproject:8499 Change-Id: I56794649c87d5cf1490e496525e6534c8f90ce32 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5233366 Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Austin Annestrand <a.annestrand@samsung.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
//
// Copyright 2021 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.
//
// CLPlatformVk.cpp: Implements the class methods for CLPlatformVk.
#include "libANGLE/renderer/vulkan/CLPlatformVk.h"
#include "libANGLE/renderer/vulkan/CLContextVk.h"
#include "libANGLE/renderer/vulkan/CLDeviceVk.h"
#include "libANGLE/renderer/vulkan/DisplayVk.h"
#include "libANGLE/renderer/vulkan/RendererVk.h"
#include "libANGLE/CLPlatform.h"
#include "libANGLE/cl_utils.h"
#include "anglebase/no_destructor.h"
#include "common/angle_version_info.h"
namespace rx
{
namespace
{
std::string CreateExtensionString(const NameVersionVector &extList)
{
std::string extensions;
for (const cl_name_version &ext : extList)
{
extensions += ext.name;
extensions += ' ';
}
if (!extensions.empty())
{
extensions.pop_back();
}
return extensions;
}
angle::Result InitBackendRenderer(egl::Display *display)
{
// Initialize the backend RendererVk by initializing a dummy/default EGL display object
// TODO(aannestrand) Implement display-less RendererVk 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);
}
return angle::Result::Continue;
}
} // namespace
CLPlatformVk::~CLPlatformVk()
{
mDisplay->getImplementation()->terminate();
}
CLPlatformImpl::Info CLPlatformVk::createInfo() const
{
NameVersionVector extList = {
cl_name_version{CL_MAKE_VERSION(3, 0, 0), "cl_khr_icd"},
cl_name_version{CL_MAKE_VERSION(3, 0, 0), "cl_khr_extended_versioning"}};
Info info;
info.name.assign("ANGLE Vulkan");
info.profile.assign("FULL_PROFILE");
info.versionStr.assign(GetVersionString());
info.hostTimerRes = 0u;
info.extensionsWithVersion = std::move(extList);
info.version = GetVersion();
info.initializeExtensions(CreateExtensionString(extList));
return info;
}
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)
{
case VK_PHYSICAL_DEVICE_TYPE_CPU:
type |= CL_DEVICE_TYPE_CPU;
break;
case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
type |= CL_DEVICE_TYPE_GPU;
break;
case VK_PHYSICAL_DEVICE_TYPE_OTHER:
case VK_PHYSICAL_DEVICE_TYPE_MAX_ENUM:
// The default OpenCL device must not be a CL_DEVICE_TYPE_CUSTOM device.
// Thus, we override type bitfield to custom only.
type = CL_DEVICE_TYPE_CUSTOM;
break;
}
createDatas.emplace_back(type, [this](const cl::Device &device) {
return CLDeviceVk::Ptr(
new CLDeviceVk(device, GetImplAs<DisplayVk>(mDisplay)->getRenderer()));
});
return createDatas;
}
angle::Result CLPlatformVk::createContext(cl::Context &context,
const cl::DevicePtrs &devices,
bool userSync,
CLContextImpl::Ptr *contextOut)
{
*contextOut = CLContextImpl::Ptr(new (std::nothrow) CLContextVk(context, mDisplay, devices));
if (*contextOut == nullptr)
{
ANGLE_CL_RETURN_ERROR(CL_OUT_OF_HOST_MEMORY);
}
return angle::Result::Continue;
}
angle::Result CLPlatformVk::createContextFromType(cl::Context &context,
cl::DeviceType deviceType,
bool userSync,
CLContextImpl::Ptr *contextOut)
{
ASSERT(mDisplay);
ASSERT(mDisplay->isInitialized());
const VkPhysicalDeviceType &vkPhysicalDeviceType =
GetImplAs<DisplayVk>(mDisplay)->getRenderer()->getPhysicalDeviceProperties().deviceType;
if (deviceType.isSet(CL_DEVICE_TYPE_CPU) && vkPhysicalDeviceType != VK_PHYSICAL_DEVICE_TYPE_CPU)
{
ANGLE_CL_RETURN_ERROR(CL_DEVICE_NOT_FOUND);
}
else if (deviceType.isSet(CL_DEVICE_TYPE_GPU))
{
switch (vkPhysicalDeviceType)
{
case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
break;
default:
ANGLE_CL_RETURN_ERROR(CL_DEVICE_NOT_FOUND);
}
}
else
{
ANGLE_CL_RETURN_ERROR(CL_DEVICE_NOT_FOUND);
}
cl::DevicePtrs devices;
for (const auto &platformDevice : mPlatform.getDevices())
{
const auto &platformDeviceInfo = platformDevice->getInfo();
if (platformDeviceInfo.type.isSet(deviceType))
{
devices.push_back(platformDevice);
}
}
*contextOut = CLContextImpl::Ptr(new (std::nothrow) CLContextVk(context, mDisplay, devices));
if (*contextOut == nullptr)
{
ANGLE_CL_RETURN_ERROR(CL_OUT_OF_HOST_MEMORY);
}
return angle::Result::Continue;
}
angle::Result CLPlatformVk::unloadCompiler()
{
return angle::Result::Continue;
}
void CLPlatformVk::Initialize(CreateFuncs &createFuncs)
{
createFuncs.emplace_back(
[](const cl::Platform &platform) { return Ptr(new CLPlatformVk(platform)); });
}
const std::string &CLPlatformVk::GetVersionString()
{
static const angle::base::NoDestructor<const std::string> sVersion(
"OpenCL " + std::to_string(CL_VERSION_MAJOR(GetVersion())) + "." +
std::to_string(CL_VERSION_MINOR(GetVersion())) + " ANGLE " +
angle::GetANGLEVersionString());
return *sVersion;
}
CLPlatformVk::CLPlatformVk(const cl::Platform &platform) : CLPlatformImpl(platform)
{
mDisplay = egl::Display::GetDisplayFromNativeDisplay(EGL_PLATFORM_ANGLE_ANGLE,
EGL_DEFAULT_DISPLAY, egl::AttributeMap{});
ANGLE_CL_IMPL_TRY(InitBackendRenderer(mDisplay));
}
} // namespace rx