| 1 |
/* |
| 2 |
* Copyright (C) 2020-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/windows/gl_sharing_windows.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 gmmHelper = context->getDevice(0)->getRootDeviceEnvironment().getGmmHelper(); |
| 37 |
auto memoryManager = context->getMemoryManager(); |
| 38 |
cl_image_desc imgDesc = {}; |
| 39 |
cl_image_format imgFormat = {}; |
| 40 |
McsSurfaceInfo mcsSurfaceInfo = {}; |
| 41 |
|
| 42 |
CL_GL_RESOURCE_INFO texInfo = {}; |
| 43 |
texInfo.name = texture; |
| 44 |
texInfo.target = getBaseTargetType(target); |
| 45 |
|
| 46 |
GLSharingFunctionsWindows *sharingFunctions = context->getSharing<GLSharingFunctionsWindows>(); |
| 47 |
|
| 48 |
if (target == GL_RENDERBUFFER_EXT) { |
| 49 |
sharingFunctions->acquireSharedRenderBuffer(&texInfo); |
| 50 |
} else { |
| 51 |
sharingFunctions->acquireSharedTexture(&texInfo); |
| 52 |
} |
| 53 |
|
| 54 |
errorCode.set(CL_SUCCESS); |
| 55 |
|
| 56 |
AllocationProperties allocProperties(context->getDevice(0)->getRootDeviceIndex(), |
| 57 |
false, // allocateMemory |
| 58 |
0u, // size |
| 59 |
AllocationType::sharedImage, |
| 60 |
false, // isMultiStorageAllocation |
| 61 |
context->getDeviceBitfieldForAllocation(context->getDevice(0)->getRootDeviceIndex())); |
| 62 |
MemoryManager::OsHandleData osHandleData{texInfo.globalShareHandle}; |
| 63 |
auto alloc = memoryManager->createGraphicsAllocationFromSharedHandle(osHandleData, allocProperties, false, false, true, nullptr); |
| 64 |
|
| 65 |
if (alloc == nullptr) { |
| 66 |
errorCode.set(CL_INVALID_GL_OBJECT); |
| 67 |
return nullptr; |
| 68 |
} |
| 69 |
if (texInfo.pGmmResInfo) { |
| 70 |
DEBUG_BREAK_IF(alloc->getDefaultGmm() != nullptr); |
| 71 |
alloc->setDefaultGmm(new Gmm(gmmHelper, texInfo.pGmmResInfo)); |
| 72 |
} |
| 73 |
|
| 74 |
auto gmm = alloc->getDefaultGmm(); |
| 75 |
|
| 76 |
imgDesc.image_type = getClMemObjectType(target); |
| 77 |
if (target == GL_TEXTURE_BUFFER) { |
| 78 |
imgDesc.image_width = texInfo.textureBufferWidth; |
| 79 |
imgDesc.image_row_pitch = texInfo.textureBufferSize; |
| 80 |
} else { |
| 81 |
imgDesc.image_width = gmm->gmmResourceInfo->getBaseWidth(); |
| 82 |
imgDesc.image_row_pitch = gmm->gmmResourceInfo->getRenderPitch(); |
| 83 |
if (imgDesc.image_row_pitch == 0) { |
| 84 |
size_t alignedWidth = alignUp(imgDesc.image_width, gmm->gmmResourceInfo->getHAlign()); |
| 85 |
size_t bpp = gmm->gmmResourceInfo->getBitsPerPixel() >> 3; |
| 86 |
imgDesc.image_row_pitch = alignedWidth * bpp; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
uint32_t numSamples = static_cast<uint32_t>(gmm->gmmResourceInfo->getNumSamples()); |
| 91 |
imgDesc.num_samples = getValidParam(numSamples, 0u, 1u); |
| 92 |
imgDesc.image_height = gmm->gmmResourceInfo->getBaseHeight(); |
| 93 |
imgDesc.image_array_size = gmm->gmmResourceInfo->getArraySize(); |
| 94 |
if (target == GL_TEXTURE_3D) { |
| 95 |
imgDesc.image_depth = gmm->gmmResourceInfo->getBaseDepth(); |
| 96 |
} |
| 97 |
|
| 98 |
if (imgDesc.image_array_size > 1 || imgDesc.image_depth > 1) { |
| 99 |
GMM_REQ_OFFSET_INFO gmmReqInfo = {}; |
| 100 |
gmmReqInfo.ArrayIndex = imgDesc.image_array_size > 1 ? 1 : 0; |
| 101 |
gmmReqInfo.Slice = imgDesc.image_depth > 1 ? 1 : 0; |
| 102 |
gmmReqInfo.ReqLock = 1; |
| 103 |
gmm->gmmResourceInfo->getOffset(gmmReqInfo); |
| 104 |
imgDesc.image_slice_pitch = gmmReqInfo.Lock.Offset; |
| 105 |
} else { |
| 106 |
imgDesc.image_slice_pitch = alloc->getUnderlyingBufferSize(); |
| 107 |
} |
| 108 |
|
| 109 |
uint32_t cubeFaceIndex = GmmTypesConverter::getCubeFaceIndex(target); |
| 110 |
|
| 111 |
auto qPitch = gmm->queryQPitch(gmm->gmmResourceInfo->getResourceType()); |
| 112 |
|
| 113 |
if (setClImageFormat(texInfo.glInternalFormat, imgFormat) == false) { |
| 114 |
memoryManager->freeGraphicsMemory(alloc); |
| 115 |
errorCode.set(CL_INVALID_GL_OBJECT); |
| 116 |
return nullptr; |
| 117 |
} |
| 118 |
auto surfaceFormatInfoAddress = Image::getSurfaceFormatFromTable(flags, &imgFormat, context->getDevice(0)->getHardwareInfo().capabilityTable.supportsOcl21Features); |
| 119 |
if (!surfaceFormatInfoAddress) { |
| 120 |
memoryManager->freeGraphicsMemory(alloc); |
| 121 |
errorCode.set(CL_INVALID_GL_OBJECT); |
| 122 |
return nullptr; |
| 123 |
} |
| 124 |
auto surfaceFormatInfo = *surfaceFormatInfoAddress; |
| 125 |
if (texInfo.glInternalFormat != GL_RGB10) { |
| 126 |
surfaceFormatInfo.surfaceFormat.genxSurfaceFormat = (SurfaceFormat)texInfo.glHWFormat; |
| 127 |
} |
| 128 |
|
| 129 |
GraphicsAllocation *mcsAlloc = nullptr; |
| 130 |
if (texInfo.globalShareHandleMCS) { |
| 131 |
AllocationProperties allocProperties(context->getDevice(0)->getRootDeviceIndex(), 0, AllocationType::mcs, context->getDeviceBitfieldForAllocation(context->getDevice(0)->getRootDeviceIndex())); |
| 132 |
MemoryManager::OsHandleData osHandleData{texInfo.globalShareHandleMCS}; |
| 133 |
mcsAlloc = memoryManager->createGraphicsAllocationFromSharedHandle(osHandleData, allocProperties, false, false, true, nullptr); |
| 134 |
if (texInfo.pGmmResInfoMCS) { |
| 135 |
DEBUG_BREAK_IF(mcsAlloc->getDefaultGmm() != nullptr); |
| 136 |
mcsAlloc->setDefaultGmm(new Gmm(gmmHelper, texInfo.pGmmResInfoMCS)); |
| 137 |
} |
| 138 |
mcsSurfaceInfo.pitch = getValidParam(static_cast<uint32_t>(mcsAlloc->getDefaultGmm()->gmmResourceInfo->getRenderPitch() / 128)); |
| 139 |
mcsSurfaceInfo.qPitch = mcsAlloc->getDefaultGmm()->gmmResourceInfo->getQPitch(); |
| 140 |
} |
| 141 |
mcsSurfaceInfo.multisampleCount = GmmTypesConverter::getRenderMultisamplesCount(static_cast<uint32_t>(imgDesc.num_samples)); |
| 142 |
|
| 143 |
if (miplevel < 0) { |
| 144 |
imgDesc.num_mip_levels = gmm->gmmResourceInfo->getMaxLod() + 1; |
| 145 |
} |
| 146 |
|
| 147 |
ImageInfo imgInfo = {}; |
| 148 |
imgInfo.imgDesc = Image::convertDescriptor(imgDesc); |
| 149 |
imgInfo.surfaceFormat = &surfaceFormatInfo.surfaceFormat; |
| 150 |
imgInfo.qPitch = qPitch; |
| 151 |
imgInfo.isDisplayable = gmm->gmmResourceInfo->isDisplayable(); |
| 152 |
|
| 153 |
auto glTexture = new GlTexture(sharingFunctions, getClGlObjectType(target), texture, texInfo, target, std::max(miplevel, 0)); |
| 154 |
|
| 155 |
if (texInfo.isAuxEnabled && alloc->getDefaultGmm()->unifiedAuxTranslationCapable()) { |
| 156 |
const auto &hwInfo = context->getDevice(0)->getHardwareInfo(); |
| 157 |
const auto &productHelper = context->getDevice(0)->getProductHelper(); |
| 158 |
alloc->getDefaultGmm()->setCompressionEnabled(productHelper.isPageTableManagerSupported(hwInfo) ? memoryManager->mapAuxGpuVA(alloc) |
| 159 |
: true); |
| 160 |
} |
| 161 |
auto multiGraphicsAllocation = MultiGraphicsAllocation(context->getDevice(0)->getRootDeviceIndex()); |
| 162 |
multiGraphicsAllocation.addAllocation(alloc); |
| 163 |
|
| 164 |
return Image::createSharedImage(context, glTexture, mcsSurfaceInfo, std::move(multiGraphicsAllocation), mcsAlloc, flags, 0, &surfaceFormatInfo, imgInfo, cubeFaceIndex, |
| 165 |
std::max(miplevel, 0), imgInfo.imgDesc.numMipLevels); |
| 166 |
} // namespace NEO |
| 167 |
|
| 168 |
void GlTexture::synchronizeObject(UpdateData &updateData) { |
| 169 |
auto sharingFunctions = static_cast<GLSharingFunctionsWindows *>(this->sharingFunctions); |
| 170 |
CL_GL_RESOURCE_INFO resourceInfo = {0}; |
| 171 |
resourceInfo.name = this->clGlObjectId; |
| 172 |
if (target == GL_RENDERBUFFER_EXT) { |
| 173 |
sharingFunctions->acquireSharedRenderBuffer(&resourceInfo); |
| 174 |
} else { |
| 175 |
sharingFunctions->acquireSharedTexture(&resourceInfo); |
| 176 |
// Set texture buffer offset acquired from OpenGL layer in graphics allocation |
| 177 |
updateData.memObject->getGraphicsAllocation(updateData.rootDeviceIndex)->setAllocationOffset(resourceInfo.textureBufferOffset); |
| 178 |
} |
| 179 |
updateData.sharedHandle = resourceInfo.globalShareHandle; |
| 180 |
updateData.synchronizationStatus = SynchronizeStatus::ACQUIRE_SUCCESFUL; |
| 181 |
} |
| 182 |
|
| 183 |
cl_int GlTexture::getGlTextureInfo(cl_gl_texture_info paramName, size_t paramValueSize, void *paramValue, size_t *paramValueSizeRet) const { |
| 184 |
GetInfoHelper info(paramValue, paramValueSize, paramValueSizeRet); |
| 185 |
|
| 186 |
if (paramName == CL_GL_TEXTURE_TARGET) { |
| 187 |
info.set<GLenum>(target); |
| 188 |
} else if (paramName == CL_GL_MIPMAP_LEVEL) { |
| 189 |
info.set<GLenum>(miplevel); |
| 190 |
} else if (paramName == CL_GL_NUM_SAMPLES) { |
| 191 |
info.set<GLsizei>(textureInfo.numberOfSamples > 1 ? textureInfo.numberOfSamples : 0); |
| 192 |
} else { |
| 193 |
return CL_INVALID_VALUE; |
| 194 |
} |
| 195 |
return CL_SUCCESS; |
| 196 |
} |
| 197 |
|
| 198 |
cl_mem_object_type GlTexture::getClMemObjectType(cl_GLenum glType) { |
| 199 |
return static_cast<cl_mem_object_type>(getClObjectType(glType, false)); |
| 200 |
} |
| 201 |
cl_gl_object_type GlTexture::getClGlObjectType(cl_GLenum glType) { |
| 202 |
return static_cast<cl_gl_object_type>(getClObjectType(glType, true)); |
| 203 |
} |
| 204 |
|
| 205 |
uint32_t GlTexture::getClObjectType(cl_GLenum glType, bool returnClGlObjectType) { |
| 206 |
// return cl_gl_object_type if returnClGlObjectType is ture, otherwise cl_mem_object_type |
| 207 |
uint32_t retValue = 0; |
| 208 |
switch (glType) { |
| 209 |
case GL_TEXTURE_1D: |
| 210 |
retValue = returnClGlObjectType ? CL_GL_OBJECT_TEXTURE1D : CL_MEM_OBJECT_IMAGE1D; |
| 211 |
break; |
| 212 |
case GL_TEXTURE_1D_ARRAY: |
| 213 |
retValue = returnClGlObjectType ? CL_GL_OBJECT_TEXTURE1D_ARRAY : CL_MEM_OBJECT_IMAGE1D_ARRAY; |
| 214 |
break; |
| 215 |
case GL_TEXTURE_2D: |
| 216 |
case GL_TEXTURE_RECTANGLE: |
| 217 |
case GL_TEXTURE_CUBE_MAP_NEGATIVE_X: |
| 218 |
case GL_TEXTURE_CUBE_MAP_POSITIVE_X: |
| 219 |
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: |
| 220 |
case GL_TEXTURE_CUBE_MAP_POSITIVE_Y: |
| 221 |
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: |
| 222 |
case GL_TEXTURE_CUBE_MAP_POSITIVE_Z: |
| 223 |
case GL_TEXTURE_2D_MULTISAMPLE: |
| 224 |
retValue = returnClGlObjectType ? CL_GL_OBJECT_TEXTURE2D : CL_MEM_OBJECT_IMAGE2D; |
| 225 |
break; |
| 226 |
case GL_TEXTURE_2D_ARRAY: |
| 227 |
case GL_TEXTURE_2D_MULTISAMPLE_ARRAY: |
| 228 |
retValue = returnClGlObjectType ? CL_GL_OBJECT_TEXTURE2D_ARRAY : CL_MEM_OBJECT_IMAGE2D_ARRAY; |
| 229 |
break; |
| 230 |
case GL_TEXTURE_3D: |
| 231 |
retValue = returnClGlObjectType ? CL_GL_OBJECT_TEXTURE3D : CL_MEM_OBJECT_IMAGE3D; |
| 232 |
break; |
| 233 |
case GL_TEXTURE_BUFFER: |
| 234 |
retValue = returnClGlObjectType ? CL_GL_OBJECT_TEXTURE_BUFFER : CL_MEM_OBJECT_IMAGE1D_BUFFER; |
| 235 |
break; |
| 236 |
case GL_RENDERBUFFER_EXT: |
| 237 |
retValue = returnClGlObjectType ? CL_GL_OBJECT_RENDERBUFFER : CL_MEM_OBJECT_IMAGE2D; |
| 238 |
break; |
| 239 |
default: |
| 240 |
retValue = 0; |
| 241 |
break; |
| 242 |
} |
| 243 |
return retValue; |
| 244 |
} |
| 245 |
|
| 246 |
cl_GLenum GlTexture::getBaseTargetType(cl_GLenum target) { |
| 247 |
cl_GLenum returnTarget = 0; |
| 248 |
switch (target) { |
| 249 |
case GL_TEXTURE_CUBE_MAP_NEGATIVE_X: |
| 250 |
case GL_TEXTURE_CUBE_MAP_POSITIVE_X: |
| 251 |
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: |
| 252 |
case GL_TEXTURE_CUBE_MAP_POSITIVE_Y: |
| 253 |
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: |
| 254 |
case GL_TEXTURE_CUBE_MAP_POSITIVE_Z: |
| 255 |
returnTarget = GL_TEXTURE_CUBE_MAP_ARB; |
| 256 |
break; |
| 257 |
default: |
| 258 |
returnTarget = target; |
| 259 |
break; |
| 260 |
} |
| 261 |
return returnTarget; |
| 262 |
} |
| 263 |
|
| 264 |
void GlTexture::releaseResource(MemObj *memObject, uint32_t rootDeviceIndex) { |
| 265 |
auto sharingFunctions = static_cast<GLSharingFunctionsWindows *>(this->sharingFunctions); |
| 266 |
if (target == GL_RENDERBUFFER_EXT) { |
| 267 |
sharingFunctions->releaseSharedRenderBuffer(&textureInfo); |
| 268 |
} else { |
| 269 |
sharingFunctions->releaseSharedTexture(&textureInfo); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
void GlTexture::resolveGraphicsAllocationChange(osHandle currentSharedHandle, UpdateData *updateData) { |
| 274 |
GlSharing::resolveGraphicsAllocationChange(currentSharedHandle, updateData); |
| 275 |
} |
| 276 |
|
| 277 |
} // namespace NEO |