Debian Codegraph @

opencl/source/sharings/gl/windows/gl_arb_sync_event_windows.cpp

raw
1 /*
2 * Copyright (C) 2020-2024 Intel Corporation
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8 #include "shared/source/command_stream/command_stream_receiver.h"
9 #include "shared/source/device/device.h"
10 #include "shared/source/os_interface/os_interface.h"
11 #include "shared/source/os_interface/windows/gdi_interface.h"
12 #include "shared/source/os_interface/windows/os_context_win.h"
13 #include "shared/source/os_interface/windows/sys_calls.h"
14 #include "shared/source/os_interface/windows/wddm/wddm.h"
15
16 #include "opencl/extensions/public/cl_gl_private_intel.h"
17 #include "opencl/source/command_queue/command_queue.h"
18 #include "opencl/source/context/context.h"
19 #include "opencl/source/helpers/base_object.h"
20 #include "opencl/source/sharings/gl/gl_arb_sync_event.h"
21 #include "opencl/source/sharings/gl/windows/gl_sharing_windows.h"
22
23 #include <GL/gl.h>
24
25 namespace NEO {
26
27 void destroySync(Gdi &gdi, D3DKMT_HANDLE sync) {
28 if (!sync) {
29 return;
30 }
31 D3DKMT_DESTROYSYNCHRONIZATIONOBJECT destroySyncInfo = {};
32 destroySyncInfo.hSyncObject = sync;
33 [[maybe_unused]] NTSTATUS status = gdi.destroySynchronizationObject(&destroySyncInfo);
34 DEBUG_BREAK_IF(STATUS_SUCCESS != status);
35 }
36
37 void destroyEvent(OSInterface &osInterface, HANDLE event) {
38 if (!event) {
39 return;
40 }
41
42 [[maybe_unused]] auto ret = SysCalls::closeHandle(event);
43 DEBUG_BREAK_IF(TRUE != ret);
44 }
45
46 void cleanupArbSyncObject(OSInterface &osInterface, CL_GL_SYNC_INFO *glSyncInfo) {
47 if (nullptr == glSyncInfo) {
48 return;
49 }
50
51 auto gdi = osInterface.getDriverModel()->as<Wddm>()->getGdi();
52 UNRECOVERABLE_IF(nullptr == gdi);
53
54 destroySync(*gdi, glSyncInfo->serverSynchronizationObject);
55 destroySync(*gdi, glSyncInfo->clientSynchronizationObject);
56 destroySync(*gdi, glSyncInfo->submissionSynchronizationObject);
57 destroyEvent(osInterface, glSyncInfo->event);
58 destroyEvent(osInterface, glSyncInfo->submissionEvent);
59
60 destroyArbSyncEventName(glSyncInfo->eventName);
61 destroyArbSyncEventName(glSyncInfo->submissionEventName);
62 }
63
64 bool setupArbSyncObject(GLSharingFunctions &sharing, OSInterface &osInterface, CL_GL_SYNC_INFO &glSyncInfo) {
65 auto &sharingFunctions = static_cast<GLSharingFunctionsWindows &>(sharing);
66
67 glSyncInfo.hContextToBlock = static_cast<D3DKMT_HANDLE>(sharingFunctions.getGLContextHandle());
68 auto glDevice = static_cast<D3DKMT_HANDLE>(sharingFunctions.getGLDeviceHandle());
69 auto wddm = osInterface.getDriverModel()->as<Wddm>();
70
71 D3DKMT_CREATESYNCHRONIZATIONOBJECT serverSyncInitInfo = {};
72 serverSyncInitInfo.hDevice = glDevice;
73 serverSyncInitInfo.Info.Type = D3DDDI_SEMAPHORE;
74 serverSyncInitInfo.Info.Semaphore.MaxCount = 32;
75 serverSyncInitInfo.Info.Semaphore.InitialCount = 0;
76 NTSTATUS serverSyncInitStatus = wddm->getGdi()->createSynchronizationObject(&serverSyncInitInfo);
77 glSyncInfo.serverSynchronizationObject = serverSyncInitInfo.hSyncObject;
78
79 glSyncInfo.eventName = createArbSyncEventName();
80 glSyncInfo.event = SysCalls::createEvent(nullptr, TRUE, FALSE, glSyncInfo.eventName);
81
82 D3DKMT_CREATESYNCHRONIZATIONOBJECT2 clientSyncInitInfo = {};
83 clientSyncInitInfo.hDevice = glDevice;
84 clientSyncInitInfo.Info.Type = D3DDDI_CPU_NOTIFICATION;
85 clientSyncInitInfo.Info.CPUNotification.Event = glSyncInfo.event;
86 NTSTATUS clientSyncInitStatus = wddm->getGdi()->createSynchronizationObject2(&clientSyncInitInfo);
87 glSyncInfo.clientSynchronizationObject = clientSyncInitInfo.hSyncObject;
88
89 D3DKMT_CREATESYNCHRONIZATIONOBJECT2 submissionSyncEventInfo = {};
90 glSyncInfo.submissionEventName = createArbSyncEventName();
91 glSyncInfo.submissionEvent = SysCalls::createEvent(nullptr, TRUE, FALSE, glSyncInfo.submissionEventName);
92
93 submissionSyncEventInfo.hDevice = glDevice;
94 submissionSyncEventInfo.Info.Type = D3DDDI_CPU_NOTIFICATION;
95 submissionSyncEventInfo.Info.CPUNotification.Event = glSyncInfo.submissionEvent;
96 auto submissionSyncInitStatus = wddm->getGdi()->createSynchronizationObject2(&submissionSyncEventInfo);
97
98 glSyncInfo.submissionSynchronizationObject = submissionSyncEventInfo.hSyncObject;
99 glSyncInfo.waitCalled = false;
100
101 bool setupFailed =
102 (glSyncInfo.event == nullptr) ||
103 (glSyncInfo.submissionEvent == nullptr) ||
104 (STATUS_SUCCESS != serverSyncInitStatus) ||
105 (STATUS_SUCCESS != clientSyncInitStatus) ||
106 (STATUS_SUCCESS != submissionSyncInitStatus);
107
108 if (setupFailed) {
109 DEBUG_BREAK_IF(true);
110 cleanupArbSyncObject(osInterface, &glSyncInfo);
111 return false;
112 }
113
114 return true;
115 }
116
117 void signalArbSyncObject(OsContext &osContext, CL_GL_SYNC_INFO &glSyncInfo) {
118 auto osContextWin = static_cast<OsContextWin *>(&osContext);
119 UNRECOVERABLE_IF(!osContextWin);
120 auto wddm = osContextWin->getWddm();
121
122 D3DKMT_SIGNALSYNCHRONIZATIONOBJECT signalServerClientSyncInfo = {};
123 signalServerClientSyncInfo.hContext = osContextWin->getWddmContextHandle();
124 signalServerClientSyncInfo.Flags.SignalAtSubmission = 0; // Wait for GPU to complete processing command buffer
125 signalServerClientSyncInfo.ObjectHandleArray[0] = glSyncInfo.serverSynchronizationObject;
126 signalServerClientSyncInfo.ObjectHandleArray[1] = glSyncInfo.clientSynchronizationObject;
127 signalServerClientSyncInfo.ObjectCount = 2;
128 [[maybe_unused]] NTSTATUS status = wddm->getGdi()->signalSynchronizationObject(&signalServerClientSyncInfo);
129 if (STATUS_SUCCESS != status) {
130 DEBUG_BREAK_IF(true);
131 return;
132 }
133
134 D3DKMT_SIGNALSYNCHRONIZATIONOBJECT signalSubmissionSyncInfo = {};
135 signalSubmissionSyncInfo.hContext = osContextWin->getWddmContextHandle();
136 signalSubmissionSyncInfo.Flags.SignalAtSubmission = 1; // Don't wait for GPU to complete processing command buffer
137 signalSubmissionSyncInfo.ObjectHandleArray[0] = glSyncInfo.submissionSynchronizationObject;
138 signalSubmissionSyncInfo.ObjectCount = 1;
139 status = wddm->getGdi()->signalSynchronizationObject(&signalSubmissionSyncInfo);
140 DEBUG_BREAK_IF(STATUS_SUCCESS != status);
141 }
142
143 void serverWaitForArbSyncObject(OSInterface &osInterface, CL_GL_SYNC_INFO &glSyncInfo) {
144 auto wddm = osInterface.getDriverModel()->as<Wddm>();
145
146 D3DKMT_WAITFORSYNCHRONIZATIONOBJECT waitForSyncInfo = {};
147 waitForSyncInfo.hContext = glSyncInfo.hContextToBlock;
148 waitForSyncInfo.ObjectCount = 1;
149 waitForSyncInfo.ObjectHandleArray[0] = glSyncInfo.serverSynchronizationObject;
150
151 NTSTATUS status = wddm->getGdi()->waitForSynchronizationObject(&waitForSyncInfo);
152 if (status != STATUS_SUCCESS) {
153 DEBUG_BREAK_IF(true);
154 return;
155 }
156 glSyncInfo.waitCalled = true;
157 }
158
159 GlArbSyncEvent::GlArbSyncEvent(Context &context)
160 : Event(&context, nullptr, CL_COMMAND_GL_FENCE_SYNC_OBJECT_KHR, CompletionStamp::notReady, CompletionStamp::notReady),
161 glSyncInfo(std::make_unique<CL_GL_SYNC_INFO>()) {
162 }
163
164 bool GlArbSyncEvent::setBaseEvent(Event &ev) {
165 UNRECOVERABLE_IF(this->baseEvent != nullptr);
166 UNRECOVERABLE_IF(ev.getContext() == nullptr);
167 UNRECOVERABLE_IF(ev.getCommandQueue() == nullptr);
168 auto cmdQueue = ev.getCommandQueue();
169 auto osInterface = cmdQueue->getGpgpuCommandStreamReceiver().getOSInterface();
170 UNRECOVERABLE_IF(osInterface == nullptr);
171 if (false == ctx->getSharing<GLSharingFunctionsWindows>()->glArbSyncObjectSetup(*osInterface, *glSyncInfo)) {
172 return false;
173 }
174
175 this->baseEvent = &ev;
176 this->cmdQueue = cmdQueue;
177 this->cmdQueue->incRefInternal();
178 this->baseEvent->incRefInternal();
179 this->osInterface = osInterface;
180 ev.addChild(*this);
181 return true;
182 }
183
184 GlArbSyncEvent::~GlArbSyncEvent() {
185 if (baseEvent != nullptr) {
186 ctx->getSharing<GLSharingFunctionsWindows>()->glArbSyncObjectCleanup(*osInterface, glSyncInfo.get());
187 baseEvent->decRefInternal();
188 }
189 }
190
191 GlArbSyncEvent *GlArbSyncEvent::create(Event &baseEvent) {
192 if (baseEvent.getContext() == nullptr) {
193 return nullptr;
194 }
195 auto arbSyncEvent = new GlArbSyncEvent(*baseEvent.getContext());
196 if (false == arbSyncEvent->setBaseEvent(baseEvent)) {
197 delete arbSyncEvent;
198 arbSyncEvent = nullptr;
199 }
200
201 return arbSyncEvent;
202 }
203
204 void GlArbSyncEvent::unblockEventBy(Event &event, TaskCountType taskLevel, int32_t transitionStatus) {
205 DEBUG_BREAK_IF(&event != this->baseEvent);
206 if ((transitionStatus > CL_SUBMITTED) || (transitionStatus < 0)) {
207 return;
208 }
209
210 event.getCommandQueue()->getGpgpuCommandStreamReceiver().stopDirectSubmission(true);
211 ctx->getSharing<GLSharingFunctionsWindows>()->glArbSyncObjectSignal(event.getCommandQueue()->getGpgpuCommandStreamReceiver().getOsContext(), *glSyncInfo);
212 ctx->getSharing<GLSharingFunctionsWindows>()->glArbSyncObjectWaitServer(*osInterface, *glSyncInfo);
213 }
214 } // namespace NEO
215
216 extern "C" CL_API_ENTRY cl_int CL_API_CALL
217 clEnqueueMarkerWithSyncObjectINTEL(cl_command_queue commandQueue,
218 cl_event *event,
219 cl_context *context) {
220 return CL_INVALID_OPERATION;
221 }
222
223 extern "C" CL_API_ENTRY cl_int CL_API_CALL
224 clGetCLObjectInfoINTEL(cl_mem memObj,
225 void *pResourceInfo) {
226 return CL_INVALID_OPERATION;
227 }
228
229 extern "C" CL_API_ENTRY cl_int CL_API_CALL
230 clGetCLEventInfoINTEL(cl_event event, PCL_GL_SYNC_INFO *pSyncInfoHandleRet, cl_context *pClContextRet) {
231 if ((nullptr == pSyncInfoHandleRet) || (nullptr == pClContextRet)) {
232 return CL_INVALID_ARG_VALUE;
233 }
234
235 auto neoEvent = NEO::castToObject<NEO::Event>(event);
236 if (nullptr == neoEvent) {
237 return CL_INVALID_EVENT;
238 }
239
240 if (neoEvent->getCommandType() != CL_COMMAND_RELEASE_GL_OBJECTS) {
241 *pSyncInfoHandleRet = nullptr;
242 *pClContextRet = static_cast<cl_context>(neoEvent->getContext());
243 return CL_SUCCESS;
244 }
245
246 auto sharing = neoEvent->getContext()->getSharing<NEO::GLSharingFunctionsWindows>();
247 if (sharing == nullptr) {
248 return CL_INVALID_OPERATION;
249 }
250
251 NEO::GlArbSyncEvent *arbSyncEvent = sharing->getOrCreateGlArbSyncEvent(*neoEvent);
252 if (nullptr == arbSyncEvent) {
253 return CL_OUT_OF_RESOURCES;
254 }
255
256 neoEvent->updateExecutionStatus();
257 CL_GL_SYNC_INFO *syncInfo = arbSyncEvent->getSyncInfo();
258 *pSyncInfoHandleRet = syncInfo;
259 *pClContextRet = static_cast<cl_context>(neoEvent->getContext());
260
261 return CL_SUCCESS;
262 }
263
264 extern "C" CL_API_ENTRY cl_int CL_API_CALL
265 clReleaseGlSharedEventINTEL(cl_event event) {
266 auto neoEvent = NEO::castToObject<NEO::Event>(event);
267 if (nullptr == neoEvent) {
268 return CL_INVALID_EVENT;
269 }
270 auto arbSyncEvent = neoEvent->getContext()->getSharing<NEO::GLSharingFunctionsWindows>()->getGlArbSyncEvent(*neoEvent);
271 neoEvent->getContext()->getSharing<NEO::GLSharingFunctionsWindows>()->removeGlArbSyncEventMapping(*neoEvent);
272 if (nullptr != arbSyncEvent) {
273 arbSyncEvent->release();
274 }
275 neoEvent->release();
276
277 return CL_SUCCESS;
278 }