Hash :
dc8b2e2b
Author :
Date :
2024-06-26T12:44:06
CL: Pass in memory properties from cl entry point The spec requires the valid properties to be reported as by `clGetMemObjectInfo()`. So pass in the properties as is post their validation. Bug: angleproject:378017028 Change-Id: I8b7ddc9d0e71ea56b2f8d81764fc3a21cee6bef4 Signed-off-by: Gowtham Tammana <g.tammana@samsung.com> Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6004684 Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
//
// 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.
//
// CLContext.cpp: Implements the cl::Context class.
#include "libANGLE/CLContext.h"
#include "libANGLE/CLBuffer.h"
#include "libANGLE/CLCommandQueue.h"
#include "libANGLE/CLEvent.h"
#include "libANGLE/CLImage.h"
#include "libANGLE/CLMemory.h"
#include "libANGLE/CLProgram.h"
#include "libANGLE/CLSampler.h"
#include <cstring>
namespace cl
{
angle::Result Context::getInfo(ContextInfo name,
size_t valueSize,
void *value,
size_t *valueSizeRet) const
{
std::vector<cl_device_id> devices;
cl_uint valUInt = 0u;
const void *copyValue = nullptr;
size_t copySize = 0u;
switch (name)
{
case ContextInfo::ReferenceCount:
valUInt = getRefCount();
copyValue = &valUInt;
copySize = sizeof(valUInt);
break;
case ContextInfo::NumDevices:
valUInt = static_cast<decltype(valUInt)>(mDevices.size());
copyValue = &valUInt;
copySize = sizeof(valUInt);
break;
case ContextInfo::Devices:
devices.reserve(mDevices.size());
for (const DevicePtr &device : mDevices)
{
devices.emplace_back(device->getNative());
}
copyValue = devices.data();
copySize = devices.size() * sizeof(decltype(devices)::value_type);
break;
case ContextInfo::Properties:
copyValue = mProperties.data();
copySize = mProperties.size() * sizeof(decltype(mProperties)::value_type);
break;
default:
ASSERT(false);
ANGLE_CL_RETURN_ERROR(CL_INVALID_VALUE);
}
if (value != nullptr)
{
// CL_INVALID_VALUE if size in bytes specified by param_value_size is < size of return type
// as specified in the Context Attributes table and param_value is not a NULL value.
if (valueSize < copySize)
{
ANGLE_CL_RETURN_ERROR(CL_INVALID_VALUE);
}
if (copyValue != nullptr)
{
std::memcpy(value, copyValue, copySize);
}
}
if (valueSizeRet != nullptr)
{
*valueSizeRet = copySize;
}
return angle::Result::Continue;
}
cl_command_queue Context::createCommandQueueWithProperties(cl_device_id device,
const cl_queue_properties *properties)
{
CommandQueue::PropArray propArray;
CommandQueueProperties props;
cl_uint size = CommandQueue::kNoSize;
if (properties != nullptr)
{
const cl_queue_properties *propIt = properties;
while (*propIt != 0)
{
switch (*propIt++)
{
case CL_QUEUE_PROPERTIES:
props = static_cast<cl_command_queue_properties>(*propIt++);
break;
case CL_QUEUE_SIZE:
size = static_cast<decltype(size)>(*propIt++);
break;
}
}
// Include the trailing zero
++propIt;
propArray.reserve(propIt - properties);
propArray.insert(propArray.cend(), properties, propIt);
}
return Object::Create<CommandQueue>(*this, device->cast<Device>(), std::move(propArray), props,
size);
}
cl_command_queue Context::createCommandQueue(cl_device_id device, CommandQueueProperties properties)
{
return Object::Create<CommandQueue>(*this, device->cast<Device>(), properties);
}
cl_mem Context::createBuffer(const cl_mem_properties *properties,
MemFlags flags,
size_t size,
void *hostPtr)
{
// If the properties argument specified in clCreateBufferWithProperties or
// clCreateImageWithProperties used to create memobj was not NULL, the implementation must
// return the values specified in the properties argument in the same order and without
// including additional properties. So pass them as is.
Memory::PropArray propArray;
if (properties != nullptr)
{
const cl_mem_properties *propIt = properties;
while (*propIt != 0)
{
propArray.push_back(*propIt);
++propIt;
}
// there is at least one property - special property 0
propArray.push_back(0);
}
return Object::Create<Buffer>(*this, std::move(propArray), flags, size, hostPtr);
}
cl_mem Context::createImage(const cl_mem_properties *properties,
MemFlags flags,
const cl_image_format *format,
const cl_image_desc *desc,
void *hostPtr)
{
const ImageDescriptor imageDesc = {FromCLenum<MemObjectType>(desc->image_type),
desc->image_width,
desc->image_height,
desc->image_depth,
desc->image_array_size,
desc->image_row_pitch,
desc->image_slice_pitch,
desc->num_mip_levels,
desc->num_samples};
Memory::PropArray propArray;
// If the properties argument specified in clCreateBufferWithProperties or
// clCreateImageWithProperties used to create memobj was not NULL, the implementation must
// return the values specified in the properties argument in the same order and without
// including additional properties. So Pass them as is.
if (properties != nullptr)
{
const cl_mem_properties *propIt = properties;
while (*propIt != 0)
{
propArray.push_back(*propIt);
++propIt;
}
// there is at least one property - special property 0
propArray.push_back(0);
}
return Object::Create<Image>(*this, std::move(propArray), flags, *format, imageDesc,
Memory::Cast(desc->buffer), hostPtr);
}
cl_mem Context::createImage2D(MemFlags flags,
const cl_image_format *format,
size_t width,
size_t height,
size_t rowPitch,
void *hostPtr)
{
const ImageDescriptor imageDesc(MemObjectType::Image2D, width, height, 0u, 0u, rowPitch, 0u, 0u,
0u);
return Object::Create<Image>(*this, Memory::PropArray{}, flags, *format, imageDesc, nullptr,
hostPtr);
}
cl_mem Context::createImage3D(MemFlags flags,
const cl_image_format *format,
size_t width,
size_t height,
size_t depth,
size_t rowPitch,
size_t slicePitch,
void *hostPtr)
{
const ImageDescriptor imageDesc(MemObjectType::Image3D, width, height, depth, 0u, rowPitch,
slicePitch, 0u, 0u);
return Object::Create<Image>(*this, Memory::PropArray{}, flags, *format, imageDesc, nullptr,
hostPtr);
}
angle::Result Context::getSupportedImageFormats(MemFlags flags,
MemObjectType imageType,
cl_uint numEntries,
cl_image_format *imageFormats,
cl_uint *numImageFormats)
{
return mImpl->getSupportedImageFormats(flags, imageType, numEntries, imageFormats,
numImageFormats);
}
cl_sampler Context::createSamplerWithProperties(const cl_sampler_properties *properties)
{
Sampler::PropArray propArray;
cl_bool normalizedCoords = CL_TRUE;
AddressingMode addressingMode = AddressingMode::Clamp;
FilterMode filterMode = FilterMode::Nearest;
if (properties != nullptr)
{
const cl_sampler_properties *propIt = properties;
while (*propIt != 0)
{
switch (*propIt++)
{
case CL_SAMPLER_NORMALIZED_COORDS:
normalizedCoords = static_cast<decltype(normalizedCoords)>(*propIt++);
break;
case CL_SAMPLER_ADDRESSING_MODE:
addressingMode = FromCLenum<AddressingMode>(static_cast<CLenum>(*propIt++));
break;
case CL_SAMPLER_FILTER_MODE:
filterMode = FromCLenum<FilterMode>(static_cast<CLenum>(*propIt++));
break;
}
}
// Include the trailing zero
++propIt;
propArray.reserve(propIt - properties);
propArray.insert(propArray.cend(), properties, propIt);
}
return Object::Create<Sampler>(*this, std::move(propArray), normalizedCoords, addressingMode,
filterMode);
}
cl_sampler Context::createSampler(cl_bool normalizedCoords,
AddressingMode addressingMode,
FilterMode filterMode)
{
return Object::Create<Sampler>(*this, Sampler::PropArray{}, normalizedCoords, addressingMode,
filterMode);
}
cl_program Context::createProgramWithSource(cl_uint count,
const char **strings,
const size_t *lengths)
{
std::string source;
if (lengths == nullptr)
{
while (count-- != 0u)
{
source.append(*strings++);
}
}
else
{
while (count-- != 0u)
{
if (*lengths != 0u)
{
source.append(*strings++, *lengths);
}
else
{
source.append(*strings++);
}
++lengths;
}
}
return Object::Create<Program>(*this, std::move(source));
}
cl_program Context::createProgramWithIL(const void *il, size_t length)
{
return Object::Create<Program>(*this, il, length);
}
cl_program Context::createProgramWithBinary(cl_uint numDevices,
const cl_device_id *devices,
const size_t *lengths,
const unsigned char **binaries,
cl_int *binaryStatus)
{
DevicePtrs devs;
devs.reserve(numDevices);
while (numDevices-- != 0u)
{
devs.emplace_back(&(*devices++)->cast<Device>());
}
return Object::Create<Program>(*this, std::move(devs), lengths, binaries, binaryStatus);
}
cl_program Context::createProgramWithBuiltInKernels(cl_uint numDevices,
const cl_device_id *devices,
const char *kernelNames)
{
DevicePtrs devs;
devs.reserve(numDevices);
while (numDevices-- != 0u)
{
devs.emplace_back(&(*devices++)->cast<Device>());
}
return Object::Create<Program>(*this, std::move(devs), kernelNames);
}
cl_program Context::linkProgram(cl_uint numDevices,
const cl_device_id *deviceList,
const char *options,
cl_uint numInputPrograms,
const cl_program *inputPrograms,
ProgramCB pfnNotify,
void *userData)
{
DevicePtrs devices;
devices.reserve(numDevices);
while (numDevices-- != 0u)
{
devices.emplace_back(&(*deviceList++)->cast<Device>());
}
ProgramPtrs programs;
programs.reserve(numInputPrograms);
while (numInputPrograms-- != 0u)
{
programs.emplace_back(&(*inputPrograms++)->cast<Program>());
}
return Object::Create<Program>(*this, devices, options, programs, pfnNotify, userData);
}
cl_event Context::createUserEvent()
{
return Object::Create<Event>(*this);
}
angle::Result Context::waitForEvents(cl_uint numEvents, const cl_event *eventList)
{
return mImpl->waitForEvents(Event::Cast(numEvents, eventList));
}
Context::~Context() = default;
void Context::ErrorCallback(const char *errinfo, const void *privateInfo, size_t cb, void *userData)
{
Context *const context = static_cast<Context *>(userData);
if (!Context::IsValid(context))
{
WARN() << "Context error for invalid context";
return;
}
if (context->mNotify != nullptr)
{
context->mNotify(errinfo, privateInfo, cb, context->mUserData);
}
}
Context::Context(Platform &platform,
PropArray &&properties,
DevicePtrs &&devices,
ContextErrorCB notify,
void *userData,
bool userSync)
: mPlatform(platform),
mProperties(std::move(properties)),
mNotify(notify),
mUserData(userData),
mImpl(nullptr),
mDevices(std::move(devices))
{
ANGLE_CL_IMPL_TRY(platform.getImpl().createContext(*this, mDevices, userSync, &mImpl));
}
Context::Context(Platform &platform,
PropArray &&properties,
DeviceType deviceType,
ContextErrorCB notify,
void *userData,
bool userSync)
: mPlatform(platform),
mProperties(std::move(properties)),
mNotify(notify),
mUserData(userData),
mImpl(nullptr)
{
if (!IsError(platform.getImpl().createContextFromType(*this, deviceType, userSync, &mImpl)))
{
ANGLE_CL_IMPL_TRY(mImpl->getDevices(&mDevices));
}
}
} // namespace cl