diff --git a/runtime/mem_obj/buffer.cpp b/runtime/mem_obj/buffer.cpp index 2cbf0693b6..7597a04a51 100644 --- a/runtime/mem_obj/buffer.cpp +++ b/runtime/mem_obj/buffer.cpp @@ -268,8 +268,15 @@ Buffer *Buffer::create(Context *context, return nullptr; } - pBuffer->setHostPtrMinSize(size); + if (properties.flags & CL_MEM_USE_HOST_PTR) { + if (!zeroCopyAllowed && !isHostPtrSVM) { + AllocationProperties properties{false, size, GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR, false}; + properties.flags.flushL3RequiredForRead = properties.flags.flushL3RequiredForWrite = true; + mapAllocation = memoryManager->allocateGraphicsMemoryWithProperties(properties, hostPtr); + } + } pBuffer->mapAllocation = mapAllocation; + pBuffer->setHostPtrMinSize(size); if (copyMemoryFromHostPtr) { auto gmm = memory->getDefaultGmm(); diff --git a/unit_tests/mem_obj/buffer_tests.cpp b/unit_tests/mem_obj/buffer_tests.cpp index ef2e33df89..6e4f72bacc 100644 --- a/unit_tests/mem_obj/buffer_tests.cpp +++ b/unit_tests/mem_obj/buffer_tests.cpp @@ -267,6 +267,28 @@ TEST(Buffer, givenNullptrPassedToBufferCreateWhenNoSharedContextOrRenderCompress } } +TEST(Buffer, givenHostPtrPassedToBufferCreateWhenMemUseHostPtrFlagisSetAndBufferIsNotZeroCopyThenCreateMapAllocationWithHostPtr) { + std::unique_ptr device(MockDevice::createWithNewExecutionEnvironment(nullptr)); + MockContext ctx(device.get()); + + cl_int retVal = 0; + cl_mem_flags flags = CL_MEM_USE_HOST_PTR; + auto size = MemoryConstants::pageSize; + void *ptr = (void *)alignedMalloc(size * 2, MemoryConstants::pageSize); + auto ptrOffset = 1; + void *offsetedPtr = (void *)((uintptr_t)ptr + ptrOffset); + + std::unique_ptr buffer(Buffer::create(&ctx, flags, MemoryConstants::pageSize, offsetedPtr, retVal)); + ASSERT_NE(nullptr, buffer.get()); + + auto mapAllocation = buffer->getMapAllocation(); + EXPECT_NE(nullptr, mapAllocation); + EXPECT_EQ(offsetedPtr, mapAllocation->getUnderlyingBuffer()); + EXPECT_EQ(GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR, mapAllocation->getAllocationType()); + + alignedFree(ptr); +} + TEST(Buffer, givenAlignedHostPtrPassedToBufferCreateWhenNoSharedContextOrRenderCompressedBuffersThenBuffersAllocationTypeIsBufferHostMemory) { std::unique_ptr device(MockDevice::createWithNewExecutionEnvironment(nullptr)); MockContext ctx(device.get());