Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2020-2024 Intel Corporation
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8 #include "shared/source/execution_environment/root_device_environment.h"
9 #include "shared/source/gmm_helper/gmm.h"
10 #include "shared/source/helpers/get_info.h"
11 #include "shared/source/memory_manager/allocation_properties.h"
12 #include "shared/source/memory_manager/memory_manager.h"
13
14 #include "opencl/extensions/public/cl_gl_private_intel.h"
15 #include "opencl/source/cl_device/cl_device.h"
16 #include "opencl/source/context/context.h"
17 #include "opencl/source/mem_obj/buffer.h"
18 #include "opencl/source/sharings/gl/gl_buffer.h"
19 #include "opencl/source/sharings/gl/windows/gl_sharing_windows.h"
20
21 #include "config.h"
22
23 using namespace NEO;
24
25 Buffer *GlBuffer::createSharedGlBuffer(Context *context, cl_mem_flags flags, unsigned int bufferId, cl_int *errcodeRet) {
26 ErrorCodeHelper errorCode(errcodeRet, CL_SUCCESS);
27 CL_GL_BUFFER_INFO bufferInfo = {0};
28 bufferInfo.bufferName = bufferId;
29 bufferInfo.createOrDestroy = true;
30
31 GLSharingFunctionsWindows *sharingFunctions = context->getSharing<GLSharingFunctionsWindows>();
32 if (sharingFunctions->acquireSharedBufferINTEL(&bufferInfo) == GL_FALSE) {
33 errorCode.set(CL_INVALID_GL_OBJECT);
34 return nullptr;
35 }
36
37 auto graphicsAllocation = GlBuffer::createGraphicsAllocation(context, bufferId, bufferInfo);
38 if (!graphicsAllocation) {
39 errorCode.set(CL_INVALID_GL_OBJECT);
40 return nullptr;
41 }
42
43 auto glHandler = new GlBuffer(sharingFunctions, bufferId);
44 auto rootDeviceIndex = graphicsAllocation->getRootDeviceIndex();
45 auto multiGraphicsAllocation = MultiGraphicsAllocation(rootDeviceIndex);
46 multiGraphicsAllocation.addAllocation(graphicsAllocation);
47
48 return Buffer::createSharedBuffer(context, flags, glHandler, std::move(multiGraphicsAllocation));
49 }
50
51 void GlBuffer::synchronizeObject(UpdateData &updateData) {
52 auto sharingFunctions = static_cast<GLSharingFunctionsWindows *>(this->sharingFunctions);
53
54 CL_GL_BUFFER_INFO bufferInfo = {};
55 bufferInfo.bufferName = this->clGlObjectId;
56 sharingFunctions->acquireSharedBufferINTEL(&bufferInfo);
57
58 auto graphicsAllocation = updateData.memObject->getGraphicsAllocation(updateData.rootDeviceIndex);
59
60 updateData.sharedHandle = bufferInfo.globalShareHandle;
61 updateData.synchronizationStatus = SynchronizeStatus::ACQUIRE_SUCCESFUL;
62 graphicsAllocation->setAllocationOffset(bufferInfo.bufferOffset);
63
64 const auto currentSharedHandle = graphicsAllocation->peekSharedHandle();
65 if (currentSharedHandle != updateData.sharedHandle) {
66 updateData.updateData = new CL_GL_BUFFER_INFO(bufferInfo);
67 }
68 }
69
70 void GlBuffer::resolveGraphicsAllocationChange(osHandle currentSharedHandle, UpdateData *updateData) {
71 const auto memObject = updateData->memObject;
72 if (currentSharedHandle != updateData->sharedHandle) {
73 const auto bufferInfo = std::unique_ptr<CL_GL_BUFFER_INFO>(static_cast<CL_GL_BUFFER_INFO *>(updateData->updateData));
74
75 auto oldGraphicsAllocation = memObject->getGraphicsAllocation(updateData->rootDeviceIndex);
76 popGraphicsAllocationFromReuse(oldGraphicsAllocation);
77
78 Context *context = memObject->getContext();
79 auto newGraphicsAllocation = createGraphicsAllocation(context, clGlObjectId, *bufferInfo);
80 if (newGraphicsAllocation == nullptr) {
81 updateData->synchronizationStatus = SynchronizeStatus::SYNCHRONIZE_ERROR;
82 memObject->removeGraphicsAllocation(updateData->rootDeviceIndex);
83 } else {
84 updateData->synchronizationStatus = SynchronizeStatus::ACQUIRE_SUCCESFUL;
85 memObject->resetGraphicsAllocation(newGraphicsAllocation);
86 }
87
88 if (updateData->synchronizationStatus == SynchronizeStatus::ACQUIRE_SUCCESFUL) {
89 memObject->getGraphicsAllocation(updateData->rootDeviceIndex)->setAllocationOffset(bufferInfo->bufferOffset);
90 }
91 }
92 }
93
94 void GlBuffer::popGraphicsAllocationFromReuse(GraphicsAllocation *graphicsAllocation) {
95 auto sharingFunctions = static_cast<GLSharingFunctionsWindows *>(this->sharingFunctions);
96
97 std::unique_lock<std::mutex> lock(sharingFunctions->mutex);
98
99 auto &graphicsAllocations = sharingFunctions->graphicsAllocationsForGlBufferReuse;
100 auto foundIter = std::find_if(graphicsAllocations.begin(), graphicsAllocations.end(),
101 [&graphicsAllocation](const std::pair<unsigned int, GraphicsAllocation *> &entry) {
102 return entry.second == graphicsAllocation;
103 });
104 if (foundIter != graphicsAllocations.end()) {
105 std::iter_swap(foundIter, graphicsAllocations.end() - 1);
106 graphicsAllocations.pop_back();
107 }
108 graphicsAllocation->decReuseCount();
109 }
110
111 void GlBuffer::releaseReusedGraphicsAllocation() {
112 auto sharingFunctions = static_cast<GLSharingFunctionsWindows *>(this->sharingFunctions);
113
114 std::unique_lock<std::mutex> lock(sharingFunctions->mutex);
115
116 auto &allocationsVector = sharingFunctions->graphicsAllocationsForGlBufferReuse;
117 auto itEnd = allocationsVector.end();
118 for (auto it = allocationsVector.begin(); it != itEnd; it++) {
119 if (it->first == clGlObjectId) {
120 it->second->decReuseCount();
121 if (it->second->peekReuseCount() == 0) {
122 std::iter_swap(it, itEnd - 1);
123 allocationsVector.pop_back();
124 }
125 break;
126 }
127 }
128 }
129
130 GraphicsAllocation *GlBuffer::createGraphicsAllocation(Context *context, unsigned int bufferId, _tagCLGLBufferInfo &bufferInfo) {
131 GLSharingFunctionsWindows *sharingFunctions = context->getSharing<GLSharingFunctionsWindows>();
132 auto &allocationsVector = sharingFunctions->graphicsAllocationsForGlBufferReuse;
133 GraphicsAllocation *graphicsAllocation = nullptr;
134 bool reusedAllocation = false;
135
136 std::unique_lock<std::mutex> lock(sharingFunctions->mutex);
137 auto endIter = allocationsVector.end();
138
139 auto foundIter =
140 std::find_if(allocationsVector.begin(), endIter,
141 [&bufferId](const std::pair<unsigned int, GraphicsAllocation *> &entry) { return entry.first == bufferId; });
142
143 if (foundIter != endIter) {
144 graphicsAllocation = foundIter->second;
145 reusedAllocation = true;
146 }
147
148 if (!graphicsAllocation) {
149 AllocationProperties properties = {context->getDevice(0)->getRootDeviceIndex(),
150 false, // allocateMemory
151 0u, // size
152 AllocationType::sharedBuffer,
153 false, // isMultiStorageAllocation
154 context->getDeviceBitfieldForAllocation(context->getDevice(0)->getRootDeviceIndex())};
155 // couldn't find allocation for reuse - create new
156 MemoryManager::OsHandleData osHandleData{bufferInfo.globalShareHandle};
157 graphicsAllocation =
158 context->getMemoryManager()->createGraphicsAllocationFromSharedHandle(osHandleData, properties, true, false, true, nullptr);
159 }
160
161 if (!graphicsAllocation) {
162 return nullptr;
163 }
164 graphicsAllocation->incReuseCount(); // decremented in releaseReusedGraphicsAllocation() called from MemObj destructor
165
166 if (!reusedAllocation) {
167 sharingFunctions->graphicsAllocationsForGlBufferReuse.push_back(std::make_pair(bufferId, graphicsAllocation));
168 if (bufferInfo.pGmmResInfo) {
169 DEBUG_BREAK_IF(graphicsAllocation->getDefaultGmm() != nullptr);
170 auto gmmHelper = context->getDevice(0)->getRootDeviceEnvironment().getGmmHelper();
171 graphicsAllocation->setDefaultGmm(new Gmm(gmmHelper, bufferInfo.pGmmResInfo));
172 }
173 }
174
175 return graphicsAllocation;
176 }
177
178 void GlBuffer::callReleaseResource(bool createOrDestroy) {
179 auto sharingFunctions = static_cast<GLSharingFunctionsWindows *>(this->sharingFunctions);
180 CL_GL_BUFFER_INFO bufferInfo = {};
181 bufferInfo.bufferName = this->clGlObjectId;
182 bufferInfo.createOrDestroy = createOrDestroy;
183 sharingFunctions->releaseSharedBufferINTEL(&bufferInfo);
184 }
185
186 void GlBuffer::releaseResource(MemObj *memObject, uint32_t rootDeviceIndex) {
187 callReleaseResource(false);
188 }