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