Hash :
3bc4e17f
Author :
Date :
2024-05-22T16:15:23
Vulkan: Pack familyIndex and queueIndex into single uint16_t Practically these are very small number. So we really don't need a uint32_t for each index. The only trick is that even though these indices are uint32_t, some of predefined values are ~1u/~2u which does not fit uint8_t. In this CL, int8_t is used so that all these values are properly downsized and expanded by compiler without branch. Bug: b/337135577 Change-Id: I45ac8ea14c4d7a6f65cb012df70013050f725583 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5560510 Reviewed-by: Yuxin Hu <yuxinhu@google.com> Commit-Queue: Charlie Lao <cclao@google.com> 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
//
// 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.
//
// CLContextVk.cpp: Implements the class methods for CLContextVk.
#include "libANGLE/renderer/vulkan/CLContextVk.h"
#include "libANGLE/renderer/vulkan/CLCommandQueueVk.h"
#include "libANGLE/renderer/vulkan/CLEventVk.h"
#include "libANGLE/renderer/vulkan/CLMemoryVk.h"
#include "libANGLE/renderer/vulkan/CLProgramVk.h"
#include "libANGLE/renderer/vulkan/vk_renderer.h"
#include "libANGLE/renderer/vulkan/vk_utils.h"
#include "libANGLE/CLBuffer.h"
#include "libANGLE/CLContext.h"
#include "libANGLE/CLEvent.h"
#include "libANGLE/CLProgram.h"
#include "libANGLE/cl_utils.h"
namespace rx
{
CLContextVk::CLContextVk(const cl::Context &context, const cl::DevicePtrs devicePtrs)
: CLContextImpl(context),
vk::Context(getPlatform()->getRenderer()),
mAssociatedDevices(devicePtrs)
{
mDeviceQueueIndex = mRenderer->getDefaultDeviceQueueIndex();
}
CLContextVk::~CLContextVk() = default;
void CLContextVk::handleError(VkResult errorCode,
const char *file,
const char *function,
unsigned int line)
{
ASSERT(errorCode != VK_SUCCESS);
CLenum clErrorCode = CL_SUCCESS;
switch (errorCode)
{
case VK_ERROR_TOO_MANY_OBJECTS:
case VK_ERROR_OUT_OF_HOST_MEMORY:
case VK_ERROR_OUT_OF_DEVICE_MEMORY:
clErrorCode = CL_OUT_OF_HOST_MEMORY;
break;
default:
clErrorCode = CL_INVALID_OPERATION;
}
ERR() << "Internal Vulkan error (" << errorCode << "): " << VulkanResultString(errorCode);
ERR() << " CL error (" << clErrorCode << ")";
if (errorCode == VK_ERROR_DEVICE_LOST)
{
handleDeviceLost();
}
ANGLE_CL_SET_ERROR(clErrorCode);
}
void CLContextVk::handleDeviceLost() const
{
// For now just notify the renderer
getRenderer()->notifyDeviceLost();
}
angle::Result CLContextVk::getDevices(cl::DevicePtrs *devicePtrsOut) const
{
ASSERT(!mAssociatedDevices.empty());
*devicePtrsOut = mAssociatedDevices;
return angle::Result::Continue;
}
angle::Result CLContextVk::createCommandQueue(const cl::CommandQueue &commandQueue,
CLCommandQueueImpl::Ptr *commandQueueOut)
{
CLCommandQueueVk *queueImpl = new CLCommandQueueVk(commandQueue);
if (queueImpl == nullptr)
{
ANGLE_CL_RETURN_ERROR(CL_OUT_OF_HOST_MEMORY);
}
ANGLE_TRY(queueImpl->init());
*commandQueueOut = CLCommandQueueVk::Ptr(std::move(queueImpl));
return angle::Result::Continue;
}
angle::Result CLContextVk::createBuffer(const cl::Buffer &buffer,
void *hostPtr,
CLMemoryImpl::Ptr *bufferOut)
{
CLBufferVk *memory = new (std::nothrow) CLBufferVk(buffer);
if (memory == nullptr)
{
ANGLE_CL_RETURN_ERROR(CL_OUT_OF_HOST_MEMORY);
}
ANGLE_TRY(memory->create(hostPtr));
*bufferOut = CLMemoryImpl::Ptr(memory);
mAssociatedObjects->mMemories.emplace(buffer.getNative());
return angle::Result::Continue;
}
angle::Result CLContextVk::createImage(const cl::Image &image,
cl::MemFlags flags,
const cl_image_format &format,
const cl::ImageDescriptor &desc,
void *hostPtr,
CLMemoryImpl::Ptr *imageOut)
{
UNIMPLEMENTED();
ANGLE_CL_RETURN_ERROR(CL_OUT_OF_RESOURCES);
}
angle::Result CLContextVk::getSupportedImageFormats(cl::MemFlags flags,
cl::MemObjectType imageType,
cl_uint numEntries,
cl_image_format *imageFormats,
cl_uint *numImageFormats)
{
UNIMPLEMENTED();
ANGLE_CL_RETURN_ERROR(CL_OUT_OF_RESOURCES);
}
angle::Result CLContextVk::createSampler(const cl::Sampler &sampler, CLSamplerImpl::Ptr *samplerOut)
{
UNIMPLEMENTED();
ANGLE_CL_RETURN_ERROR(CL_OUT_OF_RESOURCES);
}
angle::Result CLContextVk::createProgramWithSource(const cl::Program &program,
const std::string &source,
CLProgramImpl::Ptr *programOut)
{
CLProgramVk *programVk = new (std::nothrow) CLProgramVk(program);
if (programVk == nullptr)
{
ANGLE_CL_RETURN_ERROR(CL_OUT_OF_HOST_MEMORY);
}
ANGLE_TRY(programVk->init());
*programOut = CLProgramImpl::Ptr(std::move(programVk));
return angle::Result::Continue;
}
angle::Result CLContextVk::createProgramWithIL(const cl::Program &program,
const void *il,
size_t length,
CLProgramImpl::Ptr *programOut)
{
UNIMPLEMENTED();
ANGLE_CL_RETURN_ERROR(CL_OUT_OF_RESOURCES);
}
angle::Result CLContextVk::createProgramWithBinary(const cl::Program &program,
const size_t *lengths,
const unsigned char **binaries,
cl_int *binaryStatus,
CLProgramImpl::Ptr *programOut)
{
CLProgramVk *programVk = new (std::nothrow) CLProgramVk(program);
if (programVk == nullptr)
{
ANGLE_CL_RETURN_ERROR(CL_OUT_OF_HOST_MEMORY);
}
ANGLE_TRY(programVk->init(lengths, binaries, binaryStatus));
*programOut = CLProgramImpl::Ptr(std::move(programVk));
return angle::Result::Continue;
}
angle::Result CLContextVk::createProgramWithBuiltInKernels(const cl::Program &program,
const char *kernel_names,
CLProgramImpl::Ptr *programOut)
{
UNIMPLEMENTED();
ANGLE_CL_RETURN_ERROR(CL_OUT_OF_RESOURCES);
}
angle::Result CLContextVk::linkProgram(const cl::Program &program,
const cl::DevicePtrs &devices,
const char *options,
const cl::ProgramPtrs &inputPrograms,
cl::Program *notify,
CLProgramImpl::Ptr *programOut)
{
const cl::DevicePtrs &devicePtrs = !devices.empty() ? devices : mContext.getDevices();
CLProgramVk::Ptr programImpl = CLProgramVk::Ptr(new (std::nothrow) CLProgramVk(program));
if (programImpl == nullptr)
{
ANGLE_CL_RETURN_ERROR(CL_OUT_OF_HOST_MEMORY);
}
cl::DevicePtrs linkDeviceList;
CLProgramVk::LinkProgramsList linkProgramsList;
cl::BitField libraryOrObject(CL_PROGRAM_BINARY_TYPE_LIBRARY |
CL_PROGRAM_BINARY_TYPE_COMPILED_OBJECT);
for (const cl::DevicePtr &devicePtr : devicePtrs)
{
CLProgramVk::LinkPrograms linkPrograms;
for (const cl::ProgramPtr &inputProgram : inputPrograms)
{
const CLProgramVk::DeviceProgramData *deviceProgramData =
inputProgram->getImpl<CLProgramVk>().getDeviceProgramData(devicePtr->getNative());
// Should be valid at this point
ASSERT(deviceProgramData != nullptr);
if (libraryOrObject.isSet(deviceProgramData->binaryType))
{
linkPrograms.push_back(deviceProgramData);
}
}
if (!linkPrograms.empty())
{
linkDeviceList.push_back(devicePtr);
linkProgramsList.push_back(linkPrograms);
}
}
// Perform link
if (notify)
{
std::shared_ptr<angle::WaitableEvent> asyncEvent =
mContext.getPlatform().getMultiThreadPool()->postWorkerTask(
std::make_shared<CLAsyncBuildTask>(
programImpl.get(), linkDeviceList, std::string(options ? options : ""), "",
CLProgramVk::BuildType::LINK, linkProgramsList, notify));
ASSERT(asyncEvent != nullptr);
}
else
{
if (!programImpl->buildInternal(linkDeviceList, std::string(options ? options : ""), "",
CLProgramVk::BuildType::LINK, linkProgramsList))
{
ANGLE_CL_RETURN_ERROR(CL_LINK_PROGRAM_FAILURE);
}
}
*programOut = std::move(programImpl);
return angle::Result::Continue;
}
angle::Result CLContextVk::createUserEvent(const cl::Event &event, CLEventImpl::Ptr *eventOut)
{
*eventOut = CLEventImpl::Ptr(new (std::nothrow) CLEventVk(event));
if (*eventOut == nullptr)
{
ANGLE_CL_RETURN_ERROR(CL_OUT_OF_HOST_MEMORY);
}
return angle::Result::Continue;
}
angle::Result CLContextVk::waitForEvents(const cl::EventPtrs &events)
{
for (auto &event : events)
{
CLEventVk *eventVk = &event.get()->getImpl<CLEventVk>();
if (eventVk->isUserEvent())
{
ANGLE_TRY(eventVk->waitForUserEventStatus());
}
else
{
// TODO rework this to instead (flush w/ ResourceUse serial wait) once we move away from
// spawning a submit-thread/Task for flush routine
// https://anglebug.com/8669
ANGLE_TRY(event->getCommandQueue()->finish());
}
}
return angle::Result::Continue;
}
} // namespace rx