diff --git a/runtime/helpers/blit_commands_helper.cpp b/runtime/helpers/blit_commands_helper.cpp index 7aa631f0d9..46b1bdf7bb 100644 --- a/runtime/helpers/blit_commands_helper.cpp +++ b/runtime/helpers/blit_commands_helper.cpp @@ -46,18 +46,50 @@ BlitProperties BlitProperties::constructPropertiesForReadWriteBuffer(BlitterCons CommandStreamReceiver &commandStreamReceiver, const BuiltinOpParams &builtinOpParams, bool blocking) { - auto mapAllocation = builtinOpParams.mapAllocation; + GraphicsAllocation *hostAllocation = builtinOpParams.mapAllocation; + GraphicsAllocation *gpuAllocation = nullptr; + size_t copyOffset = 0; + size_t memObjOffset = 0; + + void *hostPtr = nullptr; + size_t hostPtrOffset = 0; + if (BlitterConstants::BlitDirection::HostPtrToBuffer == blitDirection) { - return constructPropertiesForReadWriteBuffer(blitDirection, commandStreamReceiver, builtinOpParams.dstMemObj->getGraphicsAllocation(), - builtinOpParams.dstMemObj->getOffset(), mapAllocation, builtinOpParams.srcPtr, - builtinOpParams.srcOffset.x, blocking, builtinOpParams.dstOffset.x, - builtinOpParams.size.x); - } else { - return constructPropertiesForReadWriteBuffer(blitDirection, commandStreamReceiver, builtinOpParams.srcMemObj->getGraphicsAllocation(), - builtinOpParams.srcMemObj->getOffset(), mapAllocation, builtinOpParams.dstPtr, - builtinOpParams.dstOffset.x, blocking, builtinOpParams.srcOffset.x, - builtinOpParams.size.x); + // write buffer + if (builtinOpParams.dstSvmAlloc) { + gpuAllocation = builtinOpParams.dstSvmAlloc; + hostAllocation = builtinOpParams.srcSvmAlloc; + } else { + gpuAllocation = builtinOpParams.dstMemObj->getGraphicsAllocation(); + memObjOffset = builtinOpParams.dstMemObj->getOffset(); + } + + hostPtr = builtinOpParams.srcPtr; + hostPtrOffset = builtinOpParams.srcOffset.x; + copyOffset = builtinOpParams.dstOffset.x; } + + if (BlitterConstants::BlitDirection::BufferToHostPtr == blitDirection) { + // read buffer + if (builtinOpParams.srcSvmAlloc) { + gpuAllocation = builtinOpParams.srcSvmAlloc; + hostAllocation = builtinOpParams.dstSvmAlloc; + } else { + gpuAllocation = builtinOpParams.srcMemObj->getGraphicsAllocation(); + memObjOffset = builtinOpParams.srcMemObj->getOffset(); + } + + hostPtr = builtinOpParams.dstPtr; + hostPtrOffset = builtinOpParams.dstOffset.x; + copyOffset = builtinOpParams.srcOffset.x; + } + + UNRECOVERABLE_IF(BlitterConstants::BlitDirection::HostPtrToBuffer != blitDirection && + BlitterConstants::BlitDirection::BufferToHostPtr != blitDirection); + + return constructPropertiesForReadWriteBuffer(blitDirection, commandStreamReceiver, gpuAllocation, memObjOffset, + hostAllocation, hostPtr, hostPtrOffset, blocking, copyOffset, + builtinOpParams.size.x); } BlitProperties BlitProperties::constructPropertiesForCopyBuffer(GraphicsAllocation *dstAllocation, GraphicsAllocation *srcAllocation, diff --git a/unit_tests/command_stream/command_stream_receiver_hw_tests.cpp b/unit_tests/command_stream/command_stream_receiver_hw_tests.cpp index 9bd9d73010..55db67bf1e 100644 --- a/unit_tests/command_stream/command_stream_receiver_hw_tests.cpp +++ b/unit_tests/command_stream/command_stream_receiver_hw_tests.cpp @@ -10,6 +10,7 @@ #include "core/helpers/preamble.h" #include "core/helpers/ptr_math.h" #include "core/memory_manager/graphics_allocation.h" +#include "core/memory_manager/unified_memory_manager.h" #include "core/os_interface/linux/debug_env_reader.h" #include "core/unit_tests/helpers/debug_manager_state_restore.h" #include "core/unit_tests/utilities/base_object_utils.h" @@ -24,6 +25,7 @@ #include "runtime/helpers/blit_commands_helper.h" #include "runtime/helpers/cache_policy.h" #include "runtime/mem_obj/buffer.h" +#include "runtime/mem_obj/mem_obj_helper.h" #include "runtime/memory_manager/memory_manager.h" #include "runtime/os_interface/debug_settings_manager.h" #include "runtime/os_interface/os_context.h" @@ -43,6 +45,7 @@ #include "unit_tests/mocks/mock_event.h" #include "unit_tests/mocks/mock_internal_allocation_storage.h" #include "unit_tests/mocks/mock_kernel.h" +#include "unit_tests/mocks/mock_memory_manager.h" #include "unit_tests/mocks/mock_submissions_aggregator.h" #include "unit_tests/mocks/mock_timestamp_container.h" @@ -786,6 +789,49 @@ HWTEST_F(BcsTests, givenMapAllocationInBuiltinOpParamsWhenConstructingThenUseItA memoryManager->freeGraphicsMemory(mapAllocation); } +HWTEST_F(BcsTests, givenNonZeroCopySvmAllocationWhenConstructingBlitPropertiesForReadWriteBufferCallThenSetValidAllocations) { + auto &csr = pDevice->getUltCommandStreamReceiver(); + MockMemoryManager mockMemoryManager(true, true); + SVMAllocsManager svmAllocsManager(&mockMemoryManager); + + auto svmAllocationProperties = MemObjHelper::getSvmAllocationProperties(CL_MEM_READ_WRITE); + auto svmAlloc = svmAllocsManager.createSVMAlloc(1, svmAllocationProperties); + auto svmData = svmAllocsManager.getSVMAlloc(svmAlloc); + + EXPECT_NE(nullptr, svmData->gpuAllocation); + EXPECT_NE(nullptr, svmData->cpuAllocation); + EXPECT_NE(svmData->gpuAllocation, svmData->cpuAllocation); + + { + // from hostPtr + BuiltinOpParams builtinOpParams = {}; + builtinOpParams.dstSvmAlloc = svmData->gpuAllocation; + builtinOpParams.srcSvmAlloc = svmData->cpuAllocation; + builtinOpParams.srcPtr = reinterpret_cast(svmData->cpuAllocation->getGpuAddress()); + builtinOpParams.size.x = 1; + + auto blitProperties = BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection::HostPtrToBuffer, + csr, builtinOpParams, true); + EXPECT_EQ(svmData->cpuAllocation, blitProperties.srcAllocation); + EXPECT_EQ(svmData->gpuAllocation, blitProperties.dstAllocation); + } + { + // to hostPtr + BuiltinOpParams builtinOpParams = {}; + builtinOpParams.srcSvmAlloc = svmData->gpuAllocation; + builtinOpParams.dstSvmAlloc = svmData->cpuAllocation; + builtinOpParams.dstPtr = reinterpret_cast(svmData->cpuAllocation->getGpuAddress()); + builtinOpParams.size.x = 1; + + auto blitProperties = BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection::BufferToHostPtr, + csr, builtinOpParams, true); + EXPECT_EQ(svmData->cpuAllocation, blitProperties.dstAllocation); + EXPECT_EQ(svmData->gpuAllocation, blitProperties.srcAllocation); + } + + svmAllocsManager.freeSVMAlloc(svmAlloc); +} + HWTEST_F(BcsTests, givenBufferWithOffsetWhenBlitOperationCalledThenProgramCorrectGpuAddresses) { auto &csr = pDevice->getUltCommandStreamReceiver();