Commit 93aff937 by John Plate Committed by Commit Bot

Refactor CL platform object

Move GetPlatformInfo implementation from stubs to cl::Platform, because stubs are meant to be small. Also move CLPlatformImpl::Info instance to cl::Platform as it is more efficient to be accessed from there, and more consistent with cl::Device. Bug: angleproject:5904 Change-Id: I4c0ba6390467d6ef357e38a5ef08f54b0a88d423 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2880663 Commit-Queue: John Plate <jplate@google.com> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarCody Northrop <cnorthrop@google.com>
parent e369a282
......@@ -7,6 +7,8 @@
#include "libANGLE/CLPlatform.h"
#include <cstring>
namespace cl
{
......@@ -24,14 +26,74 @@ bool IsDeviceTypeMatch(cl_device_type select, cl_device_type type)
Platform::~Platform() = default;
void Platform::CreatePlatform(const cl_icd_dispatch &dispatch,
rx::CLPlatformImpl::InitData &initData)
cl_int Platform::getInfo(PlatformInfo name, size_t valueSize, void *value, size_t *sizeRet)
{
rx::CLDeviceImpl::InitList deviceInitList = initData->getDevices();
if (!deviceInitList.empty())
const void *copyValue = nullptr;
size_t copySize = 0u;
switch (name)
{
GetList().emplace_back(new Platform(dispatch, initData, std::move(deviceInitList)));
case PlatformInfo::Profile:
copyValue = mInfo.mProfile.c_str();
copySize = mInfo.mProfile.length() + 1u;
break;
case PlatformInfo::Version:
copyValue = mInfo.mVersionStr.c_str();
copySize = mInfo.mVersionStr.length() + 1u;
break;
case PlatformInfo::NumericVersion:
copyValue = &mInfo.mVersion;
copySize = sizeof(mInfo.mVersion);
break;
case PlatformInfo::Name:
copyValue = mInfo.mName.c_str();
copySize = mInfo.mName.length() + 1u;
break;
case PlatformInfo::Vendor:
copyValue = kVendor;
copySize = sizeof(kVendor);
break;
case PlatformInfo::Extensions:
copyValue = mInfo.mExtensions.c_str();
copySize = mInfo.mExtensions.length() + 1u;
break;
case PlatformInfo::ExtensionsWithVersion:
if (mInfo.mExtensionsWithVersion.empty())
{
return CL_INVALID_VALUE;
}
copyValue = mInfo.mExtensionsWithVersion.data();
copySize = mInfo.mExtensionsWithVersion.size() *
sizeof(decltype(mInfo.mExtensionsWithVersion)::value_type);
break;
case PlatformInfo::HostTimerResolution:
copyValue = &mInfo.mHostTimerRes;
copySize = sizeof(mInfo.mHostTimerRes);
break;
case PlatformInfo::IcdSuffix:
copyValue = kIcdSuffix;
copySize = sizeof(kIcdSuffix);
break;
default:
return CL_INVALID_VALUE;
}
if (value != nullptr)
{
if (valueSize < copySize)
{
return CL_INVALID_VALUE;
}
if (copyValue != nullptr)
{
std::memcpy(value, copyValue, copySize);
}
}
if (sizeRet != nullptr)
{
*sizeRet = copySize;
}
return CL_SUCCESS;
}
cl_int Platform::getDeviceIDs(cl_device_type deviceType,
......@@ -60,11 +122,22 @@ cl_int Platform::getDeviceIDs(cl_device_type deviceType,
return found == 0u ? CL_DEVICE_NOT_FOUND : CL_SUCCESS;
}
void Platform::CreatePlatform(const cl_icd_dispatch &dispatch,
rx::CLPlatformImpl::InitData &initData)
{
rx::CLDeviceImpl::InitList deviceInitList = initData.first->getDevices();
if (!deviceInitList.empty())
{
GetList().emplace_back(new Platform(dispatch, initData, std::move(deviceInitList)));
}
}
Platform::Platform(const cl_icd_dispatch &dispatch,
rx::CLPlatformImpl::InitData &initData,
rx::CLDeviceImpl::InitList &&deviceInitList)
: _cl_platform_id(dispatch),
mImpl(std::move(initData)),
mImpl(std::move(initData.first)),
mInfo(std::move(initData.second)),
mDevices(Device::CreateDevices(*this, std::move(deviceInitList)))
{}
......
......@@ -25,17 +25,11 @@ class Platform final : public _cl_platform_id, public Object
~Platform();
const char *getProfile() const;
const char *getVersionString() const;
cl_version getVersion() const;
const char *getName() const;
const char *getExtensions() const;
const rx::NameVersionVector &getExtensionsWithVersion() const;
cl_ulong getHostTimerResolution() const;
bool hasDevice(const Device *device) const;
const Device::PtrList &getDevices() const;
cl_int getInfo(PlatformInfo name, size_t valueSize, void *value, size_t *sizeRet);
cl_int getDeviceIDs(cl_device_type deviceType,
cl_uint numEntries,
Device **devices,
......@@ -49,7 +43,6 @@ class Platform final : public _cl_platform_id, public Object
static bool IsValidOrDefault(const Platform *platform);
static constexpr const char *GetVendor();
static constexpr const char *GetIcdSuffix();
private:
Platform(const cl_icd_dispatch &dispatch,
......@@ -59,47 +52,13 @@ class Platform final : public _cl_platform_id, public Object
static PtrList &GetList();
const rx::CLPlatformImpl::Ptr mImpl;
const rx::CLPlatformImpl::Info mInfo;
const Device::PtrList mDevices;
static constexpr char kVendor[] = "ANGLE";
static constexpr char kIcdSuffix[] = "ANGLE";
};
inline const char *Platform::getProfile() const
{
return mImpl->getInfo().mProfile.c_str();
}
inline const char *Platform::getVersionString() const
{
return mImpl->getInfo().mVersionStr.c_str();
}
inline cl_version Platform::getVersion() const
{
return mImpl->getInfo().mVersion;
}
inline const char *Platform::getName() const
{
return mImpl->getInfo().mName.c_str();
}
inline const char *Platform::getExtensions() const
{
return mImpl->getInfo().mExtensions.c_str();
}
inline const rx::NameVersionVector &Platform::getExtensionsWithVersion() const
{
return mImpl->getInfo().mExtensionList;
}
inline cl_ulong Platform::getHostTimerResolution() const
{
return mImpl->getInfo().mHostTimerRes;
}
inline bool Platform::hasDevice(const Device *device) const
{
return std::find_if(mDevices.cbegin(), mDevices.cend(), [=](const Device::Ptr &ptr) {
......@@ -147,11 +106,6 @@ constexpr const char *Platform::GetVendor()
return kVendor;
}
constexpr const char *Platform::GetIcdSuffix()
{
return kIcdSuffix;
}
} // namespace cl
#endif // LIBANGLE_CLPLATFORM_H_
......@@ -18,24 +18,9 @@ CLPlatformImpl::Info::Info(Info &&) = default;
CLPlatformImpl::Info &CLPlatformImpl::Info::operator=(Info &&) = default;
CLPlatformImpl::Info::Info(std::string &&profile,
std::string &&versionStr,
cl_version version,
std::string &&name,
std::string &&extensions,
NameVersionVector &&extensionList,
cl_ulong hostTimerRes)
: mProfile(std::move(profile)),
mVersionStr(std::move(versionStr)),
mVersion(version),
mName(std::move(name)),
mExtensions(std::move(extensions)),
mExtensionList(std::move(extensionList)),
mHostTimerRes(hostTimerRes)
{}
CLPlatformImpl::CLPlatformImpl(Info &&info) : mInfo(std::move(info)) {}
CLPlatformImpl::~CLPlatformImpl() = default;
bool CLPlatformImpl::Info::isValid() const
{
return !mProfile.empty();
}
} // namespace rx
......@@ -16,10 +16,6 @@ namespace rx
class CLPlatformImpl : angle::NonCopyable
{
public:
using Ptr = std::unique_ptr<CLPlatformImpl>;
using InitData = Ptr;
using InitList = std::list<InitData>;
struct Info
{
Info();
......@@ -31,39 +27,27 @@ class CLPlatformImpl : angle::NonCopyable
Info(Info &&);
Info &operator=(Info &&);
Info(std::string &&profile,
std::string &&versionStr,
cl_version version,
std::string &&name,
std::string &&extensions,
NameVersionVector &&extensionList,
cl_ulong hostTimerRes);
bool isValid() const;
std::string mProfile;
std::string mVersionStr;
cl_version mVersion;
std::string mName;
std::string mExtensions;
NameVersionVector mExtensionList;
NameVersionVector mExtensionsWithVersion;
cl_ulong mHostTimerRes;
};
explicit CLPlatformImpl(Info &&info);
virtual ~CLPlatformImpl();
using Ptr = std::unique_ptr<CLPlatformImpl>;
using InitData = std::pair<Ptr, Info>;
using InitList = std::list<InitData>;
const Info &getInfo();
CLPlatformImpl() = default;
virtual ~CLPlatformImpl() = default;
virtual CLDeviceImpl::InitList getDevices() = 0;
protected:
const Info mInfo;
};
inline const CLPlatformImpl::Info &CLPlatformImpl::getInfo()
{
return mInfo;
}
} // namespace rx
#endif // LIBANGLE_RENDERER_CLPLATFORMIMPL_H_
......@@ -107,40 +107,49 @@ CLPlatformCL::InitList CLPlatformCL::GetPlatforms(bool isIcd)
{
if (vendorIt->platform != nullptr)
{
rx::CLPlatformImpl::Ptr impl = Create(vendorIt->platform);
if (impl)
Info info = GetInfo(vendorIt->platform);
if (info.isValid())
{
initList.emplace_back(std::move(impl));
initList.emplace_back(new CLPlatformCL(vendorIt->platform), std::move(info));
}
}
}
return initList;
}
CLPlatformCL::CLPlatformCL(cl_platform_id platform, Info &&info)
: CLPlatformImpl(std::move(info)), mPlatform(platform)
{}
#define ANGLE_GET_INFO(info, size, param, size_ret) \
result = platform->getDispatch().clGetPlatformInfo(platform, info, size, param, size_ret)
#define ANGLE_TRY_GET_INFO(info, size, param, size_ret) \
do \
{ \
ANGLE_GET_INFO(info, size, param, size_ret); \
if (result != CL_SUCCESS) \
{ \
ERR() << "Failed to query CL platform info"; \
return std::unique_ptr<CLPlatformCL>(); \
} \
CLPlatformCL::CLPlatformCL(cl_platform_id platform) : mPlatform(platform) {}
#define ANGLE_GET_INFO_SIZE(name, size_ret) \
platform->getDispatch().clGetPlatformInfo(platform, name, 0u, nullptr, size_ret)
#define ANGLE_GET_INFO_SIZE_RET(name, size_ret) \
do \
{ \
if (ANGLE_GET_INFO_SIZE(name, size_ret) != CL_SUCCESS) \
{ \
ERR() << "Failed to query CL platform info for " << name; \
return info; \
} \
} while (0)
std::unique_ptr<CLPlatformCL> CLPlatformCL::Create(cl_platform_id platform)
#define ANGLE_GET_INFO(name, size, param) \
platform->getDispatch().clGetPlatformInfo(platform, name, size, param, nullptr)
#define ANGLE_GET_INFO_RET(name, size, param) \
do \
{ \
if (ANGLE_GET_INFO(name, size, param) != CL_SUCCESS) \
{ \
ERR() << "Failed to query CL platform info for " << name; \
return info; \
} \
} while (0)
CLPlatformImpl::Info CLPlatformCL::GetInfo(cl_platform_id platform)
{
cl_int result = 0;
size_t paramSize = 0u;
std::vector<std::string::value_type> param;
CLPlatformImpl::Info info;
Info info;
size_t valueSize = 0u;
std::vector<char> valString;
// Verify that the platform is valid
ASSERT(platform != nullptr);
......@@ -149,24 +158,24 @@ std::unique_ptr<CLPlatformCL> CLPlatformCL::Create(cl_platform_id platform)
ASSERT(platform->getDispatch().clGetDeviceInfo != nullptr);
// Skip ANGLE CL implementation to prevent passthrough loop
ANGLE_TRY_GET_INFO(CL_PLATFORM_VENDOR, 0u, nullptr, &paramSize);
param.resize(paramSize, '\0');
ANGLE_TRY_GET_INFO(CL_PLATFORM_VENDOR, paramSize, param.data(), nullptr);
if (std::string(param.data()).compare(cl::Platform::GetVendor()) == 0)
ANGLE_GET_INFO_SIZE_RET(CL_PLATFORM_VENDOR, &valueSize);
valString.resize(valueSize, '\0');
ANGLE_GET_INFO_RET(CL_PLATFORM_VENDOR, valueSize, valString.data());
if (std::string(valString.data()).compare(cl::Platform::GetVendor()) == 0)
{
ERR() << "Tried to create CL pass-through back end for ANGLE library";
return std::unique_ptr<CLPlatformCL>();
return info;
}
// Skip platform if it is not ICD compatible
ANGLE_TRY_GET_INFO(CL_PLATFORM_EXTENSIONS, 0u, nullptr, &paramSize);
param.resize(paramSize, '\0');
ANGLE_TRY_GET_INFO(CL_PLATFORM_EXTENSIONS, paramSize, param.data(), nullptr);
info.mExtensions.assign(param.data());
ANGLE_GET_INFO_SIZE_RET(CL_PLATFORM_EXTENSIONS, &valueSize);
valString.resize(valueSize, '\0');
ANGLE_GET_INFO_RET(CL_PLATFORM_EXTENSIONS, valueSize, valString.data());
info.mExtensions.assign(valString.data());
if (info.mExtensions.find("cl_khr_icd") == std::string::npos)
{
WARN() << "CL platform is not ICD compatible";
return std::unique_ptr<CLPlatformCL>();
return info;
}
// Filter out extensions which are not (yet) supported to be passed through
......@@ -202,15 +211,10 @@ std::unique_ptr<CLPlatformCL> CLPlatformCL::Create(cl_platform_id platform)
}
// Fetch common platform info
ANGLE_TRY_GET_INFO(CL_PLATFORM_PROFILE, 0u, nullptr, &paramSize);
param.resize(paramSize, '\0');
ANGLE_TRY_GET_INFO(CL_PLATFORM_PROFILE, paramSize, param.data(), nullptr);
info.mProfile.assign(param.data());
ANGLE_TRY_GET_INFO(CL_PLATFORM_VERSION, 0u, nullptr, &paramSize);
param.resize(paramSize, '\0');
ANGLE_TRY_GET_INFO(CL_PLATFORM_VERSION, paramSize, param.data(), nullptr);
info.mVersionStr.assign(param.data());
ANGLE_GET_INFO_SIZE_RET(CL_PLATFORM_VERSION, &valueSize);
valString.resize(valueSize, '\0');
ANGLE_GET_INFO_RET(CL_PLATFORM_VERSION, valueSize, valString.data());
info.mVersionStr.assign(valString.data());
info.mVersionStr += " (ANGLE " ANGLE_VERSION_STRING ")";
const std::string::size_type spacePos = info.mVersionStr.find(' ');
......@@ -218,7 +222,7 @@ std::unique_ptr<CLPlatformCL> CLPlatformCL::Create(cl_platform_id platform)
if (spacePos == std::string::npos || dotPos == std::string::npos)
{
ERR() << "Failed to extract version from OpenCL version string: " << info.mVersionStr;
return std::unique_ptr<CLPlatformCL>();
return info;
}
const cl_uint major =
static_cast<cl_uint>(std::strtol(&info.mVersionStr[spacePos + 1u], nullptr, 10));
......@@ -227,11 +231,11 @@ std::unique_ptr<CLPlatformCL> CLPlatformCL::Create(cl_platform_id platform)
if (major == 0)
{
ERR() << "Failed to extract version from OpenCL version string: " << info.mVersionStr;
return std::unique_ptr<CLPlatformCL>();
return info;
}
ANGLE_GET_INFO(CL_PLATFORM_NUMERIC_VERSION, sizeof(info.mVersion), &info.mVersion, nullptr);
if (result != CL_SUCCESS)
if (ANGLE_GET_INFO(CL_PLATFORM_NUMERIC_VERSION, sizeof(info.mVersion), &info.mVersion) !=
CL_SUCCESS)
{
info.mVersion = CL_MAKE_VERSION(major, minor, 0);
}
......@@ -242,23 +246,24 @@ std::unique_ptr<CLPlatformCL> CLPlatformCL::Create(cl_platform_id platform)
<< " does not match version string: " << info.mVersionStr;
}
ANGLE_TRY_GET_INFO(CL_PLATFORM_NAME, 0u, nullptr, &paramSize);
param.resize(paramSize, '\0');
ANGLE_TRY_GET_INFO(CL_PLATFORM_NAME, paramSize, param.data(), nullptr);
ANGLE_GET_INFO_SIZE_RET(CL_PLATFORM_NAME, &valueSize);
valString.resize(valueSize, '\0');
ANGLE_GET_INFO_RET(CL_PLATFORM_NAME, valueSize, valString.data());
info.mName.assign("ANGLE pass-through -> ");
info.mName += param.data();
info.mName += valString.data();
ANGLE_GET_INFO(CL_PLATFORM_EXTENSIONS_WITH_VERSION, 0u, nullptr, &paramSize);
if (result == CL_SUCCESS)
if (ANGLE_GET_INFO_SIZE(CL_PLATFORM_EXTENSIONS_WITH_VERSION, &valueSize) == CL_SUCCESS &&
(valueSize % sizeof(decltype(info.mExtensionsWithVersion)::value_type)) == 0u)
{
info.mExtensionList.resize(paramSize);
ANGLE_TRY_GET_INFO(CL_PLATFORM_EXTENSIONS_WITH_VERSION, paramSize,
info.mExtensionList.data(), nullptr);
info.mExtensionsWithVersion.resize(
valueSize / sizeof(decltype(info.mExtensionsWithVersion)::value_type));
ANGLE_GET_INFO_RET(CL_PLATFORM_EXTENSIONS_WITH_VERSION, valueSize,
info.mExtensionsWithVersion.data());
// Filter out extensions which are not (yet) supported to be passed through
const ExtensionSet &supported = GetSupportedExtensions();
NameVersionVector::const_iterator extIt = info.mExtensionList.cbegin();
while (extIt != info.mExtensionList.cend())
const ExtensionSet &supported = GetSupportedExtensions();
auto extIt = info.mExtensionsWithVersion.cbegin();
while (extIt != info.mExtensionsWithVersion.cend())
{
if (supported.find(extIt->name) != supported.cend())
{
......@@ -266,15 +271,21 @@ std::unique_ptr<CLPlatformCL> CLPlatformCL::Create(cl_platform_id platform)
}
else
{
extIt = info.mExtensionList.erase(extIt);
extIt = info.mExtensionsWithVersion.erase(extIt);
}
}
}
ANGLE_GET_INFO(CL_PLATFORM_HOST_TIMER_RESOLUTION, sizeof(info.mHostTimerRes),
&info.mHostTimerRes, nullptr);
&info.mHostTimerRes);
// Get this last, so the info is invalid if anything before fails
ANGLE_GET_INFO_SIZE_RET(CL_PLATFORM_PROFILE, &valueSize);
valString.resize(valueSize, '\0');
ANGLE_GET_INFO_RET(CL_PLATFORM_PROFILE, valueSize, valString.data());
info.mProfile.assign(valString.data());
return std::unique_ptr<CLPlatformCL>(new CLPlatformCL(platform, std::move(info)));
return info;
}
} // namespace rx
......@@ -25,9 +25,9 @@ class CLPlatformCL : public CLPlatformImpl
static InitList GetPlatforms(bool isIcd);
private:
CLPlatformCL(cl_platform_id platform, Info &&info);
explicit CLPlatformCL(cl_platform_id platform);
static std::unique_ptr<CLPlatformCL> Create(cl_platform_id platform);
static Info GetInfo(cl_platform_id platform);
const cl_platform_id mPlatform;
};
......
......@@ -33,8 +33,6 @@ std::string CreateExtensionString(const NameVersionVector &extList)
}
} // anonymous namespace
CLPlatformVk::CLPlatformVk(Info &&info) : CLPlatformImpl(std::move(info)) {}
CLPlatformVk::~CLPlatformVk() = default;
CLDeviceImpl::InitList CLPlatformVk::getDevices()
......@@ -53,13 +51,18 @@ CLPlatformVk::InitList CLPlatformVk::GetPlatforms()
NameVersionVector extList = {
cl_name_version{CL_MAKE_VERSION(1, 0, 0), "cl_khr_icd"},
cl_name_version{CL_MAKE_VERSION(1, 0, 0), "cl_khr_extended_versioning"}};
std::string extensions = CreateExtensionString(extList);
Info info("FULL_PROFILE", std::string(GetVersionString()), GetVersion(), "ANGLE Vulkan",
std::move(extensions), std::move(extList), 0u);
Info info;
info.mProfile.assign("FULL_PROFILE");
info.mVersionStr.assign(GetVersionString());
info.mVersion = GetVersion();
info.mName.assign("ANGLE Vulkan");
info.mExtensions.assign(CreateExtensionString(extList));
info.mExtensionsWithVersion = std::move(extList);
info.mHostTimerRes = 0u;
InitList initList;
initList.emplace_back(new CLPlatformVk(std::move(info)));
initList.emplace_back(new CLPlatformVk, std::move(info));
return initList;
}
......@@ -71,4 +74,6 @@ const std::string &CLPlatformVk::GetVersionString()
return *sVersion;
}
CLPlatformVk::CLPlatformVk() = default;
} // namespace rx
......@@ -27,7 +27,7 @@ class CLPlatformVk : public CLPlatformImpl
static const std::string &GetVersionString();
private:
explicit CLPlatformVk(Info &&info);
CLPlatformVk();
};
constexpr cl_version CLPlatformVk::GetVersion()
......
......@@ -21,8 +21,6 @@
#include "libANGLE/Debug.h"
#include <cstring>
#define WARN_NOT_SUPPORTED(command) \
do \
{ \
......@@ -105,73 +103,8 @@ cl_int GetPlatformInfo(Platform *platform,
void *param_value,
size_t *param_value_size_ret)
{
cl_version version = 0u;
cl_ulong hostTimerRes = 0u;
const void *value = nullptr;
size_t value_size = 0u;
switch (param_name)
{
case PlatformInfo::Profile:
value = platform->getProfile();
value_size = std::strlen(platform->getProfile()) + 1u;
break;
case PlatformInfo::Version:
value = platform->getVersionString();
value_size = std::strlen(platform->getVersionString()) + 1u;
break;
case PlatformInfo::NumericVersion:
version = platform->getVersion();
value = &version;
value_size = sizeof(version);
break;
case PlatformInfo::Name:
value = platform->getName();
value_size = std::strlen(platform->getName()) + 1u;
break;
case PlatformInfo::Vendor:
value = Platform::GetVendor();
value_size = std::strlen(Platform::GetVendor()) + 1u;
break;
case PlatformInfo::Extensions:
value = platform->getExtensions();
value_size = std::strlen(platform->getExtensions()) + 1u;
break;
case PlatformInfo::ExtensionsWithVersion:
if (platform->getExtensionsWithVersion().empty())
{
return CL_INVALID_VALUE;
}
value = platform->getExtensionsWithVersion().data();
value_size = platform->getExtensionsWithVersion().size() * sizeof(cl_name_version);
break;
case PlatformInfo::HostTimerResolution:
hostTimerRes = platform->getHostTimerResolution();
value = &hostTimerRes;
value_size = sizeof(hostTimerRes);
break;
case PlatformInfo::IcdSuffix:
value = Platform::GetIcdSuffix();
value_size = std::strlen(Platform::GetIcdSuffix()) + 1u;
break;
default:
return CL_INVALID_VALUE;
}
if (param_value != nullptr)
{
if (param_value_size < value_size)
{
return CL_INVALID_VALUE;
}
if (value != nullptr)
{
std::memcpy(param_value, value, value_size);
}
}
if (param_value_size_ret != nullptr)
{
*param_value_size_ret = value_size;
}
return CL_SUCCESS;
return (platform != nullptr ? platform : Platform::GetDefault())
->getInfo(param_name, param_value_size, param_value, param_value_size_ret);
}
cl_int GetDeviceIDs(Platform *platform,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment