Debian Codegraph @

opencl/source/sharings/d3d/d3d_surface.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_surface.h"
9
10 #include "shared/source/gmm_helper/gmm.h"
11 #include "shared/source/helpers/get_info.h"
12 #include "shared/source/helpers/hw_info.h"
13 #include "shared/source/memory_manager/allocation_properties.h"
14 #include "shared/source/memory_manager/memory_manager.h"
15
16 #include "opencl/source/cl_device/cl_device.h"
17 #include "opencl/source/context/context.h"
18 #include "opencl/source/helpers/cl_memory_properties_helpers.h"
19 #include "opencl/source/helpers/gmm_types_converter.h"
20 #include "opencl/source/mem_obj/image.h"
21 #include "opencl/source/mem_obj/mem_obj_helper.h"
22
23 #include "mmsystem.h"
24
25 using namespace NEO;
26
27 D3DSurface::D3DSurface(Context *context, cl_dx9_surface_info_khr *surfaceInfo, D3D9Surface *surfaceStaging, cl_uint plane,
28 ImagePlane imagePlane, cl_dx9_media_adapter_type_khr adapterType, bool sharedResource, bool lockable)
29 : D3DSharing(context, surfaceInfo->resource, surfaceStaging, plane, sharedResource), lockable(lockable), adapterType(adapterType),
30 surfaceInfo(*surfaceInfo), plane(plane), imagePlane(imagePlane), d3d9Surface(surfaceInfo->resource),
31 d3d9SurfaceStaging(surfaceStaging) {
32 if (sharingFunctions) {
33 resourceDevice = sharingFunctions->getDevice();
34 }
35 };
36
37 Image *D3DSurface::create(Context *context, cl_dx9_surface_info_khr *surfaceInfo, cl_mem_flags flags,
38 cl_dx9_media_adapter_type_khr adapterType, cl_uint plane, cl_int *retCode) {
39 ErrorCodeHelper err(retCode, CL_SUCCESS);
40 D3D9Surface *surfaceStaging = nullptr;
41 ImageInfo imgInfo = {};
42 cl_image_format imgFormat = {};
43 McsSurfaceInfo mcsSurfaceInfo = {};
44 ImagePlane imagePlane = ImagePlane::noPlane;
45
46 if (!context || !context->getSharing<D3DSharingFunctions<D3DTypesHelper::D3D9>>() || !context->getSharing<D3DSharingFunctions<D3DTypesHelper::D3D9>>()->getDevice()) {
47 err.set(CL_INVALID_CONTEXT);
48 return nullptr;
49 }
50 auto sharingFcns = context->getSharing<D3DSharingFunctions<D3DTypesHelper::D3D9>>();
51 if (sharingFcns->isTracked(surfaceInfo->resource, plane)) {
52 err.set(CL_INVALID_DX9_RESOURCE_INTEL);
53 return nullptr;
54 }
55
56 sharingFcns->updateDevice(surfaceInfo->resource);
57
58 imgInfo.imgDesc.imageType = ImageType::image2D;
59
60 D3D9SurfaceDesc surfaceDesc = {};
61 sharingFcns->getTexture2dDesc(&surfaceDesc, surfaceInfo->resource);
62 imgInfo.imgDesc.imageWidth = surfaceDesc.Width;
63 imgInfo.imgDesc.imageHeight = surfaceDesc.Height;
64
65 if (surfaceDesc.Pool != D3DPOOL_DEFAULT) {
66 err.set(CL_INVALID_DX9_RESOURCE_INTEL);
67 return nullptr;
68 }
69
70 err.set(findImgFormat(surfaceDesc.Format, imgFormat, plane, imagePlane));
71 if (err.localErrcode != CL_SUCCESS) {
72 return nullptr;
73 }
74
75 imgInfo.plane = GmmTypesConverter::convertPlane(imagePlane);
76 auto *clSurfaceFormat = Image::getSurfaceFormatFromTable(flags, &imgFormat, context->getDevice(0)->getHardwareInfo().capabilityTable.supportsOcl21Features);
77 imgInfo.surfaceFormat = &clSurfaceFormat->surfaceFormat;
78
79 bool isSharedResource = false;
80 bool lockable = false;
81 auto rootDeviceIndex = context->getDevice(0)->getRootDeviceIndex();
82
83 GraphicsAllocation *alloc = nullptr;
84 if (surfaceInfo->shared_handle) {
85 isSharedResource = true;
86 AllocationProperties allocProperties(rootDeviceIndex,
87 false, // allocateMemory
88 0u, // size
89 AllocationType::sharedImage,
90 false, // isMultiStorageAllocation
91 context->getDeviceBitfieldForAllocation(rootDeviceIndex));
92 MemoryManager::OsHandleData osHandleData{surfaceInfo->shared_handle};
93 alloc = context->getMemoryManager()->createGraphicsAllocationFromSharedHandle(osHandleData, allocProperties,
94 false, false, true, nullptr);
95 updateImgInfoAndDesc(alloc->getDefaultGmm(), imgInfo, imagePlane, 0u);
96 } else {
97 lockable = !(surfaceDesc.Usage & D3DResourceFlags::USAGE_RENDERTARGET) || imagePlane != ImagePlane::noPlane;
98 if (!lockable) {
99 sharingFcns->createTexture2d(&surfaceStaging, &surfaceDesc, 0u);
100 }
101 if (imagePlane == ImagePlane::planeU || imagePlane == ImagePlane::planeV || imagePlane == ImagePlane::planeUV) {
102 imgInfo.imgDesc.imageWidth /= 2;
103 imgInfo.imgDesc.imageHeight /= 2;
104 }
105 MemoryProperties memoryProperties =
106 ClMemoryPropertiesHelper::createMemoryProperties(flags, 0, 0, &context->getDevice(0)->getDevice());
107 AllocationProperties allocProperties = MemObjHelper::getAllocationPropertiesWithImageInfo(rootDeviceIndex, imgInfo,
108 true, // allocateMemory
109 memoryProperties, context->getDevice(0)->getHardwareInfo(),
110 context->getDeviceBitfieldForAllocation(rootDeviceIndex),
111 context->isSingleDeviceContext());
112 allocProperties.allocationType = AllocationType::sharedResourceCopy;
113
114 alloc = context->getMemoryManager()->allocateGraphicsMemoryInPreferredPool(allocProperties, nullptr);
115
116 imgInfo.imgDesc.imageRowPitch = imgInfo.rowPitch;
117 imgInfo.imgDesc.imageSlicePitch = imgInfo.slicePitch;
118 }
119 DEBUG_BREAK_IF(!alloc);
120
121 auto surface = new D3DSurface(context, surfaceInfo, surfaceStaging, plane, imagePlane, adapterType, isSharedResource, lockable);
122 auto multiGraphicsAllocation = MultiGraphicsAllocation(rootDeviceIndex);
123 multiGraphicsAllocation.addAllocation(alloc);
124
125 return Image::createSharedImage(context, surface, mcsSurfaceInfo, std::move(multiGraphicsAllocation), nullptr, flags, 0, clSurfaceFormat, imgInfo, __GMM_NO_CUBE_MAP, 0, 0);
126 }
127
128 void D3DSurface::synchronizeObject(UpdateData &updateData) {
129 D3DLOCKED_RECT lockedRect = {};
130 sharingFunctions->setDevice(resourceDevice);
131 if (sharedResource && !context->getInteropUserSyncEnabled()) {
132 sharingFunctions->flushAndWait(d3dQuery);
133 } else if (!sharedResource) {
134 if (lockable) {
135 sharingFunctions->lockRect(d3d9Surface, &lockedRect, D3DLOCK_READONLY);
136 } else {
137 sharingFunctions->getRenderTargetData(d3d9Surface, d3d9SurfaceStaging);
138 sharingFunctions->lockRect(d3d9SurfaceStaging, &lockedRect, D3DLOCK_READONLY);
139 }
140
141 auto image = castToObjectOrAbort<Image>(updateData.memObject);
142 auto graphicsAllocation = image->getGraphicsAllocation(updateData.rootDeviceIndex);
143 auto sys = lockedRect.pBits;
144 auto gpu = context->getMemoryManager()->lockResource(graphicsAllocation);
145 auto pitch = static_cast<ULONG>(lockedRect.Pitch);
146 auto height = static_cast<ULONG>(image->getImageDesc().image_height);
147
148 graphicsAllocation->getDefaultGmm()->resourceCopyBlt(sys, gpu, pitch, height, 1u, imagePlane);
149
150 context->getMemoryManager()->unlockResource(graphicsAllocation);
151
152 if (lockable) {
153 sharingFunctions->unlockRect(d3d9Surface);
154 } else {
155 sharingFunctions->unlockRect(d3d9SurfaceStaging);
156 }
157
158 sharingFunctions->flushAndWait(d3dQuery);
159 }
160 updateData.synchronizationStatus = SynchronizeStatus::ACQUIRE_SUCCESFUL;
161 }
162
163 void D3DSurface::releaseResource(MemObj *memObject, uint32_t rootDeviceIndex) {
164 D3DLOCKED_RECT lockedRect = {};
165 auto image = castToObject<Image>(memObject);
166 if (!image) {
167 return;
168 }
169
170 sharingFunctions->setDevice(resourceDevice);
171 if (!sharedResource) {
172 if (lockable) {
173 sharingFunctions->lockRect(d3d9Surface, &lockedRect, 0);
174 } else {
175 sharingFunctions->lockRect(d3d9SurfaceStaging, &lockedRect, 0);
176 }
177
178 auto sys = lockedRect.pBits;
179 auto graphicsAllocation = image->getGraphicsAllocation(rootDeviceIndex);
180 auto gpu = context->getMemoryManager()->lockResource(graphicsAllocation);
181 auto pitch = static_cast<ULONG>(lockedRect.Pitch);
182 auto height = static_cast<ULONG>(image->getImageDesc().image_height);
183
184 graphicsAllocation->getDefaultGmm()->resourceCopyBlt(sys, gpu, pitch, height, 0u, imagePlane);
185
186 context->getMemoryManager()->unlockResource(graphicsAllocation);
187
188 if (lockable) {
189 sharingFunctions->unlockRect(d3d9Surface);
190 } else {
191 sharingFunctions->unlockRect(d3d9SurfaceStaging);
192 sharingFunctions->updateSurface(d3d9SurfaceStaging, d3d9Surface);
193 }
194 }
195 }
196
197 const std::map<const D3DFORMAT, const cl_image_format> D3DSurface::D3DtoClFormatConversions = {
198 {D3DFMT_R32F, {CL_R, CL_FLOAT}},
199 {D3DFMT_R16F, {CL_R, CL_HALF_FLOAT}},
200 {D3DFMT_L16, {CL_R, CL_UNORM_INT16}},
201 {D3DFMT_A8, {CL_A, CL_UNORM_INT8}},
202 {D3DFMT_L8, {CL_R, CL_UNORM_INT8}},
203 {D3DFMT_G32R32F, {CL_RG, CL_FLOAT}},
204 {D3DFMT_G16R16F, {CL_RG, CL_HALF_FLOAT}},
205 {D3DFMT_G16R16, {CL_RG, CL_UNORM_INT16}},
206 {D3DFMT_A8L8, {CL_RG, CL_UNORM_INT8}},
207 {D3DFMT_A32B32G32R32F, {CL_RGBA, CL_FLOAT}},
208 {D3DFMT_A16B16G16R16F, {CL_RGBA, CL_HALF_FLOAT}},
209 {D3DFMT_A16B16G16R16, {CL_RGBA, CL_UNORM_INT16}},
210 {D3DFMT_X8B8G8R8, {CL_RGBA, CL_UNORM_INT8}},
211 {D3DFMT_A8B8G8R8, {CL_RGBA, CL_UNORM_INT8}},
212 {D3DFMT_A8R8G8B8, {CL_BGRA, CL_UNORM_INT8}},
213 {D3DFMT_X8R8G8B8, {CL_BGRA, CL_UNORM_INT8}},
214 {D3DFMT_YUY2, {CL_YUYV_INTEL, CL_UNORM_INT8}},
215 {D3DFMT_UYVY, {CL_UYVY_INTEL, CL_UNORM_INT8}},
216
217 // The specific channel_order for NV12 is selected in findImgFormat
218 {static_cast<D3DFORMAT>(MAKEFOURCC('N', 'V', '1', '2')), {CL_R | CL_RG, CL_UNORM_INT8}},
219 {static_cast<D3DFORMAT>(MAKEFOURCC('Y', 'V', '1', '2')), {CL_R, CL_UNORM_INT8}},
220 {static_cast<D3DFORMAT>(MAKEFOURCC('Y', 'V', 'Y', 'U')), {CL_YVYU_INTEL, CL_UNORM_INT8}},
221 {static_cast<D3DFORMAT>(MAKEFOURCC('V', 'Y', 'U', 'Y')), {CL_VYUY_INTEL, CL_UNORM_INT8}}};
222
223 const std::vector<D3DFORMAT> D3DSurface::D3DPlane1Formats = {
224 static_cast<D3DFORMAT>(MAKEFOURCC('N', 'V', '1', '2')),
225 static_cast<D3DFORMAT>(MAKEFOURCC('Y', 'V', '1', '2'))};
226
227 const std::vector<D3DFORMAT> D3DSurface::D3DPlane2Formats =
228 {static_cast<D3DFORMAT>(MAKEFOURCC('Y', 'V', '1', '2'))};
229
230 cl_int D3DSurface::findImgFormat(D3DFORMAT d3dFormat, cl_image_format &imgFormat, cl_uint plane, ImagePlane &imagePlane) {
231 imagePlane = ImagePlane::noPlane;
232 static const cl_image_format unknownFormat = {0, 0};
233
234 auto element = D3DtoClFormatConversions.find(d3dFormat);
235 if (element == D3DtoClFormatConversions.end()) {
236 imgFormat = unknownFormat;
237 return CL_INVALID_IMAGE_FORMAT_DESCRIPTOR;
238 }
239
240 imgFormat = element->second;
241 int d3dFormatValue = d3dFormat;
242 switch (d3dFormatValue) {
243 case MAKEFOURCC('N', 'V', '1', '2'):
244 switch (plane) {
245 case 0:
246 imgFormat.image_channel_order = CL_R;
247 imagePlane = ImagePlane::planeY;
248 return CL_SUCCESS;
249 case 1:
250 imgFormat.image_channel_order = CL_RG;
251 imagePlane = ImagePlane::planeUV;
252 return CL_SUCCESS;
253 default:
254 imgFormat = unknownFormat;
255 return CL_INVALID_VALUE;
256 }
257
258 case MAKEFOURCC('Y', 'V', '1', '2'):
259 switch (plane) {
260 case 0:
261 imagePlane = ImagePlane::planeY;
262 return CL_SUCCESS;
263
264 case 1:
265 imagePlane = ImagePlane::planeV;
266 return CL_SUCCESS;
267
268 case 2:
269 imagePlane = ImagePlane::planeU;
270 return CL_SUCCESS;
271
272 default:
273 imgFormat = unknownFormat;
274 return CL_INVALID_VALUE;
275 }
276 }
277
278 if (plane > 0) {
279 return CL_INVALID_VALUE;
280 }
281 return CL_SUCCESS;
282 }
283
284 int D3DSurface::validateUpdateData(UpdateData &updateData) {
285 auto image = castToObject<Image>(updateData.memObject);
286 if (!image) {
287 return CL_INVALID_MEM_OBJECT;
288 }
289 return CL_SUCCESS;
290 }