2018-09-05 16:10:26 +08:00
|
|
|
/*
|
2019-01-09 19:56:38 +08:00
|
|
|
* Copyright (C) 2018-2019 Intel Corporation
|
2018-09-05 16:10:26 +08:00
|
|
|
*
|
2018-09-17 20:03:37 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2018-09-05 16:10:26 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2019-01-29 16:54:50 +08:00
|
|
|
#include "common/helpers/bit_helpers.h"
|
2018-10-31 16:51:31 +08:00
|
|
|
#include "public/cl_ext_private.h"
|
2019-01-28 22:27:15 +08:00
|
|
|
#include "runtime/context/context_type.h"
|
2018-09-05 16:10:26 +08:00
|
|
|
#include "runtime/mem_obj/mem_obj.h"
|
2018-09-17 20:03:37 +08:00
|
|
|
#include "runtime/memory_manager/memory_manager.h"
|
2018-09-05 16:10:26 +08:00
|
|
|
|
2019-02-27 18:39:32 +08:00
|
|
|
#include "CL/cl.h"
|
|
|
|
#include "mem_obj_types.h"
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2018-09-05 16:10:26 +08:00
|
|
|
|
|
|
|
class MemObjHelper {
|
|
|
|
public:
|
2018-10-31 16:51:31 +08:00
|
|
|
static bool parseMemoryProperties(const cl_mem_properties_intel *properties, MemoryProperties &propertiesStruct);
|
|
|
|
|
2019-03-12 21:19:01 +08:00
|
|
|
static bool validateMemoryPropertiesForBuffer(const MemoryProperties &properties) {
|
|
|
|
if (!MemObjHelper::checkUsedFlagsForBuffer(properties)) {
|
2018-10-31 16:51:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check all the invalid flags combination. */
|
2019-01-29 16:54:50 +08:00
|
|
|
if ((isValueSet(properties.flags, CL_MEM_READ_WRITE | CL_MEM_READ_ONLY)) ||
|
|
|
|
(isValueSet(properties.flags, CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY)) ||
|
|
|
|
(isValueSet(properties.flags, CL_MEM_READ_ONLY | CL_MEM_WRITE_ONLY)) ||
|
|
|
|
(isValueSet(properties.flags, CL_MEM_ALLOC_HOST_PTR | CL_MEM_USE_HOST_PTR)) ||
|
|
|
|
(isValueSet(properties.flags, CL_MEM_COPY_HOST_PTR | CL_MEM_USE_HOST_PTR)) ||
|
|
|
|
(isValueSet(properties.flags, CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS)) ||
|
|
|
|
(isValueSet(properties.flags, CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_WRITE_ONLY)) ||
|
|
|
|
(isValueSet(properties.flags, CL_MEM_HOST_WRITE_ONLY | CL_MEM_HOST_NO_ACCESS))) {
|
2018-10-31 16:51:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-09-05 16:10:26 +08:00
|
|
|
|
2018-10-31 16:51:31 +08:00
|
|
|
return validateExtraMemoryProperties(properties);
|
2018-09-05 16:10:26 +08:00
|
|
|
}
|
|
|
|
|
2019-03-12 21:19:01 +08:00
|
|
|
static bool validateMemoryPropertiesForImage(const MemoryProperties &properties, cl_mem parent) {
|
|
|
|
if (!MemObjHelper::checkUsedFlagsForImage(properties)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check all the invalid flags combination. */
|
|
|
|
if ((!isValueSet(properties.flags, CL_MEM_ACCESS_FLAGS_UNRESTRICTED_INTEL)) &&
|
|
|
|
(isValueSet(properties.flags, CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY) ||
|
|
|
|
isValueSet(properties.flags, CL_MEM_READ_WRITE | CL_MEM_READ_ONLY) ||
|
|
|
|
isValueSet(properties.flags, CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY) ||
|
|
|
|
isValueSet(properties.flags, CL_MEM_ALLOC_HOST_PTR | CL_MEM_USE_HOST_PTR) ||
|
|
|
|
isValueSet(properties.flags, CL_MEM_COPY_HOST_PTR | CL_MEM_USE_HOST_PTR) ||
|
|
|
|
isValueSet(properties.flags, CL_MEM_HOST_WRITE_ONLY | CL_MEM_HOST_READ_ONLY) ||
|
|
|
|
isValueSet(properties.flags, CL_MEM_HOST_WRITE_ONLY | CL_MEM_HOST_NO_ACCESS) ||
|
|
|
|
isValueSet(properties.flags, CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS) ||
|
|
|
|
isValueSet(properties.flags, CL_MEM_NO_ACCESS_INTEL | CL_MEM_READ_WRITE) ||
|
|
|
|
isValueSet(properties.flags, CL_MEM_NO_ACCESS_INTEL | CL_MEM_WRITE_ONLY) ||
|
|
|
|
isValueSet(properties.flags, CL_MEM_NO_ACCESS_INTEL | CL_MEM_READ_ONLY))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-15 01:26:50 +08:00
|
|
|
auto parentMemObj = castToObject<MemObj>(parent);
|
2019-03-12 21:19:01 +08:00
|
|
|
if (parentMemObj != nullptr && properties.flags) {
|
|
|
|
auto parentFlags = parentMemObj->getFlags();
|
|
|
|
/* Check whether flags are compatible with parent. */
|
2019-03-18 17:31:08 +08:00
|
|
|
if (isValueSet(properties.flags, CL_MEM_ALLOC_HOST_PTR) ||
|
|
|
|
isValueSet(properties.flags, CL_MEM_COPY_HOST_PTR) ||
|
|
|
|
isValueSet(properties.flags, CL_MEM_USE_HOST_PTR) ||
|
|
|
|
((!isValueSet(parentFlags, CL_MEM_ACCESS_FLAGS_UNRESTRICTED_INTEL)) &&
|
|
|
|
(!isValueSet(properties.flags, CL_MEM_ACCESS_FLAGS_UNRESTRICTED_INTEL)) &&
|
|
|
|
((isValueSet(parentFlags, CL_MEM_WRITE_ONLY) && isValueSet(properties.flags, CL_MEM_READ_WRITE)) ||
|
|
|
|
(isValueSet(parentFlags, CL_MEM_WRITE_ONLY) && isValueSet(properties.flags, CL_MEM_READ_ONLY)) ||
|
|
|
|
(isValueSet(parentFlags, CL_MEM_READ_ONLY) && isValueSet(properties.flags, CL_MEM_READ_WRITE)) ||
|
|
|
|
(isValueSet(parentFlags, CL_MEM_READ_ONLY) && isValueSet(properties.flags, CL_MEM_WRITE_ONLY)) ||
|
|
|
|
(isValueSet(parentFlags, CL_MEM_NO_ACCESS_INTEL) && isValueSet(properties.flags, CL_MEM_READ_WRITE)) ||
|
|
|
|
(isValueSet(parentFlags, CL_MEM_NO_ACCESS_INTEL) && isValueSet(properties.flags, CL_MEM_WRITE_ONLY)) ||
|
|
|
|
(isValueSet(parentFlags, CL_MEM_NO_ACCESS_INTEL) && isValueSet(properties.flags, CL_MEM_READ_ONLY)) ||
|
|
|
|
(isValueSet(parentFlags, CL_MEM_HOST_NO_ACCESS) && isValueSet(properties.flags, CL_MEM_HOST_WRITE_ONLY)) ||
|
|
|
|
(isValueSet(parentFlags, CL_MEM_HOST_NO_ACCESS) && isValueSet(properties.flags, CL_MEM_HOST_READ_ONLY))))) {
|
2019-03-12 21:19:01 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return validateExtraMemoryProperties(properties);
|
|
|
|
}
|
|
|
|
|
2019-04-03 17:22:04 +08:00
|
|
|
static AllocationProperties getAllocationProperties(MemoryProperties properties, bool allocateMemory,
|
2019-01-28 22:27:15 +08:00
|
|
|
size_t size, GraphicsAllocation::AllocationType type);
|
2019-04-04 15:36:34 +08:00
|
|
|
static AllocationProperties getAllocationProperties(ImageInfo &imgInfo, bool allocateMemory) {
|
|
|
|
return {allocateMemory, imgInfo, GraphicsAllocation::AllocationType::IMAGE};
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fillCachePolicyInProperties(AllocationProperties &allocationProperties, const MemoryProperties &memoryProperties) {
|
|
|
|
allocationProperties.flags.uncacheable = isValueSet(memoryProperties.flags_intel, CL_MEM_LOCALLY_UNCACHED_RESOURCE);
|
|
|
|
auto cacheFlushRequired = !allocationProperties.flags.uncacheable && !isValueSet(memoryProperties.flags, CL_MEM_READ_ONLY);
|
|
|
|
allocationProperties.flags.flushL3RequiredForRead = cacheFlushRequired;
|
|
|
|
allocationProperties.flags.flushL3RequiredForWrite = cacheFlushRequired;
|
|
|
|
}
|
2018-09-17 20:03:37 +08:00
|
|
|
|
2018-09-05 16:10:26 +08:00
|
|
|
static bool checkMemFlagsForSubBuffer(cl_mem_flags flags) {
|
|
|
|
const cl_mem_flags allValidFlags =
|
|
|
|
CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY |
|
|
|
|
CL_MEM_HOST_WRITE_ONLY | CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS;
|
|
|
|
|
2019-01-29 16:54:50 +08:00
|
|
|
return isFieldValid(flags, allValidFlags);
|
2018-09-05 16:10:26 +08:00
|
|
|
}
|
2019-01-09 19:56:38 +08:00
|
|
|
|
2019-03-19 19:57:47 +08:00
|
|
|
static bool isSuitableForRenderCompression(bool renderCompressed, const MemoryProperties &properties, ContextType contextType, bool preferCompression);
|
2019-01-28 22:27:15 +08:00
|
|
|
|
2019-01-09 19:56:38 +08:00
|
|
|
protected:
|
2019-03-12 21:19:01 +08:00
|
|
|
static bool checkUsedFlagsForBuffer(const MemoryProperties &properties) {
|
|
|
|
MemoryProperties acceptedProperties;
|
|
|
|
addCommonMemoryProperties(acceptedProperties);
|
|
|
|
addExtraMemoryProperties(acceptedProperties);
|
2019-01-09 19:56:38 +08:00
|
|
|
|
2019-03-12 21:19:01 +08:00
|
|
|
return (isFieldValid(properties.flags, acceptedProperties.flags) &&
|
|
|
|
isFieldValid(properties.flags_intel, acceptedProperties.flags_intel));
|
|
|
|
}
|
2019-01-09 19:56:38 +08:00
|
|
|
|
2019-03-12 21:19:01 +08:00
|
|
|
static bool checkUsedFlagsForImage(const MemoryProperties &properties) {
|
|
|
|
MemoryProperties acceptedProperties;
|
|
|
|
addCommonMemoryProperties(acceptedProperties);
|
|
|
|
addImageMemoryProperties(acceptedProperties);
|
|
|
|
addExtraMemoryProperties(acceptedProperties);
|
2019-01-09 19:56:38 +08:00
|
|
|
|
2019-03-12 21:19:01 +08:00
|
|
|
return (isFieldValid(properties.flags, acceptedProperties.flags) &&
|
|
|
|
isFieldValid(properties.flags_intel, acceptedProperties.flags_intel));
|
2019-01-09 19:56:38 +08:00
|
|
|
}
|
|
|
|
|
2019-03-12 21:19:01 +08:00
|
|
|
static inline void addCommonMemoryProperties(MemoryProperties &properties) {
|
|
|
|
properties.flags |=
|
|
|
|
CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY |
|
|
|
|
CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR | CL_MEM_USE_HOST_PTR |
|
|
|
|
CL_MEM_HOST_WRITE_ONLY | CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS;
|
|
|
|
properties.flags_intel |= CL_MEM_LOCALLY_UNCACHED_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void addImageMemoryProperties(MemoryProperties &properties) {
|
|
|
|
properties.flags |= CL_MEM_NO_ACCESS_INTEL | CL_MEM_ACCESS_FLAGS_UNRESTRICTED_INTEL;
|
|
|
|
}
|
2019-01-09 19:56:38 +08:00
|
|
|
|
|
|
|
static void addExtraMemoryProperties(MemoryProperties &properties);
|
2019-03-12 21:19:01 +08:00
|
|
|
static bool validateExtraMemoryProperties(const MemoryProperties &properties);
|
2018-09-05 16:10:26 +08:00
|
|
|
};
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|