| 1 |
/* |
| 2 |
* Copyright (C) 2023-2024 Intel Corporation |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "shared/source/execution_environment/root_device_environment.h" |
| 9 |
#include "shared/source/gmm_helper/gmm.h" |
| 10 |
#include "shared/source/gmm_helper/gmm_helper.h" |
| 11 |
#include "shared/source/gmm_helper/resource_info.h" |
| 12 |
#include "shared/source/helpers/aligned_memory.h" |
| 13 |
#include "shared/source/helpers/get_info.h" |
| 14 |
#include "shared/source/helpers/gfx_core_helper.h" |
| 15 |
#include "shared/source/helpers/hw_info.h" |
| 16 |
#include "shared/source/memory_manager/allocation_properties.h" |
| 17 |
#include "shared/source/memory_manager/memory_manager.h" |
| 18 |
#include "shared/source/os_interface/product_helper.h" |
| 19 |
|
| 20 |
#include "opencl/extensions/public/cl_gl_private_intel.h" |
| 21 |
#include "opencl/source/cl_device/cl_device.h" |
| 22 |
#include "opencl/source/context/context.h" |
| 23 |
#include "opencl/source/helpers/gmm_types_converter.h" |
| 24 |
#include "opencl/source/mem_obj/image.h" |
| 25 |
#include "opencl/source/sharings/gl/gl_texture.h" |
| 26 |
#include "opencl/source/sharings/gl/linux/gl_sharing_linux.h" |
| 27 |
|
| 28 |
#include "CL/cl_gl.h" |
| 29 |
#include "config.h" |
| 30 |
#include <GL/gl.h> |
| 31 |
|
| 32 |
namespace NEO { |
| 33 |
Image *GlTexture::createSharedGlTexture(Context *context, cl_mem_flags flags, cl_GLenum target, cl_GLint miplevel, cl_GLuint texture, |
| 34 |
cl_int *errcodeRet) { |
| 35 |
ErrorCodeHelper errorCode(errcodeRet, CL_INVALID_GL_OBJECT); |
| 36 |
auto memoryManager = context->getMemoryManager(); |
| 37 |
cl_image_desc imgDesc = {}; |
| 38 |
ImageInfo imgInfo = {}; |
| 39 |
cl_image_format imgFormat = {}; |
| 40 |
McsSurfaceInfo mcsSurfaceInfo = {}; |
| 41 |
|
| 42 |
/* Prepare export request */ |
| 43 |
struct mesa_glinterop_export_in texIn = {}; |
| 44 |
struct mesa_glinterop_export_out texOut = {}; |
| 45 |
|
| 46 |
texIn.version = 2; |
| 47 |
texIn.target = getBaseTargetType(target); |
| 48 |
texIn.obj = texture; |
| 49 |
texIn.miplevel = miplevel; |
| 50 |
|
| 51 |
switch (flags) { |
| 52 |
case CL_MEM_READ_ONLY: |
| 53 |
texIn.access = MESA_GLINTEROP_ACCESS_READ_ONLY; |
| 54 |
break; |
| 55 |
case CL_MEM_WRITE_ONLY: |
| 56 |
texIn.access = MESA_GLINTEROP_ACCESS_WRITE_ONLY; |
| 57 |
break; |
| 58 |
case CL_MEM_READ_WRITE: |
| 59 |
texIn.access = MESA_GLINTEROP_ACCESS_READ_WRITE; |
| 60 |
break; |
| 61 |
default: |
| 62 |
errorCode.set(CL_INVALID_VALUE); |
| 63 |
return nullptr; |
| 64 |
} |
| 65 |
|
| 66 |
if (texIn.target != GL_TEXTURE_2D) { |
| 67 |
printf("target %x not supported\n", target); |
| 68 |
errorCode.set(CL_INVALID_GL_OBJECT); |
| 69 |
return nullptr; |
| 70 |
} |
| 71 |
|
| 72 |
texOut.version = 2; |
| 73 |
|
| 74 |
/* Call MESA interop */ |
| 75 |
GLSharingFunctionsLinux *sharingFunctions = context->getSharing<GLSharingFunctionsLinux>(); |
| 76 |
|
| 77 |
int retValue = sharingFunctions->exportObject(&texIn, &texOut); |
| 78 |
if ((retValue != MESA_GLINTEROP_SUCCESS) || (texOut.version != 2)) { |
| 79 |
switch (retValue) { |
| 80 |
case MESA_GLINTEROP_INVALID_DISPLAY: |
| 81 |
case MESA_GLINTEROP_INVALID_CONTEXT: |
| 82 |
errorCode.set(CL_INVALID_CONTEXT); |
| 83 |
break; |
| 84 |
case MESA_GLINTEROP_INVALID_TARGET: |
| 85 |
errorCode.set(CL_INVALID_VALUE); |
| 86 |
break; |
| 87 |
case MESA_GLINTEROP_INVALID_MIP_LEVEL: |
| 88 |
errorCode.set(CL_INVALID_MIP_LEVEL); |
| 89 |
break; |
| 90 |
case MESA_GLINTEROP_INVALID_OBJECT: |
| 91 |
errorCode.set(CL_INVALID_GL_OBJECT); |
| 92 |
break; |
| 93 |
case MESA_GLINTEROP_UNSUPPORTED: |
| 94 |
errorCode.set(CL_INVALID_IMAGE_FORMAT_DESCRIPTOR); |
| 95 |
break; |
| 96 |
case MESA_GLINTEROP_INVALID_OPERATION: |
| 97 |
errorCode.set(CL_INVALID_OPERATION); |
| 98 |
break; |
| 99 |
case MESA_GLINTEROP_OUT_OF_HOST_MEMORY: |
| 100 |
errorCode.set(CL_OUT_OF_HOST_MEMORY); |
| 101 |
break; |
| 102 |
case MESA_GLINTEROP_OUT_OF_RESOURCES: |
| 103 |
default: |
| 104 |
errorCode.set(CL_OUT_OF_RESOURCES); |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/* Map result for rest of the function */ |
| 110 |
CL_GL_RESOURCE_INFO texInfo = {}; |
| 111 |
texInfo.name = texIn.obj, |
| 112 |
texInfo.globalShareHandle = static_cast<unsigned int>(texOut.dmabuf_fd); |
| 113 |
texInfo.glInternalFormat = static_cast<GLint>(texOut.internal_format); |
| 114 |
texInfo.textureBufferSize = static_cast<GLint>(texOut.buf_size); |
| 115 |
texInfo.textureBufferOffset = static_cast<GLint>(texOut.buf_offset); |
| 116 |
|
| 117 |
imgDesc.image_width = texOut.width; |
| 118 |
imgDesc.image_height = texOut.height; |
| 119 |
imgDesc.image_depth = texOut.depth; |
| 120 |
imgDesc.image_row_pitch = texOut.stride; |
| 121 |
|
| 122 |
imgInfo.imgDesc.imageType = ImageType::image2D; |
| 123 |
imgInfo.imgDesc.imageWidth = imgDesc.image_width; |
| 124 |
imgInfo.imgDesc.imageHeight = imgDesc.image_height; |
| 125 |
imgInfo.imgDesc.imageDepth = imgDesc.image_depth; |
| 126 |
imgInfo.imgDesc.imageRowPitch = imgDesc.image_row_pitch; |
| 127 |
imgInfo.linearStorage = (texOut.modifier == 0); |
| 128 |
|
| 129 |
errorCode.set(CL_SUCCESS); |
| 130 |
|
| 131 |
if (setClImageFormat(texInfo.glInternalFormat, imgFormat) == false) { |
| 132 |
errorCode.set(CL_INVALID_GL_OBJECT); |
| 133 |
return nullptr; |
| 134 |
} |
| 135 |
|
| 136 |
auto surfaceFormatInfoAddress = Image::getSurfaceFormatFromTable(flags, &imgFormat, context->getDevice(0)->getHardwareInfo().capabilityTable.supportsOcl21Features); |
| 137 |
if (!surfaceFormatInfoAddress) { |
| 138 |
errorCode.set(CL_INVALID_GL_OBJECT); |
| 139 |
return nullptr; |
| 140 |
} |
| 141 |
auto surfaceFormatInfo = *surfaceFormatInfoAddress; |
| 142 |
imgInfo.surfaceFormat = &surfaceFormatInfo.surfaceFormat; |
| 143 |
AllocationProperties allocProperties(context->getDevice(0)->getRootDeviceIndex(), |
| 144 |
false, // allocateMemory |
| 145 |
&imgInfo, |
| 146 |
AllocationType::sharedImage, |
| 147 |
context->getDeviceBitfieldForAllocation(context->getDevice(0)->getRootDeviceIndex())); |
| 148 |
MemoryManager::OsHandleData osHandleData{texInfo.globalShareHandle}; |
| 149 |
auto alloc = memoryManager->createGraphicsAllocationFromSharedHandle(osHandleData, allocProperties, false, false, false, nullptr); |
| 150 |
|
| 151 |
if (alloc == nullptr) { |
| 152 |
errorCode.set(CL_INVALID_GL_OBJECT); |
| 153 |
return nullptr; |
| 154 |
} |
| 155 |
memoryManager->closeSharedHandle(alloc); |
| 156 |
|
| 157 |
auto gmm = alloc->getDefaultGmm(); |
| 158 |
|
| 159 |
imgDesc.image_type = getClMemObjectType(target); |
| 160 |
if (target == GL_TEXTURE_BUFFER) { |
| 161 |
imgDesc.image_row_pitch = texInfo.textureBufferSize; |
| 162 |
} else if (imgDesc.image_row_pitch == 0) { |
| 163 |
imgDesc.image_row_pitch = gmm->gmmResourceInfo->getRenderPitch(); |
| 164 |
if (imgDesc.image_row_pitch == 0) { |
| 165 |
size_t alignedWidth = alignUp(imgDesc.image_width, gmm->gmmResourceInfo->getHAlign()); |
| 166 |
size_t bpp = gmm->gmmResourceInfo->getBitsPerPixel() >> 3; |
| 167 |
imgDesc.image_row_pitch = alignedWidth * bpp; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
uint32_t numSamples = static_cast<uint32_t>(gmm->gmmResourceInfo->getNumSamples()); |
| 172 |
imgDesc.num_samples = getValidParam(numSamples, 0u, 1u); |
| 173 |
imgDesc.image_height = gmm->gmmResourceInfo->getBaseHeight(); |
| 174 |
imgDesc.image_array_size = gmm->gmmResourceInfo->getArraySize(); |
| 175 |
if (target == GL_TEXTURE_3D) { |
| 176 |
imgDesc.image_depth = gmm->gmmResourceInfo->getBaseDepth(); |
| 177 |
} |
| 178 |
|
| 179 |
if (imgDesc.image_array_size > 1 || imgDesc.image_depth > 1) { |
| 180 |
GMM_REQ_OFFSET_INFO gmmReqInfo = {}; |
| 181 |
gmmReqInfo.ArrayIndex = imgDesc.image_array_size > 1 ? 1 : 0; |
| 182 |
gmmReqInfo.Slice = imgDesc.image_depth > 1 ? 1 : 0; |
| 183 |
gmmReqInfo.ReqLock = 1; |
| 184 |
gmm->gmmResourceInfo->getOffset(gmmReqInfo); |
| 185 |
imgDesc.image_slice_pitch = gmmReqInfo.Lock.Offset; |
| 186 |
} else { |
| 187 |
imgDesc.image_slice_pitch = alloc->getUnderlyingBufferSize(); |
| 188 |
} |
| 189 |
|
| 190 |
uint32_t cubeFaceIndex = GmmTypesConverter::getCubeFaceIndex(target); |
| 191 |
|
| 192 |
uint32_t qPitch = gmm->queryQPitch(gmm->gmmResourceInfo->getResourceType()); |
| 193 |
|
| 194 |
GraphicsAllocation *mcsAlloc = nullptr; |
| 195 |
|
| 196 |
imgDesc.image_type = CL_MEM_OBJECT_IMAGE2D; |
| 197 |
imgInfo.imgDesc = Image::convertDescriptor(imgDesc); |
| 198 |
|
| 199 |
imgInfo.surfaceFormat = &surfaceFormatInfo.surfaceFormat; |
| 200 |
imgInfo.qPitch = qPitch; |
| 201 |
imgInfo.isDisplayable = gmm->gmmResourceInfo->isDisplayable(); |
| 202 |
|
| 203 |
auto glTexture = new GlTexture(sharingFunctions, getClGlObjectType(target), texture, texInfo, target, std::max(miplevel, 0)); |
| 204 |
|
| 205 |
if (texInfo.isAuxEnabled && alloc->getDefaultGmm()->unifiedAuxTranslationCapable()) { |
| 206 |
const auto &hwInfo = context->getDevice(0)->getHardwareInfo(); |
| 207 |
const auto &productHelper = context->getDevice(0)->getRootDeviceEnvironment().getHelper<ProductHelper>(); |
| 208 |
alloc->getDefaultGmm()->setCompressionEnabled(productHelper.isPageTableManagerSupported(hwInfo) ? memoryManager->mapAuxGpuVA(alloc) |
| 209 |
: true); |
| 210 |
} |
| 211 |
auto multiGraphicsAllocation = MultiGraphicsAllocation(context->getDevice(0)->getRootDeviceIndex()); |
| 212 |
multiGraphicsAllocation.addAllocation(alloc); |
| 213 |
|
| 214 |
return Image::createSharedImage(context, glTexture, mcsSurfaceInfo, std::move(multiGraphicsAllocation), mcsAlloc, flags, 0, &surfaceFormatInfo, imgInfo, cubeFaceIndex, |
| 215 |
std::max(miplevel, 0), imgInfo.imgDesc.numMipLevels); |
| 216 |
} |
| 217 |
|
| 218 |
void GlTexture::synchronizeObject(UpdateData &updateData) { |
| 219 |
auto sharingFunctions = static_cast<GLSharingFunctionsLinux *>(this->sharingFunctions); |
| 220 |
|
| 221 |
/* Prepare flush request */ |
| 222 |
struct mesa_glinterop_export_in texIn = {}; |
| 223 |
struct mesa_glinterop_flush_out syncOut = {}; |
| 224 |
int fenceFd = -1; |
| 225 |
|
| 226 |
texIn.version = 2; |
| 227 |
texIn.target = this->target; |
| 228 |
texIn.obj = this->clGlObjectId; |
| 229 |
texIn.miplevel = this->miplevel; |
| 230 |
|
| 231 |
syncOut.version = 1; |
| 232 |
syncOut.fence_fd = &fenceFd; |
| 233 |
|
| 234 |
bool success = sharingFunctions->flushObjectsAndWait(1, &texIn, &syncOut); |
| 235 |
updateData.synchronizationStatus = success ? SynchronizeStatus::ACQUIRE_SUCCESFUL : SynchronizeStatus::SYNCHRONIZE_ERROR; |
| 236 |
} |
| 237 |
|
| 238 |
cl_int GlTexture::getGlTextureInfo(cl_gl_texture_info paramName, size_t paramValueSize, void *paramValue, size_t *paramValueSizeRet) const { |
| 239 |
GetInfoHelper info(paramValue, paramValueSize, paramValueSizeRet); |
| 240 |
|
| 241 |
if (paramName == CL_GL_TEXTURE_TARGET) { |
| 242 |
info.set<GLenum>(target); |
| 243 |
} else if (paramName == CL_GL_MIPMAP_LEVEL) { |
| 244 |
info.set<GLenum>(miplevel); |
| 245 |
} else if (paramName == CL_GL_NUM_SAMPLES) { |
| 246 |
info.set<GLsizei>(textureInfo.numberOfSamples > 1 ? textureInfo.numberOfSamples : 0); |
| 247 |
} else { |
| 248 |
return CL_INVALID_VALUE; |
| 249 |
} |
| 250 |
return CL_SUCCESS; |
| 251 |
} |
| 252 |
|
| 253 |
cl_mem_object_type GlTexture::getClMemObjectType(cl_GLenum glType) { |
| 254 |
return static_cast<cl_mem_object_type>(getClObjectType(glType, false)); |
| 255 |
} |
| 256 |
cl_gl_object_type GlTexture::getClGlObjectType(cl_GLenum glType) { |
| 257 |
return static_cast<cl_gl_object_type>(getClObjectType(glType, true)); |
| 258 |
} |
| 259 |
|
| 260 |
uint32_t GlTexture::getClObjectType(cl_GLenum glType, bool returnClGlObjectType) { |
| 261 |
// return cl_gl_object_type if returnClGlObjectType is ture, otherwise cl_mem_object_type |
| 262 |
uint32_t retValue = 0; |
| 263 |
switch (glType) { |
| 264 |
case GL_TEXTURE_1D: |
| 265 |
retValue = returnClGlObjectType ? CL_GL_OBJECT_TEXTURE1D : CL_MEM_OBJECT_IMAGE1D; |
| 266 |
break; |
| 267 |
case GL_TEXTURE_1D_ARRAY: |
| 268 |
retValue = returnClGlObjectType ? CL_GL_OBJECT_TEXTURE1D_ARRAY : CL_MEM_OBJECT_IMAGE1D_ARRAY; |
| 269 |
break; |
| 270 |
case GL_TEXTURE_2D: |
| 271 |
case GL_TEXTURE_RECTANGLE: |
| 272 |
case GL_TEXTURE_CUBE_MAP_NEGATIVE_X: |
| 273 |
case GL_TEXTURE_CUBE_MAP_POSITIVE_X: |
| 274 |
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: |
| 275 |
case GL_TEXTURE_CUBE_MAP_POSITIVE_Y: |
| 276 |
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: |
| 277 |
case GL_TEXTURE_CUBE_MAP_POSITIVE_Z: |
| 278 |
case GL_TEXTURE_2D_MULTISAMPLE: |
| 279 |
retValue = returnClGlObjectType ? CL_GL_OBJECT_TEXTURE2D : CL_MEM_OBJECT_IMAGE2D; |
| 280 |
break; |
| 281 |
case GL_TEXTURE_2D_ARRAY: |
| 282 |
case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: |
| 283 |
retValue = returnClGlObjectType ? CL_GL_OBJECT_TEXTURE2D_ARRAY : CL_MEM_OBJECT_IMAGE2D_ARRAY; |
| 284 |
break; |
| 285 |
case GL_TEXTURE_3D: |
| 286 |
retValue = returnClGlObjectType ? CL_GL_OBJECT_TEXTURE3D : CL_MEM_OBJECT_IMAGE3D; |
| 287 |
break; |
| 288 |
case GL_TEXTURE_BUFFER: |
| 289 |
retValue = returnClGlObjectType ? CL_GL_OBJECT_TEXTURE_BUFFER : CL_MEM_OBJECT_IMAGE1D_BUFFER; |
| 290 |
break; |
| 291 |
case GL_RENDERBUFFER_EXT: |
| 292 |
retValue = returnClGlObjectType ? CL_GL_OBJECT_RENDERBUFFER : CL_MEM_OBJECT_IMAGE2D; |
| 293 |
break; |
| 294 |
default: |
| 295 |
retValue = 0; |
| 296 |
break; |
| 297 |
} |
| 298 |
return retValue; |
| 299 |
} |
| 300 |
|
| 301 |
cl_GLenum GlTexture::getBaseTargetType(cl_GLenum target) { |
| 302 |
cl_GLenum returnTarget = 0; |
| 303 |
switch (target) { |
| 304 |
case GL_TEXTURE_CUBE_MAP_NEGATIVE_X: |
| 305 |
case GL_TEXTURE_CUBE_MAP_POSITIVE_X: |
| 306 |
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: |
| 307 |
case GL_TEXTURE_CUBE_MAP_POSITIVE_Y: |
| 308 |
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: |
| 309 |
case GL_TEXTURE_CUBE_MAP_POSITIVE_Z: |
| 310 |
returnTarget = GL_TEXTURE_CUBE_MAP_ARB; |
| 311 |
break; |
| 312 |
default: |
| 313 |
returnTarget = target; |
| 314 |
break; |
| 315 |
} |
| 316 |
return returnTarget; |
| 317 |
} |
| 318 |
|
| 319 |
void GlTexture::releaseResource(MemObj *memObject, uint32_t rootDeviceIndex) { |
| 320 |
auto memoryManager = memObject->getMemoryManager(); |
| 321 |
memoryManager->closeSharedHandle(memObject->getGraphicsAllocation(rootDeviceIndex)); |
| 322 |
} |
| 323 |
void GlTexture::resolveGraphicsAllocationChange(osHandle currentSharedHandle, UpdateData *updateData) { |
| 324 |
const auto memObject = updateData->memObject; |
| 325 |
auto graphicsAllocation = memObject->getGraphicsAllocation(updateData->rootDeviceIndex); |
| 326 |
graphicsAllocation->setSharedHandle(updateData->sharedHandle); |
| 327 |
} |
| 328 |
|
| 329 |
} // namespace NEO |