Hash :
1071079a
Author :
Date :
2024-10-23T17:32:37
CL/VK: Fix frontend cl_mem_flags default access Additionally, if neither of these flags are specified: - CL_MEM_READ_WRITE - CL_MEM_READ_ONLY - CL_MEM_WRITE_WRITE Then we need to bitwise OR the default kernel access flag (according to spec): CL_MEM_READ_WRITE Bug: angleproject:377942757 Change-Id: I5decd05096a834469fdb6bca941284a400b2c69c Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6003804 Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Austin Annestrand <a.annestrand@samsung.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
//
// Copyright 2021 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// CLMemory.cpp: Implements the cl::Memory class.
#include "libANGLE/CLMemory.h"
#include "libANGLE/CLBuffer.h"
#include "libANGLE/CLContext.h"
#include "libANGLE/CLImage.h"
#include "libANGLE/cl_utils.h"
#include <cstring>
namespace cl
{
namespace
{
MemFlags InheritMemFlags(MemFlags flags, Memory *parent)
{
if (parent != nullptr)
{
const MemFlags parentFlags = parent->getFlags();
const MemFlags access(CL_MEM_READ_WRITE | CL_MEM_READ_ONLY | CL_MEM_WRITE_ONLY);
const MemFlags hostAccess(CL_MEM_HOST_WRITE_ONLY | CL_MEM_HOST_READ_ONLY |
CL_MEM_HOST_NO_ACCESS);
const MemFlags hostPtrFlags(CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR |
CL_MEM_COPY_HOST_PTR);
if (flags.excludes(access))
{
flags.set(parentFlags.mask(access));
}
if (flags.excludes(hostAccess))
{
flags.set(parentFlags.mask(hostAccess));
}
flags.set(parentFlags.mask(hostPtrFlags));
}
return flags;
}
} // namespace
angle::Result Memory::setDestructorCallback(MemoryCB pfnNotify, void *userData)
{
mDestructorCallbacks->emplace(pfnNotify, userData);
return angle::Result::Continue;
}
angle::Result Memory::getInfo(MemInfo name,
size_t valueSize,
void *value,
size_t *valueSizeRet) const
{
static_assert(
std::is_same<cl_uint, cl_bool>::value && std::is_same<cl_uint, cl_mem_object_type>::value,
"OpenCL type mismatch");
cl_uint valUInt = 0u;
void *valPointer = nullptr;
const void *copyValue = nullptr;
size_t copySize = 0u;
switch (name)
{
case MemInfo::Type:
valUInt = ToCLenum(getType());
copyValue = &valUInt;
copySize = sizeof(valUInt);
break;
case MemInfo::Flags:
copyValue = &mFlags;
copySize = sizeof(mFlags);
break;
case MemInfo::Size:
copyValue = &mSize;
copySize = sizeof(mSize);
break;
case MemInfo::HostPtr:
copyValue = &mHostPtr;
copySize = sizeof(mHostPtr);
break;
case MemInfo::MapCount:
valUInt = mMapCount;
copyValue = &valUInt;
copySize = sizeof(valUInt);
break;
case MemInfo::ReferenceCount:
valUInt = getRefCount();
copyValue = &valUInt;
copySize = sizeof(valUInt);
break;
case MemInfo::Context:
valPointer = mContext->getNative();
copyValue = &valPointer;
copySize = sizeof(valPointer);
break;
case MemInfo::AssociatedMemObject:
valPointer = Memory::CastNative(mParent.get());
copyValue = &valPointer;
copySize = sizeof(valPointer);
break;
case MemInfo::Offset:
copyValue = &mOffset;
copySize = sizeof(mOffset);
break;
case MemInfo::UsesSVM_Pointer:
valUInt = CL_FALSE; // TODO(jplate) Check for SVM pointer anglebug.com/42264535
copyValue = &valUInt;
copySize = sizeof(valUInt);
break;
case MemInfo::Properties:
copyValue = mProperties.data();
copySize = mProperties.size() * sizeof(decltype(mProperties)::value_type);
break;
default:
ANGLE_CL_RETURN_ERROR(CL_INVALID_VALUE);
}
if (value != nullptr)
{
// CL_INVALID_VALUE if size in bytes specified by param_value_size is < size of return type
// as described in the Memory Object Info table and param_value is not NULL.
if (valueSize < copySize)
{
ANGLE_CL_RETURN_ERROR(CL_INVALID_VALUE);
}
if (copyValue != nullptr)
{
std::memcpy(value, copyValue, copySize);
}
}
if (valueSizeRet != nullptr)
{
*valueSizeRet = copySize;
}
return angle::Result::Continue;
}
Memory::~Memory()
{
std::stack<CallbackData> callbacks;
mDestructorCallbacks->swap(callbacks);
while (!callbacks.empty())
{
const MemoryCB callback = callbacks.top().first;
void *const userData = callbacks.top().second;
callbacks.pop();
callback(this, userData);
}
}
Memory::Memory(const Buffer &buffer,
Context &context,
PropArray &&properties,
MemFlags flags,
size_t size,
void *hostPtr)
: mContext(&context),
mProperties(std::move(properties)),
mFlags(flags.excludes(CL_MEM_READ_WRITE | CL_MEM_READ_ONLY | CL_MEM_WRITE_ONLY)
? cl::MemFlags{flags.get() | CL_MEM_READ_WRITE}
: flags),
mHostPtr(flags.intersects(CL_MEM_USE_HOST_PTR) ? hostPtr : nullptr),
mImpl(nullptr),
mSize(size),
mMapCount(0u)
{
ANGLE_CL_IMPL_TRY(context.getImpl().createBuffer(buffer, hostPtr, &mImpl));
}
Memory::Memory(const Buffer &buffer, Buffer &parent, MemFlags flags, size_t offset, size_t size)
: mContext(parent.mContext),
mFlags(InheritMemFlags(flags, &parent)),
mHostPtr(parent.mHostPtr != nullptr ? static_cast<char *>(parent.mHostPtr) + offset
: nullptr),
mParent(&parent),
mOffset(offset),
mImpl(nullptr),
mSize(size),
mMapCount(0u)
{
ANGLE_CL_IMPL_TRY(parent.mImpl->createSubBuffer(buffer, flags, size, &mImpl));
}
Memory::Memory(Context &context,
PropArray &&properties,
MemFlags flags,
Memory *parent,
void *hostPtr)
: mContext(&context),
mProperties(std::move(properties)),
mFlags(InheritMemFlags(flags, parent)),
mHostPtr(flags.intersects(CL_MEM_USE_HOST_PTR) ? hostPtr : nullptr),
mParent(parent),
mImpl(nullptr),
mSize(0u),
mMapCount(0u)
{}
} // namespace cl