Commit c2fd3388 by John Plate Committed by Commit Bot

CL: Add front end object references to back end objects

Add front end object references to back end objects, which requires a significant amount of refactoring, because the back end objects have to be constructed during the construction of the front end objects, so that the references can be passed to the back end objects, which then can be passed to the front end member initialization. That would have been easier with inheritance than with PImpl. Bug: angleproject:5904 Change-Id: Ib58e6a698e76987bdd63cd8088f923424d6c622b Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2897249 Commit-Queue: John Plate <jplate@google.com> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarCody Northrop <cnorthrop@google.com>
parent a7ae63e4
...@@ -42,9 +42,6 @@ struct Dispatch ...@@ -42,9 +42,6 @@ struct Dispatch
constexpr const cl_icd_dispatch &getDispatch() { return *mDispatch; } constexpr const cl_icd_dispatch &getDispatch() { return *mDispatch; }
protected:
bool isCompatible(void *ptr) const { return ptr == &mDispatch; }
private: private:
// This has to be the first member to be OpenCL ICD compatible // This has to be the first member to be OpenCL ICD compatible
const cl_icd_dispatch *const mDispatch; const cl_icd_dispatch *const mDispatch;
......
...@@ -78,21 +78,22 @@ cl_int Context::getInfo(ContextInfo name, size_t valueSize, void *value, size_t ...@@ -78,21 +78,22 @@ cl_int Context::getInfo(ContextInfo name, size_t valueSize, void *value, size_t
bool Context::IsValid(const _cl_context *context) bool Context::IsValid(const _cl_context *context)
{ {
const Platform::PtrList &platforms = Platform::GetPlatforms(); const Platform::PtrList &platforms = Platform::GetPlatforms();
return std::find_if(platforms.cbegin(), platforms.cend(), [=](const Platform::Ptr &platform) { return std::find_if(platforms.cbegin(), platforms.cend(), [=](const PlatformPtr &platform) {
return platform->hasContext(context); return platform->hasContext(context);
}) != platforms.cend(); }) != platforms.cend();
} }
Context::Context(Platform &platform, Context::Context(Platform &platform,
PropArray &&properties, PropArray &&properties,
Device::RefList &&devices, DeviceRefList &&devices,
ContextErrorCB notify, ContextErrorCB notify,
void *userData, void *userData,
bool userSync, bool userSync,
cl_int *errcodeRet) cl_int *errcodeRet)
: _cl_context(platform.getDispatch()), : _cl_context(platform.getDispatch()),
mPlatform(platform), mPlatform(platform),
mImpl(platform.createContext(devices, ErrorCallback, this, userSync, errcodeRet)), mImpl(
platform.mImpl->createContext(*this, devices, ErrorCallback, this, userSync, errcodeRet)),
mProperties(std::move(properties)), mProperties(std::move(properties)),
mDevices(std::move(devices)), mDevices(std::move(devices)),
mNotify(notify), mNotify(notify),
...@@ -108,10 +109,14 @@ Context::Context(Platform &platform, ...@@ -108,10 +109,14 @@ Context::Context(Platform &platform,
cl_int *errcodeRet) cl_int *errcodeRet)
: _cl_context(platform.getDispatch()), : _cl_context(platform.getDispatch()),
mPlatform(platform), mPlatform(platform),
mImpl(platform.mImpl mImpl(platform.mImpl->createContextFromType(*this,
->createContextFromType(deviceType, ErrorCallback, this, userSync, errcodeRet)), deviceType,
ErrorCallback,
this,
userSync,
errcodeRet)),
mProperties(std::move(properties)), mProperties(std::move(properties)),
mDevices(mImpl ? platform.mapDevices(mImpl->getDevices()) : Device::RefList{}), mDevices(mImpl ? mImpl->getDevices() : DeviceRefList{}),
mNotify(notify), mNotify(notify),
mUserData(userData) mUserData(userData)
{} {}
......
...@@ -21,8 +21,7 @@ namespace cl ...@@ -21,8 +21,7 @@ namespace cl
class Context final : public _cl_context, public Object class Context final : public _cl_context, public Object
{ {
public: public:
using Ptr = std::unique_ptr<Context>; using PtrList = std::list<ContextPtr>;
using PtrList = std::list<Ptr>;
using RefPtr = RefPointer<Context>; using RefPtr = RefPointer<Context>;
using PropArray = std::vector<cl_context_properties>; using PropArray = std::vector<cl_context_properties>;
...@@ -40,7 +39,7 @@ class Context final : public _cl_context, public Object ...@@ -40,7 +39,7 @@ class Context final : public _cl_context, public Object
private: private:
Context(Platform &platform, Context(Platform &platform,
PropArray &&properties, PropArray &&properties,
Device::RefList &&devices, DeviceRefList &&devices,
ContextErrorCB notify, ContextErrorCB notify,
void *userData, void *userData,
bool userSync, bool userSync,
...@@ -62,7 +61,7 @@ class Context final : public _cl_context, public Object ...@@ -62,7 +61,7 @@ class Context final : public _cl_context, public Object
Platform &mPlatform; Platform &mPlatform;
const rx::CLContextImpl::Ptr mImpl; const rx::CLContextImpl::Ptr mImpl;
const PropArray mProperties; const PropArray mProperties;
const Device::RefList mDevices; const DeviceRefList mDevices;
const ContextErrorCB mNotify; const ContextErrorCB mNotify;
void *const mUserData; void *const mUserData;
......
...@@ -273,7 +273,7 @@ cl_int Device::getInfo(DeviceInfo name, size_t valueSize, void *value, size_t *v ...@@ -273,7 +273,7 @@ cl_int Device::getInfo(DeviceInfo name, size_t valueSize, void *value, size_t *v
// Handle all mapped values // Handle all mapped values
case DeviceInfo::Platform: case DeviceInfo::Platform:
valPointer = &mPlatform; valPointer = static_cast<cl_platform_id>(&mPlatform);
copyValue = &valPointer; copyValue = &valPointer;
copySize = sizeof(valPointer); copySize = sizeof(valPointer);
break; break;
...@@ -282,8 +282,9 @@ cl_int Device::getInfo(DeviceInfo name, size_t valueSize, void *value, size_t *v ...@@ -282,8 +282,9 @@ cl_int Device::getInfo(DeviceInfo name, size_t valueSize, void *value, size_t *v
{ {
return CL_INVALID_VALUE; return CL_INVALID_VALUE;
} }
copyValue = &mParent; valPointer = static_cast<cl_device_id>(mParent.get());
copySize = sizeof(mParent); copyValue = &valPointer;
copySize = sizeof(valPointer);
break; break;
case DeviceInfo::ReferenceCount: case DeviceInfo::ReferenceCount:
if (mInfo.mVersion < CL_MAKE_VERSION(1, 2, 0)) if (mInfo.mVersion < CL_MAKE_VERSION(1, 2, 0))
...@@ -323,67 +324,49 @@ cl_int Device::getInfo(DeviceInfo name, size_t valueSize, void *value, size_t *v ...@@ -323,67 +324,49 @@ cl_int Device::getInfo(DeviceInfo name, size_t valueSize, void *value, size_t *v
cl_int Device::createSubDevices(const cl_device_partition_property *properties, cl_int Device::createSubDevices(const cl_device_partition_property *properties,
cl_uint numDevices, cl_uint numDevices,
cl_device_id *devices, cl_device_id *subDevices,
cl_uint *numDevicesRet) cl_uint *numDevicesRet)
{ {
if (devices == nullptr) if (subDevices == nullptr)
{ {
numDevices = 0u; numDevices = 0u;
} }
rx::CLDeviceImpl::PtrList ptrList; DevicePtrList subDeviceList;
const cl_int result = mImpl->createSubDevices(properties, numDevices, ptrList, numDevicesRet); const cl_int result =
mImpl->createSubDevices(*this, properties, numDevices, subDeviceList, numDevicesRet);
if (result == CL_SUCCESS) if (result == CL_SUCCESS)
{ {
while (!ptrList.empty()) for (const DevicePtr &subDevice : subDeviceList)
{ {
rx::CLDeviceImpl::Info info = ptrList.front()->createInfo(); *subDevices++ = subDevice.get();
if (!info.isValid())
{
return CL_INVALID_VALUE;
}
mSubDevices.emplace_back(
new Device(mPlatform, this, std::move(ptrList.front()), std::move(info)));
ptrList.pop_front();
*devices++ = mSubDevices.back().get();
} }
mSubDevices.splice(mSubDevices.cend(), std::move(subDeviceList));
} }
return result; return result;
} }
Device::PtrList Device::CreateDevices(Platform &platform, rx::CLDeviceImpl::PtrList &&implList) DevicePtr Device::CreateDevice(Platform &platform,
DeviceRefPtr &&parent,
const CreateImplFunc &createImplFunc)
{ {
PtrList devices; DevicePtr device(new Device(platform, std::move(parent), createImplFunc));
while (!implList.empty()) return device->mInfo.isValid() ? std::move(device) : DevicePtr{};
{
rx::CLDeviceImpl::Info info = implList.front()->createInfo();
if (!info.isValid())
{
return Device::PtrList{};
}
devices.emplace_back(
new Device(platform, nullptr, std::move(implList.front()), std::move(info)));
implList.pop_front();
}
return devices;
} }
bool Device::IsValid(const _cl_device_id *device) bool Device::IsValid(const _cl_device_id *device)
{ {
const Platform::PtrList &platforms = Platform::GetPlatforms(); const Platform::PtrList &platforms = Platform::GetPlatforms();
return std::find_if(platforms.cbegin(), platforms.cend(), [=](const Platform::Ptr &platform) { return std::find_if(platforms.cbegin(), platforms.cend(), [=](const PlatformPtr &platform) {
return platform->hasDevice(device); return platform->hasDevice(device);
}) != platforms.cend(); }) != platforms.cend();
} }
Device::Device(Platform &platform, Device::Device(Platform &platform, DeviceRefPtr &&parent, const CreateImplFunc &createImplFunc)
Device *parent,
rx::CLDeviceImpl::Ptr &&impl,
rx::CLDeviceImpl::Info &&info)
: _cl_device_id(platform.getDispatch()), : _cl_device_id(platform.getDispatch()),
mPlatform(platform), mPlatform(platform),
mParent(parent), mParent(std::move(parent)),
mImpl(std::move(impl)), mImpl(createImplFunc(*this)),
mInfo(std::move(info)) mInfo(mImpl->createInfo())
{} {}
void Device::destroySubDevice(Device *device) void Device::destroySubDevice(Device *device)
...@@ -396,7 +379,6 @@ void Device::destroySubDevice(Device *device) ...@@ -396,7 +379,6 @@ void Device::destroySubDevice(Device *device)
if (deviceIt != mSubDevices.cend()) if (deviceIt != mSubDevices.cend())
{ {
mSubDevices.erase(deviceIt); mSubDevices.erase(deviceIt);
release();
} }
else else
{ {
......
...@@ -10,24 +10,24 @@ ...@@ -10,24 +10,24 @@
#define LIBANGLE_CLDEVICE_H_ #define LIBANGLE_CLDEVICE_H_
#include "libANGLE/CLObject.h" #include "libANGLE/CLObject.h"
#include "libANGLE/CLRefPointer.h"
#include "libANGLE/renderer/CLDeviceImpl.h" #include "libANGLE/renderer/CLDeviceImpl.h"
#include <functional>
namespace cl namespace cl
{ {
class Device final : public _cl_device_id, public Object class Device final : public _cl_device_id, public Object
{ {
public: public:
using Ptr = std::unique_ptr<Device>; using CreateImplFunc = std::function<rx::CLDeviceImpl::Ptr(const cl::Device &)>;
using PtrList = std::list<Ptr>;
using RefPtr = RefPointer<Device>;
using RefList = std::vector<RefPtr>;
~Device(); ~Device();
Platform &getPlatform() const noexcept; Platform &getPlatform() const noexcept;
bool isRoot() const noexcept; bool isRoot() const noexcept;
rx::CLDeviceImpl &getImpl() const;
const rx::CLDeviceImpl::Info &getInfo() const;
bool hasSubDevice(const _cl_device_id *device) const; bool hasSubDevice(const _cl_device_id *device) const;
void retain() noexcept; void retain() noexcept;
...@@ -38,28 +38,27 @@ class Device final : public _cl_device_id, public Object ...@@ -38,28 +38,27 @@ class Device final : public _cl_device_id, public Object
cl_int createSubDevices(const cl_device_partition_property *properties, cl_int createSubDevices(const cl_device_partition_property *properties,
cl_uint numDevices, cl_uint numDevices,
cl_device_id *devices, cl_device_id *subDevices,
cl_uint *numDevicesRet); cl_uint *numDevicesRet);
static PtrList CreateDevices(Platform &platform, rx::CLDeviceImpl::PtrList &&implList); static DevicePtr CreateDevice(Platform &platform,
DeviceRefPtr &&parent,
const CreateImplFunc &createImplFunc);
static bool IsValid(const _cl_device_id *device); static bool IsValid(const _cl_device_id *device);
static bool IsValidType(cl_device_type type); static bool IsValidType(cl_device_type type);
private: private:
Device(Platform &platform, Device(Platform &platform, DeviceRefPtr &&parent, const CreateImplFunc &createImplFunc);
Device *parent,
rx::CLDeviceImpl::Ptr &&impl,
rx::CLDeviceImpl::Info &&info);
void destroySubDevice(Device *device); void destroySubDevice(Device *device);
Platform &mPlatform; Platform &mPlatform;
Device *const mParent; const DeviceRefPtr mParent;
const rx::CLDeviceImpl::Ptr mImpl; const rx::CLDeviceImpl::Ptr mImpl;
const rx::CLDeviceImpl::Info mInfo; const rx::CLDeviceImpl::Info mInfo;
PtrList mSubDevices; DevicePtrList mSubDevices;
friend class Platform; friend class Platform;
}; };
...@@ -71,12 +70,22 @@ inline Platform &Device::getPlatform() const noexcept ...@@ -71,12 +70,22 @@ inline Platform &Device::getPlatform() const noexcept
inline bool Device::isRoot() const noexcept inline bool Device::isRoot() const noexcept
{ {
return mParent == nullptr; return !mParent;
}
inline rx::CLDeviceImpl &Device::getImpl() const
{
return *mImpl;
}
inline const rx::CLDeviceImpl::Info &Device::getInfo() const
{
return mInfo;
} }
inline bool Device::hasSubDevice(const _cl_device_id *device) const inline bool Device::hasSubDevice(const _cl_device_id *device) const
{ {
return std::find_if(mSubDevices.cbegin(), mSubDevices.cend(), [=](const Device::Ptr &ptr) { return std::find_if(mSubDevices.cbegin(), mSubDevices.cend(), [=](const DevicePtr &ptr) {
return ptr.get() == device || ptr->hasSubDevice(device); return ptr.get() == device || ptr->hasSubDevice(device);
}) != mSubDevices.cend(); }) != mSubDevices.cend();
} }
......
...@@ -18,7 +18,6 @@ namespace cl ...@@ -18,7 +18,6 @@ namespace cl
class Object class Object
{ {
public: public:
// This class cannot be virtual as its derived classes need to have standard layout
Object() = default; Object() = default;
~Object() ~Object()
......
...@@ -67,32 +67,6 @@ Platform::~Platform() ...@@ -67,32 +67,6 @@ Platform::~Platform()
removeRef(); removeRef();
} }
Device::RefList Platform::mapDevices(const rx::CLDeviceImpl::List &deviceImplList) const
{
Device::RefList devices;
for (rx::CLDeviceImpl *impl : deviceImplList)
{
auto it = mDevices.cbegin();
while (it != mDevices.cend() && (*it)->mImpl.get() != impl)
{
++it;
}
if (it != mDevices.cend())
{
devices.emplace_back(it->get());
}
else
{
ERR() << "Device not found in platform list";
}
}
if (devices.size() != deviceImplList.size())
{
devices.clear();
}
return devices;
}
cl_int Platform::getInfo(PlatformInfo name, size_t valueSize, void *value, size_t *valueSizeRet) cl_int Platform::getInfo(PlatformInfo name, size_t valueSize, void *value, size_t *valueSizeRet)
{ {
const void *copyValue = nullptr; const void *copyValue = nullptr;
...@@ -177,7 +151,7 @@ cl_int Platform::getDeviceIDs(cl_device_type deviceType, ...@@ -177,7 +151,7 @@ cl_int Platform::getDeviceIDs(cl_device_type deviceType,
cl_uint *numDevices) const cl_uint *numDevices) const
{ {
cl_uint found = 0u; cl_uint found = 0u;
for (const Device::Ptr &device : mDevices) for (const DevicePtr &device : mDevices)
{ {
cl_device_type type = 0u; cl_device_type type = 0u;
if (device->getInfoULong(DeviceInfo::Type, &type) == CL_SUCCESS && if (device->getInfoULong(DeviceInfo::Type, &type) == CL_SUCCESS &&
...@@ -197,6 +171,15 @@ cl_int Platform::getDeviceIDs(cl_device_type deviceType, ...@@ -197,6 +171,15 @@ cl_int Platform::getDeviceIDs(cl_device_type deviceType,
return found == 0u ? CL_DEVICE_NOT_FOUND : CL_SUCCESS; return found == 0u ? CL_DEVICE_NOT_FOUND : CL_SUCCESS;
} }
void Platform::CreatePlatform(const cl_icd_dispatch &dispatch, const CreateImplFunc &createImplFunc)
{
PlatformPtr platform(new Platform(dispatch, createImplFunc));
if (platform->mInfo.isValid() && !platform->mDevices.empty())
{
GetList().emplace_back(std::move(platform));
}
}
cl_int Platform::GetPlatformIDs(cl_uint num_entries, cl_int Platform::GetPlatformIDs(cl_uint num_entries,
cl_platform_id *platforms, cl_platform_id *platforms,
cl_uint *num_platforms) cl_uint *num_platforms)
...@@ -229,7 +212,7 @@ cl_context Platform::CreateContext(const cl_context_properties *properties, ...@@ -229,7 +212,7 @@ cl_context Platform::CreateContext(const cl_context_properties *properties,
bool userSync = false; bool userSync = false;
Context::PropArray propArray = ParseContextProperties(properties, platform, userSync); Context::PropArray propArray = ParseContextProperties(properties, platform, userSync);
ASSERT(platform != nullptr); ASSERT(platform != nullptr);
Device::RefList refDevices; DeviceRefList refDevices;
while (numDevices-- != 0u) while (numDevices-- != 0u)
{ {
refDevices.emplace_back(static_cast<Device *>(*devices++)); refDevices.emplace_back(static_cast<Device *>(*devices++));
...@@ -267,38 +250,12 @@ cl_context Platform::CreateContextFromType(const cl_context_properties *properti ...@@ -267,38 +250,12 @@ cl_context Platform::CreateContextFromType(const cl_context_properties *properti
return platform->mContexts.back().get(); return platform->mContexts.back().get();
} }
void Platform::CreatePlatform(const cl_icd_dispatch &dispatch, Platform::Platform(const cl_icd_dispatch &dispatch, const CreateImplFunc &createImplFunc)
rx::CLPlatformImpl::InitData &initData)
{
Ptr platform(new Platform(dispatch, initData));
if (!platform->mDevices.empty())
{
GetList().emplace_back(std::move(platform));
}
}
Platform::Platform(const cl_icd_dispatch &dispatch, rx::CLPlatformImpl::InitData &initData)
: _cl_platform_id(dispatch), : _cl_platform_id(dispatch),
mImpl(std::move(std::get<0>(initData))), mImpl(createImplFunc(*this)),
mInfo(std::move(std::get<1>(initData))), mInfo(mImpl->createInfo()),
mDevices(Device::CreateDevices(*this, std::move(std::get<2>(initData)))) mDevices(mImpl->createDevices(*this))
{ {}
ASSERT(isCompatible(this));
}
rx::CLContextImpl::Ptr Platform::createContext(const Device::RefList &devices,
ContextErrorCB notify,
void *userData,
bool userSync,
cl_int *errcodeRet)
{
rx::CLDeviceImpl::List deviceImplList;
for (const Device::RefPtr &device : devices)
{
deviceImplList.emplace_back(device->mImpl.get());
}
return mImpl->createContext(std::move(deviceImplList), notify, userData, userSync, errcodeRet);
}
void Platform::destroyContext(Context *context) void Platform::destroyContext(Context *context)
{ {
......
...@@ -15,21 +15,22 @@ ...@@ -15,21 +15,22 @@
#include "anglebase/no_destructor.h" #include "anglebase/no_destructor.h"
#include <functional>
namespace cl namespace cl
{ {
class Platform final : public _cl_platform_id, public Object class Platform final : public _cl_platform_id, public Object
{ {
public: public:
using Ptr = std::unique_ptr<Platform>; using PtrList = std::list<PlatformPtr>;
using PtrList = std::list<Ptr>; using CreateImplFunc = std::function<rx::CLPlatformImpl::Ptr(const cl::Platform &)>;
~Platform(); ~Platform();
const rx::CLPlatformImpl::Info &getInfo() const;
bool hasDevice(const _cl_device_id *device) const; bool hasDevice(const _cl_device_id *device) const;
const Device::PtrList &getDevices() const; const DevicePtrList &getDevices() const;
Device::RefList mapDevices(const rx::CLDeviceImpl::List &deviceImplList) const;
bool hasContext(const _cl_context *context) const; bool hasContext(const _cl_context *context) const;
cl_int getInfo(PlatformInfo name, size_t valueSize, void *value, size_t *valueSizeRet); cl_int getInfo(PlatformInfo name, size_t valueSize, void *value, size_t *valueSizeRet);
...@@ -39,6 +40,9 @@ class Platform final : public _cl_platform_id, public Object ...@@ -39,6 +40,9 @@ class Platform final : public _cl_platform_id, public Object
cl_device_id *devices, cl_device_id *devices,
cl_uint *numDevices) const; cl_uint *numDevices) const;
static void CreatePlatform(const cl_icd_dispatch &dispatch,
const CreateImplFunc &createImplFunc);
static cl_int GetPlatformIDs(cl_uint num_entries, static cl_int GetPlatformIDs(cl_uint num_entries,
cl_platform_id *platforms, cl_platform_id *platforms,
cl_uint *num_platforms); cl_uint *num_platforms);
...@@ -56,26 +60,16 @@ class Platform final : public _cl_platform_id, public Object ...@@ -56,26 +60,16 @@ class Platform final : public _cl_platform_id, public Object
void *userData, void *userData,
cl_int *errcodeRet); cl_int *errcodeRet);
static void CreatePlatform(const cl_icd_dispatch &dispatch,
rx::CLPlatformImpl::InitData &initData);
static const PtrList &GetPlatforms(); static const PtrList &GetPlatforms();
static Platform *GetDefault(); static Platform *GetDefault();
static Platform *CastOrDefault(cl_platform_id platform); static Platform *CastOrDefault(cl_platform_id platform);
static bool IsValid(const _cl_platform_id *platform); static bool IsValid(const _cl_platform_id *platform);
static bool IsValidOrDefault(const _cl_platform_id *platform); static bool IsValidOrDefault(const _cl_platform_id *platform);
static constexpr const char *GetVendor(); static constexpr const char *GetVendor();
private: private:
Platform(const cl_icd_dispatch &dispatch, rx::CLPlatformImpl::InitData &initData); Platform(const cl_icd_dispatch &dispatch, const CreateImplFunc &createImplFunc);
rx::CLContextImpl::Ptr createContext(const Device::RefList &devices,
ContextErrorCB notify,
void *userData,
bool userSync,
cl_int *errcodeRet);
void destroyContext(Context *context); void destroyContext(Context *context);
...@@ -83,7 +77,7 @@ class Platform final : public _cl_platform_id, public Object ...@@ -83,7 +77,7 @@ class Platform final : public _cl_platform_id, public Object
const rx::CLPlatformImpl::Ptr mImpl; const rx::CLPlatformImpl::Ptr mImpl;
const rx::CLPlatformImpl::Info mInfo; const rx::CLPlatformImpl::Info mInfo;
const Device::PtrList mDevices; const DevicePtrList mDevices;
Context::PtrList mContexts; Context::PtrList mContexts;
...@@ -93,21 +87,26 @@ class Platform final : public _cl_platform_id, public Object ...@@ -93,21 +87,26 @@ class Platform final : public _cl_platform_id, public Object
friend class Context; friend class Context;
}; };
inline const rx::CLPlatformImpl::Info &Platform::getInfo() const
{
return mInfo;
}
inline bool Platform::hasDevice(const _cl_device_id *device) const inline bool Platform::hasDevice(const _cl_device_id *device) const
{ {
return std::find_if(mDevices.cbegin(), mDevices.cend(), [=](const Device::Ptr &ptr) { return std::find_if(mDevices.cbegin(), mDevices.cend(), [=](const DevicePtr &ptr) {
return ptr.get() == device || ptr->hasSubDevice(device); return ptr.get() == device || ptr->hasSubDevice(device);
}) != mDevices.cend(); }) != mDevices.cend();
} }
inline const Device::PtrList &Platform::getDevices() const inline const DevicePtrList &Platform::getDevices() const
{ {
return mDevices; return mDevices;
} }
inline bool Platform::hasContext(const _cl_context *context) const inline bool Platform::hasContext(const _cl_context *context) const
{ {
return std::find_if(mContexts.cbegin(), mContexts.cend(), [=](const Context::Ptr &ptr) { return std::find_if(mContexts.cbegin(), mContexts.cend(), [=](const ContextPtr &ptr) {
return ptr.get() == context; return ptr.get() == context;
}) != mContexts.cend(); }) != mContexts.cend();
} }
...@@ -136,8 +135,9 @@ inline Platform *Platform::CastOrDefault(cl_platform_id platform) ...@@ -136,8 +135,9 @@ inline Platform *Platform::CastOrDefault(cl_platform_id platform)
inline bool Platform::IsValid(const _cl_platform_id *platform) inline bool Platform::IsValid(const _cl_platform_id *platform)
{ {
const PtrList &platforms = GetPlatforms(); const PtrList &platforms = GetPlatforms();
return std::find_if(platforms.cbegin(), platforms.cend(), return std::find_if(platforms.cbegin(), platforms.cend(), [=](const PlatformPtr &ptr) {
[=](const Ptr &ptr) { return ptr.get() == platform; }) != platforms.cend(); return ptr.get() == platform;
}) != platforms.cend();
} }
// Our CL implementation defines that a nullptr value chooses the platform that we provide as // Our CL implementation defines that a nullptr value chooses the platform that we provide as
......
...@@ -8,9 +8,6 @@ ...@@ -8,9 +8,6 @@
#ifndef LIBANGLE_CLREFPOINTER_H_ #ifndef LIBANGLE_CLREFPOINTER_H_
#define LIBANGLE_CLREFPOINTER_H_ #define LIBANGLE_CLREFPOINTER_H_
#include "libANGLE/CLtypes.h"
#include "libANGLE/Debug.h"
#include <algorithm> #include <algorithm>
namespace cl namespace cl
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#ifndef LIBANGLE_CLTYPES_H_ #ifndef LIBANGLE_CLTYPES_H_
#define LIBANGLE_CLTYPES_H_ #define LIBANGLE_CLTYPES_H_
#include "angle_cl.h" #include "libANGLE/CLRefPointer.h"
#include "common/PackedCLEnums_autogen.h" #include "common/PackedCLEnums_autogen.h"
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
namespace cl namespace cl
{ {
class CommandQueue; class CommandQueue;
class Context; class Context;
class Device; class Device;
...@@ -32,6 +33,22 @@ class Object; ...@@ -32,6 +33,22 @@ class Object;
class Platform; class Platform;
class Program; class Program;
class Sampler; class Sampler;
using CommandQueuePtr = std::unique_ptr<CommandQueue>;
using ContextPtr = std::unique_ptr<Context>;
using DevicePtr = std::unique_ptr<Device>;
using EventPtr = std::unique_ptr<Event>;
using KernelPtr = std::unique_ptr<Kernel>;
using MemoryPtr = std::unique_ptr<Memory>;
using ObjectPtr = std::unique_ptr<Object>;
using PlatformPtr = std::unique_ptr<Platform>;
using ProgramPtr = std::unique_ptr<Program>;
using SamplerPtr = std::unique_ptr<Sampler>;
using DevicePtrList = std::list<DevicePtr>;
using DeviceRefPtr = RefPointer<Device>;
using DeviceRefList = std::vector<DeviceRefPtr>;
} // namespace cl } // namespace cl
#endif // LIBANGLE_CLTYPES_H_ #endif // LIBANGLE_CLTYPES_H_
...@@ -7,28 +7,11 @@ ...@@ -7,28 +7,11 @@
#include "libANGLE/renderer/CLContextImpl.h" #include "libANGLE/renderer/CLContextImpl.h"
#include "libANGLE/renderer/CLPlatformImpl.h"
#include "libANGLE/Debug.h"
namespace rx namespace rx
{ {
CLContextImpl::CLContextImpl(CLPlatformImpl &platform, CLDeviceImpl::List &&devices) CLContextImpl::CLContextImpl(const cl::Context &context) : mContext(context) {}
: mPlatform(platform), mDevices(std::move(devices))
{}
CLContextImpl::~CLContextImpl() CLContextImpl::~CLContextImpl() = default;
{
auto it = std::find(mPlatform.mContexts.cbegin(), mPlatform.mContexts.cend(), this);
if (it != mPlatform.mContexts.cend())
{
mPlatform.mContexts.erase(it);
}
else
{
ERR() << "Context not in platform's list";
}
}
} // namespace rx } // namespace rx
...@@ -16,30 +16,17 @@ namespace rx ...@@ -16,30 +16,17 @@ namespace rx
class CLContextImpl : angle::NonCopyable class CLContextImpl : angle::NonCopyable
{ {
public: public:
using Ptr = std::unique_ptr<CLContextImpl>; using Ptr = std::unique_ptr<CLContextImpl>;
using List = std::list<CLContextImpl *>;
CLContextImpl(CLPlatformImpl &platform, CLDeviceImpl::List &&devices); CLContextImpl(const cl::Context &context);
virtual ~CLContextImpl(); virtual ~CLContextImpl();
template <typename T> virtual cl::DeviceRefList getDevices() const = 0;
T &getPlatform() const
{
return static_cast<T &>(mPlatform);
}
const CLDeviceImpl::List &getDevices() const;
protected: protected:
CLPlatformImpl &mPlatform; const cl::Context &mContext;
const CLDeviceImpl::List mDevices;
}; };
inline const CLDeviceImpl::List &CLContextImpl::getDevices() const
{
return mDevices;
}
} // namespace rx } // namespace rx
#endif // LIBANGLE_RENDERER_CLCONTEXTIMPL_H_ #endif // LIBANGLE_RENDERER_CLCONTEXTIMPL_H_
...@@ -20,24 +20,8 @@ CLDeviceImpl::Info::Info(Info &&) = default; ...@@ -20,24 +20,8 @@ CLDeviceImpl::Info::Info(Info &&) = default;
CLDeviceImpl::Info &CLDeviceImpl::Info::operator=(Info &&) = default; CLDeviceImpl::Info &CLDeviceImpl::Info::operator=(Info &&) = default;
CLDeviceImpl::CLDeviceImpl(CLPlatformImpl &platform, CLDeviceImpl *parent) CLDeviceImpl::CLDeviceImpl(const cl::Device &device) : mDevice(device) {}
: mPlatform(platform), mParent(parent)
{}
CLDeviceImpl::~CLDeviceImpl() CLDeviceImpl::~CLDeviceImpl() = default;
{
if (mParent != nullptr)
{
auto it = std::find(mParent->mSubDevices.cbegin(), mParent->mSubDevices.cend(), this);
if (it != mParent->mSubDevices.cend())
{
mParent->mSubDevices.erase(it);
}
else
{
ERR() << "Sub-device not in parent's list";
}
}
}
} // namespace rx } // namespace rx
...@@ -16,6 +16,8 @@ namespace rx ...@@ -16,6 +16,8 @@ namespace rx
class CLDeviceImpl : angle::NonCopyable class CLDeviceImpl : angle::NonCopyable
{ {
public: public:
using Ptr = std::unique_ptr<CLDeviceImpl>;
struct Info struct Info
{ {
Info(); Info();
...@@ -41,19 +43,9 @@ class CLDeviceImpl : angle::NonCopyable ...@@ -41,19 +43,9 @@ class CLDeviceImpl : angle::NonCopyable
std::vector<cl_device_partition_property> mPartitionType; std::vector<cl_device_partition_property> mPartitionType;
}; };
using Ptr = std::unique_ptr<CLDeviceImpl>; CLDeviceImpl(const cl::Device &device);
using PtrList = std::list<Ptr>;
using List = std::list<CLDeviceImpl *>;
CLDeviceImpl(CLPlatformImpl &platform, CLDeviceImpl *parent);
virtual ~CLDeviceImpl(); virtual ~CLDeviceImpl();
template <typename T>
T &getPlatform() const
{
return static_cast<T &>(mPlatform);
}
virtual Info createInfo() const = 0; virtual Info createInfo() const = 0;
virtual cl_int getInfoUInt(cl::DeviceInfo name, cl_uint *value) const = 0; virtual cl_int getInfoUInt(cl::DeviceInfo name, cl_uint *value) const = 0;
...@@ -62,16 +54,14 @@ class CLDeviceImpl : angle::NonCopyable ...@@ -62,16 +54,14 @@ class CLDeviceImpl : angle::NonCopyable
virtual cl_int getInfoStringLength(cl::DeviceInfo name, size_t *value) const = 0; virtual cl_int getInfoStringLength(cl::DeviceInfo name, size_t *value) const = 0;
virtual cl_int getInfoString(cl::DeviceInfo name, size_t size, char *value) const = 0; virtual cl_int getInfoString(cl::DeviceInfo name, size_t size, char *value) const = 0;
virtual cl_int createSubDevices(const cl_device_partition_property *properties, virtual cl_int createSubDevices(cl::Device &device,
const cl_device_partition_property *properties,
cl_uint numDevices, cl_uint numDevices,
PtrList &implList, cl::DevicePtrList &subDeviceList,
cl_uint *numDevicesRet) = 0; cl_uint *numDevicesRet) = 0;
protected: protected:
CLPlatformImpl &mPlatform; const cl::Device &mDevice;
CLDeviceImpl *const mParent;
List mSubDevices;
}; };
} // namespace rx } // namespace rx
......
...@@ -18,7 +18,7 @@ CLPlatformImpl::Info::Info(Info &&) = default; ...@@ -18,7 +18,7 @@ CLPlatformImpl::Info::Info(Info &&) = default;
CLPlatformImpl::Info &CLPlatformImpl::Info::operator=(Info &&) = default; CLPlatformImpl::Info &CLPlatformImpl::Info::operator=(Info &&) = default;
CLPlatformImpl::CLPlatformImpl(CLDeviceImpl::List &&devices) : mDevices(std::move(devices)) {} CLPlatformImpl::CLPlatformImpl(const cl::Platform &platform) : mPlatform(platform) {}
CLPlatformImpl::~CLPlatformImpl() = default; CLPlatformImpl::~CLPlatformImpl() = default;
......
...@@ -19,6 +19,8 @@ namespace rx ...@@ -19,6 +19,8 @@ namespace rx
class CLPlatformImpl : angle::NonCopyable class CLPlatformImpl : angle::NonCopyable
{ {
public: public:
using Ptr = std::unique_ptr<CLPlatformImpl>;
struct Info struct Info
{ {
Info(); Info();
...@@ -41,40 +43,31 @@ class CLPlatformImpl : angle::NonCopyable ...@@ -41,40 +43,31 @@ class CLPlatformImpl : angle::NonCopyable
cl_ulong mHostTimerRes = 0u; cl_ulong mHostTimerRes = 0u;
}; };
using Ptr = std::unique_ptr<CLPlatformImpl>; explicit CLPlatformImpl(const cl::Platform &platform);
using InitData = std::tuple<Ptr, Info, CLDeviceImpl::PtrList>;
using InitList = std::list<InitData>;
explicit CLPlatformImpl(CLDeviceImpl::List &&devices);
virtual ~CLPlatformImpl(); virtual ~CLPlatformImpl();
const CLDeviceImpl::List &getDevices() const; // For initialization only
virtual Info createInfo() const = 0;
virtual cl::DevicePtrList createDevices(cl::Platform &platform) const = 0;
virtual CLContextImpl::Ptr createContext(CLDeviceImpl::List &&devices, virtual CLContextImpl::Ptr createContext(const cl::Context &context,
const cl::DeviceRefList &devices,
cl::ContextErrorCB notify, cl::ContextErrorCB notify,
void *userData, void *userData,
bool userSync, bool userSync,
cl_int *errcodeRet) = 0; cl_int *errcodeRet) = 0;
virtual CLContextImpl::Ptr createContextFromType(cl_device_type deviceType, virtual CLContextImpl::Ptr createContextFromType(const cl::Context &context,
cl_device_type deviceType,
cl::ContextErrorCB notify, cl::ContextErrorCB notify,
void *userData, void *userData,
bool userSync, bool userSync,
cl_int *errcodeRet) = 0; cl_int *errcodeRet) = 0;
protected: protected:
const CLDeviceImpl::List mDevices; const cl::Platform &mPlatform;
CLContextImpl::List mContexts;
friend class CLContextImpl;
}; };
inline const CLDeviceImpl::List &CLPlatformImpl::getDevices() const
{
return mDevices;
}
} // namespace rx } // namespace rx
#endif // LIBANGLE_RENDERER_CLPLATFORMIMPL_H_ #endif // LIBANGLE_RENDERER_CLPLATFORMIMPL_H_
...@@ -7,23 +7,66 @@ ...@@ -7,23 +7,66 @@
#include "libANGLE/renderer/cl/CLContextCL.h" #include "libANGLE/renderer/cl/CLContextCL.h"
#include "libANGLE/renderer/cl/CLPlatformCL.h" #include "libANGLE/renderer/cl/CLDeviceCL.h"
#include "libANGLE/CLContext.h"
#include "libANGLE/CLDevice.h"
#include "libANGLE/CLPlatform.h"
#include "libANGLE/Debug.h" #include "libANGLE/Debug.h"
namespace rx namespace rx
{ {
CLContextCL::CLContextCL(CLPlatformCL &platform, CLDeviceImpl::List &&devices, cl_context context) CLContextCL::CLContextCL(const cl::Context &context, cl_context native)
: CLContextImpl(platform, std::move(devices)), mContext(context) : CLContextImpl(context), mNative(native)
{} {}
CLContextCL::~CLContextCL() CLContextCL::~CLContextCL()
{ {
if (mContext->getDispatch().clReleaseContext(mContext) != CL_SUCCESS) if (mNative->getDispatch().clReleaseContext(mNative) != CL_SUCCESS)
{ {
ERR() << "Error while releasing CL context"; ERR() << "Error while releasing CL context";
} }
} }
cl::DeviceRefList CLContextCL::getDevices() const
{
size_t valueSize = 0u;
cl_int result = mNative->getDispatch().clGetContextInfo(mNative, CL_CONTEXT_DEVICES, 0u,
nullptr, &valueSize);
if (result == CL_SUCCESS && (valueSize % sizeof(cl_device_id)) == 0u)
{
std::vector<cl_device_id> nativeDevices(valueSize / sizeof(cl_device_id), nullptr);
result = mNative->getDispatch().clGetContextInfo(mNative, CL_CONTEXT_DEVICES, valueSize,
nativeDevices.data(), nullptr);
if (result == CL_SUCCESS)
{
const cl::DevicePtrList &platformDevices = mContext.getPlatform().getDevices();
cl::DeviceRefList devices;
for (cl_device_id nativeDevice : nativeDevices)
{
auto it = platformDevices.cbegin();
while (it != platformDevices.cend() &&
static_cast<CLDeviceCL &>((*it)->getImpl()).getNative() != nativeDevice)
{
++it;
}
if (it != platformDevices.cend())
{
devices.emplace_back(it->get());
}
else
{
ERR() << "Device not found in platform list";
return cl::DeviceRefList{};
}
}
return devices;
}
}
ERR() << "Error fetching devices from CL context, code: " << result;
return cl::DeviceRefList{};
}
} // namespace rx } // namespace rx
...@@ -18,11 +18,13 @@ namespace rx ...@@ -18,11 +18,13 @@ namespace rx
class CLContextCL : public CLContextImpl class CLContextCL : public CLContextImpl
{ {
public: public:
CLContextCL(CLPlatformCL &platform, CLDeviceImpl::List &&devices, cl_context context); CLContextCL(const cl::Context &context, cl_context native);
~CLContextCL() override; ~CLContextCL() override;
cl::DeviceRefList getDevices() const override;
private: private:
const cl_context mContext; const cl_context mNative;
}; };
} // namespace rx } // namespace rx
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "libANGLE/renderer/cl/CLPlatformCL.h" #include "libANGLE/renderer/cl/CLPlatformCL.h"
#include "libANGLE/renderer/cl/cl_util.h" #include "libANGLE/renderer/cl/cl_util.h"
#include "libANGLE/CLDevice.h"
#include "libANGLE/Debug.h" #include "libANGLE/Debug.h"
namespace rx namespace rx
...@@ -45,8 +46,7 @@ bool GetDeviceInfo(cl_device_id device, cl::DeviceInfo name, std::vector<T> &vec ...@@ -45,8 +46,7 @@ bool GetDeviceInfo(cl_device_id device, cl::DeviceInfo name, std::vector<T> &vec
CLDeviceCL::~CLDeviceCL() CLDeviceCL::~CLDeviceCL()
{ {
if (mVersion >= CL_MAKE_VERSION(1, 2, 0) && if (!mDevice.isRoot() && mNative->getDispatch().clReleaseDevice(mNative) != CL_SUCCESS)
mDevice->getDispatch().clReleaseDevice(mDevice) != CL_SUCCESS)
{ {
ERR() << "Error while releasing CL device"; ERR() << "Error while releasing CL device";
} }
...@@ -55,17 +55,26 @@ CLDeviceCL::~CLDeviceCL() ...@@ -55,17 +55,26 @@ CLDeviceCL::~CLDeviceCL()
CLDeviceImpl::Info CLDeviceCL::createInfo() const CLDeviceImpl::Info CLDeviceCL::createInfo() const
{ {
Info info; Info info;
info.mVersion = mVersion;
std::vector<char> valString; std::vector<char> valString;
if (!GetDeviceInfo(mDevice, cl::DeviceInfo::Extensions, valString)) if (!GetDeviceInfo(mNative, cl::DeviceInfo::Version, valString))
{
return Info{};
}
info.mVersion = ExtractCLVersion(valString.data());
if (info.mVersion == 0u)
{
return Info{};
}
if (!GetDeviceInfo(mNative, cl::DeviceInfo::Extensions, valString))
{ {
return Info{}; return Info{};
} }
info.mExtensions.assign(valString.data()); info.mExtensions.assign(valString.data());
RemoveUnsupportedCLExtensions(info.mExtensions); RemoveUnsupportedCLExtensions(info.mExtensions);
if (!GetDeviceInfo(mDevice, cl::DeviceInfo::MaxWorkItemSizes, info.mMaxWorkItemSizes)) if (!GetDeviceInfo(mNative, cl::DeviceInfo::MaxWorkItemSizes, info.mMaxWorkItemSizes))
{ {
return Info{}; return Info{};
} }
...@@ -80,21 +89,21 @@ CLDeviceImpl::Info CLDeviceCL::createInfo() const ...@@ -80,21 +89,21 @@ CLDeviceImpl::Info CLDeviceCL::createInfo() const
return Info{}; return Info{};
} }
if (mVersion >= CL_MAKE_VERSION(1, 2, 0) && if (info.mVersion >= CL_MAKE_VERSION(1, 2, 0) &&
(!GetDeviceInfo(mDevice, cl::DeviceInfo::PartitionProperties, info.mPartitionProperties) || (!GetDeviceInfo(mNative, cl::DeviceInfo::PartitionProperties, info.mPartitionProperties) ||
!GetDeviceInfo(mDevice, cl::DeviceInfo::PartitionType, info.mPartitionType))) !GetDeviceInfo(mNative, cl::DeviceInfo::PartitionType, info.mPartitionType)))
{ {
return Info{}; return Info{};
} }
if (mVersion >= CL_MAKE_VERSION(3, 0, 0) && if (info.mVersion >= CL_MAKE_VERSION(3, 0, 0) &&
(!GetDeviceInfo(mDevice, cl::DeviceInfo::ILsWithVersion, info.mILsWithVersion) || (!GetDeviceInfo(mNative, cl::DeviceInfo::ILsWithVersion, info.mILsWithVersion) ||
!GetDeviceInfo(mDevice, cl::DeviceInfo::BuiltInKernelsWithVersion, !GetDeviceInfo(mNative, cl::DeviceInfo::BuiltInKernelsWithVersion,
info.mBuiltInKernelsWithVersion) || info.mBuiltInKernelsWithVersion) ||
!GetDeviceInfo(mDevice, cl::DeviceInfo::OpenCL_C_AllVersions, !GetDeviceInfo(mNative, cl::DeviceInfo::OpenCL_C_AllVersions,
info.mOpenCL_C_AllVersions) || info.mOpenCL_C_AllVersions) ||
!GetDeviceInfo(mDevice, cl::DeviceInfo::OpenCL_C_Features, info.mOpenCL_C_Features) || !GetDeviceInfo(mNative, cl::DeviceInfo::OpenCL_C_Features, info.mOpenCL_C_Features) ||
!GetDeviceInfo(mDevice, cl::DeviceInfo::ExtensionsWithVersion, !GetDeviceInfo(mNative, cl::DeviceInfo::ExtensionsWithVersion,
info.mExtensionsWithVersion))) info.mExtensionsWithVersion)))
{ {
return Info{}; return Info{};
...@@ -106,93 +115,69 @@ CLDeviceImpl::Info CLDeviceCL::createInfo() const ...@@ -106,93 +115,69 @@ CLDeviceImpl::Info CLDeviceCL::createInfo() const
cl_int CLDeviceCL::getInfoUInt(cl::DeviceInfo name, cl_uint *value) const cl_int CLDeviceCL::getInfoUInt(cl::DeviceInfo name, cl_uint *value) const
{ {
return mDevice->getDispatch().clGetDeviceInfo(mDevice, cl::ToCLenum(name), sizeof(*value), return mNative->getDispatch().clGetDeviceInfo(mNative, cl::ToCLenum(name), sizeof(*value),
value, nullptr); value, nullptr);
} }
cl_int CLDeviceCL::getInfoULong(cl::DeviceInfo name, cl_ulong *value) const cl_int CLDeviceCL::getInfoULong(cl::DeviceInfo name, cl_ulong *value) const
{ {
return mDevice->getDispatch().clGetDeviceInfo(mDevice, cl::ToCLenum(name), sizeof(*value), return mNative->getDispatch().clGetDeviceInfo(mNative, cl::ToCLenum(name), sizeof(*value),
value, nullptr); value, nullptr);
} }
cl_int CLDeviceCL::getInfoSizeT(cl::DeviceInfo name, size_t *value) const cl_int CLDeviceCL::getInfoSizeT(cl::DeviceInfo name, size_t *value) const
{ {
return mDevice->getDispatch().clGetDeviceInfo(mDevice, cl::ToCLenum(name), sizeof(*value), return mNative->getDispatch().clGetDeviceInfo(mNative, cl::ToCLenum(name), sizeof(*value),
value, nullptr); value, nullptr);
} }
cl_int CLDeviceCL::getInfoStringLength(cl::DeviceInfo name, size_t *value) const cl_int CLDeviceCL::getInfoStringLength(cl::DeviceInfo name, size_t *value) const
{ {
return mDevice->getDispatch().clGetDeviceInfo(mDevice, cl::ToCLenum(name), 0u, nullptr, value); return mNative->getDispatch().clGetDeviceInfo(mNative, cl::ToCLenum(name), 0u, nullptr, value);
} }
cl_int CLDeviceCL::getInfoString(cl::DeviceInfo name, size_t size, char *value) const cl_int CLDeviceCL::getInfoString(cl::DeviceInfo name, size_t size, char *value) const
{ {
return mDevice->getDispatch().clGetDeviceInfo(mDevice, cl::ToCLenum(name), size, value, return mNative->getDispatch().clGetDeviceInfo(mNative, cl::ToCLenum(name), size, value,
nullptr); nullptr);
} }
cl_int CLDeviceCL::createSubDevices(const cl_device_partition_property *properties, cl_int CLDeviceCL::createSubDevices(cl::Device &device,
const cl_device_partition_property *properties,
cl_uint numDevices, cl_uint numDevices,
PtrList &implList, cl::DevicePtrList &subDeviceList,
cl_uint *numDevicesRet) cl_uint *numDevicesRet)
{ {
if (mVersion < CL_MAKE_VERSION(1, 2, 0))
{
return CL_INVALID_VALUE;
}
if (numDevices == 0u) if (numDevices == 0u)
{ {
return mDevice->getDispatch().clCreateSubDevices(mDevice, properties, 0u, nullptr, return mNative->getDispatch().clCreateSubDevices(mNative, properties, 0u, nullptr,
numDevicesRet); numDevicesRet);
} }
std::vector<cl_device_id> devices(numDevices, nullptr); std::vector<cl_device_id> nativeSubDevices(numDevices, nullptr);
const cl_int result = mDevice->getDispatch().clCreateSubDevices(mDevice, properties, numDevices, const cl_int result = mNative->getDispatch().clCreateSubDevices(
devices.data(), nullptr); mNative, properties, numDevices, nativeSubDevices.data(), nullptr);
if (result == CL_SUCCESS) if (result == CL_SUCCESS)
{ {
for (cl_device_id device : devices) for (cl_device_id nativeSubDevice : nativeSubDevices)
{ {
implList.emplace_back(CLDeviceCL::Create(getPlatform<CLPlatformCL>(), this, device)); const cl::Device::CreateImplFunc createImplFunc = [&](const cl::Device &device) {
if (!implList.back()) return Ptr(new CLDeviceCL(device, nativeSubDevice));
};
subDeviceList.emplace_back(cl::Device::CreateDevice(
device.getPlatform(), cl::DeviceRefPtr(&device), createImplFunc));
if (!subDeviceList.back())
{ {
implList.clear(); subDeviceList.clear();
return CL_INVALID_VALUE; return CL_INVALID_VALUE;
} }
mSubDevices.emplace_back(implList.back().get());
} }
} }
return result; return result;
} }
CLDeviceCL *CLDeviceCL::Create(CLPlatformCL &platform, CLDeviceCL *parent, cl_device_id device) CLDeviceCL::CLDeviceCL(const cl::Device &device, cl_device_id native)
{ : CLDeviceImpl(device), mNative(native)
size_t valueSize = 0u;
if (device->getDispatch().clGetDeviceInfo(device, CL_DEVICE_VERSION, 0u, nullptr, &valueSize) ==
CL_SUCCESS)
{
std::vector<char> valString(valueSize, '\0');
if (device->getDispatch().clGetDeviceInfo(device, CL_DEVICE_VERSION, valueSize,
valString.data(), nullptr) == CL_SUCCESS)
{
const cl_version version = ExtractCLVersion(valString.data());
if (version != 0u)
{
return new CLDeviceCL(platform, parent, device, version);
}
}
}
ERR() << "Failed to query version for device";
return nullptr;
}
CLDeviceCL::CLDeviceCL(CLPlatformCL &platform,
CLDeviceCL *parent,
cl_device_id device,
cl_version version)
: CLDeviceImpl(platform, parent), mDevice(device), mVersion(version)
{} {}
} // namespace rx } // namespace rx
...@@ -30,23 +30,23 @@ class CLDeviceCL : public CLDeviceImpl ...@@ -30,23 +30,23 @@ class CLDeviceCL : public CLDeviceImpl
cl_int getInfoStringLength(cl::DeviceInfo name, size_t *value) const override; cl_int getInfoStringLength(cl::DeviceInfo name, size_t *value) const override;
cl_int getInfoString(cl::DeviceInfo name, size_t size, char *value) const override; cl_int getInfoString(cl::DeviceInfo name, size_t size, char *value) const override;
cl_int createSubDevices(const cl_device_partition_property *properties, cl_int createSubDevices(cl::Device &device,
const cl_device_partition_property *properties,
cl_uint numDevices, cl_uint numDevices,
PtrList &implList, cl::DevicePtrList &subDeviceList,
cl_uint *numDevicesRet) override; cl_uint *numDevicesRet) override;
static CLDeviceCL *Create(CLPlatformCL &platform, CLDeviceCL *parent, cl_device_id device);
private: private:
CLDeviceCL(CLPlatformCL &platform, CLDeviceCL *parent, cl_device_id device, cl_version version); CLDeviceCL(const cl::Device &device, cl_device_id native);
const cl_device_id mNative;
const cl_device_id mDevice; friend class CLPlatformCL;
const cl_version mVersion;
}; };
inline cl_device_id CLDeviceCL::getNative() inline cl_device_id CLDeviceCL::getNative()
{ {
return mDevice; return mNative;
} }
} // namespace rx } // namespace rx
......
...@@ -20,34 +20,36 @@ class CLPlatformCL : public CLPlatformImpl ...@@ -20,34 +20,36 @@ class CLPlatformCL : public CLPlatformImpl
cl_platform_id getNative(); cl_platform_id getNative();
CLContextImpl::Ptr createContext(CLDeviceImpl::List &&deviceImplList, Info createInfo() const override;
cl::DevicePtrList createDevices(cl::Platform &platform) const override;
CLContextImpl::Ptr createContext(const cl::Context &context,
const cl::DeviceRefList &devices,
cl::ContextErrorCB notify, cl::ContextErrorCB notify,
void *userData, void *userData,
bool userSync, bool userSync,
cl_int *errcodeRet) override; cl_int *errcodeRet) override;
CLContextImpl::Ptr createContextFromType(cl_device_type deviceType, CLContextImpl::Ptr createContextFromType(const cl::Context &context,
cl_device_type deviceType,
cl::ContextErrorCB notify, cl::ContextErrorCB notify,
void *userData, void *userData,
bool userSync, bool userSync,
cl_int *errcodeRet) override; cl_int *errcodeRet) override;
static InitList GetPlatforms(bool isIcd); static void Initialize(const cl_icd_dispatch &dispatch, bool isIcd);
private: private:
CLPlatformCL(cl_platform_id platform, cl_version version, CLDeviceImpl::PtrList &devices); CLPlatformCL(const cl::Platform &platform, cl_platform_id native);
static Info GetInfo(cl_platform_id platform);
const cl_platform_id mPlatform; const cl_platform_id mNative;
const cl_version mVersion;
friend class CLContextCL; friend class CLContextCL;
}; };
inline cl_platform_id CLPlatformCL::getNative() inline cl_platform_id CLPlatformCL::getNative()
{ {
return mPlatform; return mNative;
} }
} // namespace rx } // namespace rx
......
...@@ -7,14 +7,10 @@ ...@@ -7,14 +7,10 @@
#include "libANGLE/renderer/vulkan/CLContextVk.h" #include "libANGLE/renderer/vulkan/CLContextVk.h"
#include "libANGLE/renderer/vulkan/CLPlatformVk.h"
namespace rx namespace rx
{ {
CLContextVk::CLContextVk(CLPlatformVk &platform, CLDeviceImpl::List &&devices) CLContextVk::CLContextVk(const cl::Context &context) : CLContextImpl(context) {}
: CLContextImpl(platform, std::move(devices))
{}
CLContextVk::~CLContextVk() = default; CLContextVk::~CLContextVk() = default;
......
...@@ -18,7 +18,7 @@ namespace rx ...@@ -18,7 +18,7 @@ namespace rx
class CLContextVk : public CLContextImpl class CLContextVk : public CLContextImpl
{ {
public: public:
CLContextVk(CLPlatformVk &platform, CLDeviceImpl::List &&devices); CLContextVk(const cl::Context &context);
~CLContextVk() override; ~CLContextVk() override;
}; };
......
...@@ -12,8 +12,7 @@ ...@@ -12,8 +12,7 @@
namespace rx namespace rx
{ {
CLDeviceVk::CLDeviceVk(CLPlatformVk &platform, CLDeviceVk *parent) : CLDeviceImpl(platform, parent) CLDeviceVk::CLDeviceVk(const cl::Device &device) : CLDeviceImpl(device) {}
{}
CLDeviceVk::~CLDeviceVk() = default; CLDeviceVk::~CLDeviceVk() = default;
...@@ -48,9 +47,10 @@ cl_int CLDeviceVk::getInfoString(cl::DeviceInfo name, size_t size, char *value) ...@@ -48,9 +47,10 @@ cl_int CLDeviceVk::getInfoString(cl::DeviceInfo name, size_t size, char *value)
return CL_INVALID_VALUE; return CL_INVALID_VALUE;
} }
cl_int CLDeviceVk::createSubDevices(const cl_device_partition_property *properties, cl_int CLDeviceVk::createSubDevices(cl::Device &device,
const cl_device_partition_property *properties,
cl_uint numDevices, cl_uint numDevices,
PtrList &deviceImplList, cl::DevicePtrList &subDeviceList,
cl_uint *numDevicesRet) cl_uint *numDevicesRet)
{ {
return CL_INVALID_VALUE; return CL_INVALID_VALUE;
......
...@@ -18,7 +18,7 @@ namespace rx ...@@ -18,7 +18,7 @@ namespace rx
class CLDeviceVk : public CLDeviceImpl class CLDeviceVk : public CLDeviceImpl
{ {
public: public:
CLDeviceVk(CLPlatformVk &platform, CLDeviceVk *parent); explicit CLDeviceVk(const cl::Device &device);
~CLDeviceVk() override; ~CLDeviceVk() override;
Info createInfo() const override; Info createInfo() const override;
...@@ -29,9 +29,10 @@ class CLDeviceVk : public CLDeviceImpl ...@@ -29,9 +29,10 @@ class CLDeviceVk : public CLDeviceImpl
cl_int getInfoStringLength(cl::DeviceInfo name, size_t *value) const override; cl_int getInfoStringLength(cl::DeviceInfo name, size_t *value) const override;
cl_int getInfoString(cl::DeviceInfo name, size_t size, char *value) const override; cl_int getInfoString(cl::DeviceInfo name, size_t size, char *value) const override;
cl_int createSubDevices(const cl_device_partition_property *properties, cl_int createSubDevices(cl::Device &device,
const cl_device_partition_property *properties,
cl_uint numDevices, cl_uint numDevices,
PtrList &deviceImplList, cl::DevicePtrList &subDeviceList,
cl_uint *numDevicesRet) override; cl_uint *numDevicesRet) override;
}; };
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
#include "libANGLE/renderer/vulkan/CLDeviceVk.h" #include "libANGLE/renderer/vulkan/CLDeviceVk.h"
#include "libANGLE/CLPlatform.h"
#include "anglebase/no_destructor.h" #include "anglebase/no_destructor.h"
#include "common/angle_version.h" #include "common/angle_version.h"
...@@ -17,6 +19,7 @@ namespace rx ...@@ -17,6 +19,7 @@ namespace rx
namespace namespace
{ {
std::string CreateExtensionString(const NameVersionVector &extList) std::string CreateExtensionString(const NameVersionVector &extList)
{ {
std::string extensions; std::string extensions;
...@@ -32,17 +35,43 @@ std::string CreateExtensionString(const NameVersionVector &extList) ...@@ -32,17 +35,43 @@ std::string CreateExtensionString(const NameVersionVector &extList)
return extensions; return extensions;
} }
CLDeviceImpl::List CreateDevices(CLPlatformVk &platform, CLDeviceImpl::PtrList &implList)
{
implList.emplace_back(new CLDeviceVk(platform, nullptr));
return CLDeviceImpl::List(1u, implList.back().get());
}
} // namespace } // namespace
CLPlatformVk::~CLPlatformVk() = default; CLPlatformVk::~CLPlatformVk() = default;
CLContextImpl::Ptr CLPlatformVk::createContext(CLDeviceImpl::List &&deviceImplList, CLPlatformImpl::Info CLPlatformVk::createInfo() const
{
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"}};
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;
return info;
}
cl::DevicePtrList CLPlatformVk::createDevices(cl::Platform &platform) const
{
cl::DevicePtrList devices;
const cl::Device::CreateImplFunc createImplFunc = [](const cl::Device &device) {
return CLDeviceVk::Ptr(new CLDeviceVk(device));
};
devices.emplace_back(cl::Device::CreateDevice(platform, nullptr, createImplFunc));
if (!devices.back())
{
devices.clear();
}
return devices;
}
CLContextImpl::Ptr CLPlatformVk::createContext(const cl::Context &context,
const cl::DeviceRefList &devices,
cl::ContextErrorCB notify, cl::ContextErrorCB notify,
void *userData, void *userData,
bool userSync, bool userSync,
...@@ -52,7 +81,8 @@ CLContextImpl::Ptr CLPlatformVk::createContext(CLDeviceImpl::List &&deviceImplLi ...@@ -52,7 +81,8 @@ CLContextImpl::Ptr CLPlatformVk::createContext(CLDeviceImpl::List &&deviceImplLi
return contextImpl; return contextImpl;
} }
CLContextImpl::Ptr CLPlatformVk::createContextFromType(cl_device_type deviceType, CLContextImpl::Ptr CLPlatformVk::createContextFromType(const cl::Context &context,
cl_device_type deviceType,
cl::ContextErrorCB notify, cl::ContextErrorCB notify,
void *userData, void *userData,
bool userSync, bool userSync,
...@@ -62,32 +92,12 @@ CLContextImpl::Ptr CLPlatformVk::createContextFromType(cl_device_type deviceType ...@@ -62,32 +92,12 @@ CLContextImpl::Ptr CLPlatformVk::createContextFromType(cl_device_type deviceType
return contextImpl; return contextImpl;
} }
CLPlatformVk::InitList CLPlatformVk::GetPlatforms() void CLPlatformVk::Initialize(const cl_icd_dispatch &dispatch)
{ {
NameVersionVector extList = { const cl::Platform::CreateImplFunc createImplFunc = [](const cl::Platform &platform) {
cl_name_version{CL_MAKE_VERSION(1, 0, 0), "cl_khr_icd"}, return Ptr(new CLPlatformVk(platform));
cl_name_version{CL_MAKE_VERSION(1, 0, 0), "cl_khr_extended_versioning"}}; };
cl::Platform::CreatePlatform(dispatch, createImplFunc);
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;
if (info.isValid())
{
CLDeviceImpl::PtrList devices;
Ptr platform(new CLPlatformVk(devices));
if (!devices.empty())
{
initList.emplace_back(std::move(platform), std::move(info), std::move(devices));
}
}
return initList;
} }
const std::string &CLPlatformVk::GetVersionString() const std::string &CLPlatformVk::GetVersionString()
...@@ -98,8 +108,6 @@ const std::string &CLPlatformVk::GetVersionString() ...@@ -98,8 +108,6 @@ const std::string &CLPlatformVk::GetVersionString()
return *sVersion; return *sVersion;
} }
CLPlatformVk::CLPlatformVk(CLDeviceImpl::PtrList &devices) CLPlatformVk::CLPlatformVk(const cl::Platform &platform) : CLPlatformImpl(platform) {}
: CLPlatformImpl(CreateDevices(*this, devices))
{}
} // namespace rx } // namespace rx
...@@ -10,8 +10,6 @@ ...@@ -10,8 +10,6 @@
#include "libANGLE/renderer/CLPlatformImpl.h" #include "libANGLE/renderer/CLPlatformImpl.h"
#include <string>
namespace rx namespace rx
{ {
...@@ -20,24 +18,30 @@ class CLPlatformVk : public CLPlatformImpl ...@@ -20,24 +18,30 @@ class CLPlatformVk : public CLPlatformImpl
public: public:
~CLPlatformVk() override; ~CLPlatformVk() override;
CLContextImpl::Ptr createContext(CLDeviceImpl::List &&deviceImplList, Info createInfo() const override;
cl::DevicePtrList createDevices(cl::Platform &platform) const override;
CLContextImpl::Ptr createContext(const cl::Context &context,
const cl::DeviceRefList &devices,
cl::ContextErrorCB notify, cl::ContextErrorCB notify,
void *userData, void *userData,
bool userSync, bool userSync,
cl_int *errcodeRet) override; cl_int *errcodeRet) override;
CLContextImpl::Ptr createContextFromType(cl_device_type deviceType, CLContextImpl::Ptr createContextFromType(const cl::Context &context,
cl_device_type deviceType,
cl::ContextErrorCB notify, cl::ContextErrorCB notify,
void *userData, void *userData,
bool userSync, bool userSync,
cl_int *errcodeRet) override; cl_int *errcodeRet) override;
static InitList GetPlatforms(); static void Initialize(const cl_icd_dispatch &dispatch);
static constexpr cl_version GetVersion(); static constexpr cl_version GetVersion();
static const std::string &GetVersionString(); static const std::string &GetVersionString();
private: private:
explicit CLPlatformVk(CLDeviceImpl::PtrList &devices); explicit CLPlatformVk(const cl::Platform &platform);
}; };
constexpr cl_version CLPlatformVk::GetVersion() constexpr cl_version CLPlatformVk::GetVersion()
......
...@@ -9,8 +9,6 @@ ...@@ -9,8 +9,6 @@
#include "libGLESv2/cl_dispatch_table.h" #include "libGLESv2/cl_dispatch_table.h"
#include "libANGLE/CLPlatform.h"
#ifdef ANGLE_ENABLE_CL_PASSTHROUGH #ifdef ANGLE_ENABLE_CL_PASSTHROUGH
# include "libANGLE/renderer/cl/CLPlatformCL.h" # include "libANGLE/renderer/cl/CLPlatformCL.h"
#endif #endif
...@@ -31,21 +29,11 @@ void InitBackEnds(bool isIcd) ...@@ -31,21 +29,11 @@ void InitBackEnds(bool isIcd)
initialized = true; initialized = true;
#ifdef ANGLE_ENABLE_CL_PASSTHROUGH #ifdef ANGLE_ENABLE_CL_PASSTHROUGH
rx::CLPlatformImpl::InitList initListCL = rx::CLPlatformCL::GetPlatforms(isIcd); rx::CLPlatformCL::Initialize(gCLIcdDispatchTable, isIcd);
while (!initListCL.empty())
{
Platform::CreatePlatform(gCLIcdDispatchTable, initListCL.front());
initListCL.pop_front();
}
#endif #endif
#ifdef ANGLE_ENABLE_VULKAN #ifdef ANGLE_ENABLE_VULKAN
rx::CLPlatformImpl::InitList initListVk = rx::CLPlatformVk::GetPlatforms(); rx::CLPlatformVk::Initialize(gCLIcdDispatchTable);
while (!initListVk.empty())
{
Platform::CreatePlatform(gCLIcdDispatchTable, initListVk.front());
initListVk.pop_front();
}
#endif #endif
} }
......
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