From 1dbab32755194a167e83eeb222e3ccb4f138f6f1 Mon Sep 17 00:00:00 2001 From: "Hoppe, Mateusz" Date: Wed, 29 May 2019 12:33:09 +0200 Subject: [PATCH] Add querySupportedFormats to GLSharingFunctions Related-To: NEO-612 Change-Id: I15cff764ac76c6dc18620c4992991091f03b0ee6 Signed-off-by: Hoppe, Mateusz --- .../windows/gl/gl_sharing_win.cpp | 35 ++++ runtime/sharings/gl/gl_cl_image_format.cpp | 190 +----------------- runtime/sharings/gl/gl_sharing.cpp | 48 +++++ runtime/sharings/gl/gl_sharing.h | 7 + unit_tests/sharings/gl/gl_sharing_tests.cpp | 107 ++++++++++ 5 files changed, 205 insertions(+), 182 deletions(-) diff --git a/runtime/os_interface/windows/gl/gl_sharing_win.cpp b/runtime/os_interface/windows/gl/gl_sharing_win.cpp index 25f1a491f8..8484a0749a 100644 --- a/runtime/os_interface/windows/gl/gl_sharing_win.cpp +++ b/runtime/os_interface/windows/gl/gl_sharing_win.cpp @@ -10,6 +10,7 @@ #include "runtime/sharings/gl/gl_arb_sync_event.h" #include "runtime/sharings/gl/gl_sharing.h" +#include #include #include @@ -133,4 +134,38 @@ void GLSharingFunctions::createBackupContext() { pfnWglShareLists(GLHGLRCHandle, GLHGLRCHandleBkpCtx); } } + +cl_int GLSharingFunctions::getSupportedFormats(cl_mem_flags flags, + cl_mem_object_type imageType, + size_t numEntries, + cl_GLenum *formats, + uint32_t *numImageFormats) { + if (flags != CL_MEM_READ_ONLY && flags != CL_MEM_WRITE_ONLY && flags != CL_MEM_READ_WRITE && flags != CL_MEM_KERNEL_READ_AND_WRITE) { + return CL_INVALID_VALUE; + } + + if (imageType != CL_MEM_OBJECT_IMAGE1D && imageType != CL_MEM_OBJECT_IMAGE2D && + imageType != CL_MEM_OBJECT_IMAGE3D && imageType != CL_MEM_OBJECT_IMAGE1D_ARRAY && + imageType != CL_MEM_OBJECT_IMAGE1D_BUFFER) { + return CL_INVALID_VALUE; + } + + const auto formatsCount = GlSharing::gLToCLFormats.size(); + if (numImageFormats != nullptr) { + *numImageFormats = static_cast(formatsCount); + } + + if (formats != nullptr && formatsCount > 0) { + auto elementsToCopy = std::min(numEntries, formatsCount); + uint32_t i = 0; + for (auto &x : GlSharing::gLToCLFormats) { + formats[i++] = x.first; + if (i == elementsToCopy) { + break; + } + } + } + + return CL_SUCCESS; +} } // namespace NEO diff --git a/runtime/sharings/gl/gl_cl_image_format.cpp b/runtime/sharings/gl/gl_cl_image_format.cpp index c09904a219..dabd459452 100644 --- a/runtime/sharings/gl/gl_cl_image_format.cpp +++ b/runtime/sharings/gl/gl_cl_image_format.cpp @@ -7,6 +7,7 @@ #include "public/cl_gl_private_intel.h" #include "runtime/gmm_helper/gmm_helper.h" +#include "runtime/sharings/gl/gl_sharing.h" #include "runtime/sharings/gl/gl_texture.h" #include "GL/gl.h" @@ -14,188 +15,13 @@ namespace NEO { bool GlTexture::setClImageFormat(int glFormat, cl_image_format &clImgFormat) { - switch (glFormat) { - case GL_RGBA8: - clImgFormat.image_channel_data_type = CL_UNORM_INT8; - clImgFormat.image_channel_order = CL_RGBA; - break; - case GL_RGBA8I: - clImgFormat.image_channel_data_type = CL_SIGNED_INT8; - clImgFormat.image_channel_order = CL_RGBA; - break; - case GL_RGBA16: - clImgFormat.image_channel_data_type = CL_UNORM_INT16; - clImgFormat.image_channel_order = CL_RGBA; - break; - case GL_RGBA16I: - clImgFormat.image_channel_data_type = CL_SIGNED_INT16; - clImgFormat.image_channel_order = CL_RGBA; - break; - case GL_RGBA32I: - clImgFormat.image_channel_data_type = CL_SIGNED_INT32; - clImgFormat.image_channel_order = CL_RGBA; - break; - case GL_RGBA8UI: - clImgFormat.image_channel_data_type = CL_UNSIGNED_INT8; - clImgFormat.image_channel_order = CL_RGBA; - break; - case GL_RGBA16UI: - clImgFormat.image_channel_data_type = CL_UNSIGNED_INT16; - clImgFormat.image_channel_order = CL_RGBA; - break; - case GL_RGBA32UI: - clImgFormat.image_channel_data_type = CL_UNSIGNED_INT32; - clImgFormat.image_channel_order = CL_RGBA; - break; - case GL_RGBA16F: - clImgFormat.image_channel_data_type = CL_HALF_FLOAT; - clImgFormat.image_channel_order = CL_RGBA; - break; - case GL_RGBA32F: - clImgFormat.image_channel_data_type = CL_FLOAT; - clImgFormat.image_channel_order = CL_RGBA; - break; - case GL_RGBA: - clImgFormat.image_channel_data_type = CL_UNORM_INT8; - clImgFormat.image_channel_order = CL_RGBA; - break; - case GL_RGBA8_SNORM: - clImgFormat.image_channel_data_type = CL_SNORM_INT8; - clImgFormat.image_channel_order = CL_RGBA; - break; - case GL_RGBA16_SNORM: - clImgFormat.image_channel_data_type = CL_SNORM_INT16; - clImgFormat.image_channel_order = CL_RGBA; - break; - case GL_BGRA: - clImgFormat.image_channel_data_type = CL_UNORM_INT8; - clImgFormat.image_channel_order = CL_BGRA; - break; - case GL_R8: - clImgFormat.image_channel_data_type = CL_UNORM_INT8; - clImgFormat.image_channel_order = CL_R; - break; - case GL_R8_SNORM: - clImgFormat.image_channel_data_type = CL_SNORM_INT8; - clImgFormat.image_channel_order = CL_R; - break; - case GL_R16: - clImgFormat.image_channel_data_type = CL_UNORM_INT16; - clImgFormat.image_channel_order = CL_R; - break; - case GL_R16_SNORM: - clImgFormat.image_channel_data_type = CL_SNORM_INT16; - clImgFormat.image_channel_order = CL_R; - break; - case GL_R16F: - clImgFormat.image_channel_data_type = CL_HALF_FLOAT; - clImgFormat.image_channel_order = CL_R; - break; - case GL_R32F: - clImgFormat.image_channel_data_type = CL_FLOAT; - clImgFormat.image_channel_order = CL_R; - break; - case GL_R8I: - clImgFormat.image_channel_data_type = CL_SIGNED_INT8; - clImgFormat.image_channel_order = CL_R; - break; - case GL_R16I: - clImgFormat.image_channel_data_type = CL_SIGNED_INT16; - clImgFormat.image_channel_order = CL_R; - break; - case GL_R32I: - clImgFormat.image_channel_data_type = CL_SIGNED_INT32; - clImgFormat.image_channel_order = CL_R; - break; - case GL_R8UI: - clImgFormat.image_channel_data_type = CL_UNSIGNED_INT8; - clImgFormat.image_channel_order = CL_R; - break; - case GL_R16UI: - clImgFormat.image_channel_data_type = CL_UNSIGNED_INT16; - clImgFormat.image_channel_order = CL_R; - break; - case GL_R32UI: - clImgFormat.image_channel_data_type = CL_UNSIGNED_INT32; - clImgFormat.image_channel_order = CL_R; - break; - case GL_DEPTH_COMPONENT32F: - clImgFormat.image_channel_data_type = CL_FLOAT; - clImgFormat.image_channel_order = CL_DEPTH; - break; - case GL_DEPTH_COMPONENT16: - clImgFormat.image_channel_data_type = CL_UNORM_INT16; - clImgFormat.image_channel_order = CL_DEPTH; - break; - case GL_DEPTH24_STENCIL8: - clImgFormat.image_channel_data_type = CL_UNORM_INT24; - clImgFormat.image_channel_order = CL_DEPTH_STENCIL; - break; - case GL_DEPTH32F_STENCIL8: - clImgFormat.image_channel_data_type = CL_FLOAT; - clImgFormat.image_channel_order = CL_DEPTH_STENCIL; - break; - case GL_SRGB8_ALPHA8: - clImgFormat.image_channel_data_type = CL_UNORM_INT8; - clImgFormat.image_channel_order = CL_sRGBA; - break; - case GL_RG8: - clImgFormat.image_channel_data_type = CL_UNORM_INT8; - clImgFormat.image_channel_order = CL_RG; - break; - case GL_RG8_SNORM: - clImgFormat.image_channel_data_type = CL_SNORM_INT8; - clImgFormat.image_channel_order = CL_RG; - break; - case GL_RG16: - clImgFormat.image_channel_data_type = CL_UNORM_INT16; - clImgFormat.image_channel_order = CL_RG; - break; - case GL_RG16_SNORM: - clImgFormat.image_channel_data_type = CL_SNORM_INT16; - clImgFormat.image_channel_order = CL_RG; - break; - case GL_RG16F: - clImgFormat.image_channel_data_type = CL_HALF_FLOAT; - clImgFormat.image_channel_order = CL_RG; - break; - case GL_RG32F: - clImgFormat.image_channel_data_type = CL_FLOAT; - clImgFormat.image_channel_order = CL_RG; - break; - case GL_RG8I: - clImgFormat.image_channel_data_type = CL_SIGNED_INT8; - clImgFormat.image_channel_order = CL_RG; - break; - case GL_RG16I: - clImgFormat.image_channel_data_type = CL_SIGNED_INT16; - clImgFormat.image_channel_order = CL_RG; - break; - case GL_RG32I: - clImgFormat.image_channel_data_type = CL_SIGNED_INT32; - clImgFormat.image_channel_order = CL_RG; - break; - case GL_RG8UI: - clImgFormat.image_channel_data_type = CL_UNSIGNED_INT8; - clImgFormat.image_channel_order = CL_RG; - break; - case GL_RG16UI: - clImgFormat.image_channel_data_type = CL_UNSIGNED_INT16; - clImgFormat.image_channel_order = CL_RG; - break; - case GL_RG32UI: - clImgFormat.image_channel_data_type = CL_UNSIGNED_INT32; - clImgFormat.image_channel_order = CL_RG; - break; - case GL_RGB10: - clImgFormat.image_channel_data_type = CL_UNORM_INT16; - clImgFormat.image_channel_order = CL_RGBA; - break; - default: - clImgFormat.image_channel_data_type = 0; - clImgFormat.image_channel_order = 0; - return false; + auto clFormat = GlSharing::gLToCLFormats.find(static_cast(glFormat)); + + if (clFormat != GlSharing::gLToCLFormats.end()) { + clImgFormat.image_channel_data_type = clFormat->second.image_channel_data_type; + clImgFormat.image_channel_order = clFormat->second.image_channel_order; + return true; } - return true; + return false; } } // namespace NEO diff --git a/runtime/sharings/gl/gl_sharing.cpp b/runtime/sharings/gl/gl_sharing.cpp index 3fb0b3d441..cf2be4a5c6 100644 --- a/runtime/sharings/gl/gl_sharing.cpp +++ b/runtime/sharings/gl/gl_sharing.cpp @@ -13,9 +13,57 @@ #include "runtime/sharings/gl/gl_arb_sync_event.h" #include "runtime/sharings/sharing_factory.h" +#include + namespace NEO { const uint32_t GLSharingFunctions::sharingId = SharingType::CLGL_SHARING; +const std::unordered_map GlSharing::gLToCLFormats = { + {GL_RGBA8, {CL_RGBA, CL_UNORM_INT8}}, + {GL_RGBA8I, {CL_RGBA, CL_SIGNED_INT8}}, + {GL_RGBA16, {CL_RGBA, CL_UNORM_INT16}}, + {GL_RGBA16I, {CL_RGBA, CL_SIGNED_INT16}}, + {GL_RGBA32I, {CL_RGBA, CL_SIGNED_INT32}}, + {GL_RGBA8UI, {CL_RGBA, CL_UNSIGNED_INT8}}, + {GL_RGBA16UI, {CL_RGBA, CL_UNSIGNED_INT16}}, + {GL_RGBA32UI, {CL_RGBA, CL_UNSIGNED_INT32}}, + {GL_RGBA16F, {CL_RGBA, CL_HALF_FLOAT}}, + {GL_RGBA32F, {CL_RGBA, CL_FLOAT}}, + {GL_RGBA, {CL_RGBA, CL_UNORM_INT8}}, + {GL_RGBA8_SNORM, {CL_RGBA, CL_SNORM_INT8}}, + {GL_RGBA16_SNORM, {CL_RGBA, CL_SNORM_INT16}}, + {GL_BGRA, {CL_BGRA, CL_UNORM_INT8}}, + {GL_R8, {CL_R, CL_UNORM_INT8}}, + {GL_R8_SNORM, {CL_R, CL_SNORM_INT8}}, + {GL_R16, {CL_R, CL_UNORM_INT16}}, + {GL_R16_SNORM, {CL_R, CL_SNORM_INT16}}, + {GL_R16F, {CL_R, CL_HALF_FLOAT}}, + {GL_R32F, {CL_R, CL_FLOAT}}, + {GL_R8I, {CL_R, CL_SIGNED_INT8}}, + {GL_R16I, {CL_R, CL_SIGNED_INT16}}, + {GL_R32I, {CL_R, CL_SIGNED_INT32}}, + {GL_R8UI, {CL_R, CL_UNSIGNED_INT8}}, + {GL_R16UI, {CL_R, CL_UNSIGNED_INT16}}, + {GL_R32UI, {CL_R, CL_UNSIGNED_INT32}}, + {GL_DEPTH_COMPONENT32F, {CL_DEPTH, CL_FLOAT}}, + {GL_DEPTH_COMPONENT16, {CL_DEPTH, CL_UNORM_INT16}}, + {GL_DEPTH24_STENCIL8, {CL_DEPTH_STENCIL, CL_UNORM_INT24}}, + {GL_DEPTH32F_STENCIL8, {CL_DEPTH_STENCIL, CL_FLOAT}}, + {GL_SRGB8_ALPHA8, {CL_sRGBA, CL_UNORM_INT8}}, + {GL_RG8, {CL_RG, CL_UNORM_INT8}}, + {GL_RG8_SNORM, {CL_RG, CL_SNORM_INT8}}, + {GL_RG16, {CL_RG, CL_UNORM_INT16}}, + {GL_RG16_SNORM, {CL_RG, CL_SNORM_INT16}}, + {GL_RG16F, {CL_RG, CL_HALF_FLOAT}}, + {GL_RG32F, {CL_RG, CL_FLOAT}}, + {GL_RG8I, {CL_RG, CL_SIGNED_INT8}}, + {GL_RG16I, {CL_RG, CL_SIGNED_INT16}}, + {GL_RG32I, {CL_RG, CL_SIGNED_INT32}}, + {GL_RG8UI, {CL_RG, CL_UNSIGNED_INT8}}, + {GL_RG16UI, {CL_RG, CL_UNSIGNED_INT16}}, + {GL_RG32UI, {CL_RG, CL_UNSIGNED_INT32}}, + {GL_RGB10, {CL_RGBA, CL_UNORM_INT16}}}; + int GlSharing::synchronizeHandler(UpdateData &updateData) { GLContextGuard guard(*sharingFunctions); synchronizeObject(updateData); diff --git a/runtime/sharings/gl/gl_sharing.h b/runtime/sharings/gl/gl_sharing.h index 5d4f58b8c7..a0666fbc98 100644 --- a/runtime/sharings/gl/gl_sharing.h +++ b/runtime/sharings/gl/gl_sharing.h @@ -97,6 +97,12 @@ class GLSharingFunctions : public SharingFunctions { static bool isGlSharingEnabled(); + static cl_int getSupportedFormats(cl_mem_flags flags, + cl_mem_object_type imageType, + size_t numEntries, + cl_GLenum *formats, + uint32_t *numImageFormats); + GLboolean setSharedOCLContextState() { ContextInfo CtxInfo = {0}; GLboolean retVal = GLSetSharedOCLContextState(GLHDCHandle, GLHGLRCHandle, CL_TRUE, &CtxInfo); @@ -263,6 +269,7 @@ class GlSharing : public SharingHandler { *pClGlObjectId = clGlObjectId; } } + static const std::unordered_map gLToCLFormats; protected: int synchronizeHandler(UpdateData &updateData) override; diff --git a/unit_tests/sharings/gl/gl_sharing_tests.cpp b/unit_tests/sharings/gl/gl_sharing_tests.cpp index a827502c07..16c0f90e32 100644 --- a/unit_tests/sharings/gl/gl_sharing_tests.cpp +++ b/unit_tests/sharings/gl/gl_sharing_tests.cpp @@ -9,6 +9,7 @@ #include "runtime/device/device.h" #include "runtime/event/user_event.h" #include "runtime/gmm_helper/gmm.h" +#include "runtime/helpers/array_count.h" #include "runtime/mem_obj/buffer.h" #include "runtime/mem_obj/image.h" #include "runtime/os_interface/os_interface.h" @@ -762,6 +763,112 @@ TEST(glSharingBasicTest, givenGlSharingFunctionsWhenItIsConstructedThenFunctions EXPECT_NE(nullptr, glSharingFunctions.GLGetSynciv); EXPECT_NE(nullptr, glSharingFunctions.glGetStringi); } + +TEST(glSharingBasicTest, givenNumEntriesLowerThanSupportedFormatsWhenGettingSupportedFormatsThenOnlyNumEntiresAreReturned) { + MockGLSharingFunctions glSharingFunctions; + cl_mem_flags flags = CL_MEM_READ_WRITE; + cl_mem_object_type image_type = CL_MEM_OBJECT_IMAGE2D; + cl_uint numImageFormats = 0; + cl_GLenum glFormats[3] = {}; + + auto retVal = glSharingFunctions.getSupportedFormats(flags, image_type, 1, glFormats, &numImageFormats); + + EXPECT_EQ(CL_SUCCESS, retVal); + + EXPECT_EQ(static_cast(GlSharing::gLToCLFormats.size()), numImageFormats); + EXPECT_NE(0u, glFormats[0]); + EXPECT_EQ(0u, glFormats[1]); + EXPECT_EQ(0u, glFormats[2]); +} + +TEST(glSharingBasicTest, givenCorrectFlagsWhenGettingSupportedFormatsThenCorrectListIsReturned) { + MockGLSharingFunctions glSharingFunctions; + cl_mem_flags flags[] = {CL_MEM_READ_ONLY, CL_MEM_WRITE_ONLY, CL_MEM_READ_WRITE, CL_MEM_KERNEL_READ_AND_WRITE}; + cl_mem_object_type image_type = CL_MEM_OBJECT_IMAGE2D; + cl_GLenum glFormats[3] = {}; + cl_uint numImageFormats = 0; + + for (size_t i = 0; i < arrayCount(flags); i++) { + + auto result = glSharingFunctions.getSupportedFormats(flags[i], image_type, arrayCount(glFormats), glFormats, &numImageFormats); + + EXPECT_EQ(CL_SUCCESS, result); + EXPECT_EQ(static_cast(GlSharing::gLToCLFormats.size()), numImageFormats); + + for (uint32_t formatIndex = 0; formatIndex < arrayCount(glFormats); formatIndex++) { + EXPECT_NE(GlSharing::gLToCLFormats.end(), GlSharing::gLToCLFormats.find(glFormats[formatIndex])); + } + } +} + +TEST(glSharingBasicTest, givenSupportedImageTypesWhenGettingSupportedFormatsThenCorrectListIsReturned) { + MockGLSharingFunctions glSharingFunctions; + cl_mem_flags flags = CL_MEM_READ_WRITE; + cl_mem_object_type image_types[] = {CL_MEM_OBJECT_IMAGE1D, CL_MEM_OBJECT_IMAGE2D, CL_MEM_OBJECT_IMAGE3D, CL_MEM_OBJECT_IMAGE1D_ARRAY, CL_MEM_OBJECT_IMAGE1D_BUFFER}; + cl_GLenum glFormats[3] = {}; + cl_uint numImageFormats = 0; + + for (size_t i = 0; i < arrayCount(image_types); i++) { + + auto result = glSharingFunctions.getSupportedFormats(flags, image_types[i], arrayCount(glFormats), glFormats, &numImageFormats); + + EXPECT_EQ(CL_SUCCESS, result); + EXPECT_EQ(static_cast(GlSharing::gLToCLFormats.size()), numImageFormats); + + for (uint32_t formatIndex = 0; formatIndex < arrayCount(glFormats); formatIndex++) { + EXPECT_NE(GlSharing::gLToCLFormats.end(), GlSharing::gLToCLFormats.find(glFormats[formatIndex])); + } + } +} + +TEST(glSharingBasicTest, givenZeroNumEntriesWhenGettingSupportedFormatsThenNumFormatsIsReturned) { + MockGLSharingFunctions glSharingFunctions; + cl_mem_flags flags = CL_MEM_READ_WRITE; + cl_mem_object_type image_type = CL_MEM_OBJECT_IMAGE2D; + cl_uint numImageFormats = 0; + + auto result = glSharingFunctions.getSupportedFormats(flags, image_type, 0, nullptr, &numImageFormats); + + EXPECT_EQ(CL_SUCCESS, result); + EXPECT_EQ(static_cast(GlSharing::gLToCLFormats.size()), numImageFormats); +} + +TEST(glSharingBasicTest, givenNullNumImageFormatsWhenGettingSupportedFormatsThenNumFormatsIsNotDereferenced) { + MockGLSharingFunctions glSharingFunctions; + cl_mem_flags flags = CL_MEM_READ_WRITE; + cl_mem_object_type image_type = CL_MEM_OBJECT_IMAGE2D; + + auto result = glSharingFunctions.getSupportedFormats(flags, image_type, 0, nullptr, nullptr); + + EXPECT_EQ(CL_SUCCESS, result); +} + +TEST(glSharingBasicTest, givenInvalidImageTypeWhenGettingSupportedFormatsThenIvalidValueErrorIsReturned) { + MockGLSharingFunctions glSharingFunctions; + cl_mem_flags flags = CL_MEM_READ_WRITE; + cl_mem_object_type image_type = CL_MEM_OBJECT_IMAGE2D_ARRAY; + cl_GLenum glFormats[3] = {}; + cl_uint numImageFormats = 0; + + auto result = glSharingFunctions.getSupportedFormats(flags, image_type, arrayCount(glFormats), glFormats, &numImageFormats); + + EXPECT_EQ(CL_INVALID_VALUE, result); + EXPECT_EQ(0u, numImageFormats); +} + +TEST(glSharingBasicTest, givenInvalidFlagsWhenGettingSupportedFormatsThenIvalidValueErrorIsReturned) { + MockGLSharingFunctions glSharingFunctions; + cl_mem_flags flags = CL_MEM_NO_ACCESS_INTEL; + cl_mem_object_type image_type = CL_MEM_OBJECT_IMAGE2D; + cl_GLenum glFormats[3] = {}; + cl_uint numImageFormats = 0; + + auto result = glSharingFunctions.getSupportedFormats(flags, image_type, arrayCount(glFormats), glFormats, &numImageFormats); + + EXPECT_EQ(CL_INVALID_VALUE, result); + EXPECT_EQ(0u, numImageFormats); +} + TEST_F(glSharingTests, givenContextWhenCreateFromSharedBufferThenSharedImageIsReturned) { auto retVal = CL_SUCCESS; auto glBuffer = clCreateFromGLBuffer(&context, 0, bufferId, &retVal);