| 1 |
/* |
| 2 |
* Copyright (C) 2020-2024 Intel Corporation |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "opencl/source/sharings/gl/windows/gl_sharing_windows.h" |
| 9 |
|
| 10 |
#include "opencl/source/context/context.inl" |
| 11 |
#include "opencl/source/helpers/gl_helper.h" |
| 12 |
#include "opencl/source/sharings/gl/gl_arb_sync_event.h" |
| 13 |
|
| 14 |
namespace Os { |
| 15 |
extern const char *openglDllName; |
| 16 |
} |
| 17 |
|
| 18 |
namespace NEO { |
| 19 |
GLSharingFunctionsWindows::GLSharingFunctionsWindows(GLType glhdcType, GLContext glhglrcHandle, GLContext glhglrcHandleBkpCtx, GLDisplay glhdcHandle) |
| 20 |
: glHDCType(glhdcType), glHGLRCHandle(glhglrcHandle), glHGLRCHandleBkpCtx(glhglrcHandleBkpCtx), glHDCHandle(glhdcHandle) { |
| 21 |
GLSharingFunctionsWindows::initGLFunctions(); |
| 22 |
updateOpenGLContext(); |
| 23 |
createBackupContext(); |
| 24 |
} |
| 25 |
GLSharingFunctionsWindows::~GLSharingFunctionsWindows() { |
| 26 |
if (pfnWglDeleteContext) { |
| 27 |
pfnWglDeleteContext(glHGLRCHandleBkpCtx); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
bool GLSharingFunctionsWindows::isGlSharingEnabled() { |
| 32 |
static bool oglLibAvailable = std::unique_ptr<OsLibrary>(OsLibrary::loadFunc(Os::openglDllName)).get() != nullptr; |
| 33 |
return oglLibAvailable; |
| 34 |
} |
| 35 |
|
| 36 |
void GLSharingFunctionsWindows::createBackupContext() { |
| 37 |
if (pfnWglCreateContext) { |
| 38 |
glHGLRCHandleBkpCtx = pfnWglCreateContext(glHDCHandle); |
| 39 |
pfnWglShareLists(glHGLRCHandle, glHGLRCHandleBkpCtx); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
GLboolean GLSharingFunctionsWindows::setSharedOCLContextState() { |
| 44 |
ContextInfo ctxInfo = {0}; |
| 45 |
GLboolean retVal = glSetSharedOCLContextState(glHDCHandle, glHGLRCHandle, CL_TRUE, &ctxInfo); |
| 46 |
if (retVal == GL_FALSE) { |
| 47 |
return GL_FALSE; |
| 48 |
} |
| 49 |
glContextHandle = ctxInfo.contextHandle; |
| 50 |
glDeviceHandle = ctxInfo.deviceHandle; |
| 51 |
|
| 52 |
return retVal; |
| 53 |
} |
| 54 |
|
| 55 |
bool GLSharingFunctionsWindows::isOpenGlExtensionSupported(const unsigned char *pExtensionString) { |
| 56 |
bool loadedNull = (glGetStringi == nullptr) || (glGetIntegerv == nullptr); |
| 57 |
if (loadedNull) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
cl_int numberOfExtensions = 0; |
| 62 |
glGetIntegerv(GL_NUM_EXTENSIONS, &numberOfExtensions); |
| 63 |
for (cl_int i = 0; i < numberOfExtensions; i++) { |
| 64 |
std::basic_string<unsigned char> pString = glGetStringi(GL_EXTENSIONS, i); |
| 65 |
if (pString == pExtensionString) { |
| 66 |
return true; |
| 67 |
} |
| 68 |
} |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
bool GLSharingFunctionsWindows::isOpenGlSharingSupported() { |
| 73 |
|
| 74 |
std::basic_string<unsigned char> vendor = glGetString(GL_VENDOR); |
| 75 |
const unsigned char intelVendor[] = "Intel"; |
| 76 |
|
| 77 |
if ((vendor.empty()) || (vendor != intelVendor)) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
std::basic_string<unsigned char> version = glGetString(GL_VERSION); |
| 81 |
if (version.empty()) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
bool isOpenGLES = false; |
| 86 |
const unsigned char versionES[] = "OpenGL ES"; |
| 87 |
if (version.find(versionES) != std::string::npos) { |
| 88 |
isOpenGLES = true; |
| 89 |
} |
| 90 |
|
| 91 |
if (isOpenGLES == true) { |
| 92 |
const unsigned char versionES1[] = "OpenGL ES 1."; |
| 93 |
if (version.find(versionES1) != std::string::npos) { |
| 94 |
const unsigned char supportGLOES[] = "GL_OES_framebuffer_object"; |
| 95 |
if (isOpenGlExtensionSupported(supportGLOES) == false) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
} |
| 99 |
} else { |
| 100 |
if (version[0] < '3') { |
| 101 |
const unsigned char supportGLEXT[] = "GL_EXT_framebuffer_object"; |
| 102 |
if (isOpenGlExtensionSupported(supportGLEXT) == false) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
GlArbSyncEvent *GLSharingFunctionsWindows::getGlArbSyncEvent(Event &baseEvent) { |
| 112 |
std::lock_guard<std::mutex> lock{glArbEventMutex}; |
| 113 |
auto it = glArbEventMapping.find(&baseEvent); |
| 114 |
if (it != glArbEventMapping.end()) { |
| 115 |
return it->second; |
| 116 |
} |
| 117 |
return nullptr; |
| 118 |
} |
| 119 |
|
| 120 |
void GLSharingFunctionsWindows::removeGlArbSyncEventMapping(Event &baseEvent) { |
| 121 |
std::lock_guard<std::mutex> lock{glArbEventMutex}; |
| 122 |
auto it = glArbEventMapping.find(&baseEvent); |
| 123 |
if (it == glArbEventMapping.end()) { |
| 124 |
DEBUG_BREAK_IF(it == glArbEventMapping.end()); |
| 125 |
return; |
| 126 |
} |
| 127 |
glArbEventMapping.erase(it); |
| 128 |
} |
| 129 |
|
| 130 |
GLboolean GLSharingFunctionsWindows::initGLFunctions() { |
| 131 |
glLibrary.reset(OsLibrary::loadFunc(Os::openglDllName)); |
| 132 |
|
| 133 |
if (glLibrary->isLoaded()) { |
| 134 |
GlFunctionHelper wglLibrary(glLibrary.get(), "wglGetProcAddress"); |
| 135 |
glGetCurrentContext = (*glLibrary)["wglGetCurrentContext"]; |
| 136 |
glGetCurrentDisplay = (*glLibrary)["wglGetCurrentDC"]; |
| 137 |
glGetString = (*glLibrary)["glGetString"]; |
| 138 |
glGetIntegerv = (*glLibrary)["glGetIntegerv"]; |
| 139 |
pfnWglCreateContext = (*glLibrary)["wglCreateContext"]; |
| 140 |
pfnWglDeleteContext = (*glLibrary)["wglDeleteContext"]; |
| 141 |
pfnWglShareLists = (*glLibrary)["wglShareLists"]; |
| 142 |
wglMakeCurrent = (*glLibrary)["wglMakeCurrent"]; |
| 143 |
|
| 144 |
glSetSharedOCLContextState = wglLibrary["wglSetSharedOCLContextStateINTEL"]; |
| 145 |
glAcquireSharedBuffer = wglLibrary["wglAcquireSharedBufferINTEL"]; |
| 146 |
glReleaseSharedBuffer = wglLibrary["wglReleaseSharedBufferINTEL"]; |
| 147 |
glAcquireSharedRenderBuffer = wglLibrary["wglAcquireSharedRenderBufferINTEL"]; |
| 148 |
glReleaseSharedRenderBuffer = wglLibrary["wglReleaseSharedRenderBufferINTEL"]; |
| 149 |
glAcquireSharedTexture = wglLibrary["wglAcquireSharedTextureINTEL"]; |
| 150 |
glReleaseSharedTexture = wglLibrary["wglReleaseSharedTextureINTEL"]; |
| 151 |
glRetainSync = wglLibrary["wglRetainSyncINTEL"]; |
| 152 |
glReleaseSync = wglLibrary["wglReleaseSyncINTEL"]; |
| 153 |
glGetSynciv = wglLibrary["wglGetSyncivINTEL"]; |
| 154 |
glGetStringi = wglLibrary["glGetStringi"]; |
| 155 |
glGetLuid = wglLibrary["wglGetLuidINTEL"]; |
| 156 |
} |
| 157 |
this->pfnGlArbSyncObjectCleanup = cleanupArbSyncObject; |
| 158 |
this->pfnGlArbSyncObjectSetup = setupArbSyncObject; |
| 159 |
this->pfnGlArbSyncObjectSignal = signalArbSyncObject; |
| 160 |
this->pfnGlArbSyncObjectWaitServer = serverWaitForArbSyncObject; |
| 161 |
|
| 162 |
return 1; |
| 163 |
} |
| 164 |
|
| 165 |
LUID GLSharingFunctionsWindows::getAdapterLuid(GLContext glhglrcHandle) const { |
| 166 |
if (glGetLuid) { |
| 167 |
return glGetLuid(glhglrcHandle); |
| 168 |
} |
| 169 |
return {}; |
| 170 |
} |
| 171 |
|
| 172 |
template GLSharingFunctionsWindows *Context::getSharing<GLSharingFunctionsWindows>(); |
| 173 |
|
| 174 |
} // namespace NEO |