Debian Codegraph @

opencl/source/sharings/d3d/d3d_texture.cpp

raw
1 /*
2 * Copyright (C) 2018-2024 Intel Corporation
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8 #include "opencl/source/sharings/d3d/d3d_texture.h"
9
10 #include "shared/source/execution_environment/execution_environment.h"
11 #include "shared/source/execution_environment/root_device_environment.h"
12 #include "shared/source/gmm_helper/gmm.h"
13 #include "shared/source/gmm_helper/resource_info.h"
14 #include "shared/source/helpers/get_info.h"
15 #include "shared/source/helpers/gfx_core_helper.h"
16 #include "shared/source/helpers/hw_info.h"
17 #include "shared/source/memory_manager/allocation_properties.h"
18 #include "shared/source/memory_manager/memory_manager.h"
19 #include "shared/source/os_interface/product_helper.h"
20
21 #include "opencl/source/cl_device/cl_device.h"
22 #include "opencl/source/context/context.h"
23 #include "opencl/source/helpers/gmm_types_converter.h"
24 #include "opencl/source/mem_obj/image.h"
25
26 using namespace NEO;
27
28 template <typename D3D>
29 Image *D3DTexture<D3D>::create2d(Context *context, D3DTexture2d *d3dTexture, cl_mem_flags flags, cl_uint subresource, cl_int *retCode) {
30 ErrorCodeHelper err(retCode, CL_SUCCESS);
31 auto sharingFcns = context->getSharing<D3DSharingFunctions<D3D>>();
32 ImagePlane imagePlane = ImagePlane::noPlane;
33 void *sharedHandle = nullptr;
34 cl_uint arrayIndex = 0u;
35 McsSurfaceInfo mcsSurfaceInfo = {};
36 ImageInfo imgInfo = {};
37 imgInfo.imgDesc.imageType = ImageType::image2D;
38
39 D3DTexture2dDesc textureDesc = {};
40 sharingFcns->getTexture2dDesc(&textureDesc, d3dTexture);
41
42 cl_int formatSupportError = sharingFcns->validateFormatSupport(textureDesc.Format, CL_MEM_OBJECT_IMAGE2D);
43 if (formatSupportError != CL_SUCCESS) {
44 err.set(formatSupportError);
45 return nullptr;
46 }
47
48 if (D3DSharing<D3D>::isFormatWithPlane1(textureDesc.Format)) {
49 if ((subresource % 2) == 0) {
50 imagePlane = ImagePlane::planeY;
51 } else {
52 imagePlane = ImagePlane::planeUV;
53 }
54 imgInfo.plane = GmmTypesConverter::convertPlane(imagePlane);
55 arrayIndex = subresource / 2u;
56 } else if (subresource >= textureDesc.MipLevels * textureDesc.ArraySize) {
57 err.set(CL_INVALID_VALUE);
58 return nullptr;
59 }
60
61 bool sharedResource = false;
62 D3DTexture2d *textureStaging = nullptr;
63 if ((textureDesc.MiscFlags & D3DResourceFlags::MISC_SHARED ||
64 textureDesc.MiscFlags & D3DResourceFlags::MISC_SHARED_KEYEDMUTEX) &&
65 subresource % textureDesc.MipLevels == 0) {
66 textureStaging = d3dTexture;
67 sharedResource = true;
68 } else {
69 sharingFcns->createTexture2d(&textureStaging, &textureDesc, subresource);
70 }
71
72 GraphicsAllocation *alloc = nullptr;
73 auto memoryManager = context->getMemoryManager();
74 auto rootDeviceIndex = context->getDevice(0)->getRootDeviceIndex();
75
76 bool ntHandle = textureDesc.MiscFlags & D3DResourceFlags::MISC_SHARED_NTHANDLE;
77 ntHandle ? sharingFcns->getSharedNTHandle(textureStaging, &sharedHandle) : sharingFcns->getSharedHandle(textureStaging, &sharedHandle);
78 AllocationProperties allocProperties(rootDeviceIndex,
79 false, // allocateMemory
80 0u, // size
81 AllocationType::sharedImage,
82 false, // isMultiStorageAllocation
83 context->getDeviceBitfieldForAllocation(rootDeviceIndex));
84 if (memoryManager->verifyHandle(toOsHandle(sharedHandle), rootDeviceIndex, ntHandle)) {
85 MemoryManager::OsHandleData osHandleData{sharedHandle, arrayIndex};
86 alloc = memoryManager->createGraphicsAllocationFromSharedHandle(osHandleData, allocProperties, false, false, true, nullptr);
87 } else {
88 err.set(CL_INVALID_D3D11_RESOURCE_KHR);
89 return nullptr;
90 }
91
92 if (alloc == nullptr) {
93 err.set(CL_OUT_OF_HOST_MEMORY);
94 return nullptr;
95 }
96
97 D3DSharing<D3D>::updateImgInfoAndDesc(alloc->getDefaultGmm(), imgInfo, imagePlane, arrayIndex);
98
99 auto d3dTextureObj = new D3DTexture<D3D>(context, d3dTexture, subresource, textureStaging, sharedResource);
100
101 auto &executionEnvironment = memoryManager->peekExecutionEnvironment();
102 auto &rootDeviceEnvironment = *executionEnvironment.rootDeviceEnvironments[rootDeviceIndex];
103 auto hwInfo = rootDeviceEnvironment.getHardwareInfo();
104 auto &gfxCoreHelper = rootDeviceEnvironment.getHelper<GfxCoreHelper>();
105 const ClSurfaceFormatInfo *clSurfaceFormat = nullptr;
106 if ((textureDesc.Format == DXGI_FORMAT_NV12) || (textureDesc.Format == DXGI_FORMAT_P010) || (textureDesc.Format == DXGI_FORMAT_P016)) {
107 clSurfaceFormat = findYuvSurfaceFormatInfo(textureDesc.Format, imagePlane, flags);
108 imgInfo.surfaceFormat = &clSurfaceFormat->surfaceFormat;
109 } else {
110 clSurfaceFormat = D3DSharing<D3D>::findSurfaceFormatInfo(alloc->getDefaultGmm()->gmmResourceInfo->getResourceFormat(), flags, hwInfo->capabilityTable.supportsOcl21Features, gfxCoreHelper.packedFormatsSupported());
111 imgInfo.surfaceFormat = &clSurfaceFormat->surfaceFormat;
112 }
113
114 if (alloc->getDefaultGmm()->unifiedAuxTranslationCapable()) {
115 const auto &productHelper = rootDeviceEnvironment.getHelper<ProductHelper>();
116 alloc->getDefaultGmm()->setCompressionEnabled(productHelper.isPageTableManagerSupported(*hwInfo) ? memoryManager->mapAuxGpuVA(alloc)
117 : true);
118 }
119 auto multiGraphicsAllocation = MultiGraphicsAllocation(rootDeviceIndex);
120 multiGraphicsAllocation.addAllocation(alloc);
121
122 return Image::createSharedImage(context, d3dTextureObj, mcsSurfaceInfo, std::move(multiGraphicsAllocation), nullptr, flags, 0, clSurfaceFormat, imgInfo, __GMM_NO_CUBE_MAP, 0, 0);
123 }
124
125 template <typename D3D>
126 Image *D3DTexture<D3D>::create3d(Context *context, D3DTexture3d *d3dTexture, cl_mem_flags flags, cl_uint subresource, cl_int *retCode) {
127 ErrorCodeHelper err(retCode, CL_SUCCESS);
128 auto sharingFcns = context->getSharing<D3DSharingFunctions<D3D>>();
129 void *sharedHandle = nullptr;
130 McsSurfaceInfo mcsSurfaceInfo = {};
131 ImageInfo imgInfo = {};
132 imgInfo.imgDesc.imageType = ImageType::image3D;
133
134 D3DTexture3dDesc textureDesc = {};
135 sharingFcns->getTexture3dDesc(&textureDesc, d3dTexture);
136
137 cl_int formatSupportError = sharingFcns->validateFormatSupport(textureDesc.Format, CL_MEM_OBJECT_IMAGE3D);
138 if (formatSupportError != CL_SUCCESS) {
139 err.set(formatSupportError);
140 return nullptr;
141 }
142
143 if (subresource >= textureDesc.MipLevels) {
144 err.set(CL_INVALID_VALUE);
145 return nullptr;
146 }
147
148 bool sharedResource = false;
149 D3DTexture3d *textureStaging = nullptr;
150 if ((textureDesc.MiscFlags & D3DResourceFlags::MISC_SHARED || textureDesc.MiscFlags & D3DResourceFlags::MISC_SHARED_KEYEDMUTEX) &&
151 subresource == 0) {
152 textureStaging = d3dTexture;
153 sharedResource = true;
154 } else {
155 sharingFcns->createTexture3d(&textureStaging, &textureDesc, subresource);
156 }
157
158 GraphicsAllocation *alloc = nullptr;
159 auto memoryManager = context->getMemoryManager();
160 auto rootDeviceIndex = context->getDevice(0)->getRootDeviceIndex();
161
162 bool ntHandle = textureDesc.MiscFlags & D3DResourceFlags::MISC_SHARED_NTHANDLE;
163 ntHandle ? sharingFcns->getSharedNTHandle(textureStaging, &sharedHandle) : sharingFcns->getSharedHandle(textureStaging, &sharedHandle);
164 AllocationProperties allocProperties(rootDeviceIndex,
165 false, // allocateMemory
166 0u, // size
167 AllocationType::sharedImage,
168 false, // isMultiStorageAllocation
169 context->getDeviceBitfieldForAllocation(rootDeviceIndex));
170 if (memoryManager->verifyHandle(toOsHandle(sharedHandle), rootDeviceIndex, ntHandle)) {
171 MemoryManager::OsHandleData osHandleData{sharedHandle};
172 alloc = memoryManager->createGraphicsAllocationFromSharedHandle(osHandleData, allocProperties, false, false, true, nullptr);
173 } else {
174 err.set(CL_INVALID_D3D11_RESOURCE_KHR);
175 return nullptr;
176 }
177
178 bool is3DUavOrRtv = false;
179 if (textureDesc.BindFlags & D3DBindFLags::D3D11_BIND_RENDER_TARGET || textureDesc.BindFlags & D3DBindFLags::D3D11_BIND_UNORDERED_ACCESS) {
180 is3DUavOrRtv = true;
181 }
182 if (alloc == nullptr) {
183 err.set(CL_OUT_OF_HOST_MEMORY);
184 return nullptr;
185 }
186
187 D3DSharing<D3D>::updateImgInfoAndDesc(alloc->getDefaultGmm(), imgInfo, ImagePlane::noPlane, 0u);
188
189 auto &executionEnvironment = memoryManager->peekExecutionEnvironment();
190 auto &rootDeviceEnvironment = *executionEnvironment.rootDeviceEnvironments[rootDeviceIndex];
191 auto hwInfo = rootDeviceEnvironment.getHardwareInfo();
192 auto &gfxCoreHelper = rootDeviceEnvironment.getHelper<GfxCoreHelper>();
193 auto d3dTextureObj = new D3DTexture<D3D>(context, d3dTexture, subresource, textureStaging, sharedResource);
194 auto *clSurfaceFormat = D3DSharing<D3D>::findSurfaceFormatInfo(alloc->getDefaultGmm()->gmmResourceInfo->getResourceFormat(), flags, hwInfo->capabilityTable.supportsOcl21Features, gfxCoreHelper.packedFormatsSupported());
195 imgInfo.qPitch = alloc->getDefaultGmm()->queryQPitch(GMM_RESOURCE_TYPE::RESOURCE_3D);
196
197 imgInfo.surfaceFormat = &clSurfaceFormat->surfaceFormat;
198
199 if (alloc->getDefaultGmm()->unifiedAuxTranslationCapable()) {
200 const auto &productHelper = rootDeviceEnvironment.getHelper<ProductHelper>();
201 alloc->getDefaultGmm()->setCompressionEnabled(productHelper.isPageTableManagerSupported(*hwInfo) ? memoryManager->mapAuxGpuVA(alloc)
202 : true);
203 }
204 auto multiGraphicsAllocation = MultiGraphicsAllocation(rootDeviceIndex);
205 multiGraphicsAllocation.addAllocation(alloc);
206
207 auto image = Image::createSharedImage(context, d3dTextureObj, mcsSurfaceInfo, std::move(multiGraphicsAllocation), nullptr, flags, 0, clSurfaceFormat, imgInfo, __GMM_NO_CUBE_MAP, 0, 0);
208 image->setAs3DUavOrRtvImage(is3DUavOrRtv);
209 return image;
210 }
211
212 template <typename D3D>
213 const ClSurfaceFormatInfo *D3DTexture<D3D>::findYuvSurfaceFormatInfo(DXGI_FORMAT dxgiFormat, ImagePlane imagePlane, cl_mem_flags flags) {
214 cl_image_format imgFormat = {};
215 if (imagePlane == ImagePlane::planeY) {
216 imgFormat.image_channel_order = CL_R;
217 } else {
218 imgFormat.image_channel_order = CL_RG;
219 }
220 if ((dxgiFormat == DXGI_FORMAT_P010) || (dxgiFormat == DXGI_FORMAT_P016)) {
221 imgFormat.image_channel_data_type = CL_UNORM_INT16;
222 } else {
223 imgFormat.image_channel_data_type = CL_UNORM_INT8;
224 }
225
226 return Image::getSurfaceFormatFromTable(flags, &imgFormat, false /* supportsOcl20Features */);
227 }
228
229 template class NEO::D3DTexture<D3DTypesHelper::D3D10>;
230 template class NEO::D3DTexture<D3DTypesHelper::D3D11>;