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
|
/*
* Copyright (C) 2020-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "level_zero/core/source/image/image_imp.h"
#include "shared/source/device/device.h"
#include "shared/source/execution_environment/execution_environment.h"
#include "shared/source/execution_environment/root_device_environment.h"
#include "shared/source/gmm_helper/gmm.h"
#include "shared/source/helpers/basic_math.h"
#include "shared/source/helpers/bindless_heaps_helper.h"
#include "shared/source/helpers/gfx_core_helper.h"
#include "shared/source/helpers/hw_info.h"
#include "shared/source/memory_manager/memory_manager.h"
#include "level_zero/core/source/device/device.h"
#include "level_zero/core/source/device/device_imp.h"
#include "level_zero/core/source/driver/driver_handle_imp.h"
#include "level_zero/core/source/image/image_formats.h"
#include "neo_igfxfmid.h"
namespace L0 {
ImageAllocatorFn imageFactory[IGFX_MAX_PRODUCT] = {};
bool isImportedWin32Handle(const ze_image_desc_t *imgDesc) {
const ze_base_desc_t *extendedDesc = reinterpret_cast<const ze_base_desc_t *>(imgDesc->pNext);
bool importedWin32Handle = false;
while (extendedDesc) {
if (extendedDesc->stype == ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMPORT_WIN32) {
importedWin32Handle = true;
}
extendedDesc = reinterpret_cast<const ze_base_desc_t *>(extendedDesc->pNext);
}
return importedWin32Handle;
}
void getImageDescriptorFor3ChEmulation(const ze_image_desc_t *origImgDesc, ze_image_desc_t *imgDesc) {
*imgDesc = *origImgDesc;
if (origImgDesc->format.layout == ZE_IMAGE_FORMAT_LAYOUT_16_16_16) {
imgDesc->format.layout = ZE_IMAGE_FORMAT_LAYOUT_16_16_16_16;
imgDesc->format.x = ZE_IMAGE_FORMAT_SWIZZLE_R;
imgDesc->format.y = ZE_IMAGE_FORMAT_SWIZZLE_G;
imgDesc->format.z = ZE_IMAGE_FORMAT_SWIZZLE_B;
imgDesc->format.w = ZE_IMAGE_FORMAT_SWIZZLE_1;
}
if (origImgDesc->format.layout == ZE_IMAGE_FORMAT_LAYOUT_8_8_8) {
imgDesc->format.layout = ZE_IMAGE_FORMAT_LAYOUT_8_8_8_8;
imgDesc->format.x = ZE_IMAGE_FORMAT_SWIZZLE_R;
imgDesc->format.y = ZE_IMAGE_FORMAT_SWIZZLE_G;
imgDesc->format.z = ZE_IMAGE_FORMAT_SWIZZLE_B;
imgDesc->format.w = ZE_IMAGE_FORMAT_SWIZZLE_1;
}
return;
}
ImageImp::~ImageImp() {
if ((isImageView() || imageFromBuffer) && this->device != nullptr) {
auto rootIndex = this->device->getNEODevice()->getRootDeviceIndex();
if (bindlessInfo.get() && this->device->getNEODevice()->getExecutionEnvironment()->rootDeviceEnvironments[rootIndex]->getBindlessHeapsHelper() != nullptr) {
this->device->getNEODevice()->getExecutionEnvironment()->rootDeviceEnvironments[rootIndex]->getBindlessHeapsHelper()->releaseSSToReusePool(*bindlessInfo);
}
}
if (this->device != nullptr) {
if (!isImageView() && !imageFromBuffer) {
this->device->getNEODevice()->getMemoryManager()->freeGraphicsMemory(this->allocation);
}
if (implicitArgsAllocation) {
this->device->getNEODevice()->getMemoryManager()->freeGraphicsMemory(this->implicitArgsAllocation);
}
}
}
ze_result_t ImageImp::destroy() {
if (this->getAllocation() && this->device) {
auto imageAllocPtr = reinterpret_cast<const void *>(this->getAllocation()->getGpuAddress());
DriverHandleImp *driverHandle = static_cast<DriverHandleImp *>(this->device->getDriverHandle());
for (auto peerDevice : driverHandle->devices) {
this->destroyPeerImages(imageAllocPtr, peerDevice);
}
}
delete this;
return ZE_RESULT_SUCCESS;
}
ze_result_t ImageImp::destroyPeerImages(const void *ptr, Device *device) {
DeviceImp *deviceImp = static_cast<DeviceImp *>(device);
std::unique_lock<NEO::SpinLock> lock(deviceImp->peerImageAllocationsMutex);
if (deviceImp->peerImageAllocations.find(ptr) != deviceImp->peerImageAllocations.end()) {
delete deviceImp->peerImageAllocations[ptr];
deviceImp->peerImageAllocations.erase(ptr);
}
return ZE_RESULT_SUCCESS;
}
ze_result_t ImageImp::createView(Device *device, const ze_image_desc_t *desc, ze_image_handle_t *pImage) {
auto productFamily = device->getNEODevice()->getHardwareInfo().platform.eProductFamily;
ImageAllocatorFn allocator = nullptr;
allocator = imageFactory[productFamily];
ImageImp *image = nullptr;
image = static_cast<ImageImp *>((*allocator)());
image->allocation = allocation;
image->sourceImageFormatDesc = this->imageFormatDesc;
image->imgInfo = this->imgInfo;
image->imageFromBuffer = this->imageFromBuffer;
auto result = ZE_RESULT_SUCCESS;
switch (desc->format.layout) {
default:
result = image->initialize(device, desc);
break;
case ZE_IMAGE_FORMAT_LAYOUT_32_32_32:
result = ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
break;
case ZE_IMAGE_FORMAT_LAYOUT_8_8_8:
case ZE_IMAGE_FORMAT_LAYOUT_16_16_16:
if (isImportedWin32Handle(desc)) {
result = ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
} else {
ze_image_desc_t imgDesc = {};
getImageDescriptorFor3ChEmulation(desc, &imgDesc);
image->setMimickedImage(true);
result = image->initialize(device, &imgDesc);
}
break;
}
if (result != ZE_RESULT_SUCCESS) {
image->destroy();
image = nullptr;
}
*pImage = image;
return result;
}
ze_result_t ImageImp::allocateBindlessSlot() {
if (!isImageView() && !imageFromBuffer) {
if (!this->device->getNEODevice()->getMemoryManager()->allocateBindlessSlot(allocation)) {
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
if (allocation->getBindlessOffset() != std::numeric_limits<uint64_t>::max()) {
bindlessInfo = std::make_unique<NEO::SurfaceStateInHeapInfo>(allocation->getBindlessInfo());
}
return ZE_RESULT_SUCCESS;
}
auto bindlessHelper = this->device->getNEODevice()->getExecutionEnvironment()->rootDeviceEnvironments[allocation->getRootDeviceIndex()]->getBindlessHeapsHelper();
if (bindlessHelper && !bindlessInfo) {
auto &gfxCoreHelper = this->device->getNEODevice()->getExecutionEnvironment()->rootDeviceEnvironments[allocation->getRootDeviceIndex()]->getHelper<NEO::GfxCoreHelper>();
const auto surfStateCount = NEO::BindlessImageSlot::max;
auto surfaceStateSize = surfStateCount * gfxCoreHelper.getRenderSurfaceStateSize();
auto surfaceStateInfo = bindlessHelper->allocateSSInHeap(surfaceStateSize, allocation, NEO::BindlessHeapsHelper::globalSsh);
if (surfaceStateInfo.heapAllocation == nullptr) {
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
bindlessInfo = std::make_unique<NEO::SurfaceStateInHeapInfo>(surfaceStateInfo);
}
return ZE_RESULT_SUCCESS;
}
NEO::SurfaceStateInHeapInfo *ImageImp::getBindlessSlot() {
return bindlessInfo.get();
}
ze_result_t Image::create(uint32_t productFamily, Device *device, const ze_image_desc_t *desc, Image **pImage) {
ze_result_t result = ZE_RESULT_SUCCESS;
ImageAllocatorFn allocator = nullptr;
if (productFamily < IGFX_MAX_PRODUCT) {
allocator = imageFactory[productFamily];
}
ImageImp *image = nullptr;
if (allocator) {
image = static_cast<ImageImp *>((*allocator)());
switch (desc->format.layout) {
default:
result = image->initialize(device, desc);
break;
case ZE_IMAGE_FORMAT_LAYOUT_8_8_8:
case ZE_IMAGE_FORMAT_LAYOUT_16_16_16:
if (isImportedWin32Handle(desc)) {
result = ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
} else {
ze_image_desc_t imgDesc = {};
getImageDescriptorFor3ChEmulation(desc, &imgDesc);
image->setMimickedImage(true);
result = image->initialize(device, &imgDesc);
}
break;
case ZE_IMAGE_FORMAT_LAYOUT_32_32_32:
result = ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
break;
}
if (result != ZE_RESULT_SUCCESS) {
image->destroy();
image = nullptr;
}
} else {
result = ZE_RESULT_ERROR_UNKNOWN;
}
*pImage = image;
return result;
}
ze_result_t Image::getPitchFor2dImage(
ze_device_handle_t hDevice,
size_t imageWidth,
size_t imageHeight,
unsigned int elementSizeInByte,
size_t *rowPitch) {
NEO::ImageInfo imgInfo = {};
imgInfo.imgDesc.imageType = NEO::ImageType::image2D;
imgInfo.imgDesc.imageWidth = imageWidth;
imgInfo.imgDesc.imageHeight = imageHeight;
imgInfo.linearStorage = true;
[[maybe_unused]] uint32_t exponent;
switch (elementSizeInByte) {
default:
exponent = Math::log2(elementSizeInByte);
if (exponent >= 5u) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
imgInfo.surfaceFormat = &ImageFormats::surfaceFormatsForRedescribe[exponent % 5];
break;
case 3:
imgInfo.surfaceFormat = &ImageFormats::surfaceFormatsForRedescribe[5];
break;
case 6:
imgInfo.surfaceFormat = &ImageFormats::surfaceFormatsForRedescribe[6];
break;
}
Device *device = Device::fromHandle(hDevice);
*rowPitch = ImageImp::getRowPitchFor2dImage(device, imgInfo);
return ZE_RESULT_SUCCESS;
}
ze_result_t ImageImp::getDeviceOffset(uint64_t *deviceOffset) {
if (!this->bindlessImage) {
return ZE_RESULT_ERROR_NOT_AVAILABLE;
}
DEBUG_BREAK_IF(this->getBindlessSlot() == nullptr);
*deviceOffset = this->getBindlessSlot()->surfaceStateOffset;
return ZE_RESULT_SUCCESS;
}
size_t ImageImp::getRowPitchFor2dImage(Device *device, const NEO::ImageInfo &imgInfo) {
NEO::StorageInfo storageInfo = {};
NEO::ImageInfo info = imgInfo;
DeviceImp *deviceImp = static_cast<DeviceImp *>(device);
NEO::Gmm gmm(deviceImp->getNEODevice()->getExecutionEnvironment()->rootDeviceEnvironments[deviceImp->getRootDeviceIndex()]->getGmmHelper(),
info,
storageInfo,
false);
return info.rowPitch;
}
} // namespace L0
|