| 1 | /* |
| 2 | * Copyright (C) 2018-2025 Intel Corporation |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "shared/source/helpers/aligned_memory.h" |
| 9 | #include "shared/source/helpers/get_info.h" |
| 10 | #include "shared/source/memory_manager/os_agnostic_memory_manager.h" |
| 11 | #include "shared/test/common/helpers/default_hw_info.h" |
| 12 | #include "shared/test/common/mocks/mock_execution_environment.h" |
| 13 | #include "shared/test/common/mocks/mock_gmm.h" |
| 14 | #include "shared/test/common/mocks/mock_gmm_resource_info.h" |
| 15 | #include "shared/test/common/test_macros/test.h" |
| 16 | |
| 17 | #include "opencl/source/cl_device/cl_device.h" |
| 18 | #include "opencl/source/helpers/gmm_types_converter.h" |
| 19 | #include "opencl/source/mem_obj/image.h" |
| 20 | #include "opencl/source/sharings/gl/gl_texture.h" |
| 21 | #include "opencl/test/unit_test/mocks/gl/windows/mock_gl_sharing_windows.h" |
| 22 | #include "opencl/test/unit_test/mocks/mock_context.h" |
| 23 | |
| 24 | #include "gtest/gtest.h" |
| 25 | |
| 26 | #include <memory> |
| 27 | |
| 28 | namespace NEO { |
| 29 | class CreateFromGlTexture : public ::testing::Test { |
| 30 | public: |
| 31 | // temp solution - we need to query size from GMM: |
| 32 | class TempMM : public OsAgnosticMemoryManager { |
| 33 | public: |
| 34 | TempMM() : OsAgnosticMemoryManager(*(new MockExecutionEnvironment(defaultHwInfo.get()))) { |
| 35 | mockExecutionEnvironment.reset(&executionEnvironment); |
| 36 | } |
| 37 | GraphicsAllocation *createGraphicsAllocationFromSharedHandle(const OsHandleData &osHandleData, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation, bool reuseSharedAllocation, void *mapPointer) override { |
| 38 | auto alloc = OsAgnosticMemoryManager::createGraphicsAllocationFromSharedHandle(osHandleData, properties, requireSpecificBitness, isHostIpcAllocation, reuseSharedAllocation, mapPointer); |
| 39 | if (osHandleData.handle == CreateFromGlTexture::mcsHandle) { |
| 40 | alloc->setDefaultGmm(forceMcsGmm); |
| 41 | } else { |
| 42 | alloc->setDefaultGmm(forceGmm); |
| 43 | } |
| 44 | return alloc; |
| 45 | } |
| 46 | size_t forceAllocationSize; |
| 47 | Gmm *forceGmm = nullptr; |
| 48 | Gmm *forceMcsGmm = nullptr; |
| 49 | std::unique_ptr<ExecutionEnvironment> mockExecutionEnvironment; |
| 50 | }; |
| 51 | |
| 52 | void SetUp() override { |
| 53 | imgDesc = {}; |
| 54 | imgInfo = {}; |
| 55 | clContext.setSharingFunctions(glSharing->sharingFunctions.release()); |
| 56 | clContext.memoryManager = &tempMM; |
| 57 | } |
| 58 | |
| 59 | void TearDown() override { |
| 60 | gmm.release(); |
| 61 | mcsGmm.release(); |
| 62 | } |
| 63 | |
| 64 | void updateImgInfoAndForceGmm() { |
| 65 | imgInfo = MockGmm::initImgInfo(imgDesc, 0, nullptr); |
| 66 | gmm = MockGmm::queryImgParams(clContext.getDevice(0)->getGmmHelper(), imgInfo, false); |
| 67 | |
| 68 | tempMM.forceAllocationSize = imgInfo.size; |
| 69 | tempMM.forceGmm = gmm.get(); |
| 70 | |
| 71 | if (glSharing->textureInfoOutput.globalShareHandleMCS != 0) { |
| 72 | ImageDescriptor mcsImgDesc = {}; |
| 73 | mcsImgDesc.imageHeight = 128; |
| 74 | mcsImgDesc.imageRowPitch = 256; |
| 75 | mcsImgDesc.imageWidth = 128; |
| 76 | mcsImgDesc.imageType = ImageType::image2D; |
| 77 | auto mcsImgInfo = MockGmm::initImgInfo(mcsImgDesc, 0, nullptr); |
| 78 | mcsGmm = MockGmm::queryImgParams(clContext.getDevice(0)->getGmmHelper(), mcsImgInfo, false); |
| 79 | tempMM.forceMcsGmm = mcsGmm.get(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | ImageDescriptor imgDesc; |
| 84 | ImageInfo imgInfo = {}; |
| 85 | std::unique_ptr<Gmm> gmm; |
| 86 | std::unique_ptr<Gmm> mcsGmm; |
| 87 | TempMM tempMM; |
| 88 | MockContext clContext; |
| 89 | std::unique_ptr<MockGlSharing> glSharing = std::make_unique<MockGlSharing>(); |
| 90 | cl_int retVal; |
| 91 | static const unsigned int mcsHandle = 0xFF; |
| 92 | }; |
| 93 | |
| 94 | class CreateFromGlTextureTestsWithParams : public CreateFromGlTexture, |
| 95 | public ::testing::WithParamInterface<unsigned int /*cl_GLenum*/> { |
| 96 | }; |
| 97 | |
| 98 | class CreateFromGlTextureTests : public CreateFromGlTexture { |
| 99 | }; |
| 100 | |
| 101 | INSTANTIATE_TEST_SUITE_P( |
| 102 | CreateFromGlTextureTestsWithParams, |
| 103 | CreateFromGlTextureTestsWithParams, |
| 104 | testing::ValuesIn(glTextureTargets::supportedTargets)); |
| 105 | |
| 106 | TEST_P(CreateFromGlTextureTestsWithParams, givenAllTextureSpecificParamsWhenCreateIsCalledThenFillImageDescription) { |
| 107 | unsigned int target = GetParam(); |
| 108 | unsigned int baseTarget = GlTexture::getBaseTargetType(target); |
| 109 | imgDesc.imageType = Image::convertType(GlTexture::getClMemObjectType(target)); |
| 110 | imgDesc.imageWidth = 5; |
| 111 | if (target == GL_TEXTURE_1D_ARRAY || target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) { |
| 112 | imgDesc.imageArraySize = 5; |
| 113 | } |
| 114 | if (target == GL_TEXTURE_2D || target == GL_TEXTURE_RECTANGLE || |
| 115 | target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D || |
| 116 | target == GL_RENDERBUFFER_EXT || baseTarget == GL_TEXTURE_CUBE_MAP_ARB || |
| 117 | target == GL_TEXTURE_2D_MULTISAMPLE || target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) { |
| 118 | imgDesc.imageHeight = 5; |
| 119 | } |
| 120 | |
| 121 | if (target == GL_TEXTURE_3D) { |
| 122 | imgDesc.imageDepth = 5; |
| 123 | } |
| 124 | |
| 125 | if (target == GL_TEXTURE_BUFFER) { |
| 126 | // size and width for texture buffer are queried from textureInfo - not from gmm |
| 127 | glSharing->textureInfoOutput.textureBufferWidth = 64; |
| 128 | glSharing->textureInfoOutput.textureBufferSize = 1024; |
| 129 | glSharing->uploadDataToTextureInfo(); |
| 130 | } |
| 131 | |
| 132 | if (target == GL_TEXTURE_2D_MULTISAMPLE || target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) { |
| 133 | imgDesc.numSamples = 16; |
| 134 | glSharing->textureInfoOutput.numberOfSamples = 16; |
| 135 | glSharing->textureInfoOutput.globalShareHandleMCS = CreateFromGlTexture::mcsHandle; |
| 136 | glSharing->uploadDataToTextureInfo(); |
| 137 | } |
| 138 | |
| 139 | updateImgInfoAndForceGmm(); |
| 140 | |
| 141 | auto glImage = GlTexture::createSharedGlTexture(&clContext, (cl_mem_flags)0, target, 0, 0, &retVal); |
| 142 | |
| 143 | ASSERT_EQ(CL_SUCCESS, retVal); |
| 144 | if (target == GL_RENDERBUFFER_EXT) { |
| 145 | EXPECT_EQ(1, glSharing->dllParam->getParam("glAcquireSharedRenderBufferCalled")); |
| 146 | } else { |
| 147 | EXPECT_EQ(1, glSharing->dllParam->getParam("glAcquireSharedTextureCalled")); |
| 148 | } |
| 149 | |
| 150 | EXPECT_EQ(GmmTypesConverter::getCubeFaceIndex(target), glImage->getCubeFaceIndex()); |
| 151 | |
| 152 | auto glTexture = reinterpret_cast<GlTexture *>(glImage->peekSharingHandler()); |
| 153 | EXPECT_EQ(glTexture->getTarget(), target); |
| 154 | |
| 155 | EXPECT_EQ(glImage->getImageDesc().image_type, Image::convertType(imgDesc.imageType)); |
| 156 | if (target == GL_TEXTURE_BUFFER) { |
| 157 | EXPECT_EQ(glImage->getImageDesc().image_width, |
| 158 | static_cast<size_t>(glTexture->getTextureInfo()->textureBufferWidth)); |
| 159 | EXPECT_EQ(glImage->getImageDesc().image_row_pitch, |
| 160 | static_cast<size_t>(glTexture->getTextureInfo()->textureBufferSize)); |
| 161 | } else { |
| 162 | EXPECT_EQ(glImage->getImageDesc().image_width, gmm->gmmResourceInfo->getBaseWidth()); |
| 163 | size_t slicePitch = glImage->getHostPtrSlicePitch(); |
| 164 | size_t rowPitch = glImage->getHostPtrRowPitch(); |
| 165 | EXPECT_EQ(glImage->getImageDesc().image_row_pitch, rowPitch); |
| 166 | EXPECT_EQ(glImage->getImageDesc().image_slice_pitch, slicePitch); |
| 167 | |
| 168 | size_t gmmRowPitch = gmm->gmmResourceInfo->getRenderPitch(); |
| 169 | if (gmmRowPitch == 0) { |
| 170 | size_t alignedWidth = alignUp(glImage->getImageDesc().image_width, gmm->gmmResourceInfo->getHAlign()); |
| 171 | size_t bpp = gmm->gmmResourceInfo->getBitsPerPixel() >> 3; |
| 172 | EXPECT_EQ(glImage->getImageDesc().image_row_pitch, alignedWidth * bpp); |
| 173 | } else { |
| 174 | EXPECT_EQ(glImage->getImageDesc().image_row_pitch, gmmRowPitch); |
| 175 | } |
| 176 | |
| 177 | size_t imageInfoRowPitch = 0; |
| 178 | retVal = clGetImageInfo(glImage, CL_IMAGE_ROW_PITCH, sizeof(size_t), &imageInfoRowPitch, NULL); |
| 179 | ASSERT_EQ(CL_SUCCESS, retVal); |
| 180 | ASSERT_EQ(rowPitch, imageInfoRowPitch); |
| 181 | |
| 182 | size_t imageInfoSlicePitch = 0; |
| 183 | slicePitch *= !(glImage->getImageDesc().image_type == CL_MEM_OBJECT_IMAGE2D || glImage->getImageDesc().image_type == CL_MEM_OBJECT_IMAGE1D || glImage->getImageDesc().image_type == CL_MEM_OBJECT_IMAGE1D_BUFFER); |
| 184 | |
| 185 | retVal = clGetImageInfo(glImage, CL_IMAGE_SLICE_PITCH, sizeof(size_t), &imageInfoSlicePitch, NULL); |
| 186 | ASSERT_EQ(CL_SUCCESS, retVal); |
| 187 | ASSERT_EQ(slicePitch, imageInfoSlicePitch); |
| 188 | } |
| 189 | |
| 190 | EXPECT_EQ(glImage->getImageDesc().image_height, gmm->gmmResourceInfo->getBaseHeight()); |
| 191 | EXPECT_EQ(glImage->getImageDesc().image_array_size, gmm->gmmResourceInfo->getArraySize()); |
| 192 | if (target == GL_TEXTURE_3D) { |
| 193 | EXPECT_EQ(glImage->getImageDesc().image_depth, gmm->gmmResourceInfo->getBaseDepth()); |
| 194 | } else { |
| 195 | EXPECT_EQ(glImage->getImageDesc().image_depth, 0u); |
| 196 | } |
| 197 | |
| 198 | if (imgDesc.imageArraySize > 1 || imgDesc.imageDepth > 1) { |
| 199 | GMM_REQ_OFFSET_INFO gmmReqInfo = {}; |
| 200 | gmmReqInfo.ArrayIndex = imgDesc.imageArraySize > 1 ? 1 : 0; |
| 201 | gmmReqInfo.Slice = imgDesc.imageDepth > 1 ? 1 : 0; |
| 202 | gmmReqInfo.ReqLock = 1; |
| 203 | gmm->gmmResourceInfo->getOffset(gmmReqInfo); |
| 204 | size_t expectedSlicePitch = gmmReqInfo.Lock.Offset; |
| 205 | EXPECT_EQ(glImage->getImageDesc().image_slice_pitch, expectedSlicePitch); |
| 206 | } else { |
| 207 | EXPECT_EQ(glImage->getImageDesc().image_slice_pitch, imgInfo.size); |
| 208 | } |
| 209 | |
| 210 | EXPECT_EQ(glImage->getQPitch(), gmm->queryQPitch()); |
| 211 | |
| 212 | // gmm returns 1 by default - OCL requires 0 |
| 213 | uint32_t numSamples = static_cast<uint32_t>(gmm->gmmResourceInfo->getNumSamples()); |
| 214 | auto expectedNumSamples = getValidParam(numSamples, 0u, 1u); |
| 215 | EXPECT_EQ(expectedNumSamples, glImage->getImageDesc().num_samples); |
| 216 | |
| 217 | if (target == GL_TEXTURE_2D_MULTISAMPLE || target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) { |
| 218 | EXPECT_NE(nullptr, glImage->getMcsAllocation()); |
| 219 | EXPECT_EQ(getValidParam(static_cast<uint32_t>(mcsGmm->gmmResourceInfo->getRenderPitch() / 128)), |
| 220 | glImage->getMcsSurfaceInfo().pitch); |
| 221 | EXPECT_EQ(static_cast<uint32_t>(mcsGmm->gmmResourceInfo->getQPitch()), |
| 222 | glImage->getMcsSurfaceInfo().qPitch); |
| 223 | EXPECT_EQ(GmmTypesConverter::getRenderMultisamplesCount(static_cast<uint32_t>(gmm->gmmResourceInfo->getNumSamples())), |
| 224 | glImage->getMcsSurfaceInfo().multisampleCount); |
| 225 | } |
| 226 | |
| 227 | delete glImage; |
| 228 | } |
| 229 | |
| 230 | TEST_P(CreateFromGlTextureTestsWithParams, givenArrayTextureTargetAndArraySizeEqualOneWhenCreateIsCalledThenSlicePitchAndSizeAreEqual) { |
| 231 | unsigned int target = GetParam(); |
| 232 | // only array targets |
| 233 | if (target == GL_TEXTURE_1D_ARRAY || |
| 234 | target == GL_TEXTURE_2D_ARRAY) { |
| 235 | imgDesc.imageType = Image::convertType(GlTexture::getClMemObjectType(target)); |
| 236 | imgDesc.imageWidth = 5; |
| 237 | if (target == GL_TEXTURE_2D_ARRAY) { |
| 238 | imgDesc.imageHeight = 5; |
| 239 | } |
| 240 | imgDesc.imageArraySize = 1; |
| 241 | |
| 242 | updateImgInfoAndForceGmm(); |
| 243 | |
| 244 | auto glImage = GlTexture::createSharedGlTexture(&clContext, (cl_mem_flags)0, target, 0, 0, &retVal); |
| 245 | |
| 246 | EXPECT_EQ(CL_SUCCESS, retVal); |
| 247 | EXPECT_EQ(glImage->getImageDesc().image_slice_pitch, imgInfo.size); |
| 248 | |
| 249 | delete glImage; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | TEST_P(CreateFromGlTextureTestsWithParams, givenZeroRowPitchFromGmmWhenCreatingTextureThenComputeIt) { |
| 254 | unsigned int target = GL_TEXTURE_2D; |
| 255 | |
| 256 | imgDesc.imageType = Image::convertType(GlTexture::getClMemObjectType(target)); |
| 257 | imgDesc.imageWidth = 5; |
| 258 | imgDesc.imageHeight = 5; |
| 259 | imgDesc.imageArraySize = 1; |
| 260 | |
| 261 | updateImgInfoAndForceGmm(); |
| 262 | |
| 263 | auto mockResInfo = static_cast<MockGmmResourceInfo *>(gmm->gmmResourceInfo.get()); |
| 264 | mockResInfo->overrideReturnedRenderPitch(0u); |
| 265 | |
| 266 | auto alignedWidth = alignUp(imgDesc.imageWidth, gmm->gmmResourceInfo->getHAlign()); |
| 267 | auto expectedRowPitch = alignedWidth * (gmm->gmmResourceInfo->getBitsPerPixel() >> 3); |
| 268 | |
| 269 | auto glImage = std::unique_ptr<Image>(GlTexture::createSharedGlTexture(&clContext, (cl_mem_flags)0, target, 0, 0, &retVal)); |
| 270 | |
| 271 | EXPECT_EQ(CL_SUCCESS, retVal); |
| 272 | EXPECT_EQ(imgInfo.size, glImage->getImageDesc().image_slice_pitch); |
| 273 | EXPECT_EQ(expectedRowPitch, glImage->getImageDesc().image_row_pitch); |
| 274 | } |
| 275 | |
| 276 | TEST_F(CreateFromGlTextureTests, GivenGlTextureTargetAndMipLevelNegativeWhenCreateIsCalledThenMipMappedImageIsCreated) { |
| 277 | unsigned int target = GL_TEXTURE_3D; |
| 278 | cl_GLint miplevel = -1; |
| 279 | |
| 280 | imgDesc.imageType = Image::convertType(GlTexture::getClMemObjectType(target)); |
| 281 | imgDesc.imageHeight = 13; |
| 282 | imgDesc.imageWidth = 15; |
| 283 | imgDesc.imageDepth = 7; |
| 284 | |
| 285 | updateImgInfoAndForceGmm(); |
| 286 | |
| 287 | auto glImage = std::unique_ptr<Image>(GlTexture::createSharedGlTexture(&clContext, 0u, target, miplevel, 0, &retVal)); |
| 288 | EXPECT_EQ(CL_SUCCESS, retVal); |
| 289 | |
| 290 | size_t actualHeight = 0; |
| 291 | size_t actualWidth = 0; |
| 292 | size_t actualDepth = 0; |
| 293 | |
| 294 | glImage->getImageInfo(CL_IMAGE_HEIGHT, sizeof(size_t), &actualHeight, nullptr); |
| 295 | glImage->getImageInfo(CL_IMAGE_WIDTH, sizeof(size_t), &actualWidth, nullptr); |
| 296 | glImage->getImageInfo(CL_IMAGE_DEPTH, sizeof(size_t), &actualDepth, nullptr); |
| 297 | |
| 298 | EXPECT_EQ(13u, actualHeight); |
| 299 | EXPECT_EQ(15u, actualWidth); |
| 300 | EXPECT_EQ(7u, actualDepth); |
| 301 | EXPECT_EQ(gmm->gmmResourceInfo->getMaxLod() + 1, glImage->getImageDesc().num_mip_levels); |
| 302 | EXPECT_EQ(glImage->peekBaseMipLevel(), 0); |
| 303 | } |
| 304 | |
| 305 | TEST_F(CreateFromGlTextureTests, GivenGlTextureTargetAndMipLevelNonNegativeWhenCreateIsCalledThenImageFromChosenMipLevelIsCreated) { |
| 306 | unsigned int target = GL_TEXTURE_3D; |
| 307 | cl_GLint miplevel = 2; |
| 308 | |
| 309 | imgDesc.imageType = Image::convertType(GlTexture::getClMemObjectType(target)); |
| 310 | imgDesc.imageHeight = 13; |
| 311 | imgDesc.imageWidth = 15; |
| 312 | imgDesc.imageDepth = 7; |
| 313 | |
| 314 | updateImgInfoAndForceGmm(); |
| 315 | |
| 316 | auto glImage = std::unique_ptr<Image>(GlTexture::createSharedGlTexture(&clContext, 0u, target, miplevel, 0, &retVal)); |
| 317 | EXPECT_EQ(CL_SUCCESS, retVal); |
| 318 | |
| 319 | size_t actualHeight = 0; |
| 320 | size_t actualWidth = 0; |
| 321 | size_t actualDepth = 0; |
| 322 | |
| 323 | glImage->getImageInfo(CL_IMAGE_HEIGHT, sizeof(size_t), &actualHeight, nullptr); |
| 324 | glImage->getImageInfo(CL_IMAGE_WIDTH, sizeof(size_t), &actualWidth, nullptr); |
| 325 | glImage->getImageInfo(CL_IMAGE_DEPTH, sizeof(size_t), &actualDepth, nullptr); |
| 326 | |
| 327 | EXPECT_EQ(3u, actualHeight); |
| 328 | EXPECT_EQ(3u, actualWidth); |
| 329 | EXPECT_EQ(1u, actualDepth); |
| 330 | EXPECT_GE(1u, glImage->getImageDesc().num_mip_levels); |
| 331 | EXPECT_EQ(glImage->peekBaseMipLevel(), 2); |
| 332 | } |
| 333 | |
| 334 | TEST_F(CreateFromGlTextureTests, GivenGlTextureWhenCreateIsCalledThenAllocationTypeIsSharedImage) { |
| 335 | unsigned int target = GL_TEXTURE_3D; |
| 336 | cl_GLint miplevel = 2; |
| 337 | |
| 338 | imgDesc.imageType = Image::convertType(GlTexture::getClMemObjectType(target)); |
| 339 | imgDesc.imageHeight = 13; |
| 340 | imgDesc.imageWidth = 15; |
| 341 | imgDesc.imageDepth = 7; |
| 342 | |
| 343 | updateImgInfoAndForceGmm(); |
| 344 | |
| 345 | auto glImage = std::unique_ptr<Image>(GlTexture::createSharedGlTexture(&clContext, 0u, target, miplevel, 0, &retVal)); |
| 346 | EXPECT_EQ(CL_SUCCESS, retVal); |
| 347 | |
| 348 | auto graphicsAllocation = glImage->getGraphicsAllocation(clContext.getDevice(0)->getRootDeviceIndex()); |
| 349 | ASSERT_NE(nullptr, graphicsAllocation); |
| 350 | EXPECT_EQ(AllocationType::sharedImage, graphicsAllocation->getAllocationType()); |
| 351 | } |
| 352 | } // namespace NEO |