2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2025-01-23 00:54:35 +08:00
|
|
|
* Copyright (C) 2018-2025 Intel Corporation
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
2018-09-17 20:03:37 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2023-01-11 01:16:08 +08:00
|
|
|
#include "opencl/source/mem_obj/buffer.h"
|
|
|
|
|
2022-09-21 20:39:39 +08:00
|
|
|
#include "shared/source/command_container/implicit_scaling.h"
|
2023-01-11 23:32:55 +08:00
|
|
|
#include "shared/source/device/device.h"
|
2022-12-29 20:27:52 +08:00
|
|
|
#include "shared/source/execution_environment/execution_environment.h"
|
2022-12-16 01:32:03 +08:00
|
|
|
#include "shared/source/execution_environment/root_device_environment.h"
|
2023-01-09 19:05:21 +08:00
|
|
|
#include "shared/source/gmm_helper/gmm_helper.h"
|
2023-01-20 21:01:19 +08:00
|
|
|
#include "shared/source/gmm_helper/gmm_lib.h"
|
2023-01-12 01:04:14 +08:00
|
|
|
#include "shared/source/helpers/aligned_memory.h"
|
2023-01-18 23:52:24 +08:00
|
|
|
#include "shared/source/helpers/blit_helper.h"
|
2023-02-02 00:23:01 +08:00
|
|
|
#include "shared/source/helpers/gfx_core_helper.h"
|
2023-02-06 17:05:43 +08:00
|
|
|
#include "shared/source/helpers/hw_info.h"
|
2022-11-29 03:00:39 +08:00
|
|
|
#include "shared/source/helpers/local_memory_access_modes.h"
|
2023-01-13 01:05:31 +08:00
|
|
|
#include "shared/source/helpers/memory_properties_helpers.h"
|
2023-01-20 21:01:19 +08:00
|
|
|
#include "shared/source/memory_manager/allocation_properties.h"
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/memory_manager/host_ptr_manager.h"
|
|
|
|
#include "shared/source/memory_manager/memory_operations_handler.h"
|
2023-02-03 00:34:16 +08:00
|
|
|
#include "shared/source/memory_manager/migration_sync_data.h"
|
2023-01-03 00:19:30 +08:00
|
|
|
#include "shared/source/os_interface/os_interface.h"
|
2024-10-31 16:57:43 +08:00
|
|
|
#include "shared/source/utilities/cpuintrinsics.h"
|
2020-02-24 17:22:30 +08:00
|
|
|
|
2023-01-11 23:32:55 +08:00
|
|
|
#include "opencl/source/cl_device/cl_device.h"
|
2020-02-23 05:50:57 +08:00
|
|
|
#include "opencl/source/command_queue/command_queue.h"
|
|
|
|
#include "opencl/source/context/context.h"
|
2021-10-07 01:00:24 +08:00
|
|
|
#include "opencl/source/helpers/cl_memory_properties_helpers.h"
|
2023-01-09 19:05:21 +08:00
|
|
|
#include "opencl/source/helpers/cl_validators.h"
|
2020-02-23 05:50:57 +08:00
|
|
|
#include "opencl/source/mem_obj/mem_obj_helper.h"
|
2023-01-05 19:41:06 +08:00
|
|
|
#include "opencl/source/sharings/unified/unified_buffer.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2020-05-12 19:47:44 +08:00
|
|
|
BufferFactoryFuncs bufferFactory[IGFX_MAX_CORE] = {};
|
|
|
|
|
|
|
|
namespace BufferFunctions {
|
|
|
|
ValidateInputAndCreateBufferFunc validateInputAndCreateBuffer = Buffer::validateInputAndCreateBuffer;
|
|
|
|
} // namespace BufferFunctions
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
Buffer::Buffer(Context *context,
|
2022-06-24 23:44:30 +08:00
|
|
|
const MemoryProperties &memoryProperties,
|
2019-09-24 22:05:17 +08:00
|
|
|
cl_mem_flags flags,
|
|
|
|
cl_mem_flags_intel flagsIntel,
|
2017-12-21 07:45:38 +08:00
|
|
|
size_t size,
|
|
|
|
void *memoryStorage,
|
|
|
|
void *hostPtr,
|
2022-06-25 02:38:34 +08:00
|
|
|
MultiGraphicsAllocation &&multiGraphicsAllocation,
|
2017-12-21 07:45:38 +08:00
|
|
|
bool zeroCopy,
|
|
|
|
bool isHostPtrSVM,
|
|
|
|
bool isObjectRedescribed)
|
|
|
|
: MemObj(context,
|
|
|
|
CL_MEM_OBJECT_BUFFER,
|
2019-10-09 23:29:00 +08:00
|
|
|
memoryProperties,
|
2019-09-24 22:05:17 +08:00
|
|
|
flags,
|
|
|
|
flagsIntel,
|
2017-12-21 07:45:38 +08:00
|
|
|
size,
|
|
|
|
memoryStorage,
|
|
|
|
hostPtr,
|
2020-07-21 16:21:15 +08:00
|
|
|
std::move(multiGraphicsAllocation),
|
2017-12-21 07:45:38 +08:00
|
|
|
zeroCopy,
|
|
|
|
isHostPtrSVM,
|
|
|
|
isObjectRedescribed) {
|
|
|
|
magic = objectMagic;
|
|
|
|
setHostPtrMinSize(size);
|
|
|
|
}
|
|
|
|
|
2020-07-21 16:21:15 +08:00
|
|
|
Buffer::Buffer() : MemObj(nullptr, CL_MEM_OBJECT_BUFFER, {}, 0, 0, 0, nullptr, nullptr, 0, false, false, false) {
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Buffer::~Buffer() = default;
|
|
|
|
|
|
|
|
bool Buffer::isSubBuffer() {
|
|
|
|
return this->associatedMemObject != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Buffer::isValidSubBufferOffset(size_t offset) {
|
2021-12-03 00:47:05 +08:00
|
|
|
if (multiGraphicsAllocation.getDefaultGraphicsAllocation()->isCompressionEnabled()) {
|
2019-04-02 15:42:03 +08:00
|
|
|
// From spec: "origin value is aligned to the CL_DEVICE_MEM_BASE_ADDR_ALIGN value"
|
2019-04-03 20:11:31 +08:00
|
|
|
if (!isAligned(offset, this->getContext()->getDevice(0)->getDeviceInfo().memBaseAddressAlign / 8u)) {
|
2019-04-02 15:42:03 +08:00
|
|
|
return false;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
}
|
2022-05-12 22:04:41 +08:00
|
|
|
cl_uint addressAlign = 32; // 4 byte alignment
|
|
|
|
if ((offset & (addressAlign / 8 - 1)) == 0) {
|
2019-04-02 15:42:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-12 19:47:44 +08:00
|
|
|
cl_mem Buffer::validateInputAndCreateBuffer(cl_context context,
|
|
|
|
const cl_mem_properties *properties,
|
|
|
|
cl_mem_flags flags,
|
|
|
|
cl_mem_flags_intel flagsIntel,
|
|
|
|
size_t size,
|
|
|
|
void *hostPtr,
|
|
|
|
cl_int &retVal) {
|
|
|
|
Context *pContext = nullptr;
|
2022-05-16 22:06:56 +08:00
|
|
|
retVal = validateObjects(withCastToInternal(context, &pContext));
|
2020-05-12 19:47:44 +08:00
|
|
|
if (retVal != CL_SUCCESS) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-05-24 19:37:42 +08:00
|
|
|
|
2024-11-12 16:20:49 +08:00
|
|
|
if ((flags & CL_MEM_USE_HOST_PTR) && !!debugManager.flags.ForceZeroCopyForUseHostPtr.get()) {
|
|
|
|
flags |= CL_MEM_FORCE_HOST_MEMORY_INTEL;
|
|
|
|
}
|
|
|
|
|
2020-05-12 19:47:44 +08:00
|
|
|
MemoryProperties memoryProperties{};
|
|
|
|
cl_mem_alloc_flags_intel allocflags = 0;
|
2020-06-05 06:16:55 +08:00
|
|
|
cl_mem_flags_intel emptyFlagsIntel = 0;
|
2021-10-07 01:00:24 +08:00
|
|
|
if ((false == ClMemoryPropertiesHelper::parseMemoryProperties(nullptr, memoryProperties, flags, emptyFlagsIntel, allocflags,
|
2023-12-14 00:09:52 +08:00
|
|
|
ClMemoryPropertiesHelper::ObjType::buffer, *pContext)) ||
|
2020-06-05 06:16:55 +08:00
|
|
|
(false == MemObjHelper::validateMemoryPropertiesForBuffer(memoryProperties, flags, emptyFlagsIntel, *pContext))) {
|
|
|
|
retVal = CL_INVALID_VALUE;
|
2020-05-12 19:47:44 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-10-07 01:00:24 +08:00
|
|
|
if ((false == ClMemoryPropertiesHelper::parseMemoryProperties(properties, memoryProperties, flags, flagsIntel, allocflags,
|
2023-12-14 00:09:52 +08:00
|
|
|
ClMemoryPropertiesHelper::ObjType::buffer, *pContext)) ||
|
2020-06-05 06:16:55 +08:00
|
|
|
(false == MemObjHelper::validateMemoryPropertiesForBuffer(memoryProperties, flags, flagsIntel, *pContext))) {
|
2020-05-12 19:47:44 +08:00
|
|
|
retVal = CL_INVALID_PROPERTY;
|
|
|
|
return nullptr;
|
2018-10-31 16:51:31 +08:00
|
|
|
}
|
|
|
|
|
2020-05-12 19:47:44 +08:00
|
|
|
auto pDevice = pContext->getDevice(0);
|
2019-10-17 20:18:55 +08:00
|
|
|
bool allowCreateBuffersWithUnrestrictedSize = isValueSet(flags, CL_MEM_ALLOW_UNRESTRICTED_SIZE_INTEL) ||
|
2021-08-05 18:10:20 +08:00
|
|
|
isValueSet(flagsIntel, CL_MEM_ALLOW_UNRESTRICTED_SIZE_INTEL) ||
|
2023-11-30 16:32:25 +08:00
|
|
|
debugManager.flags.AllowUnrestrictedSize.get();
|
2019-07-19 17:51:00 +08:00
|
|
|
|
2021-06-23 23:29:36 +08:00
|
|
|
if (size == 0 || (size > pDevice->getDevice().getDeviceInfo().maxMemAllocSize && !allowCreateBuffersWithUnrestrictedSize)) {
|
2019-07-19 17:51:00 +08:00
|
|
|
retVal = CL_INVALID_BUFFER_SIZE;
|
2020-05-12 19:47:44 +08:00
|
|
|
return nullptr;
|
2018-10-31 16:51:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the host ptr and data */
|
2019-10-17 20:18:55 +08:00
|
|
|
bool expectHostPtr = (flags & (CL_MEM_COPY_HOST_PTR | CL_MEM_USE_HOST_PTR)) != 0;
|
2018-10-31 16:51:31 +08:00
|
|
|
if ((hostPtr == nullptr) == expectHostPtr) {
|
|
|
|
retVal = CL_INVALID_HOST_PTR;
|
2020-05-12 19:47:44 +08:00
|
|
|
return nullptr;
|
2018-10-31 16:51:31 +08:00
|
|
|
}
|
|
|
|
|
2025-02-15 00:50:28 +08:00
|
|
|
if (expectHostPtr) {
|
|
|
|
auto svmAlloc = pContext->getSVMAllocsManager()->getSVMAlloc(hostPtr);
|
|
|
|
|
|
|
|
if (svmAlloc) {
|
|
|
|
auto rootDeviceIndex = pDevice->getRootDeviceIndex();
|
|
|
|
auto allocationEndAddress = svmAlloc->gpuAllocations.getGraphicsAllocation(rootDeviceIndex)->getGpuAddress() + svmAlloc->size;
|
|
|
|
auto bufferEndAddress = castToUint64(hostPtr) + size;
|
|
|
|
|
|
|
|
if ((size > svmAlloc->size) || (bufferEndAddress > allocationEndAddress)) {
|
|
|
|
retVal = CL_INVALID_BUFFER_SIZE;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 16:51:31 +08:00
|
|
|
// create the buffer
|
2022-04-26 18:25:41 +08:00
|
|
|
|
|
|
|
Buffer *pBuffer = nullptr;
|
|
|
|
UnifiedSharingMemoryDescription extMem{};
|
|
|
|
|
|
|
|
if (memoryProperties.handle) {
|
|
|
|
if (validateHandleType(memoryProperties, extMem)) {
|
2022-05-31 17:36:11 +08:00
|
|
|
extMem.handle = reinterpret_cast<void *>(memoryProperties.handle);
|
2022-04-26 18:25:41 +08:00
|
|
|
extMem.size = size;
|
|
|
|
pBuffer = UnifiedBuffer::createSharedUnifiedBuffer(pContext, flags, extMem, &retVal);
|
|
|
|
} else {
|
|
|
|
retVal = CL_INVALID_PROPERTY;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pBuffer = create(pContext, memoryProperties, flags, flagsIntel, size, hostPtr, retVal);
|
|
|
|
}
|
2020-05-27 23:12:32 +08:00
|
|
|
|
|
|
|
if (retVal == CL_SUCCESS) {
|
2022-04-26 18:25:41 +08:00
|
|
|
pBuffer->storeProperties(properties);
|
2020-05-27 23:12:32 +08:00
|
|
|
}
|
|
|
|
|
2022-04-26 18:25:41 +08:00
|
|
|
return pBuffer;
|
2018-10-31 16:51:31 +08:00
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
Buffer *Buffer::create(Context *context,
|
|
|
|
cl_mem_flags flags,
|
|
|
|
size_t size,
|
|
|
|
void *hostPtr,
|
2022-11-21 19:03:05 +08:00
|
|
|
AdditionalBufferCreateArgs &bufferCreateArgs,
|
2017-12-21 07:45:38 +08:00
|
|
|
cl_int &errcodeRet) {
|
2021-10-07 01:00:24 +08:00
|
|
|
return create(context, ClMemoryPropertiesHelper::createMemoryProperties(flags, 0, 0, &context->getDevice(0)->getDevice()),
|
2022-11-21 19:03:05 +08:00
|
|
|
flags, 0, size, hostPtr, bufferCreateArgs, errcodeRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer *Buffer::create(Context *context,
|
|
|
|
cl_mem_flags flags,
|
|
|
|
size_t size,
|
|
|
|
void *hostPtr,
|
|
|
|
cl_int &errcodeRet) {
|
|
|
|
AdditionalBufferCreateArgs bufferCreateArgs{};
|
|
|
|
return create(context, ClMemoryPropertiesHelper::createMemoryProperties(flags, 0, 0, &context->getDevice(0)->getDevice()),
|
|
|
|
flags, 0, size, hostPtr, bufferCreateArgs, errcodeRet);
|
2018-10-31 16:51:31 +08:00
|
|
|
}
|
|
|
|
|
2024-08-28 18:11:54 +08:00
|
|
|
extern bool checkIsGpuCopyRequiredForDcFlushMitigation(AllocationType type);
|
|
|
|
|
2022-10-11 21:16:40 +08:00
|
|
|
bool inline copyHostPointer(Buffer *buffer,
|
2023-02-03 00:34:16 +08:00
|
|
|
Device &device,
|
2022-10-11 21:16:40 +08:00
|
|
|
size_t size,
|
|
|
|
void *hostPtr,
|
|
|
|
bool implicitScalingEnabled,
|
|
|
|
cl_int &errcodeRet) {
|
2023-02-03 00:34:16 +08:00
|
|
|
auto rootDeviceIndex = device.getRootDeviceIndex();
|
2024-06-24 22:22:44 +08:00
|
|
|
auto &productHelper = device.getProductHelper();
|
2023-02-03 00:34:16 +08:00
|
|
|
auto memory = buffer->getGraphicsAllocation(rootDeviceIndex);
|
|
|
|
auto isCompressionEnabled = memory->isCompressionEnabled();
|
2022-10-11 21:16:40 +08:00
|
|
|
const bool isLocalMemory = !MemoryPoolHelper::isSystemMemoryPool(memory->getMemoryPool());
|
2024-08-28 18:11:54 +08:00
|
|
|
const bool isGpuCopyRequiredForDcFlushMitigation = productHelper.isDcFlushMitigated() && checkIsGpuCopyRequiredForDcFlushMitigation(memory->getAllocationType());
|
|
|
|
const bool gpuCopyRequired = isCompressionEnabled || isLocalMemory || isGpuCopyRequiredForDcFlushMitigation;
|
2022-10-11 21:16:40 +08:00
|
|
|
if (gpuCopyRequired) {
|
|
|
|
auto &hwInfo = device.getHardwareInfo();
|
2022-12-14 19:27:22 +08:00
|
|
|
|
|
|
|
auto &osInterface = device.getRootDeviceEnvironment().osInterface;
|
|
|
|
bool isLockable = true;
|
|
|
|
if (osInterface) {
|
|
|
|
isLockable = osInterface->isLockablePointer(memory->storageInfo.isLockable);
|
|
|
|
}
|
|
|
|
|
2022-10-11 21:16:40 +08:00
|
|
|
bool copyOnCpuAllowed = implicitScalingEnabled == false &&
|
|
|
|
size <= Buffer::maxBufferSizeForCopyOnCpu &&
|
|
|
|
isCompressionEnabled == false &&
|
2023-12-19 18:17:17 +08:00
|
|
|
productHelper.getLocalMemoryAccessMode(hwInfo) != LocalMemoryAccessMode::cpuAccessDisallowed &&
|
2024-10-31 16:57:43 +08:00
|
|
|
isLockable;
|
2024-06-24 22:22:44 +08:00
|
|
|
|
2023-11-30 16:32:25 +08:00
|
|
|
if (debugManager.flags.CopyHostPtrOnCpu.get() != -1) {
|
|
|
|
copyOnCpuAllowed = debugManager.flags.CopyHostPtrOnCpu.get() == 1;
|
2022-10-11 21:16:40 +08:00
|
|
|
}
|
|
|
|
if (auto lockedPointer = copyOnCpuAllowed ? device.getMemoryManager()->lockResource(memory) : nullptr) {
|
|
|
|
memory->setAubWritable(true, GraphicsAllocation::defaultBank);
|
|
|
|
memory->setTbxWritable(true, GraphicsAllocation::defaultBank);
|
2023-12-21 23:44:11 +08:00
|
|
|
memcpy_s(ptrOffset(lockedPointer, buffer->getOffset()), size, hostPtr, size);
|
2024-10-31 16:57:43 +08:00
|
|
|
if (isGpuCopyRequiredForDcFlushMitigation) {
|
|
|
|
CpuIntrinsics::sfence();
|
|
|
|
}
|
2022-10-11 21:16:40 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
2023-12-19 15:40:17 +08:00
|
|
|
auto blitMemoryToAllocationResult = BlitOperationResult::unsupported;
|
2022-10-11 21:16:40 +08:00
|
|
|
|
2024-08-28 18:11:54 +08:00
|
|
|
if (productHelper.isBlitterFullySupported(hwInfo) && (isLocalMemory || isGpuCopyRequiredForDcFlushMitigation)) {
|
2025-01-23 00:54:35 +08:00
|
|
|
device.stopDirectSubmissionForCopyEngine();
|
2022-10-11 21:16:40 +08:00
|
|
|
blitMemoryToAllocationResult = BlitHelperFunctions::blitMemoryToAllocation(device, memory, buffer->getOffset(), hostPtr, {size, 1, 1});
|
|
|
|
}
|
|
|
|
|
2023-12-19 15:40:17 +08:00
|
|
|
if (blitMemoryToAllocationResult != BlitOperationResult::success) {
|
2023-02-03 00:34:16 +08:00
|
|
|
auto context = buffer->getContext();
|
2022-10-11 21:16:40 +08:00
|
|
|
auto cmdQ = context->getSpecialQueue(rootDeviceIndex);
|
2023-02-03 00:34:16 +08:00
|
|
|
auto mapAllocation = buffer->getMapAllocation(rootDeviceIndex);
|
2022-10-11 21:16:40 +08:00
|
|
|
if (CL_SUCCESS != cmdQ->enqueueWriteBuffer(buffer, CL_TRUE, buffer->getOffset(), size, hostPtr, mapAllocation, 0, nullptr, nullptr)) {
|
|
|
|
errcodeRet = CL_OUT_OF_RESOURCES;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
memcpy_s(ptrOffset(memory->getUnderlyingBuffer(), buffer->getOffset()), size, hostPtr, size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-31 16:51:31 +08:00
|
|
|
Buffer *Buffer::create(Context *context,
|
2022-06-24 23:44:30 +08:00
|
|
|
const MemoryProperties &memoryProperties,
|
2019-10-17 20:18:55 +08:00
|
|
|
cl_mem_flags flags,
|
|
|
|
cl_mem_flags_intel flagsIntel,
|
2018-10-31 16:51:31 +08:00
|
|
|
size_t size,
|
|
|
|
void *hostPtr,
|
|
|
|
cl_int &errcodeRet) {
|
2022-11-21 19:03:05 +08:00
|
|
|
AdditionalBufferCreateArgs bufferCreateArgs{};
|
|
|
|
return create(context, memoryProperties, flags, flagsIntel, size, hostPtr, bufferCreateArgs, errcodeRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer *Buffer::create(Context *context,
|
|
|
|
const MemoryProperties &memoryProperties,
|
|
|
|
cl_mem_flags flags,
|
|
|
|
cl_mem_flags_intel flagsIntel,
|
|
|
|
size_t size,
|
|
|
|
void *hostPtr,
|
|
|
|
AdditionalBufferCreateArgs &bufferCreateArgs,
|
|
|
|
cl_int &errcodeRet) {
|
2022-06-24 12:08:27 +08:00
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
errcodeRet = CL_SUCCESS;
|
2023-05-02 20:27:55 +08:00
|
|
|
|
|
|
|
RootDeviceIndicesContainer rootDeviceIndices;
|
|
|
|
const RootDeviceIndicesContainer *pRootDeviceIndices;
|
|
|
|
uint32_t defaultRootDeviceIndex;
|
|
|
|
Device *defaultDevice;
|
|
|
|
|
|
|
|
if (memoryProperties.associatedDevices.empty()) {
|
|
|
|
defaultDevice = &context->getDevice(0)->getDevice();
|
|
|
|
defaultRootDeviceIndex = defaultDevice->getRootDeviceIndex();
|
|
|
|
pRootDeviceIndices = &context->getRootDeviceIndices();
|
|
|
|
} else {
|
|
|
|
for (const auto &device : memoryProperties.associatedDevices) {
|
2023-06-12 21:52:45 +08:00
|
|
|
rootDeviceIndices.pushUnique(device->getRootDeviceIndex());
|
2023-05-02 20:27:55 +08:00
|
|
|
}
|
|
|
|
defaultDevice = memoryProperties.associatedDevices[0];
|
|
|
|
defaultRootDeviceIndex = rootDeviceIndices[0];
|
|
|
|
pRootDeviceIndices = &rootDeviceIndices;
|
|
|
|
}
|
|
|
|
|
2022-10-11 21:16:40 +08:00
|
|
|
Context::BufferPoolAllocator &bufferPoolAllocator = context->getBufferPoolAllocator();
|
2023-05-02 20:27:55 +08:00
|
|
|
const bool implicitScalingEnabled = ImplicitScalingHelper::isImplicitScalingEnabled(defaultDevice->getDeviceBitfield(), true);
|
2022-10-11 21:16:40 +08:00
|
|
|
const bool useHostPtr = memoryProperties.flags.useHostPtr;
|
|
|
|
const bool copyHostPtr = memoryProperties.flags.copyHostPtr;
|
|
|
|
if (implicitScalingEnabled == false &&
|
|
|
|
useHostPtr == false &&
|
2023-05-02 20:27:55 +08:00
|
|
|
memoryProperties.flags.forceHostMemory == false &&
|
|
|
|
memoryProperties.associatedDevices.empty()) {
|
2022-10-11 21:16:40 +08:00
|
|
|
cl_int poolAllocRet = CL_SUCCESS;
|
|
|
|
auto bufferFromPool = bufferPoolAllocator.allocateBufferFromPool(memoryProperties,
|
|
|
|
flags,
|
|
|
|
flagsIntel,
|
|
|
|
size,
|
|
|
|
hostPtr,
|
|
|
|
poolAllocRet);
|
|
|
|
if (CL_SUCCESS == poolAllocRet) {
|
|
|
|
const bool needsCopy = copyHostPtr;
|
|
|
|
if (needsCopy) {
|
2023-05-02 20:27:55 +08:00
|
|
|
copyHostPointer(bufferFromPool,
|
|
|
|
*defaultDevice,
|
|
|
|
size,
|
|
|
|
hostPtr,
|
|
|
|
implicitScalingEnabled,
|
|
|
|
poolAllocRet);
|
2022-10-11 21:16:40 +08:00
|
|
|
}
|
|
|
|
if (!needsCopy || poolAllocRet == CL_SUCCESS) {
|
|
|
|
return bufferFromPool;
|
|
|
|
} else {
|
|
|
|
clReleaseMemObject(bufferFromPool);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2019-01-08 15:36:42 +08:00
|
|
|
MemoryManager *memoryManager = context->getMemoryManager();
|
2019-01-09 14:55:03 +08:00
|
|
|
UNRECOVERABLE_IF(!memoryManager);
|
2019-01-08 15:36:42 +08:00
|
|
|
|
2020-09-03 19:36:38 +08:00
|
|
|
auto maxRootDeviceIndex = context->getMaxRootDeviceIndex();
|
|
|
|
auto multiGraphicsAllocation = MultiGraphicsAllocation(maxRootDeviceIndex);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2022-06-24 12:08:27 +08:00
|
|
|
AllocationInfoType allocationInfos;
|
|
|
|
allocationInfos.resize(maxRootDeviceIndex + 1ull);
|
2020-10-21 23:33:21 +08:00
|
|
|
|
2022-06-24 12:08:27 +08:00
|
|
|
void *allocationCpuPtr = nullptr;
|
2020-09-03 19:36:38 +08:00
|
|
|
bool forceCopyHostPtr = false;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2023-05-02 20:27:55 +08:00
|
|
|
for (auto &rootDeviceIndex : *pRootDeviceIndices) {
|
2022-06-24 12:08:27 +08:00
|
|
|
allocationInfos[rootDeviceIndex] = {};
|
|
|
|
auto &allocationInfo = allocationInfos[rootDeviceIndex];
|
2018-07-24 15:06:33 +08:00
|
|
|
|
2022-12-12 21:21:09 +08:00
|
|
|
auto &executionEnvironment = memoryManager->peekExecutionEnvironment();
|
|
|
|
auto &rootDeviceEnvironment = *executionEnvironment.rootDeviceEnvironments[rootDeviceIndex];
|
|
|
|
auto hwInfo = rootDeviceEnvironment.getHardwareInfo();
|
|
|
|
auto &gfxCoreHelper = rootDeviceEnvironment.getHelper<GfxCoreHelper>();
|
2018-07-25 00:36:26 +08:00
|
|
|
|
2022-12-08 20:22:35 +08:00
|
|
|
bool compressionEnabled = MemObjHelper::isSuitableForCompression(GfxCoreHelper::compressedBuffersSupported(*hwInfo), memoryProperties, *context,
|
2023-01-04 17:45:07 +08:00
|
|
|
gfxCoreHelper.isBufferSizeSuitableForCompression(size));
|
2024-07-19 18:42:58 +08:00
|
|
|
|
2023-07-25 20:44:02 +08:00
|
|
|
allocationInfo.allocationType = getGraphicsAllocationTypeAndCompressionPreference(memoryProperties, compressionEnabled,
|
2024-07-19 18:42:58 +08:00
|
|
|
memoryManager->isLocalMemorySupported(rootDeviceIndex));
|
2021-12-02 20:21:33 +08:00
|
|
|
|
2022-06-24 12:08:27 +08:00
|
|
|
if (allocationCpuPtr) {
|
|
|
|
forceCopyHostPtr = !useHostPtr && !copyHostPtr;
|
|
|
|
checkMemory(memoryProperties, size, allocationCpuPtr, errcodeRet, allocationInfo.alignementSatisfied, allocationInfo.copyMemoryFromHostPtr, memoryManager, rootDeviceIndex, forceCopyHostPtr);
|
2020-09-03 19:36:38 +08:00
|
|
|
} else {
|
2022-06-24 12:08:27 +08:00
|
|
|
checkMemory(memoryProperties, size, hostPtr, errcodeRet, allocationInfo.alignementSatisfied, allocationInfo.copyMemoryFromHostPtr, memoryManager, rootDeviceIndex, false);
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
2020-09-03 19:36:38 +08:00
|
|
|
if (errcodeRet != CL_SUCCESS) {
|
2022-06-24 12:08:27 +08:00
|
|
|
cleanAllGraphicsAllocations(*context, *memoryManager, allocationInfos, false);
|
2020-09-03 19:36:38 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-02 23:55:48 +08:00
|
|
|
if (compressionEnabled) {
|
2022-06-24 12:08:27 +08:00
|
|
|
allocationInfo.zeroCopyAllowed = false;
|
|
|
|
allocationInfo.allocateMemory = true;
|
2019-01-08 15:36:42 +08:00
|
|
|
}
|
|
|
|
|
2022-06-24 12:08:27 +08:00
|
|
|
if (useHostPtr) {
|
2023-12-11 22:24:36 +08:00
|
|
|
if (allocationInfo.allocationType == AllocationType::bufferHostMemory) {
|
2022-06-24 12:08:27 +08:00
|
|
|
if (allocationInfo.alignementSatisfied) {
|
|
|
|
allocationInfo.zeroCopyAllowed = true;
|
|
|
|
allocationInfo.allocateMemory = false;
|
2020-09-03 19:36:38 +08:00
|
|
|
} else {
|
2022-06-24 12:08:27 +08:00
|
|
|
allocationInfo.zeroCopyAllowed = false;
|
|
|
|
allocationInfo.allocateMemory = true;
|
2020-09-03 19:36:38 +08:00
|
|
|
}
|
2019-05-15 21:04:43 +08:00
|
|
|
}
|
2018-01-26 23:53:18 +08:00
|
|
|
|
2023-11-30 16:32:25 +08:00
|
|
|
if (debugManager.flags.DisableZeroCopyForUseHostPtr.get()) {
|
2022-06-24 12:08:27 +08:00
|
|
|
allocationInfo.zeroCopyAllowed = false;
|
|
|
|
allocationInfo.allocateMemory = true;
|
2020-09-03 19:36:38 +08:00
|
|
|
}
|
2019-01-08 15:36:42 +08:00
|
|
|
|
2020-09-03 19:36:38 +08:00
|
|
|
auto svmManager = context->getSVMAllocsManager();
|
|
|
|
if (svmManager) {
|
|
|
|
auto svmData = svmManager->getSVMAlloc(hostPtr);
|
|
|
|
if (svmData) {
|
2023-12-13 18:09:37 +08:00
|
|
|
if ((svmData->memoryType == InternalMemoryType::hostUnifiedMemory) && memoryManager->isLocalMemorySupported(rootDeviceIndex)) {
|
2023-11-30 18:28:26 +08:00
|
|
|
allocationInfo.memory = nullptr;
|
2023-12-11 22:24:36 +08:00
|
|
|
allocationInfo.allocationType = AllocationType::buffer;
|
2023-11-30 18:28:26 +08:00
|
|
|
allocationInfo.isHostPtrSVM = false;
|
|
|
|
allocationInfo.zeroCopyAllowed = false;
|
|
|
|
allocationInfo.copyMemoryFromHostPtr = true;
|
|
|
|
allocationInfo.allocateMemory = true;
|
|
|
|
allocationInfo.mapAllocation = svmData->gpuAllocations.getGraphicsAllocation(rootDeviceIndex);
|
|
|
|
} else {
|
|
|
|
allocationInfo.memory = svmData->gpuAllocations.getGraphicsAllocation(rootDeviceIndex);
|
|
|
|
allocationInfo.allocationType = allocationInfo.memory->getAllocationType();
|
|
|
|
allocationInfo.isHostPtrSVM = true;
|
2023-12-11 22:24:36 +08:00
|
|
|
allocationInfo.zeroCopyAllowed = allocationInfo.memory->getAllocationType() == AllocationType::svmZeroCopy;
|
2023-11-30 18:28:26 +08:00
|
|
|
allocationInfo.copyMemoryFromHostPtr = false;
|
|
|
|
allocationInfo.allocateMemory = false;
|
|
|
|
allocationInfo.mapAllocation = svmData->cpuAllocation;
|
|
|
|
}
|
2020-09-03 19:36:38 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-25 00:36:26 +08:00
|
|
|
}
|
|
|
|
|
2022-11-21 19:03:05 +08:00
|
|
|
if (!bufferCreateArgs.doNotProvidePerformanceHints && hostPtr && context->isProvidingPerformanceHints()) {
|
2022-06-24 12:08:27 +08:00
|
|
|
if (allocationInfo.zeroCopyAllowed) {
|
2020-09-03 19:36:38 +08:00
|
|
|
context->providePerformanceHint(CL_CONTEXT_DIAGNOSTICS_LEVEL_GOOD_INTEL, CL_BUFFER_MEETS_ALIGNMENT_RESTRICTIONS, hostPtr, size);
|
|
|
|
} else {
|
|
|
|
context->providePerformanceHint(CL_CONTEXT_DIAGNOSTICS_LEVEL_BAD_INTEL, CL_BUFFER_DOESNT_MEET_ALIGNMENT_RESTRICTIONS, hostPtr, size, MemoryConstants::pageSize, MemoryConstants::pageSize);
|
|
|
|
}
|
|
|
|
}
|
2018-09-27 09:33:52 +08:00
|
|
|
|
2023-11-30 16:32:25 +08:00
|
|
|
if (debugManager.flags.DisableZeroCopyForBuffers.get()) {
|
2022-06-24 12:08:27 +08:00
|
|
|
allocationInfo.zeroCopyAllowed = false;
|
2020-09-03 19:36:38 +08:00
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2022-11-21 19:03:05 +08:00
|
|
|
if (!bufferCreateArgs.doNotProvidePerformanceHints && allocationInfo.allocateMemory && context->isProvidingPerformanceHints()) {
|
2020-09-03 19:36:38 +08:00
|
|
|
context->providePerformanceHint(CL_CONTEXT_DIAGNOSTICS_LEVEL_GOOD_INTEL, CL_BUFFER_NEEDS_ALLOCATE_MEMORY);
|
|
|
|
}
|
2018-09-27 09:33:52 +08:00
|
|
|
|
2022-06-24 12:08:27 +08:00
|
|
|
if (!allocationInfo.memory) {
|
|
|
|
if (allocationCpuPtr) {
|
|
|
|
allocationInfo.allocateMemory = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
AllocationProperties allocProperties = MemoryPropertiesHelper::getAllocationProperties(rootDeviceIndex, memoryProperties,
|
|
|
|
allocationInfo.allocateMemory, size, allocationInfo.allocationType, context->areMultiStorageAllocationsPreferred(),
|
|
|
|
*hwInfo, context->getDeviceBitfieldForAllocation(rootDeviceIndex), context->isSingleDeviceContext());
|
|
|
|
allocProperties.flags.preferCompressed = compressionEnabled;
|
2022-11-21 19:03:05 +08:00
|
|
|
allocProperties.makeDeviceBufferLockable = bufferCreateArgs.makeAllocationLockable;
|
2022-06-24 12:08:27 +08:00
|
|
|
|
|
|
|
if (allocationCpuPtr) {
|
|
|
|
allocationInfo.memory = memoryManager->createGraphicsAllocationFromExistingStorage(allocProperties, allocationCpuPtr, multiGraphicsAllocation);
|
2020-09-03 19:36:38 +08:00
|
|
|
} else {
|
2022-06-24 12:08:27 +08:00
|
|
|
allocationInfo.memory = memoryManager->allocateGraphicsMemoryWithProperties(allocProperties, hostPtr);
|
|
|
|
if (allocationInfo.memory) {
|
|
|
|
allocationCpuPtr = allocationInfo.memory->getUnderlyingBuffer();
|
2020-09-03 19:36:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2022-06-24 12:08:27 +08:00
|
|
|
auto isSystemMemory = allocationInfo.memory && MemoryPoolHelper::isSystemMemoryPool(allocationInfo.memory->getMemoryPool());
|
|
|
|
|
|
|
|
if (allocationInfo.allocateMemory && isSystemMemory) {
|
|
|
|
memoryManager->addAllocationToHostPtrManager(allocationInfo.memory);
|
2020-09-03 19:36:38 +08:00
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2022-06-25 02:38:34 +08:00
|
|
|
// if allocation failed for CL_MEM_USE_HOST_PTR case retry with non zero copy path
|
2022-06-24 12:08:27 +08:00
|
|
|
if (useHostPtr && !allocationInfo.memory && Buffer::isReadOnlyMemoryPermittedByFlags(memoryProperties)) {
|
2023-12-11 22:24:36 +08:00
|
|
|
allocationInfo.allocationType = AllocationType::bufferHostMemory;
|
2022-06-24 12:08:27 +08:00
|
|
|
allocationInfo.zeroCopyAllowed = false;
|
|
|
|
allocationInfo.copyMemoryFromHostPtr = true;
|
2020-09-03 19:36:38 +08:00
|
|
|
AllocationProperties allocProperties = MemoryPropertiesHelper::getAllocationProperties(rootDeviceIndex, memoryProperties,
|
|
|
|
true, // allocateMemory
|
2022-06-24 12:08:27 +08:00
|
|
|
size, allocationInfo.allocationType, context->areMultiStorageAllocationsPreferred(),
|
2021-07-07 20:43:54 +08:00
|
|
|
*hwInfo, context->getDeviceBitfieldForAllocation(rootDeviceIndex), context->isSingleDeviceContext());
|
2022-06-24 12:08:27 +08:00
|
|
|
allocationInfo.memory = memoryManager->allocateGraphicsMemoryWithProperties(allocProperties);
|
2020-09-03 19:36:38 +08:00
|
|
|
}
|
|
|
|
|
2022-06-24 12:08:27 +08:00
|
|
|
if (!allocationInfo.memory) {
|
2020-09-03 19:36:38 +08:00
|
|
|
errcodeRet = CL_OUT_OF_HOST_MEMORY;
|
2022-06-24 12:08:27 +08:00
|
|
|
cleanAllGraphicsAllocations(*context, *memoryManager, allocationInfos, false);
|
2020-09-03 19:36:38 +08:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-07-19 18:42:58 +08:00
|
|
|
if (!isSystemMemory) {
|
2022-06-24 12:08:27 +08:00
|
|
|
allocationInfo.zeroCopyAllowed = false;
|
2020-09-03 19:36:38 +08:00
|
|
|
if (hostPtr) {
|
2022-06-24 12:08:27 +08:00
|
|
|
if (!allocationInfo.isHostPtrSVM) {
|
|
|
|
allocationInfo.copyMemoryFromHostPtr = true;
|
2020-09-03 19:36:38 +08:00
|
|
|
}
|
2019-03-06 23:35:21 +08:00
|
|
|
}
|
2024-08-30 20:32:49 +08:00
|
|
|
} else if (!rootDeviceEnvironment.getProductHelper().isNewCoherencyModelSupported() && allocationInfo.allocationType == AllocationType::buffer && !compressionEnabled) {
|
2023-12-11 22:24:36 +08:00
|
|
|
allocationInfo.allocationType = AllocationType::bufferHostMemory;
|
2019-01-08 15:36:42 +08:00
|
|
|
}
|
2018-07-25 00:36:26 +08:00
|
|
|
|
2024-08-24 10:39:40 +08:00
|
|
|
allocationInfo.memory->setAllocationType(allocationInfo.allocationType);
|
2022-06-24 12:08:27 +08:00
|
|
|
auto isWritable = !(memoryProperties.flags.readOnly || memoryProperties.flags.hostReadOnly || memoryProperties.flags.hostNoAccess);
|
|
|
|
allocationInfo.memory->setMemObjectsAllocationWithWritableFlags(isWritable);
|
2018-07-24 15:06:33 +08:00
|
|
|
|
2022-06-24 12:08:27 +08:00
|
|
|
multiGraphicsAllocation.addAllocation(allocationInfo.memory);
|
2020-09-03 19:36:38 +08:00
|
|
|
|
|
|
|
if (forceCopyHostPtr) {
|
2022-06-24 12:08:27 +08:00
|
|
|
allocationInfo.copyMemoryFromHostPtr = false;
|
2020-09-03 19:36:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 20:27:55 +08:00
|
|
|
auto &allocationInfo = allocationInfos[defaultRootDeviceIndex];
|
2023-02-14 23:02:49 +08:00
|
|
|
auto allocation = allocationInfo.memory;
|
|
|
|
auto memoryStorage = allocation->getUnderlyingBuffer();
|
2023-05-02 20:27:55 +08:00
|
|
|
if (pRootDeviceIndices->size() > 1) {
|
2023-02-15 19:02:02 +08:00
|
|
|
multiGraphicsAllocation.setMultiStorage(!MemoryPoolHelper::isSystemMemoryPool(allocation->getMemoryPool()));
|
2023-02-14 23:02:49 +08:00
|
|
|
}
|
2020-07-10 17:21:53 +08:00
|
|
|
|
2022-06-24 12:08:27 +08:00
|
|
|
auto pBuffer = createBufferHw(context,
|
|
|
|
memoryProperties,
|
|
|
|
flags,
|
|
|
|
flagsIntel,
|
|
|
|
size,
|
|
|
|
memoryStorage,
|
|
|
|
(useHostPtr) ? hostPtr : nullptr,
|
|
|
|
std::move(multiGraphicsAllocation),
|
|
|
|
allocationInfo.zeroCopyAllowed,
|
|
|
|
allocationInfo.isHostPtrSVM,
|
|
|
|
false);
|
2020-01-14 17:56:56 +08:00
|
|
|
|
2018-07-24 15:06:33 +08:00
|
|
|
if (!pBuffer) {
|
|
|
|
errcodeRet = CL_OUT_OF_HOST_MEMORY;
|
2022-06-24 12:08:27 +08:00
|
|
|
cleanAllGraphicsAllocations(*context, *memoryManager, allocationInfos, false);
|
2020-09-03 19:36:38 +08:00
|
|
|
|
2018-07-24 15:06:33 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2021-02-13 01:44:33 +08:00
|
|
|
DBG_LOG(LogMemoryObject, __FUNCTION__, "Created Buffer: Handle: ", pBuffer, ", hostPtr: ", hostPtr, ", size: ", size,
|
2022-06-24 12:08:27 +08:00
|
|
|
", memoryStorage: ", allocationInfo.memory->getUnderlyingBuffer(),
|
2024-05-31 18:22:09 +08:00
|
|
|
", GPU address: ", std::hex, allocationInfo.memory->getGpuAddress(),
|
2022-06-24 12:08:27 +08:00
|
|
|
", memoryPool: ", getMemoryPoolString(allocationInfo.memory));
|
|
|
|
|
2023-05-02 20:27:55 +08:00
|
|
|
for (auto &rootDeviceIndex : *pRootDeviceIndices) {
|
2022-06-24 12:08:27 +08:00
|
|
|
auto &allocationInfo = allocationInfos[rootDeviceIndex];
|
|
|
|
if (useHostPtr) {
|
|
|
|
if (!allocationInfo.zeroCopyAllowed && !allocationInfo.isHostPtrSVM) {
|
2020-09-03 19:36:38 +08:00
|
|
|
AllocationProperties properties{rootDeviceIndex,
|
|
|
|
false, // allocateMemory
|
2023-12-11 22:24:36 +08:00
|
|
|
size, AllocationType::mapAllocation,
|
2020-09-03 19:36:38 +08:00
|
|
|
false, // isMultiStorageAllocation
|
|
|
|
context->getDeviceBitfieldForAllocation(rootDeviceIndex)};
|
|
|
|
properties.flags.flushL3RequiredForRead = properties.flags.flushL3RequiredForWrite = true;
|
2022-06-24 12:08:27 +08:00
|
|
|
allocationInfo.mapAllocation = memoryManager->allocateGraphicsMemoryWithProperties(properties, hostPtr);
|
2020-09-03 19:36:38 +08:00
|
|
|
}
|
2019-06-12 21:05:57 +08:00
|
|
|
}
|
2019-06-29 00:22:38 +08:00
|
|
|
|
2022-06-24 12:08:27 +08:00
|
|
|
auto isCompressionEnabled = allocationInfo.memory->isCompressionEnabled();
|
|
|
|
Buffer::provideCompressionHint(isCompressionEnabled, context, pBuffer);
|
2019-06-29 00:22:38 +08:00
|
|
|
|
2022-06-24 12:08:27 +08:00
|
|
|
if (allocationInfo.mapAllocation) {
|
|
|
|
pBuffer->mapAllocations.addAllocation(allocationInfo.mapAllocation);
|
2020-09-03 19:36:38 +08:00
|
|
|
}
|
|
|
|
pBuffer->setHostPtrMinSize(size);
|
2023-02-03 00:34:16 +08:00
|
|
|
}
|
|
|
|
if (allocationInfo.copyMemoryFromHostPtr) {
|
2023-05-02 20:27:55 +08:00
|
|
|
if (copyHostPointer(pBuffer,
|
|
|
|
*defaultDevice,
|
|
|
|
size,
|
|
|
|
hostPtr,
|
|
|
|
implicitScalingEnabled,
|
|
|
|
errcodeRet)) {
|
|
|
|
auto migrationSyncData = pBuffer->getMultiGraphicsAllocation().getMigrationSyncData();
|
|
|
|
if (migrationSyncData) {
|
|
|
|
migrationSyncData->setCurrentLocation(defaultRootDeviceIndex);
|
2023-02-03 00:34:16 +08:00
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:06:33 +08:00
|
|
|
if (errcodeRet != CL_SUCCESS) {
|
|
|
|
pBuffer->release();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:32:25 +08:00
|
|
|
if (debugManager.flags.MakeAllBuffersResident.get()) {
|
2020-07-02 17:49:46 +08:00
|
|
|
for (size_t deviceNum = 0u; deviceNum < context->getNumDevices(); deviceNum++) {
|
|
|
|
auto device = context->getDevice(deviceNum);
|
|
|
|
auto graphicsAllocation = pBuffer->getGraphicsAllocation(device->getRootDeviceIndex());
|
|
|
|
auto rootDeviceEnvironment = pBuffer->executionEnvironment->rootDeviceEnvironments[device->getRootDeviceIndex()].get();
|
2025-02-05 14:40:13 +08:00
|
|
|
rootDeviceEnvironment->memoryOperationsInterface->makeResident(&device->getDevice(), ArrayRef<GraphicsAllocation *>(&graphicsAllocation, 1), false, false);
|
2020-07-02 17:49:46 +08:00
|
|
|
}
|
2020-02-14 17:47:39 +08:00
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
return pBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer *Buffer::createSharedBuffer(Context *context, cl_mem_flags flags, SharingHandler *sharingHandler,
|
2020-07-15 18:53:15 +08:00
|
|
|
MultiGraphicsAllocation multiGraphicsAllocation) {
|
2020-07-10 17:21:53 +08:00
|
|
|
auto rootDeviceIndex = context->getDevice(0)->getRootDeviceIndex();
|
2020-07-17 05:56:26 +08:00
|
|
|
auto size = multiGraphicsAllocation.getGraphicsAllocation(rootDeviceIndex)->getUnderlyingBufferSize();
|
2020-06-05 06:16:55 +08:00
|
|
|
auto sharedBuffer = createBufferHw(
|
2021-10-07 01:00:24 +08:00
|
|
|
context, ClMemoryPropertiesHelper::createMemoryProperties(flags, 0, 0, &context->getDevice(0)->getDevice()),
|
2020-07-17 05:56:26 +08:00
|
|
|
flags, 0, size, nullptr, nullptr, std::move(multiGraphicsAllocation),
|
2020-06-05 06:16:55 +08:00
|
|
|
false, false, false);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
sharedBuffer->setSharingHandler(sharingHandler);
|
|
|
|
return sharedBuffer;
|
|
|
|
}
|
|
|
|
|
2022-06-24 23:44:30 +08:00
|
|
|
void Buffer::checkMemory(const MemoryProperties &memoryProperties,
|
2017-12-21 07:45:38 +08:00
|
|
|
size_t size,
|
|
|
|
void *hostPtr,
|
|
|
|
cl_int &errcodeRet,
|
2018-07-25 00:36:26 +08:00
|
|
|
bool &alignementSatisfied,
|
2018-01-26 23:53:18 +08:00
|
|
|
bool ©MemoryFromHostPtr,
|
2020-07-07 14:41:26 +08:00
|
|
|
MemoryManager *memoryManager,
|
2020-09-03 19:36:38 +08:00
|
|
|
uint32_t rootDeviceIndex,
|
|
|
|
bool forceCopyHostPtr) {
|
2017-12-21 07:45:38 +08:00
|
|
|
errcodeRet = CL_SUCCESS;
|
2018-07-25 00:36:26 +08:00
|
|
|
alignementSatisfied = true;
|
2017-12-21 07:45:38 +08:00
|
|
|
copyMemoryFromHostPtr = false;
|
2018-01-26 23:53:18 +08:00
|
|
|
uintptr_t minAddress = 0;
|
2018-10-24 14:46:54 +08:00
|
|
|
auto memRestrictions = memoryManager->getAlignedMallocRestrictions();
|
2018-01-26 23:53:18 +08:00
|
|
|
if (memRestrictions) {
|
|
|
|
minAddress = memRestrictions->minAddress;
|
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2019-01-08 18:01:10 +08:00
|
|
|
if (hostPtr) {
|
2020-09-03 19:36:38 +08:00
|
|
|
if (!(memoryProperties.flags.useHostPtr || memoryProperties.flags.copyHostPtr || forceCopyHostPtr)) {
|
2019-01-08 18:01:10 +08:00
|
|
|
errcodeRet = CL_INVALID_HOST_PTR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 18:31:07 +08:00
|
|
|
if (memoryProperties.flags.useHostPtr) {
|
2017-12-21 07:45:38 +08:00
|
|
|
if (hostPtr) {
|
2020-07-07 14:41:26 +08:00
|
|
|
auto fragment = memoryManager->getHostPtrManager()->getFragment({hostPtr, rootDeviceIndex});
|
2018-05-08 16:00:23 +08:00
|
|
|
if (fragment && fragment->driverAllocation) {
|
|
|
|
errcodeRet = CL_INVALID_HOST_PTR;
|
|
|
|
return;
|
|
|
|
}
|
2018-01-26 23:53:18 +08:00
|
|
|
if (alignUp(hostPtr, MemoryConstants::cacheLineSize) != hostPtr ||
|
|
|
|
alignUp(size, MemoryConstants::cacheLineSize) != size ||
|
2018-07-25 00:36:26 +08:00
|
|
|
minAddress > reinterpret_cast<uintptr_t>(hostPtr)) {
|
|
|
|
alignementSatisfied = false;
|
2017-12-21 07:45:38 +08:00
|
|
|
copyMemoryFromHostPtr = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errcodeRet = CL_INVALID_HOST_PTR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 19:36:38 +08:00
|
|
|
if (memoryProperties.flags.copyHostPtr || forceCopyHostPtr) {
|
2017-12-21 07:45:38 +08:00
|
|
|
if (hostPtr) {
|
|
|
|
copyMemoryFromHostPtr = true;
|
|
|
|
} else {
|
|
|
|
errcodeRet = CL_INVALID_HOST_PTR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-25 20:44:02 +08:00
|
|
|
AllocationType Buffer::getGraphicsAllocationTypeAndCompressionPreference(const MemoryProperties &properties,
|
2024-07-19 18:42:58 +08:00
|
|
|
bool &compressionEnabled, bool isLocalMemoryEnabled) {
|
2023-07-25 20:44:02 +08:00
|
|
|
if (properties.flags.forceHostMemory) {
|
2021-12-02 23:55:48 +08:00
|
|
|
compressionEnabled = false;
|
2023-12-11 22:24:36 +08:00
|
|
|
return AllocationType::bufferHostMemory;
|
2018-07-25 00:36:26 +08:00
|
|
|
}
|
|
|
|
|
2024-07-19 18:42:58 +08:00
|
|
|
if (properties.flags.useHostPtr && !isLocalMemoryEnabled) {
|
2021-12-02 23:55:48 +08:00
|
|
|
compressionEnabled = false;
|
2023-12-11 22:24:36 +08:00
|
|
|
return AllocationType::bufferHostMemory;
|
2018-07-25 00:36:26 +08:00
|
|
|
}
|
2019-01-08 15:36:42 +08:00
|
|
|
|
2023-12-11 22:24:36 +08:00
|
|
|
return AllocationType::buffer;
|
2018-07-25 00:36:26 +08:00
|
|
|
}
|
|
|
|
|
2020-04-22 20:37:30 +08:00
|
|
|
bool Buffer::isReadOnlyMemoryPermittedByFlags(const MemoryProperties &properties) {
|
2018-03-05 22:52:35 +08:00
|
|
|
// Host won't access or will only read and kernel will only read
|
2019-08-22 18:31:07 +08:00
|
|
|
return (properties.flags.hostNoAccess || properties.flags.hostReadOnly) && properties.flags.readOnly;
|
2018-03-05 22:52:35 +08:00
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
Buffer *Buffer::createSubBuffer(cl_mem_flags flags,
|
2019-10-23 01:51:53 +08:00
|
|
|
cl_mem_flags_intel flagsIntel,
|
2017-12-21 07:45:38 +08:00
|
|
|
const cl_buffer_region *region,
|
|
|
|
cl_int &errcodeRet) {
|
|
|
|
DEBUG_BREAK_IF(nullptr == createFunction);
|
2020-06-05 06:16:55 +08:00
|
|
|
MemoryProperties memoryProperties =
|
2021-10-07 01:00:24 +08:00
|
|
|
ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, &this->context->getDevice(0)->getDevice());
|
2022-06-25 02:38:34 +08:00
|
|
|
|
|
|
|
auto copyMultiGraphicsAllocation = MultiGraphicsAllocation{this->multiGraphicsAllocation};
|
2019-10-09 23:29:00 +08:00
|
|
|
auto buffer = createFunction(this->context, memoryProperties, flags, 0, region->size,
|
2022-12-09 21:23:06 +08:00
|
|
|
this->memoryStorage ? ptrOffset(this->memoryStorage, region->origin) : nullptr,
|
2018-05-25 21:06:44 +08:00
|
|
|
this->hostPtr ? ptrOffset(this->hostPtr, region->origin) : nullptr,
|
2022-06-25 02:38:34 +08:00
|
|
|
std::move(copyMultiGraphicsAllocation),
|
2018-12-20 03:15:32 +08:00
|
|
|
this->isZeroCopy, this->isHostPtrSVM, false);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
if (this->context->isProvidingPerformanceHints()) {
|
|
|
|
this->context->providePerformanceHint(CL_CONTEXT_DIAGNOSTICS_LEVEL_GOOD_INTEL, SUBBUFFER_SHARES_MEMORY, static_cast<cl_mem>(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer->associatedMemObject = this;
|
2022-12-09 21:23:06 +08:00
|
|
|
buffer->offset = region->origin + this->offset;
|
2017-12-21 07:45:38 +08:00
|
|
|
buffer->setParentSharingHandler(this->getSharingHandler());
|
|
|
|
this->incRefInternal();
|
|
|
|
|
|
|
|
errcodeRet = CL_SUCCESS;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:53:30 +08:00
|
|
|
uint64_t Buffer::setArgStateless(void *memory, uint32_t patchSize, uint32_t rootDeviceIndex, bool set32BitAddressing) {
|
2017-12-21 07:45:38 +08:00
|
|
|
// Subbuffers have offset that graphicsAllocation is not aware of
|
2020-06-23 22:53:30 +08:00
|
|
|
auto graphicsAllocation = multiGraphicsAllocation.getGraphicsAllocation(rootDeviceIndex);
|
2023-02-15 19:02:02 +08:00
|
|
|
auto addressToPatch = ((set32BitAddressing) ? graphicsAllocation->getGpuAddressToPatch() : graphicsAllocation->getGpuAddress()) + this->offset;
|
2019-02-27 21:59:46 +08:00
|
|
|
DEBUG_BREAK_IF(!(graphicsAllocation->isLocked() || (addressToPatch != 0) || (graphicsAllocation->getGpuBaseAddress() != 0) ||
|
2020-06-02 19:38:13 +08:00
|
|
|
(this->getCpuAddress() == nullptr && graphicsAllocation->peekSharedHandle())));
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
patchWithRequiredSize(memory, patchSize, addressToPatch);
|
2018-03-14 18:07:51 +08:00
|
|
|
|
|
|
|
return addressToPatch;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Buffer::bufferRectPitchSet(const size_t *bufferOrigin,
|
|
|
|
const size_t *region,
|
|
|
|
size_t &bufferRowPitch,
|
|
|
|
size_t &bufferSlicePitch,
|
|
|
|
size_t &hostRowPitch,
|
2021-03-10 00:56:13 +08:00
|
|
|
size_t &hostSlicePitch,
|
|
|
|
bool isSrcBuffer) {
|
2017-12-21 07:45:38 +08:00
|
|
|
if (bufferRowPitch == 0)
|
|
|
|
bufferRowPitch = region[0];
|
|
|
|
if (bufferSlicePitch == 0)
|
|
|
|
bufferSlicePitch = region[1] * bufferRowPitch;
|
|
|
|
|
|
|
|
if (hostRowPitch == 0)
|
|
|
|
hostRowPitch = region[0];
|
|
|
|
if (hostSlicePitch == 0)
|
|
|
|
hostSlicePitch = region[1] * hostRowPitch;
|
|
|
|
|
2021-03-10 00:56:13 +08:00
|
|
|
if (region[0] == 0 || region[1] == 0 || region[2] == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
if (bufferRowPitch < region[0] ||
|
|
|
|
hostRowPitch < region[0]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((bufferSlicePitch < region[1] * bufferRowPitch || bufferSlicePitch % bufferRowPitch != 0) ||
|
|
|
|
(hostSlicePitch < region[1] * hostRowPitch || hostSlicePitch % hostRowPitch != 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-10 00:56:13 +08:00
|
|
|
auto slicePitch = isSrcBuffer ? bufferSlicePitch : hostSlicePitch;
|
|
|
|
auto rowPitch = isSrcBuffer ? bufferRowPitch : hostRowPitch;
|
|
|
|
if ((bufferOrigin[2] + region[2] - 1) * slicePitch + (bufferOrigin[1] + region[1] - 1) * rowPitch + bufferOrigin[0] + region[0] > this->getSize()) {
|
2017-12-21 07:45:38 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-09 03:55:31 +08:00
|
|
|
void Buffer::transferData(void *dst, void *src, size_t copySize, size_t copyOffset) {
|
|
|
|
DBG_LOG(LogMemoryObject, __FUNCTION__, " hostPtr: ", hostPtr, ", size: ", copySize, ", offset: ", copyOffset, ", memoryStorage: ", memoryStorage);
|
|
|
|
auto dstPtr = ptrOffset(dst, copyOffset);
|
|
|
|
auto srcPtr = ptrOffset(src, copyOffset);
|
|
|
|
memcpy_s(dstPtr, copySize, srcPtr, copySize);
|
|
|
|
}
|
|
|
|
|
2018-02-18 05:26:28 +08:00
|
|
|
void Buffer::transferDataToHostPtr(MemObjSizeArray ©Size, MemObjOffsetArray ©Offset) {
|
2018-02-09 03:55:31 +08:00
|
|
|
transferData(hostPtr, memoryStorage, copySize[0], copyOffset[0]);
|
|
|
|
}
|
|
|
|
|
2018-02-18 05:26:28 +08:00
|
|
|
void Buffer::transferDataFromHostPtr(MemObjSizeArray ©Size, MemObjOffsetArray ©Offset) {
|
2018-02-09 03:55:31 +08:00
|
|
|
transferData(memoryStorage, hostPtr, copySize[0], copyOffset[0]);
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t Buffer::calculateHostPtrSize(const size_t *origin, const size_t *region, size_t rowPitch, size_t slicePitch) {
|
|
|
|
size_t hostPtrOffsetInBytes = origin[2] * slicePitch + origin[1] * rowPitch + origin[0];
|
|
|
|
size_t hostPtrRegionSizeInbytes = region[0] + rowPitch * (region[1] - 1) + slicePitch * (region[2] - 1);
|
|
|
|
size_t hostPtrSize = hostPtrOffsetInBytes + hostPtrRegionSizeInbytes;
|
|
|
|
return hostPtrSize;
|
|
|
|
}
|
|
|
|
|
2021-03-02 03:07:28 +08:00
|
|
|
bool Buffer::isReadWriteOnCpuAllowed(const Device &device) {
|
2023-07-26 22:27:51 +08:00
|
|
|
if (!allowCpuAccess()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-21 16:48:51 +08:00
|
|
|
if (forceDisallowCPUCopy) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-02 03:07:28 +08:00
|
|
|
auto rootDeviceIndex = device.getRootDeviceIndex();
|
|
|
|
|
2020-06-24 00:08:30 +08:00
|
|
|
if (this->isCompressed(rootDeviceIndex)) {
|
2020-02-21 16:48:51 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-24 00:08:30 +08:00
|
|
|
auto graphicsAllocation = multiGraphicsAllocation.getGraphicsAllocation(rootDeviceIndex);
|
|
|
|
|
2020-02-21 16:48:51 +08:00
|
|
|
if (graphicsAllocation->peekSharedHandle() != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-08 20:54:34 +08:00
|
|
|
|
2024-05-17 16:18:42 +08:00
|
|
|
if (graphicsAllocation->isAllocatedInLocalMemoryPool()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-21 18:11:36 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-25 16:49:29 +08:00
|
|
|
bool Buffer::isReadWriteOnCpuPreferred(void *ptr, size_t size, const Device &device) {
|
2020-06-24 19:38:19 +08:00
|
|
|
auto graphicsAllocation = multiGraphicsAllocation.getGraphicsAllocation(device.getRootDeviceIndex());
|
2022-06-02 05:13:52 +08:00
|
|
|
if (MemoryPoolHelper::isSystemMemoryPool(graphicsAllocation->getMemoryPool())) {
|
2022-06-25 02:38:34 +08:00
|
|
|
// if buffer is not zero copy and pointer is aligned it will be more beneficial to do the transfer on GPU
|
2020-02-24 20:56:41 +08:00
|
|
|
if (!isMemObjZeroCopy() && (reinterpret_cast<uintptr_t>(ptr) & (MemoryConstants::cacheLineSize - 1)) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-21 18:11:36 +08:00
|
|
|
|
2022-06-25 02:38:34 +08:00
|
|
|
// on low power devices larger transfers are better on the GPU
|
2020-06-24 19:38:19 +08:00
|
|
|
if (device.getSpecializedDevice<ClDevice>()->getDeviceInfo().platformLP && size > maxBufferSizeForReadWriteOnCpu) {
|
2020-02-24 20:56:41 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2020-02-21 18:11:36 +08:00
|
|
|
}
|
|
|
|
|
2020-03-11 22:36:37 +08:00
|
|
|
return false;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Buffer *Buffer::createBufferHw(Context *context,
|
2022-06-24 23:44:30 +08:00
|
|
|
const MemoryProperties &memoryProperties,
|
2019-10-17 20:18:55 +08:00
|
|
|
cl_mem_flags flags,
|
|
|
|
cl_mem_flags_intel flagsIntel,
|
2017-12-21 07:45:38 +08:00
|
|
|
size_t size,
|
|
|
|
void *memoryStorage,
|
|
|
|
void *hostPtr,
|
2022-06-25 02:38:34 +08:00
|
|
|
MultiGraphicsAllocation &&multiGraphicsAllocation,
|
2017-12-21 07:45:38 +08:00
|
|
|
bool zeroCopy,
|
|
|
|
bool isHostPtrSVM,
|
|
|
|
bool isImageRedescribed) {
|
2018-07-19 20:39:07 +08:00
|
|
|
const auto device = context->getDevice(0);
|
2017-12-21 07:45:38 +08:00
|
|
|
const auto &hwInfo = device->getHardwareInfo();
|
|
|
|
|
2019-05-08 22:00:24 +08:00
|
|
|
auto funcCreate = bufferFactory[hwInfo.platform.eRenderCoreFamily].createBufferFunction;
|
2017-12-21 07:45:38 +08:00
|
|
|
DEBUG_BREAK_IF(nullptr == funcCreate);
|
2020-07-17 05:56:26 +08:00
|
|
|
auto pBuffer = funcCreate(context, memoryProperties, flags, flagsIntel, size, memoryStorage, hostPtr, std::move(multiGraphicsAllocation),
|
2017-12-21 07:45:38 +08:00
|
|
|
zeroCopy, isHostPtrSVM, isImageRedescribed);
|
|
|
|
DEBUG_BREAK_IF(nullptr == pBuffer);
|
|
|
|
if (pBuffer) {
|
|
|
|
pBuffer->createFunction = funcCreate;
|
|
|
|
}
|
|
|
|
return pBuffer;
|
|
|
|
}
|
|
|
|
|
2020-02-21 06:54:48 +08:00
|
|
|
Buffer *Buffer::createBufferHwFromDevice(const Device *device,
|
2018-06-11 20:58:35 +08:00
|
|
|
cl_mem_flags flags,
|
2019-09-24 22:05:17 +08:00
|
|
|
cl_mem_flags_intel flagsIntel,
|
2018-06-11 20:58:35 +08:00
|
|
|
size_t size,
|
|
|
|
void *memoryStorage,
|
|
|
|
void *hostPtr,
|
2022-06-25 02:38:34 +08:00
|
|
|
MultiGraphicsAllocation &&multiGraphicsAllocation,
|
2020-01-28 18:24:36 +08:00
|
|
|
size_t offset,
|
2018-06-11 20:58:35 +08:00
|
|
|
bool zeroCopy,
|
|
|
|
bool isHostPtrSVM,
|
|
|
|
bool isImageRedescribed) {
|
|
|
|
const auto &hwInfo = device->getHardwareInfo();
|
|
|
|
|
2019-05-08 22:00:24 +08:00
|
|
|
auto funcCreate = bufferFactory[hwInfo.platform.eRenderCoreFamily].createBufferFunction;
|
2018-06-11 20:58:35 +08:00
|
|
|
DEBUG_BREAK_IF(nullptr == funcCreate);
|
2020-07-10 17:21:53 +08:00
|
|
|
|
2021-10-07 01:00:24 +08:00
|
|
|
MemoryProperties memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, device);
|
2020-08-03 17:08:19 +08:00
|
|
|
auto pBuffer = funcCreate(nullptr, memoryProperties, flags, flagsIntel, size, memoryStorage, hostPtr, std::move(multiGraphicsAllocation),
|
2018-06-11 20:58:35 +08:00
|
|
|
zeroCopy, isHostPtrSVM, isImageRedescribed);
|
2020-06-26 00:17:44 +08:00
|
|
|
|
2020-01-28 18:24:36 +08:00
|
|
|
pBuffer->offset = offset;
|
2018-07-31 15:52:48 +08:00
|
|
|
pBuffer->executionEnvironment = device->getExecutionEnvironment();
|
2018-06-11 20:58:35 +08:00
|
|
|
return pBuffer;
|
|
|
|
}
|
|
|
|
|
2020-06-26 00:17:44 +08:00
|
|
|
uint32_t Buffer::getMocsValue(bool disableL3Cache, bool isReadOnlyArgument, uint32_t rootDeviceIndex) const {
|
2019-04-15 19:34:01 +08:00
|
|
|
uint64_t bufferAddress = 0;
|
|
|
|
size_t bufferSize = 0;
|
2020-06-26 00:17:44 +08:00
|
|
|
auto graphicsAllocation = multiGraphicsAllocation.getGraphicsAllocation(rootDeviceIndex);
|
2020-06-02 19:38:13 +08:00
|
|
|
if (graphicsAllocation) {
|
|
|
|
bufferAddress = graphicsAllocation->getGpuAddress();
|
|
|
|
bufferSize = graphicsAllocation->getUnderlyingBufferSize();
|
2019-04-15 19:34:01 +08:00
|
|
|
} else {
|
|
|
|
bufferAddress = reinterpret_cast<uint64_t>(getHostPtr());
|
|
|
|
bufferSize = getSize();
|
|
|
|
}
|
|
|
|
bufferAddress += this->offset;
|
|
|
|
|
2020-04-22 20:37:30 +08:00
|
|
|
bool readOnlyMemObj = isValueSet(getFlags(), CL_MEM_READ_ONLY) || isReadOnlyArgument;
|
2019-04-15 19:34:01 +08:00
|
|
|
bool alignedMemObj = isAligned<MemoryConstants::cacheLineSize>(bufferAddress) &&
|
|
|
|
isAligned<MemoryConstants::cacheLineSize>(bufferSize);
|
|
|
|
|
2020-06-30 23:42:12 +08:00
|
|
|
auto gmmHelper = executionEnvironment->rootDeviceEnvironments[rootDeviceIndex]->getGmmHelper();
|
2019-08-30 14:18:34 +08:00
|
|
|
if (!disableL3Cache && !isMemObjUncacheableForSurfaceState() && (alignedMemObj || readOnlyMemObj || !isMemObjZeroCopy())) {
|
2019-04-15 19:34:01 +08:00
|
|
|
return gmmHelper->getMOCS(GMM_RESOURCE_USAGE_OCL_BUFFER);
|
|
|
|
} else {
|
|
|
|
return gmmHelper->getMOCS(GMM_RESOURCE_USAGE_OCL_BUFFER_CACHELINE_MISALIGNED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 00:17:44 +08:00
|
|
|
uint32_t Buffer::getSurfaceSize(bool alignSizeForAuxTranslation, uint32_t rootDeviceIndex) const {
|
|
|
|
auto bufferAddress = getBufferAddress(rootDeviceIndex);
|
2020-06-23 18:03:43 +08:00
|
|
|
auto bufferAddressAligned = alignDown(bufferAddress, 4);
|
|
|
|
auto bufferOffset = ptrDiff(bufferAddress, bufferAddressAligned);
|
|
|
|
|
|
|
|
uint32_t surfaceSize = static_cast<uint32_t>(alignUp(getSize() + bufferOffset, alignSizeForAuxTranslation ? 512 : 4));
|
|
|
|
return surfaceSize;
|
|
|
|
}
|
|
|
|
|
2020-06-26 00:17:44 +08:00
|
|
|
uint64_t Buffer::getBufferAddress(uint32_t rootDeviceIndex) const {
|
2020-06-23 18:03:43 +08:00
|
|
|
// The graphics allocation for Host Ptr surface will be created in makeResident call and GPU address is expected to be the same as CPU address
|
2020-06-26 00:17:44 +08:00
|
|
|
auto graphicsAllocation = multiGraphicsAllocation.getGraphicsAllocation(rootDeviceIndex);
|
2020-06-23 18:03:43 +08:00
|
|
|
auto bufferAddress = (graphicsAllocation != nullptr) ? graphicsAllocation->getGpuAddress() : castToUint64(getHostPtr());
|
|
|
|
bufferAddress += this->offset;
|
|
|
|
return bufferAddress;
|
|
|
|
}
|
|
|
|
|
2020-06-24 00:08:30 +08:00
|
|
|
bool Buffer::isCompressed(uint32_t rootDeviceIndex) const {
|
2021-12-03 00:47:05 +08:00
|
|
|
return multiGraphicsAllocation.getGraphicsAllocation(rootDeviceIndex)->isCompressionEnabled();
|
2020-02-21 16:48:51 +08:00
|
|
|
}
|
|
|
|
|
2020-02-21 06:54:48 +08:00
|
|
|
void Buffer::setSurfaceState(const Device *device,
|
2017-12-21 07:45:38 +08:00
|
|
|
void *surfaceState,
|
2020-12-22 08:03:25 +08:00
|
|
|
bool forceNonAuxMode,
|
|
|
|
bool disableL3,
|
2017-12-21 07:45:38 +08:00
|
|
|
size_t svmSize,
|
|
|
|
void *svmPtr,
|
2020-01-28 18:24:36 +08:00
|
|
|
size_t offset,
|
2017-12-21 07:45:38 +08:00
|
|
|
GraphicsAllocation *gfxAlloc,
|
2019-09-24 22:05:17 +08:00
|
|
|
cl_mem_flags flags,
|
2021-03-03 20:25:26 +08:00
|
|
|
cl_mem_flags_intel flagsIntel,
|
2021-03-30 01:06:29 +08:00
|
|
|
bool areMultipleSubDevicesInContext) {
|
2020-07-15 01:42:07 +08:00
|
|
|
auto multiGraphicsAllocation = MultiGraphicsAllocation(device->getRootDeviceIndex());
|
|
|
|
if (gfxAlloc) {
|
|
|
|
multiGraphicsAllocation.addAllocation(gfxAlloc);
|
|
|
|
}
|
2020-07-17 05:56:26 +08:00
|
|
|
auto buffer = Buffer::createBufferHwFromDevice(device, flags, flagsIntel, svmSize, svmPtr, svmPtr, std::move(multiGraphicsAllocation), offset, true, false, false);
|
2024-03-08 23:30:37 +08:00
|
|
|
buffer->setArgStateful(surfaceState, forceNonAuxMode, disableL3, false, false, *device, areMultipleSubDevicesInContext);
|
2017-12-21 07:45:38 +08:00
|
|
|
delete buffer;
|
|
|
|
}
|
2019-06-29 00:22:38 +08:00
|
|
|
|
2021-12-03 00:47:05 +08:00
|
|
|
void Buffer::provideCompressionHint(bool compressionEnabled, Context *context, Buffer *buffer) {
|
2022-12-08 20:22:35 +08:00
|
|
|
if (context->isProvidingPerformanceHints() && GfxCoreHelper::compressedBuffersSupported(context->getDevice(0)->getHardwareInfo())) {
|
2021-12-03 00:47:05 +08:00
|
|
|
if (compressionEnabled) {
|
2019-06-29 00:22:38 +08:00
|
|
|
context->providePerformanceHint(CL_CONTEXT_DIAGNOSTICS_LEVEL_NEUTRAL_INTEL, BUFFER_IS_COMPRESSED, buffer);
|
|
|
|
} else {
|
|
|
|
context->providePerformanceHint(CL_CONTEXT_DIAGNOSTICS_LEVEL_NEUTRAL_INTEL, BUFFER_IS_NOT_COMPRESSED, buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|