diff --git a/runtime/mem_obj/image.cpp b/runtime/mem_obj/image.cpp index 938bd7be52..b0be968b58 100644 --- a/runtime/mem_obj/image.cpp +++ b/runtime/mem_obj/image.cpp @@ -698,45 +698,6 @@ const ClSurfaceFormatInfo &Image::getSurfaceFormatInfo() const { return surfaceFormatInfo; } -bool Image::isCopyRequired(ImageInfo &imgInfo, const void *hostPtr) { - if (!hostPtr) { - return false; - } - - size_t imageWidth = imgInfo.imgDesc.imageWidth; - size_t imageHeight = 1; - size_t imageDepth = 1; - size_t imageCount = 1; - - switch (imgInfo.imgDesc.imageType) { - case ImageType::Image3D: - imageDepth = imgInfo.imgDesc.imageDepth; - CPP_ATTRIBUTE_FALLTHROUGH; - case ImageType::Image2D: - case ImageType::Image2DArray: - imageHeight = imgInfo.imgDesc.imageHeight; - break; - default: - break; - } - - auto hostPtrRowPitch = imgInfo.imgDesc.imageRowPitch ? imgInfo.imgDesc.imageRowPitch : imageWidth * imgInfo.surfaceFormat->ImageElementSizeInBytes; - auto hostPtrSlicePitch = imgInfo.imgDesc.imageSlicePitch ? imgInfo.imgDesc.imageSlicePitch : hostPtrRowPitch * imgInfo.imgDesc.imageHeight; - - size_t pointerPassedSize = hostPtrRowPitch * imageHeight * imageDepth * imageCount; - auto alignedSizePassedPointer = alignSizeWholePage(const_cast(hostPtr), pointerPassedSize); - auto alignedSizeRequiredForAllocation = alignSizeWholePage(const_cast(hostPtr), imgInfo.size); - - // Passed pointer doesn't have enough memory, copy is needed - bool copyRequired = (alignedSizeRequiredForAllocation > alignedSizePassedPointer) | - (imgInfo.rowPitch != hostPtrRowPitch) | - (imgInfo.slicePitch != hostPtrSlicePitch) | - ((reinterpret_cast(hostPtr) & (MemoryConstants::cacheLineSize - 1)) != 0) | - !imgInfo.linearStorage; - - return copyRequired; -} - cl_mem_object_type Image::convertType(const ImageType type) { switch (type) { case ImageType::Image2D: diff --git a/runtime/mem_obj/image.h b/runtime/mem_obj/image.h index e5de21ae23..25558920b7 100644 --- a/runtime/mem_obj/image.h +++ b/runtime/mem_obj/image.h @@ -118,8 +118,6 @@ class Image : public MemObj { return (type == CL_MEM_OBJECT_IMAGE3D) || (type == CL_MEM_OBJECT_IMAGE1D_ARRAY) || (type == CL_MEM_OBJECT_IMAGE2D_ARRAY); } - static bool isCopyRequired(ImageInfo &imgInfo, const void *hostPtr); - static ImageType convertType(const cl_mem_object_type type); static cl_mem_object_type convertType(const ImageType type); static ImageDescriptor convertDescriptor(const cl_image_desc &imageDesc); diff --git a/runtime/memory_manager/memory_manager.cpp b/runtime/memory_manager/memory_manager.cpp index a4a1f03ef1..9f0a76591e 100644 --- a/runtime/memory_manager/memory_manager.cpp +++ b/runtime/memory_manager/memory_manager.cpp @@ -7,6 +7,7 @@ #include "runtime/memory_manager/memory_manager.h" +#include "common/compiler_support.h" #include "core/execution_environment/root_device_environment.h" #include "core/gmm_helper/gmm.h" #include "core/gmm_helper/gmm_helper.h" @@ -17,12 +18,13 @@ #include "core/helpers/hw_helper.h" #include "core/helpers/hw_info.h" #include "core/helpers/options.h" +#include "core/helpers/string.h" +#include "core/helpers/surface_format_info.h" #include "core/memory_manager/deferrable_allocation_deletion.h" #include "core/memory_manager/deferred_deleter.h" #include "core/memory_manager/host_ptr_manager.h" #include "core/utilities/stackvec.h" #include "runtime/command_stream/command_stream_receiver.h" -#include "runtime/mem_obj/image.h" #include "runtime/memory_manager/internal_allocation_storage.h" #include "runtime/os_interface/os_context.h" #include "runtime/os_interface/os_interface.h" @@ -103,7 +105,7 @@ GraphicsAllocation *MemoryManager::allocateGraphicsMemoryWithHostPtr(const Alloc } GraphicsAllocation *MemoryManager::allocateGraphicsMemoryForImageFromHostPtr(const AllocationData &allocationData) { - bool copyRequired = Image::isCopyRequired(*allocationData.imgInfo, allocationData.hostPtr); + bool copyRequired = isCopyRequired(*allocationData.imgInfo, allocationData.hostPtr); if (allocationData.hostPtr && !copyRequired) { return allocateGraphicsMemoryWithHostPtr(allocationData); @@ -497,4 +499,42 @@ bool MemoryManager::isHostPointerTrackingEnabled() { return (peekExecutionEnvironment().getHardwareInfo()->capabilityTable.hostPtrTrackingEnabled | is32bit); } +bool MemoryManager::isCopyRequired(ImageInfo &imgInfo, const void *hostPtr) { + if (!hostPtr) { + return false; + } + + size_t imageWidth = imgInfo.imgDesc.imageWidth; + size_t imageHeight = 1; + size_t imageDepth = 1; + size_t imageCount = 1; + + switch (imgInfo.imgDesc.imageType) { + case ImageType::Image3D: + imageDepth = imgInfo.imgDesc.imageDepth; + CPP_ATTRIBUTE_FALLTHROUGH; + case ImageType::Image2D: + case ImageType::Image2DArray: + imageHeight = imgInfo.imgDesc.imageHeight; + break; + default: + break; + } + + auto hostPtrRowPitch = imgInfo.imgDesc.imageRowPitch ? imgInfo.imgDesc.imageRowPitch : imageWidth * imgInfo.surfaceFormat->ImageElementSizeInBytes; + auto hostPtrSlicePitch = imgInfo.imgDesc.imageSlicePitch ? imgInfo.imgDesc.imageSlicePitch : hostPtrRowPitch * imgInfo.imgDesc.imageHeight; + + size_t pointerPassedSize = hostPtrRowPitch * imageHeight * imageDepth * imageCount; + auto alignedSizePassedPointer = alignSizeWholePage(const_cast(hostPtr), pointerPassedSize); + auto alignedSizeRequiredForAllocation = alignSizeWholePage(const_cast(hostPtr), imgInfo.size); + + // Passed pointer doesn't have enough memory, copy is needed + bool copyRequired = (alignedSizeRequiredForAllocation > alignedSizePassedPointer) | + (imgInfo.rowPitch != hostPtrRowPitch) | + (imgInfo.slicePitch != hostPtrSlicePitch) | + ((reinterpret_cast(hostPtr) & (MemoryConstants::cacheLineSize - 1)) != 0) | + !imgInfo.linearStorage; + + return copyRequired; +} } // namespace NEO diff --git a/runtime/memory_manager/memory_manager.h b/runtime/memory_manager/memory_manager.h index 2f8dd7f42e..c192dc1d61 100644 --- a/runtime/memory_manager/memory_manager.h +++ b/runtime/memory_manager/memory_manager.h @@ -194,6 +194,8 @@ class MemoryManager { return allocationType == GraphicsAllocation::AllocationType::KERNEL_ISA || allocationType == GraphicsAllocation::AllocationType::INTERNAL_HEAP; } + static bool isCopyRequired(ImageInfo &imgInfo, const void *hostPtr); + bool useNonSvmHostPtrAlloc(GraphicsAllocation::AllocationType allocationType) { return ((allocationType == GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR || allocationType == GraphicsAllocation::AllocationType::MAP_ALLOCATION) && (!peekExecutionEnvironment().isFullRangeSvm() || !isHostPointerTrackingEnabled()) & !is32bit); diff --git a/unit_tests/mem_obj/image_tests.cpp b/unit_tests/mem_obj/image_tests.cpp index 7c7044b7c8..c453c8454b 100644 --- a/unit_tests/mem_obj/image_tests.cpp +++ b/unit_tests/mem_obj/image_tests.cpp @@ -1144,11 +1144,6 @@ TEST(ImageTest, givenMipMapImage1DArrayWhenAskedForPtrOffsetForGpuMappingThenRet EXPECT_EQ(expectedOffset, retOffset); } -TEST(ImageTest, givenNullHostPtrWhenIsCopyRequiredIsCalledThenFalseIsReturned) { - ImageInfo imgInfo{}; - EXPECT_FALSE(Image::isCopyRequired(imgInfo, nullptr)); -} - TEST(ImageTest, givenClMemForceLinearStorageSetWhenCreateImageThenDisallowTiling) { cl_int retVal = CL_SUCCESS; MockContext context; @@ -1180,169 +1175,6 @@ TEST(ImageTest, givenClMemForceLinearStorageSetWhenCreateImageThenDisallowTiling EXPECT_EQ(CL_SUCCESS, retVal); } -TEST(ImageTest, givenAllowedTilingWhenIsCopyRequiredIsCalledThenTrueIsReturned) { - ImageInfo imgInfo{}; - cl_image_desc imageDesc{}; - imageDesc.image_type = CL_MEM_OBJECT_IMAGE2D; - imageDesc.image_width = 1; - imageDesc.image_height = 1; - - cl_image_format imageFormat = {}; - imageFormat.image_channel_data_type = CL_UNSIGNED_INT8; - imageFormat.image_channel_order = CL_R; - - cl_mem_flags flags = CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR; - auto surfaceFormat = Image::getSurfaceFormatFromTable(flags, &imageFormat); - - imgInfo.imgDesc = Image::convertDescriptor(imageDesc); - imgInfo.surfaceFormat = &surfaceFormat->surfaceFormat; - imgInfo.rowPitch = imageDesc.image_width * surfaceFormat->surfaceFormat.ImageElementSizeInBytes; - imgInfo.slicePitch = imgInfo.rowPitch * imageDesc.image_height; - imgInfo.size = imgInfo.slicePitch; - - char memory; - - EXPECT_TRUE(Image::isCopyRequired(imgInfo, &memory)); -} - -TEST(ImageTest, givenDifferentRowPitchWhenIsCopyRequiredIsCalledThenTrueIsReturned) { - ImageInfo imgInfo{}; - cl_image_desc imageDesc{}; - imageDesc.image_type = CL_MEM_OBJECT_IMAGE1D; - imageDesc.image_width = 1; - imageDesc.image_height = 1; - imageDesc.image_row_pitch = 10; - - cl_image_format imageFormat = {}; - imageFormat.image_channel_data_type = CL_UNSIGNED_INT8; - imageFormat.image_channel_order = CL_R; - - cl_mem_flags flags = CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR; - auto surfaceFormat = Image::getSurfaceFormatFromTable(flags, &imageFormat); - - imgInfo.imgDesc = Image::convertDescriptor(imageDesc); - imgInfo.surfaceFormat = &surfaceFormat->surfaceFormat; - imgInfo.rowPitch = imageDesc.image_width * surfaceFormat->surfaceFormat.ImageElementSizeInBytes; - imgInfo.slicePitch = imgInfo.rowPitch * imageDesc.image_height; - imgInfo.size = imgInfo.slicePitch; - - char memory[10]; - - EXPECT_TRUE(Image::isCopyRequired(imgInfo, memory)); -} - -TEST(ImageTest, givenDifferentSlicePitchAndTilingNotAllowedWhenIsCopyRequiredIsCalledThenTrueIsReturned) { - ImageInfo imgInfo{}; - - cl_image_format imageFormat = {}; - imageFormat.image_channel_data_type = CL_UNSIGNED_INT8; - imageFormat.image_channel_order = CL_R; - - cl_mem_flags flags = CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR; - auto surfaceFormat = Image::getSurfaceFormatFromTable(flags, &imageFormat); - - cl_image_desc imageDesc{}; - imageDesc.image_type = CL_MEM_OBJECT_IMAGE1D; - imageDesc.image_width = 4; - imageDesc.image_height = 2; - imageDesc.image_slice_pitch = imageDesc.image_width * (imageDesc.image_height + 3) * surfaceFormat->surfaceFormat.ImageElementSizeInBytes; - - imgInfo.imgDesc = Image::convertDescriptor(imageDesc); - imgInfo.surfaceFormat = &surfaceFormat->surfaceFormat; - imgInfo.rowPitch = imageDesc.image_width * surfaceFormat->surfaceFormat.ImageElementSizeInBytes; - imgInfo.slicePitch = imgInfo.rowPitch * imageDesc.image_height; - imgInfo.size = imgInfo.slicePitch; - char memory[8]; - - EXPECT_TRUE(Image::isCopyRequired(imgInfo, memory)); -} - -TEST(ImageTest, givenNotCachelinAlignedPointerWhenIsCopyRequiredIsCalledThenTrueIsReturned) { - ImageInfo imgInfo{}; - - cl_image_format imageFormat = {}; - imageFormat.image_channel_data_type = CL_UNSIGNED_INT8; - imageFormat.image_channel_order = CL_R; - - cl_mem_flags flags = CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR; - auto surfaceFormat = Image::getSurfaceFormatFromTable(flags, &imageFormat); - - cl_image_desc imageDesc{}; - imageDesc.image_type = CL_MEM_OBJECT_IMAGE1D; - imageDesc.image_width = 4096; - imageDesc.image_height = 1; - - imgInfo.imgDesc = Image::convertDescriptor(imageDesc); - imgInfo.surfaceFormat = &surfaceFormat->surfaceFormat; - imgInfo.rowPitch = imageDesc.image_width * surfaceFormat->surfaceFormat.ImageElementSizeInBytes; - imgInfo.slicePitch = imgInfo.rowPitch * imageDesc.image_height; - imgInfo.size = imgInfo.slicePitch; - char memory[8]; - - EXPECT_TRUE(Image::isCopyRequired(imgInfo, &memory[1])); -} - -TEST(ImageTest, givenCachelineAlignedPointerAndProperDescriptorValuesWhenIsCopyRequiredIsCalledThenFalseIsReturned) { - ImageInfo imgInfo{}; - - cl_image_format imageFormat = {}; - imageFormat.image_channel_data_type = CL_UNSIGNED_INT8; - imageFormat.image_channel_order = CL_R; - - cl_mem_flags flags = CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR; - auto surfaceFormat = Image::getSurfaceFormatFromTable(flags, &imageFormat); - - cl_image_desc imageDesc{}; - imageDesc.image_type = CL_MEM_OBJECT_IMAGE1D; - imageDesc.image_width = 2; - imageDesc.image_height = 1; - - imgInfo.imgDesc = Image::convertDescriptor(imageDesc); - imgInfo.surfaceFormat = &surfaceFormat->surfaceFormat; - imgInfo.rowPitch = imageDesc.image_width * surfaceFormat->surfaceFormat.ImageElementSizeInBytes; - imgInfo.slicePitch = imgInfo.rowPitch * imageDesc.image_height; - imgInfo.size = imgInfo.slicePitch; - imgInfo.linearStorage = true; - - auto hostPtr = alignedMalloc(imgInfo.size, MemoryConstants::cacheLineSize); - - EXPECT_FALSE(Image::isCopyRequired(imgInfo, hostPtr)); - alignedFree(hostPtr); -} - -TEST(ImageTest, givenForcedLinearImages3DImageAndProperDescriptorValuesWhenIsCopyRequiredIsCalledThenFalseIsReturned) { - DebugManagerStateRestore dbgRestorer; - DebugManager.flags.ForceLinearImages.set(true); - auto &hwHelper = HwHelper::get(platformDevices[0]->platform.eRenderCoreFamily); - - ImageInfo imgInfo{}; - - cl_image_format imageFormat = {}; - imageFormat.image_channel_data_type = CL_UNSIGNED_INT8; - imageFormat.image_channel_order = CL_R; - - cl_mem_flags flags = CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR; - auto surfaceFormat = Image::getSurfaceFormatFromTable(flags, &imageFormat); - - cl_image_desc imageDesc{}; - imageDesc.image_type = CL_MEM_OBJECT_IMAGE3D; - imageDesc.image_width = 2; - imageDesc.image_height = 2; - imageDesc.image_depth = 2; - - imgInfo.imgDesc = Image::convertDescriptor(imageDesc); - imgInfo.surfaceFormat = &surfaceFormat->surfaceFormat; - imgInfo.rowPitch = imageDesc.image_width * surfaceFormat->surfaceFormat.ImageElementSizeInBytes; - imgInfo.slicePitch = imgInfo.rowPitch * imageDesc.image_height; - imgInfo.size = imgInfo.slicePitch; - imgInfo.linearStorage = !hwHelper.tilingAllowed(false, Image::isImage1d(Image::convertDescriptor(imgInfo.imgDesc)), false); - - auto hostPtr = alignedMalloc(imgInfo.size, MemoryConstants::cacheLineSize); - - EXPECT_FALSE(Image::isCopyRequired(imgInfo, hostPtr)); - alignedFree(hostPtr); -} - TEST(ImageTest, givenClMemCopyHostPointerPassedToImageCreateWhenAllocationIsNotInSystemMemoryPoolThenAllocationIsWrittenByEnqueueWriteImage) { ExecutionEnvironment *executionEnvironment = platformImpl->peekExecutionEnvironment(); auto *memoryManager = new ::testing::NiceMock(*executionEnvironment); diff --git a/unit_tests/memory_manager/memory_manager_tests.cpp b/unit_tests/memory_manager/memory_manager_tests.cpp index 5da49f0057..02bf39a050 100644 --- a/unit_tests/memory_manager/memory_manager_tests.cpp +++ b/unit_tests/memory_manager/memory_manager_tests.cpp @@ -709,7 +709,7 @@ TEST(OsAgnosticMemoryManager, givenHostPointerNotRequiringCopyWhenAllocateGraphi auto hostPtr = alignedMalloc(imgDesc.image_width * imgDesc.image_height * 4, MemoryConstants::pageSize); - bool copyRequired = Image::isCopyRequired(imgInfo, hostPtr); + bool copyRequired = MockMemoryManager::isCopyRequired(imgInfo, hostPtr); EXPECT_FALSE(copyRequired); MockMemoryManager::AllocationData allocationData; @@ -748,7 +748,7 @@ TEST(OsAgnosticMemoryManager, givenHostPointerRequiringCopyWhenAllocateGraphicsM auto hostPtr = alignedMalloc(imgDesc.image_width * imgDesc.image_height * 4, MemoryConstants::pageSize); - bool copyRequired = Image::isCopyRequired(imgInfo, hostPtr); + bool copyRequired = MockMemoryManager::isCopyRequired(imgInfo, hostPtr); EXPECT_TRUE(copyRequired); MockMemoryManager::AllocationData allocationData; @@ -1811,6 +1811,174 @@ TEST(MemoryManagerTest, givenAllocationTypesThatMayNeedL3FlushWhenCallingGetAllo } } +TEST(MemoryManagerTest, givenNullHostPtrWhenIsCopyRequiredIsCalledThenFalseIsReturned) { + ImageInfo imgInfo{}; + EXPECT_FALSE(MockMemoryManager::isCopyRequired(imgInfo, nullptr)); +} + +TEST(MemoryManagerTest, givenAllowedTilingWhenIsCopyRequiredIsCalledThenTrueIsReturned) { + ImageInfo imgInfo{}; + cl_image_desc imageDesc{}; + imageDesc.image_type = CL_MEM_OBJECT_IMAGE2D; + imageDesc.image_width = 1; + imageDesc.image_height = 1; + + cl_image_format imageFormat = {}; + imageFormat.image_channel_data_type = CL_UNSIGNED_INT8; + imageFormat.image_channel_order = CL_R; + + cl_mem_flags flags = CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR; + auto surfaceFormat = Image::getSurfaceFormatFromTable(flags, &imageFormat); + + imgInfo.imgDesc = Image::convertDescriptor(imageDesc); + imgInfo.surfaceFormat = &surfaceFormat->surfaceFormat; + imgInfo.rowPitch = imageDesc.image_width * surfaceFormat->surfaceFormat.ImageElementSizeInBytes; + imgInfo.slicePitch = imgInfo.rowPitch * imageDesc.image_height; + imgInfo.size = imgInfo.slicePitch; + + char memory; + + EXPECT_TRUE(MockMemoryManager::isCopyRequired(imgInfo, &memory)); +} + +TEST(MemoryManagerTest, givenDifferentRowPitchWhenIsCopyRequiredIsCalledThenTrueIsReturned) { + ImageInfo imgInfo{}; + cl_image_desc imageDesc{}; + imageDesc.image_type = CL_MEM_OBJECT_IMAGE1D; + imageDesc.image_width = 1; + imageDesc.image_height = 1; + imageDesc.image_row_pitch = 10; + + cl_image_format imageFormat = {}; + imageFormat.image_channel_data_type = CL_UNSIGNED_INT8; + imageFormat.image_channel_order = CL_R; + + cl_mem_flags flags = CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR; + auto surfaceFormat = Image::getSurfaceFormatFromTable(flags, &imageFormat); + + imgInfo.imgDesc = Image::convertDescriptor(imageDesc); + imgInfo.surfaceFormat = &surfaceFormat->surfaceFormat; + imgInfo.rowPitch = imageDesc.image_width * surfaceFormat->surfaceFormat.ImageElementSizeInBytes; + imgInfo.slicePitch = imgInfo.rowPitch * imageDesc.image_height; + imgInfo.size = imgInfo.slicePitch; + + char memory[10]; + + EXPECT_TRUE(MockMemoryManager::isCopyRequired(imgInfo, memory)); +} + +TEST(MemoryManagerTest, givenDifferentSlicePitchAndTilingNotAllowedWhenIsCopyRequiredIsCalledThenTrueIsReturned) { + ImageInfo imgInfo{}; + + cl_image_format imageFormat = {}; + imageFormat.image_channel_data_type = CL_UNSIGNED_INT8; + imageFormat.image_channel_order = CL_R; + + cl_mem_flags flags = CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR; + auto surfaceFormat = Image::getSurfaceFormatFromTable(flags, &imageFormat); + + cl_image_desc imageDesc{}; + imageDesc.image_type = CL_MEM_OBJECT_IMAGE1D; + imageDesc.image_width = 4; + imageDesc.image_height = 2; + imageDesc.image_slice_pitch = imageDesc.image_width * (imageDesc.image_height + 3) * surfaceFormat->surfaceFormat.ImageElementSizeInBytes; + + imgInfo.imgDesc = Image::convertDescriptor(imageDesc); + imgInfo.surfaceFormat = &surfaceFormat->surfaceFormat; + imgInfo.rowPitch = imageDesc.image_width * surfaceFormat->surfaceFormat.ImageElementSizeInBytes; + imgInfo.slicePitch = imgInfo.rowPitch * imageDesc.image_height; + imgInfo.size = imgInfo.slicePitch; + char memory[8]; + + EXPECT_TRUE(MockMemoryManager::isCopyRequired(imgInfo, memory)); +} + +TEST(MemoryManagerTest, givenNotCachelinAlignedPointerWhenIsCopyRequiredIsCalledThenTrueIsReturned) { + ImageInfo imgInfo{}; + + cl_image_format imageFormat = {}; + imageFormat.image_channel_data_type = CL_UNSIGNED_INT8; + imageFormat.image_channel_order = CL_R; + + cl_mem_flags flags = CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR; + auto surfaceFormat = Image::getSurfaceFormatFromTable(flags, &imageFormat); + + cl_image_desc imageDesc{}; + imageDesc.image_type = CL_MEM_OBJECT_IMAGE1D; + imageDesc.image_width = 4096; + imageDesc.image_height = 1; + + imgInfo.imgDesc = Image::convertDescriptor(imageDesc); + imgInfo.surfaceFormat = &surfaceFormat->surfaceFormat; + imgInfo.rowPitch = imageDesc.image_width * surfaceFormat->surfaceFormat.ImageElementSizeInBytes; + imgInfo.slicePitch = imgInfo.rowPitch * imageDesc.image_height; + imgInfo.size = imgInfo.slicePitch; + char memory[8]; + + EXPECT_TRUE(MockMemoryManager::isCopyRequired(imgInfo, &memory[1])); +} + +TEST(MemoryManagerTest, givenCachelineAlignedPointerAndProperDescriptorValuesWhenIsCopyRequiredIsCalledThenFalseIsReturned) { + ImageInfo imgInfo{}; + + cl_image_format imageFormat = {}; + imageFormat.image_channel_data_type = CL_UNSIGNED_INT8; + imageFormat.image_channel_order = CL_R; + + cl_mem_flags flags = CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR; + auto surfaceFormat = Image::getSurfaceFormatFromTable(flags, &imageFormat); + + cl_image_desc imageDesc{}; + imageDesc.image_type = CL_MEM_OBJECT_IMAGE1D; + imageDesc.image_width = 2; + imageDesc.image_height = 1; + + imgInfo.imgDesc = Image::convertDescriptor(imageDesc); + imgInfo.surfaceFormat = &surfaceFormat->surfaceFormat; + imgInfo.rowPitch = imageDesc.image_width * surfaceFormat->surfaceFormat.ImageElementSizeInBytes; + imgInfo.slicePitch = imgInfo.rowPitch * imageDesc.image_height; + imgInfo.size = imgInfo.slicePitch; + imgInfo.linearStorage = true; + + auto hostPtr = alignedMalloc(imgInfo.size, MemoryConstants::cacheLineSize); + + EXPECT_FALSE(MockMemoryManager::isCopyRequired(imgInfo, hostPtr)); + alignedFree(hostPtr); +} + +TEST(MemoryManagerTest, givenForcedLinearImages3DImageAndProperDescriptorValuesWhenIsCopyRequiredIsCalledThenFalseIsReturned) { + DebugManagerStateRestore dbgRestorer; + DebugManager.flags.ForceLinearImages.set(true); + auto &hwHelper = HwHelper::get(platformDevices[0]->platform.eRenderCoreFamily); + + ImageInfo imgInfo{}; + + cl_image_format imageFormat = {}; + imageFormat.image_channel_data_type = CL_UNSIGNED_INT8; + imageFormat.image_channel_order = CL_R; + + cl_mem_flags flags = CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR; + auto surfaceFormat = Image::getSurfaceFormatFromTable(flags, &imageFormat); + + cl_image_desc imageDesc{}; + imageDesc.image_type = CL_MEM_OBJECT_IMAGE3D; + imageDesc.image_width = 2; + imageDesc.image_height = 2; + imageDesc.image_depth = 2; + + imgInfo.imgDesc = Image::convertDescriptor(imageDesc); + imgInfo.surfaceFormat = &surfaceFormat->surfaceFormat; + imgInfo.rowPitch = imageDesc.image_width * surfaceFormat->surfaceFormat.ImageElementSizeInBytes; + imgInfo.slicePitch = imgInfo.rowPitch * imageDesc.image_height; + imgInfo.size = imgInfo.slicePitch; + imgInfo.linearStorage = !hwHelper.tilingAllowed(false, Image::isImage1d(Image::convertDescriptor(imgInfo.imgDesc)), false); + + auto hostPtr = alignedMalloc(imgInfo.size, MemoryConstants::cacheLineSize); + + EXPECT_FALSE(MockMemoryManager::isCopyRequired(imgInfo, hostPtr)); + alignedFree(hostPtr); +} + TEST(HeapSelectorTest, given32bitInternalAllocationWhenSelectingHeapThenInternalHeapIsUsed) { GraphicsAllocation allocation{0, GraphicsAllocation::AllocationType::KERNEL_ISA, nullptr, 0, 0, 0, MemoryPool::MemoryNull}; allocation.set32BitAllocation(true); diff --git a/unit_tests/mocks/mock_memory_manager.h b/unit_tests/mocks/mock_memory_manager.h index 9d6e131bc8..5d52326bcf 100644 --- a/unit_tests/mocks/mock_memory_manager.h +++ b/unit_tests/mocks/mock_memory_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017-2019 Intel Corporation + * Copyright (C) 2017-2020 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -48,6 +48,7 @@ class MockMemoryManager : public MemoryManagerCreate { using MemoryManager::useNonSvmHostPtrAlloc; using OsAgnosticMemoryManager::allocateGraphicsMemoryForImageFromHostPtr; using MemoryManagerCreate::MemoryManagerCreate; + using MemoryManager::isCopyRequired; using MemoryManager::reservedMemory; MockMemoryManager(ExecutionEnvironment &executionEnvironment) : MockMemoryManager(false, executionEnvironment) {} diff --git a/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp b/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp index 3f1c795ba0..ecee68a146 100644 --- a/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp +++ b/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp @@ -1580,7 +1580,7 @@ TEST_F(DrmMemoryManagerTest, givenHostPointerNotRequiringCopyWhenAllocateGraphic imgInfo.linearStorage = true; auto hostPtr = alignedMalloc(imgDesc.image_width * imgDesc.image_height * 4, MemoryConstants::pageSize); - bool copyRequired = Image::isCopyRequired(imgInfo, hostPtr); + bool copyRequired = MockMemoryManager::isCopyRequired(imgInfo, hostPtr); EXPECT_FALSE(copyRequired); TestedDrmMemoryManager::AllocationData allocationData;