Hash :
cb87d78c
Author :
Date :
2021-07-08T08:28:04
Enable -Wshadow with Clang. Also fixes a few instances of variable shadowing in the code. Bug: angleproject:6148 Change-Id: Ic51d722a3f953f246f51af6d74abb302f832cf44 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3014875 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
//
// 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.
//
// CLContextCL.cpp: Implements the class methods for CLContextCL.
#include "libANGLE/renderer/cl/CLContextCL.h"
#include "libANGLE/renderer/cl/CLCommandQueueCL.h"
#include "libANGLE/renderer/cl/CLDeviceCL.h"
#include "libANGLE/renderer/cl/CLEventCL.h"
#include "libANGLE/renderer/cl/CLMemoryCL.h"
#include "libANGLE/renderer/cl/CLPlatformCL.h"
#include "libANGLE/renderer/cl/CLProgramCL.h"
#include "libANGLE/renderer/cl/CLSamplerCL.h"
#include "libANGLE/CLBuffer.h"
#include "libANGLE/CLCommandQueue.h"
#include "libANGLE/CLContext.h"
#include "libANGLE/CLDevice.h"
#include "libANGLE/CLEvent.h"
#include "libANGLE/CLImage.h"
#include "libANGLE/CLMemory.h"
#include "libANGLE/CLPlatform.h"
#include "libANGLE/CLProgram.h"
#include "libANGLE/CLSampler.h"
#include "libANGLE/cl_utils.h"
namespace rx
{
CLContextCL::CLContextCL(const cl::Context &context, cl_context native)
: CLContextImpl(context), mNative(native)
{}
CLContextCL::~CLContextCL()
{
if (mNative->getDispatch().clReleaseContext(mNative) != CL_SUCCESS)
{
ERR() << "Error while releasing CL context";
}
}
cl::DevicePtrs CLContextCL::getDevices(cl_int &errorCode) const
{
size_t valueSize = 0u;
errorCode = mNative->getDispatch().clGetContextInfo(mNative, CL_CONTEXT_DEVICES, 0u, nullptr,
&valueSize);
if (errorCode == CL_SUCCESS && (valueSize % sizeof(cl_device_id)) == 0u)
{
std::vector<cl_device_id> nativeDevices(valueSize / sizeof(cl_device_id), nullptr);
errorCode = mNative->getDispatch().clGetContextInfo(mNative, CL_CONTEXT_DEVICES, valueSize,
nativeDevices.data(), nullptr);
if (errorCode == CL_SUCCESS)
{
const cl::DevicePtrs &platformDevices = mContext.getPlatform().getDevices();
cl::DevicePtrs devices;
devices.reserve(nativeDevices.size());
for (cl_device_id nativeDevice : nativeDevices)
{
auto it = platformDevices.cbegin();
while (it != platformDevices.cend() &&
(*it)->getImpl<CLDeviceCL>().getNative() != nativeDevice)
{
++it;
}
if (it != platformDevices.cend())
{
devices.emplace_back(it->get());
}
else
{
ASSERT(false);
errorCode = CL_INVALID_DEVICE;
ERR() << "Device not found in platform list";
return cl::DevicePtrs{};
}
}
return devices;
}
}
return cl::DevicePtrs{};
}
CLCommandQueueImpl::Ptr CLContextCL::createCommandQueue(const cl::CommandQueue &commandQueue,
cl_int &errorCode)
{
const cl::Device &device = commandQueue.getDevice();
const cl_device_id nativeDevice = device.getImpl<CLDeviceCL>().getNative();
cl_command_queue nativeQueue = nullptr;
if (!device.isVersionOrNewer(2u, 0u))
{
nativeQueue = mNative->getDispatch().clCreateCommandQueue(
mNative, nativeDevice, commandQueue.getProperties().get(), &errorCode);
}
else
{
const cl_queue_properties propArray[] = {
CL_QUEUE_PROPERTIES, commandQueue.getProperties().get(),
commandQueue.hasSize() ? CL_QUEUE_SIZE : 0u, commandQueue.getSize(), 0u};
nativeQueue = mNative->getDispatch().clCreateCommandQueueWithProperties(
mNative, nativeDevice, propArray, &errorCode);
}
return CLCommandQueueImpl::Ptr(
nativeQueue != nullptr ? new CLCommandQueueCL(commandQueue, nativeQueue) : nullptr);
}
CLMemoryImpl::Ptr CLContextCL::createBuffer(const cl::Buffer &buffer,
size_t size,
void *hostPtr,
cl_int &errorCode)
{
cl_mem nativeBuffer = nullptr;
if (buffer.getProperties().empty())
{
nativeBuffer = mNative->getDispatch().clCreateBuffer(mNative, buffer.getFlags().get(), size,
hostPtr, &errorCode);
}
else
{
nativeBuffer = mNative->getDispatch().clCreateBufferWithProperties(
mNative, buffer.getProperties().data(), buffer.getFlags().get(), size, hostPtr,
&errorCode);
}
return CLMemoryImpl::Ptr(nativeBuffer != nullptr ? new CLMemoryCL(buffer, nativeBuffer)
: nullptr);
}
CLMemoryImpl::Ptr CLContextCL::createImage(const cl::Image &image,
cl::MemFlags flags,
const cl_image_format &format,
const cl::ImageDescriptor &desc,
void *hostPtr,
cl_int &errorCode)
{
cl_mem nativeImage = nullptr;
if (mContext.getPlatform().isVersionOrNewer(1u, 2u))
{
const cl_mem_object_type nativeType = cl::ToCLenum(desc.type);
const cl_mem nativeParent =
image.getParent() ? image.getParent()->getImpl<CLMemoryCL>().getNative() : nullptr;
const cl_image_desc nativeDesc = {
nativeType, desc.width, desc.height, desc.depth, desc.arraySize,
desc.rowPitch, desc.slicePitch, desc.numMipLevels, desc.numSamples, {nativeParent}};
if (image.getProperties().empty())
{
nativeImage = mNative->getDispatch().clCreateImage(mNative, flags.get(), &format,
&nativeDesc, hostPtr, &errorCode);
}
else
{
nativeImage = mNative->getDispatch().clCreateImageWithProperties(
mNative, image.getProperties().data(), flags.get(), &format, &nativeDesc, hostPtr,
&errorCode);
}
}
else
{
switch (desc.type)
{
case cl::MemObjectType::Image2D:
nativeImage = mNative->getDispatch().clCreateImage2D(
mNative, flags.get(), &format, desc.width, desc.height, desc.rowPitch, hostPtr,
&errorCode);
break;
case cl::MemObjectType::Image3D:
nativeImage = mNative->getDispatch().clCreateImage3D(
mNative, flags.get(), &format, desc.width, desc.height, desc.depth,
desc.rowPitch, desc.slicePitch, hostPtr, &errorCode);
break;
default:
ASSERT(false);
ERR() << "Failed to create unsupported image type";
break;
}
}
return CLMemoryImpl::Ptr(nativeImage != nullptr ? new CLMemoryCL(image, nativeImage) : nullptr);
}
cl_int CLContextCL::getSupportedImageFormats(cl::MemFlags flags,
cl::MemObjectType imageType,
cl_uint numEntries,
cl_image_format *imageFormats,
cl_uint *numImageFormats)
{
// Fetch available image formats for given flags and image type.
cl_uint numFormats = 0u;
ANGLE_CL_TRY(mNative->getDispatch().clGetSupportedImageFormats(
mNative, flags.get(), cl::ToCLenum(imageType), 0u, nullptr, &numFormats));
std::vector<cl_image_format> formats(numFormats);
ANGLE_CL_TRY(mNative->getDispatch().clGetSupportedImageFormats(
mNative, flags.get(), cl::ToCLenum(imageType), numFormats, formats.data(), nullptr));
// Filter out formats which are not supported by front end.
const CLPlatformImpl::Info &info = mContext.getPlatform().getInfo();
std::vector<cl_image_format> supportedFormats;
supportedFormats.reserve(formats.size());
std::copy_if(
formats.cbegin(), formats.cend(), std::back_inserter(supportedFormats),
[&](const cl_image_format &format) { return cl::IsValidImageFormat(&format, info); });
if (imageFormats != nullptr)
{
auto formatIt = supportedFormats.cbegin();
while (numEntries-- != 0u && formatIt != supportedFormats.cend())
{
*imageFormats++ = *formatIt++;
}
}
if (numImageFormats != nullptr)
{
*numImageFormats = static_cast<cl_uint>(supportedFormats.size());
}
return CL_SUCCESS;
}
CLSamplerImpl::Ptr CLContextCL::createSampler(const cl::Sampler &sampler, cl_int &errorCode)
{
cl_sampler nativeSampler = nullptr;
if (!mContext.getPlatform().isVersionOrNewer(2u, 0u))
{
nativeSampler = mNative->getDispatch().clCreateSampler(
mNative, sampler.getNormalizedCoords(), cl::ToCLenum(sampler.getAddressingMode()),
cl::ToCLenum(sampler.getFilterMode()), &errorCode);
}
else if (!sampler.getProperties().empty())
{
nativeSampler = mNative->getDispatch().clCreateSamplerWithProperties(
mNative, sampler.getProperties().data(), &errorCode);
}
else
{
const cl_sampler_properties propArray[] = {CL_SAMPLER_NORMALIZED_COORDS,
sampler.getNormalizedCoords(),
CL_SAMPLER_ADDRESSING_MODE,
cl::ToCLenum(sampler.getAddressingMode()),
CL_SAMPLER_FILTER_MODE,
cl::ToCLenum(sampler.getFilterMode()),
0u};
nativeSampler =
mNative->getDispatch().clCreateSamplerWithProperties(mNative, propArray, &errorCode);
}
return CLSamplerImpl::Ptr(nativeSampler != nullptr ? new CLSamplerCL(sampler, nativeSampler)
: nullptr);
}
CLProgramImpl::Ptr CLContextCL::createProgramWithSource(const cl::Program &program,
const std::string &source,
cl_int &errorCode)
{
const char *sourceStr = source.c_str();
const size_t length = source.length();
const cl_program nativeProgram = mNative->getDispatch().clCreateProgramWithSource(
mNative, 1u, &sourceStr, &length, &errorCode);
return CLProgramImpl::Ptr(nativeProgram != nullptr ? new CLProgramCL(program, nativeProgram)
: nullptr);
}
CLProgramImpl::Ptr CLContextCL::createProgramWithIL(const cl::Program &program,
const void *il,
size_t length,
cl_int &errorCode)
{
const cl_program nativeProgram =
mNative->getDispatch().clCreateProgramWithIL(mNative, il, length, &errorCode);
return CLProgramImpl::Ptr(nativeProgram != nullptr ? new CLProgramCL(program, nativeProgram)
: nullptr);
}
CLProgramImpl::Ptr CLContextCL::createProgramWithBinary(const cl::Program &program,
const size_t *lengths,
const unsigned char **binaries,
cl_int *binaryStatus,
cl_int &errorCode)
{
std::vector<cl_device_id> nativeDevices;
for (const cl::DevicePtr &device : program.getDevices())
{
nativeDevices.emplace_back(device->getImpl<CLDeviceCL>().getNative());
}
cl_program nativeProgram = mNative->getDispatch().clCreateProgramWithBinary(
mNative, static_cast<cl_uint>(nativeDevices.size()), nativeDevices.data(), lengths,
binaries, binaryStatus, &errorCode);
return CLProgramImpl::Ptr(nativeProgram != nullptr ? new CLProgramCL(program, nativeProgram)
: nullptr);
}
CLProgramImpl::Ptr CLContextCL::createProgramWithBuiltInKernels(const cl::Program &program,
const char *kernel_names,
cl_int &errorCode)
{
std::vector<cl_device_id> nativeDevices;
for (const cl::DevicePtr &device : program.getDevices())
{
nativeDevices.emplace_back(device->getImpl<CLDeviceCL>().getNative());
}
const cl_program nativeProgram = mNative->getDispatch().clCreateProgramWithBuiltInKernels(
mNative, static_cast<cl_uint>(nativeDevices.size()), nativeDevices.data(), kernel_names,
&errorCode);
return CLProgramImpl::Ptr(nativeProgram != nullptr ? new CLProgramCL(program, nativeProgram)
: nullptr);
}
CLProgramImpl::Ptr CLContextCL::linkProgram(const cl::Program &program,
const cl::DevicePtrs &devices,
const char *options,
const cl::ProgramPtrs &inputPrograms,
cl::Program *notify,
cl_int &errorCode)
{
std::vector<cl_device_id> nativeDevices;
for (const cl::DevicePtr &device : devices)
{
nativeDevices.emplace_back(device->getImpl<CLDeviceCL>().getNative());
}
const cl_uint numDevices = static_cast<cl_uint>(nativeDevices.size());
const cl_device_id *const nativeDevicesPtr =
!nativeDevices.empty() ? nativeDevices.data() : nullptr;
std::vector<cl_program> nativePrograms;
for (const cl::ProgramPtr &inputProgram : inputPrograms)
{
nativePrograms.emplace_back(inputProgram->getImpl<CLProgramCL>().getNative());
}
const cl_uint numInputHeaders = static_cast<cl_uint>(nativePrograms.size());
const cl::ProgramCB callback = notify != nullptr ? CLProgramCL::Callback : nullptr;
const cl_program nativeProgram = mNative->getDispatch().clLinkProgram(
mNative, numDevices, nativeDevicesPtr, options, numInputHeaders, nativePrograms.data(),
callback, notify, &errorCode);
return CLProgramImpl::Ptr(nativeProgram != nullptr ? new CLProgramCL(program, nativeProgram)
: nullptr);
}
CLEventImpl::Ptr CLContextCL::createUserEvent(const cl::Event &event, cl_int &errorCode)
{
const cl_event nativeEvent = mNative->getDispatch().clCreateUserEvent(mNative, &errorCode);
return CLEventImpl::Ptr(nativeEvent != nullptr ? new CLEventCL(event, nativeEvent) : nullptr);
}
cl_int CLContextCL::waitForEvents(const cl::EventPtrs &events)
{
const std::vector<cl_event> nativeEvents = CLEventCL::Cast(events);
return mNative->getDispatch().clWaitForEvents(static_cast<cl_uint>(nativeEvents.size()),
nativeEvents.data());
}
} // namespace rx