| 1 |
/* |
| 2 |
* Copyright (C) 2018-2020 Intel Corporation |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "opencl/source/sharings/gl/gl_sharing.h" |
| 9 |
|
| 10 |
#include "shared/source/helpers/string.h" |
| 11 |
#include "shared/source/helpers/timestamp_packet.h" |
| 12 |
|
| 13 |
#include "opencl/source/context/context.inl" |
| 14 |
#include "opencl/source/sharings/gl/gl_context_guard.h" |
| 15 |
#include "opencl/source/sharings/sharing_factory.h" |
| 16 |
|
| 17 |
#include <unordered_map> |
| 18 |
|
| 19 |
namespace NEO { |
| 20 |
const uint32_t GLSharingFunctions::sharingId = SharingType::CLGL_SHARING; |
| 21 |
|
| 22 |
const std::unordered_map<GLenum, const cl_image_format> GlSharing::glToCLFormats = { |
| 23 |
{GL_RGBA8, {CL_RGBA, CL_UNORM_INT8}}, |
| 24 |
{GL_RGBA8I, {CL_RGBA, CL_SIGNED_INT8}}, |
| 25 |
{GL_RGBA16, {CL_RGBA, CL_UNORM_INT16}}, |
| 26 |
{GL_RGBA16I, {CL_RGBA, CL_SIGNED_INT16}}, |
| 27 |
{GL_RGBA32I, {CL_RGBA, CL_SIGNED_INT32}}, |
| 28 |
{GL_RGBA8UI, {CL_RGBA, CL_UNSIGNED_INT8}}, |
| 29 |
{GL_RGBA16UI, {CL_RGBA, CL_UNSIGNED_INT16}}, |
| 30 |
{GL_RGBA32UI, {CL_RGBA, CL_UNSIGNED_INT32}}, |
| 31 |
{GL_RGBA16F, {CL_RGBA, CL_HALF_FLOAT}}, |
| 32 |
{GL_RGBA32F, {CL_RGBA, CL_FLOAT}}, |
| 33 |
{GL_RGBA, {CL_RGBA, CL_UNORM_INT8}}, |
| 34 |
{GL_RGBA8_SNORM, {CL_RGBA, CL_SNORM_INT8}}, |
| 35 |
{GL_RGBA16_SNORM, {CL_RGBA, CL_SNORM_INT16}}, |
| 36 |
{GL_BGRA, {CL_BGRA, CL_UNORM_INT8}}, |
| 37 |
{GL_R8, {CL_R, CL_UNORM_INT8}}, |
| 38 |
{GL_R8_SNORM, {CL_R, CL_SNORM_INT8}}, |
| 39 |
{GL_R16, {CL_R, CL_UNORM_INT16}}, |
| 40 |
{GL_R16_SNORM, {CL_R, CL_SNORM_INT16}}, |
| 41 |
{GL_R16F, {CL_R, CL_HALF_FLOAT}}, |
| 42 |
{GL_R32F, {CL_R, CL_FLOAT}}, |
| 43 |
{GL_R8I, {CL_R, CL_SIGNED_INT8}}, |
| 44 |
{GL_R16I, {CL_R, CL_SIGNED_INT16}}, |
| 45 |
{GL_R32I, {CL_R, CL_SIGNED_INT32}}, |
| 46 |
{GL_R8UI, {CL_R, CL_UNSIGNED_INT8}}, |
| 47 |
{GL_R16UI, {CL_R, CL_UNSIGNED_INT16}}, |
| 48 |
{GL_R32UI, {CL_R, CL_UNSIGNED_INT32}}, |
| 49 |
{GL_DEPTH_COMPONENT32F, {CL_DEPTH, CL_FLOAT}}, |
| 50 |
{GL_DEPTH_COMPONENT16, {CL_DEPTH, CL_UNORM_INT16}}, |
| 51 |
{GL_DEPTH24_STENCIL8, {CL_DEPTH_STENCIL, CL_UNORM_INT24}}, |
| 52 |
{GL_DEPTH32F_STENCIL8, {CL_DEPTH_STENCIL, CL_FLOAT}}, |
| 53 |
{GL_SRGB8_ALPHA8, {CL_sRGBA, CL_UNORM_INT8}}, |
| 54 |
{GL_RG8, {CL_RG, CL_UNORM_INT8}}, |
| 55 |
{GL_RG8_SNORM, {CL_RG, CL_SNORM_INT8}}, |
| 56 |
{GL_RG16, {CL_RG, CL_UNORM_INT16}}, |
| 57 |
{GL_RG16_SNORM, {CL_RG, CL_SNORM_INT16}}, |
| 58 |
{GL_RG16F, {CL_RG, CL_HALF_FLOAT}}, |
| 59 |
{GL_RG32F, {CL_RG, CL_FLOAT}}, |
| 60 |
{GL_RG8I, {CL_RG, CL_SIGNED_INT8}}, |
| 61 |
{GL_RG16I, {CL_RG, CL_SIGNED_INT16}}, |
| 62 |
{GL_RG32I, {CL_RG, CL_SIGNED_INT32}}, |
| 63 |
{GL_RG8UI, {CL_RG, CL_UNSIGNED_INT8}}, |
| 64 |
{GL_RG16UI, {CL_RG, CL_UNSIGNED_INT16}}, |
| 65 |
{GL_RG32UI, {CL_RG, CL_UNSIGNED_INT32}}, |
| 66 |
{GL_RGB10, {CL_RGBA, CL_UNORM_INT16}}}; |
| 67 |
|
| 68 |
cl_int GLSharingFunctions::getSupportedFormats(cl_mem_flags flags, |
| 69 |
cl_mem_object_type imageType, |
| 70 |
size_t numEntries, |
| 71 |
cl_GLenum *formats, |
| 72 |
uint32_t *numImageFormats) { |
| 73 |
if (flags != CL_MEM_READ_ONLY && flags != CL_MEM_WRITE_ONLY && flags != CL_MEM_READ_WRITE && flags != CL_MEM_KERNEL_READ_AND_WRITE) { |
| 74 |
return CL_INVALID_VALUE; |
| 75 |
} |
| 76 |
|
| 77 |
if (imageType != CL_MEM_OBJECT_IMAGE1D && imageType != CL_MEM_OBJECT_IMAGE2D && |
| 78 |
imageType != CL_MEM_OBJECT_IMAGE3D && imageType != CL_MEM_OBJECT_IMAGE1D_ARRAY && |
| 79 |
imageType != CL_MEM_OBJECT_IMAGE1D_BUFFER && imageType != CL_MEM_OBJECT_IMAGE2D_ARRAY) { |
| 80 |
return CL_INVALID_VALUE; |
| 81 |
} |
| 82 |
|
| 83 |
const auto formatsCount = GlSharing::glToCLFormats.size(); |
| 84 |
if (numImageFormats != nullptr) { |
| 85 |
*numImageFormats = static_cast<cl_uint>(formatsCount); |
| 86 |
} |
| 87 |
|
| 88 |
if (formats != nullptr && formatsCount > 0) { |
| 89 |
const auto elementsToCopy = std::min(numEntries, formatsCount); |
| 90 |
uint32_t outputFormatsIndex = 0; |
| 91 |
for (const auto &formatMapping : GlSharing::glToCLFormats) { |
| 92 |
formats[outputFormatsIndex++] = formatMapping.first; |
| 93 |
if (outputFormatsIndex == elementsToCopy) { |
| 94 |
break; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return CL_SUCCESS; |
| 100 |
} |
| 101 |
|
| 102 |
int GlSharing::synchronizeHandler(UpdateData &updateData) { |
| 103 |
GLContextGuard guard(*sharingFunctions); |
| 104 |
synchronizeObject(updateData); |
| 105 |
return CL_SUCCESS; |
| 106 |
} |
| 107 |
|
| 108 |
char *createArbSyncEventName() { |
| 109 |
static std::atomic<uint32_t> synchCounter{0}; |
| 110 |
uint32_t id = synchCounter++; |
| 111 |
constexpr int maxDigitsForId = std::numeric_limits<uint32_t>::digits10; |
| 112 |
static const char prefix[] = "NEO_SYNC_"; |
| 113 |
constexpr int nameMaxLen = sizeof(prefix) + maxDigitsForId + 1; |
| 114 |
char *ret = new char[nameMaxLen]; |
| 115 |
|
| 116 |
snprintf(ret, nameMaxLen, "%s_%d", prefix, id); |
| 117 |
|
| 118 |
return ret; |
| 119 |
} |
| 120 |
|
| 121 |
void destroyArbSyncEventName(char *name) { delete[] name; } |
| 122 |
|
| 123 |
template GLSharingFunctions *Context::getSharing<GLSharingFunctions>(); |
| 124 |
|
| 125 |
} // namespace NEO |