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"
|
|
|
|
|
2018-09-05 16:10:26 +08:00
|
|
|
namespace OCLRT {
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemObj *parentMemObj = castToObject<MemObj>(parent);
|
|
|
|
if (parentMemObj != nullptr && properties.flags) {
|
|
|
|
auto parentFlags = parentMemObj->getFlags();
|
|
|
|
/* Check whether flags are compatible with parent. */
|
|
|
|
if ((!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)))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return validateExtraMemoryProperties(properties);
|
|
|
|
}
|
|
|
|
|
2019-01-28 22:27:15 +08:00
|
|
|
static AllocationProperties getAllocationProperties(cl_mem_flags_intel flags, bool allocateMemory,
|
|
|
|
size_t size, GraphicsAllocation::AllocationType type);
|
2019-01-22 19:40:17 +08:00
|
|
|
static AllocationProperties getAllocationProperties(ImageInfo *imgInfo, bool allocateMemory);
|
2018-09-17 20:03:37 +08:00
|
|
|
|
2019-02-28 23:12:23 +08:00
|
|
|
static StorageInfo getStorageInfo(const MemoryProperties &properties);
|
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-12 21:19:01 +08:00
|
|
|
static bool isSuitableForRenderCompression(bool renderCompressed, const MemoryProperties &properties, ContextType contextType);
|
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
|
|
|
};
|
|
|
|
} // namespace OCLRT
|