| 1 | /* |
| 2 | * Copyright (C) 2018-2025 Intel Corporation |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "shared/test/common/helpers/gtest_helpers.h" |
| 9 | |
| 10 | #include "opencl/source/os_interface/windows/d3d_sharing_functions.h" |
| 11 | #include "opencl/source/sharings/d3d/cl_d3d_api.h" |
| 12 | #include "opencl/test/unit_test/api/cl_api_tests.h" |
| 13 | #include "opencl/test/unit_test/mocks/mock_context.h" |
| 14 | |
| 15 | #include "gtest/gtest.h" |
| 16 | |
| 17 | using namespace NEO; |
| 18 | |
| 19 | TEST(D3DContextTest, WhenContextIsCreatedThenSharingIsNotPresent) { |
| 20 | MockContext context; |
| 21 | |
| 22 | EXPECT_EQ(nullptr, context.getSharing<D3DSharingFunctions<D3DTypesHelper::D3D9>>()); |
| 23 | EXPECT_EQ(nullptr, context.getSharing<D3DSharingFunctions<D3DTypesHelper::D3D10>>()); |
| 24 | EXPECT_EQ(nullptr, context.getSharing<D3DSharingFunctions<D3DTypesHelper::D3D11>>()); |
| 25 | } |
| 26 | |
| 27 | TEST(D3DContextTest, givenDispatchTableThenItContainsValidEntries) { |
| 28 | sharingFactory.fillGlobalDispatchTable(); |
| 29 | |
| 30 | MockContext context; |
| 31 | |
| 32 | EXPECT_EQ(&clGetDeviceIDsFromDX9INTEL, context.dispatch.crtDispatch->clGetDeviceIDsFromDX9INTEL); |
| 33 | EXPECT_EQ(&clCreateFromDX9MediaSurfaceINTEL, context.dispatch.crtDispatch->clCreateFromDX9MediaSurfaceINTEL); |
| 34 | EXPECT_EQ(&clEnqueueAcquireDX9ObjectsINTEL, context.dispatch.crtDispatch->clEnqueueAcquireDX9ObjectsINTEL); |
| 35 | EXPECT_EQ(&clEnqueueReleaseDX9ObjectsINTEL, context.dispatch.crtDispatch->clEnqueueReleaseDX9ObjectsINTEL); |
| 36 | } |
| 37 | |
| 38 | struct ClIntelSharingFormatQueryDX9 : public ApiTests { |
| 39 | std::vector<D3DFORMAT> supportedNonPlanarFormats; |
| 40 | std::vector<D3DFORMAT> supportedPlanarFormats; |
| 41 | std::vector<D3DFORMAT> supportedPlane1Formats; |
| 42 | std::vector<D3DFORMAT> supportedPlane2Formats; |
| 43 | std::vector<D3DFORMAT> retrievedFormats; |
| 44 | |
| 45 | cl_uint numImageFormats; |
| 46 | |
| 47 | void SetUp() override { |
| 48 | ApiTests::SetUp(); |
| 49 | supportedNonPlanarFormats = {D3DFMT_R32F, D3DFMT_R16F, D3DFMT_L16, D3DFMT_A8, |
| 50 | D3DFMT_L8, D3DFMT_G32R32F, D3DFMT_G16R16F, D3DFMT_G16R16, |
| 51 | D3DFMT_A8L8, D3DFMT_A32B32G32R32F, D3DFMT_A16B16G16R16F, D3DFMT_A16B16G16R16, |
| 52 | D3DFMT_A8B8G8R8, D3DFMT_X8B8G8R8, D3DFMT_A8R8G8B8, D3DFMT_X8R8G8B8}; |
| 53 | |
| 54 | // Silence all checks for FOURCC codes out of range of D3DFORMAT |
| 55 | // format are supplied by vendor and many codes are not listed in enum |
| 56 | // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) |
| 57 | supportedPlanarFormats = {D3DFMT_YUY2, D3DFMT_UYVY, |
| 58 | static_cast<D3DFORMAT>(MAKEFOURCC('N', 'V', '1', '2')), |
| 59 | static_cast<D3DFORMAT>(MAKEFOURCC('Y', 'V', '1', '2')), |
| 60 | static_cast<D3DFORMAT>(MAKEFOURCC('Y', 'V', 'Y', 'U')), |
| 61 | static_cast<D3DFORMAT>(MAKEFOURCC('V', 'Y', 'U', 'Y'))}; |
| 62 | |
| 63 | supportedPlane1Formats = {static_cast<D3DFORMAT>(MAKEFOURCC('N', 'V', '1', '2')), |
| 64 | static_cast<D3DFORMAT>(MAKEFOURCC('Y', 'V', '1', '2'))}; |
| 65 | |
| 66 | supportedPlane2Formats = {static_cast<D3DFORMAT>(MAKEFOURCC('Y', 'V', '1', '2'))}; |
| 67 | // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) |
| 68 | retrievedFormats.assign(supportedNonPlanarFormats.size() + supportedPlanarFormats.size(), D3DFMT_UNKNOWN); |
| 69 | } |
| 70 | |
| 71 | void TearDown() override { |
| 72 | ApiTests::TearDown(); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | namespace ULT { |
| 77 | |
| 78 | TEST_F(ClIntelSharingFormatQueryDX9, givenInvalidContextWhenMediaSurfaceFormatsRequestedThenInvalidContextError) { |
| 79 | retVal = clGetSupportedDX9MediaSurfaceFormatsINTEL(nullptr, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, 0, |
| 80 | static_cast<cl_uint>(retrievedFormats.size()), |
| 81 | &retrievedFormats[0], |
| 82 | &numImageFormats); |
| 83 | EXPECT_EQ(CL_INVALID_CONTEXT, retVal); |
| 84 | } |
| 85 | |
| 86 | TEST_F(ClIntelSharingFormatQueryDX9, givenInvalidFlagsWhenMediaSurfaceFormatsRequestedThenInvalidValueError) { |
| 87 | retVal = clGetSupportedDX9MediaSurfaceFormatsINTEL( |
| 88 | pContext, 0, CL_MEM_OBJECT_IMAGE2D, 0, |
| 89 | static_cast<cl_uint>(retrievedFormats.size()), |
| 90 | &retrievedFormats[0], &numImageFormats); |
| 91 | EXPECT_EQ(CL_INVALID_VALUE, retVal); |
| 92 | } |
| 93 | |
| 94 | TEST_F(ClIntelSharingFormatQueryDX9, givenInvalidImageTypeWhenMediaSurfaceFormatsRequestedThenInvalidValueError) { |
| 95 | retVal = clGetSupportedDX9MediaSurfaceFormatsINTEL(pContext, CL_MEM_READ_WRITE, 0, 0, static_cast<cl_uint>(retrievedFormats.size()), |
| 96 | &retrievedFormats[0], &numImageFormats); |
| 97 | EXPECT_EQ(CL_INVALID_VALUE, retVal); |
| 98 | } |
| 99 | |
| 100 | TEST_F(ClIntelSharingFormatQueryDX9, givenValidParametersWhenRequestedMediaSurfaceFormatsBelowMaximumThenExceedingFormatAreaRemainsUntouched) { |
| 101 | for (cl_uint i = 0; i <= static_cast<cl_uint>(retrievedFormats.size()); ++i) { |
| 102 | retVal = clGetSupportedDX9MediaSurfaceFormatsINTEL(pContext, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, 0, i, |
| 103 | &retrievedFormats[0], &numImageFormats); |
| 104 | EXPECT_EQ(retVal, CL_SUCCESS); |
| 105 | for (cl_uint j = i; j < retrievedFormats.size(); ++j) { |
| 106 | EXPECT_EQ(retrievedFormats[j], D3DFMT_UNKNOWN); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | TEST_F(ClIntelSharingFormatQueryDX9, givenValidParametersWhenRequestingMediaSurfaceFormatsForPlane0ThenAllKnownFormatsAreIncludedInTheResult) { |
| 112 | retVal = clGetSupportedDX9MediaSurfaceFormatsINTEL(pContext, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, 0, |
| 113 | static_cast<cl_uint>(retrievedFormats.size()), |
| 114 | &retrievedFormats[0], |
| 115 | &numImageFormats); |
| 116 | EXPECT_EQ(retVal, CL_SUCCESS); |
| 117 | |
| 118 | EXPECT_EQ(supportedNonPlanarFormats.size() + supportedPlanarFormats.size(), numImageFormats); |
| 119 | |
| 120 | for (auto format : supportedNonPlanarFormats) { |
| 121 | auto found = std::find(retrievedFormats.begin(), retrievedFormats.end(), format); |
| 122 | EXPECT_NE(found, retrievedFormats.end()); |
| 123 | } |
| 124 | |
| 125 | for (auto format : supportedPlanarFormats) { |
| 126 | auto found = std::find(retrievedFormats.begin(), retrievedFormats.end(), format); |
| 127 | EXPECT_NE(found, retrievedFormats.end()); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | TEST_F(ClIntelSharingFormatQueryDX9, givenValidParametersWhenRequestingMediaSurfaceFormatsForPlane1ThenOnlyPlanarFormatsAreIncludedInTheResult) { |
| 132 | retVal = clGetSupportedDX9MediaSurfaceFormatsINTEL(pContext, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, 1, |
| 133 | static_cast<cl_uint>(retrievedFormats.size()), |
| 134 | &retrievedFormats[0], |
| 135 | &numImageFormats); |
| 136 | ASSERT_EQ(retVal, CL_SUCCESS); |
| 137 | |
| 138 | EXPECT_EQ(supportedPlane1Formats.size(), numImageFormats); |
| 139 | |
| 140 | for (auto format : supportedPlane1Formats) { |
| 141 | auto found = std::find(retrievedFormats.begin(), retrievedFormats.end(), format); |
| 142 | EXPECT_NE(found, retrievedFormats.end()); |
| 143 | } |
| 144 | |
| 145 | for (auto format : supportedNonPlanarFormats) { |
| 146 | auto found = std::find(retrievedFormats.begin(), retrievedFormats.end(), format); |
| 147 | EXPECT_EQ(found, retrievedFormats.end()); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | TEST_F(ClIntelSharingFormatQueryDX9, givenValidParametersWhenRequestingMediaSurfaceFormatsForPlane2ThenOnlyYV12FormatIsIncludedInTheResult) { |
| 152 | retVal = clGetSupportedDX9MediaSurfaceFormatsINTEL(pContext, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, 2, |
| 153 | static_cast<cl_uint>(retrievedFormats.size()), |
| 154 | &retrievedFormats[0], |
| 155 | &numImageFormats); |
| 156 | ASSERT_EQ(retVal, CL_SUCCESS); |
| 157 | EXPECT_EQ(supportedPlane2Formats.size(), numImageFormats); |
| 158 | |
| 159 | for (auto format : supportedPlane2Formats) { |
| 160 | auto found = std::find(retrievedFormats.begin(), retrievedFormats.end(), format); |
| 161 | EXPECT_NE(found, retrievedFormats.end()); |
| 162 | } |
| 163 | |
| 164 | for (auto format : supportedNonPlanarFormats) { |
| 165 | auto found = std::find(retrievedFormats.begin(), retrievedFormats.end(), format); |
| 166 | EXPECT_EQ(found, retrievedFormats.end()); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | TEST_F(ClIntelSharingFormatQueryDX9, givenValidParametersWhenRequestingMediaSurfaceFormatsForPlaneGraterThan2ThenZeroNumFormatsIsReturned) { |
| 171 | retVal = clGetSupportedDX9MediaSurfaceFormatsINTEL(pContext, CL_MEM_READ_WRITE, CL_MEM_OBJECT_IMAGE2D, 3, |
| 172 | 0, |
| 173 | nullptr, |
| 174 | &numImageFormats); |
| 175 | EXPECT_EQ(retVal, CL_SUCCESS); |
| 176 | EXPECT_EQ(0u, numImageFormats); |
| 177 | } |
| 178 | } // namespace ULT |