| 1 |
/* |
| 2 |
* Copyright (C) 2020-2023 Intel Corporation |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#pragma once |
| 9 |
|
| 10 |
#include "opencl/source/sharings/gl/gl_sharing.h" |
| 11 |
|
| 12 |
#include "gl_types.h" |
| 13 |
#include <GL/gl.h> |
| 14 |
|
| 15 |
namespace NEO { |
| 16 |
// OpenGL API names |
| 17 |
typedef GLboolean(OSAPI *PFNOGLSetSharedOCLContextStateINTEL)(GLDisplay hdcHandle, GLContext contextHandle, GLboolean state, GLvoid *pContextInfo); |
| 18 |
typedef GLboolean(OSAPI *PFNOGLAcquireSharedBufferINTEL)(GLDisplay hdcHandle, GLContext contextHandle, GLContext backupContextHandle, GLvoid *pBufferInfo); |
| 19 |
typedef GLboolean(OSAPI *PFNOGLAcquireSharedRenderBufferINTEL)(GLDisplay hdcHandle, GLContext contextHandle, GLContext backupContextHandle, GLvoid *pResourceInfo); |
| 20 |
typedef GLboolean(OSAPI *PFNOGLAcquireSharedTextureINTEL)(GLDisplay hdcHandle, GLContext contextHandle, GLContext backupContextHandle, GLvoid *pResourceInfo); |
| 21 |
typedef GLboolean(OSAPI *PFNOGLReleaseSharedBufferINTEL)(GLDisplay hdcHandle, GLContext contextHandle, GLContext backupContextHandle, GLvoid *pBufferInfo); |
| 22 |
typedef GLboolean(OSAPI *PFNOGLReleaseSharedRenderBufferINTEL)(GLDisplay hdcHandle, GLContext contextHandle, GLContext backupContextHandle, GLvoid *pResourceInfo); |
| 23 |
typedef GLboolean(OSAPI *PFNOGLReleaseSharedTextureINTEL)(GLDisplay hdcHandle, GLContext contextHandle, GLContext backupContextHandle, GLvoid *pResourceInfo); |
| 24 |
typedef GLContext(OSAPI *PFNOGLGetCurrentContext)(); |
| 25 |
typedef GLDisplay(OSAPI *PFNOGLGetCurrentDisplay)(); |
| 26 |
typedef GLboolean(OSAPI *PFNOGLMakeCurrent)(GLDisplay hdcHandle, void *draw, void *read, GLContext contextHandle); |
| 27 |
typedef GLboolean(OSAPI *PFNOGLRetainSyncINTEL)(GLDisplay hdcHandle, GLContext contextHandle, GLContext backupContextHandle, GLvoid *pSyncInfo); |
| 28 |
typedef GLboolean(OSAPI *PFNOGLReleaseSyncINTEL)(GLDisplay hdcHandle, GLContext contextHandle, GLContext backupContextHandle, GLvoid *pSync); |
| 29 |
typedef void(OSAPI *PFNOGLGetSyncivINTEL)(GLvoid *pSync, GLenum pname, GLint *value); |
| 30 |
|
| 31 |
typedef const GLubyte *(OSAPI *PFNglGetString)(GLenum name); |
| 32 |
typedef const GLubyte *(OSAPI *PFNglGetStringi)(GLenum name, GLuint index); |
| 33 |
typedef void(OSAPI *PFNglGetIntegerv)(GLenum pname, GLint *params); |
| 34 |
typedef void(OSAPI *PFNglBindTexture)(GLenum target, GLuint texture); |
| 35 |
|
| 36 |
// wgl |
| 37 |
typedef BOOL(OSAPI *PFNwglMakeCurrent)(HDC, HGLRC); |
| 38 |
typedef GLContext(OSAPI *PFNwglCreateContext)(GLDisplay hdcHandle); |
| 39 |
typedef int(OSAPI *PFNwglShareLists)(GLContext contextHandle, GLContext backupContextHandle); |
| 40 |
typedef BOOL(OSAPI *PFNwglDeleteContext)(HGLRC hglrcHandle); |
| 41 |
|
| 42 |
typedef bool (*PFNglArbSyncObjectSetup)(GLSharingFunctions &sharing, OSInterface &osInterface, CL_GL_SYNC_INFO &glSyncInfo); |
| 43 |
typedef void (*PFNglArbSyncObjectCleanup)(OSInterface &osInterface, CL_GL_SYNC_INFO *glSyncInfo); |
| 44 |
typedef void (*PFNglArbSyncObjectSignal)(OsContext &osContext, CL_GL_SYNC_INFO &glSyncInfo); |
| 45 |
typedef void (*PFNglArbSyncObjectWaitServer)(OSInterface &osInterface, CL_GL_SYNC_INFO &glSyncInfo); |
| 46 |
|
| 47 |
typedef LUID(OSAPI *PFNGLGETLUIDINTEL)(HGLRC hglrcHandle); |
| 48 |
|
| 49 |
class GLSharingFunctionsWindows : public GLSharingFunctions { |
| 50 |
public: |
| 51 |
GLSharingFunctionsWindows() = default; |
| 52 |
GLSharingFunctionsWindows(GLType glhdcType, GLContext glhglrcHandle, GLContext glhglrcHandleBkpCtx, GLDisplay glhdcHandle); |
| 53 |
~GLSharingFunctionsWindows() override; |
| 54 |
|
| 55 |
OS_HANDLE getGLDeviceHandle() const { return glDeviceHandle; } |
| 56 |
OS_HANDLE getGLContextHandle() const { return glContextHandle; } |
| 57 |
|
| 58 |
GLboolean initGLFunctions() override; |
| 59 |
bool isOpenGlSharingSupported() override; |
| 60 |
bool isHandleCompatible(const DriverModel &driverModel, uint32_t handle) const override; |
| 61 |
bool isGlHdcHandleMissing(uint32_t handle) const override { return handle == 0u; } |
| 62 |
static bool isGlSharingEnabled(); |
| 63 |
|
| 64 |
// Arb sync event |
| 65 |
template <typename EventType = GlArbSyncEvent> |
| 66 |
auto getOrCreateGlArbSyncEvent(Event &baseEvent) -> decltype(EventType::create(baseEvent)); |
| 67 |
GlArbSyncEvent *getGlArbSyncEvent(Event &baseEvent); |
| 68 |
void removeGlArbSyncEventMapping(Event &baseEvent); |
| 69 |
|
| 70 |
// Gl functions |
| 71 |
GLboolean acquireSharedBufferINTEL(GLvoid *pBufferInfo) { |
| 72 |
return glAcquireSharedBuffer(glHDCHandle, glHGLRCHandle, glHGLRCHandleBkpCtx, pBufferInfo); |
| 73 |
} |
| 74 |
GLboolean releaseSharedBufferINTEL(GLvoid *pBufferInfo) { |
| 75 |
return glReleaseSharedBuffer(glHDCHandle, glHGLRCHandle, glHGLRCHandleBkpCtx, pBufferInfo); |
| 76 |
} |
| 77 |
GLboolean acquireSharedRenderBuffer(GLvoid *pResourceInfo) { |
| 78 |
return glAcquireSharedRenderBuffer(glHDCHandle, glHGLRCHandle, glHGLRCHandleBkpCtx, pResourceInfo); |
| 79 |
} |
| 80 |
GLboolean releaseSharedRenderBuffer(GLvoid *pResourceInfo) { |
| 81 |
return glReleaseSharedRenderBuffer(glHDCHandle, glHGLRCHandle, glHGLRCHandleBkpCtx, pResourceInfo); |
| 82 |
} |
| 83 |
GLboolean acquireSharedTexture(GLvoid *pResourceInfo) { |
| 84 |
return glAcquireSharedTexture(glHDCHandle, glHGLRCHandle, glHGLRCHandleBkpCtx, pResourceInfo); |
| 85 |
} |
| 86 |
GLboolean releaseSharedTexture(GLvoid *pResourceInfo) { |
| 87 |
return glReleaseSharedTexture(glHDCHandle, glHGLRCHandle, glHGLRCHandleBkpCtx, pResourceInfo); |
| 88 |
} |
| 89 |
GLboolean retainSync(GLvoid *pSyncInfo) { |
| 90 |
return glRetainSync(glHDCHandle, glHGLRCHandle, glHGLRCHandleBkpCtx, pSyncInfo); |
| 91 |
} |
| 92 |
GLboolean releaseSync(GLvoid *pSync) { |
| 93 |
return glReleaseSync(glHDCHandle, glHGLRCHandle, glHGLRCHandleBkpCtx, pSync); |
| 94 |
} |
| 95 |
void getSynciv(GLvoid *pSync, GLenum pname, GLint *value) { |
| 96 |
return glGetSynciv(pSync, pname, value); |
| 97 |
} |
| 98 |
GLContext getCurrentContext() { |
| 99 |
return glGetCurrentContext(); |
| 100 |
} |
| 101 |
GLDisplay getCurrentDisplay() { |
| 102 |
return glGetCurrentDisplay(); |
| 103 |
} |
| 104 |
GLboolean makeCurrent(GLContext contextHandle, GLDisplay displayHandle = 0) { |
| 105 |
if (displayHandle == 0) { |
| 106 |
displayHandle = glHDCHandle; |
| 107 |
} |
| 108 |
return this->wglMakeCurrent(displayHandle, contextHandle); |
| 109 |
} |
| 110 |
GLContext getBackupContextHandle() { |
| 111 |
return glHGLRCHandleBkpCtx; |
| 112 |
} |
| 113 |
GLContext getContextHandle() { |
| 114 |
return glHGLRCHandle; |
| 115 |
} |
| 116 |
bool glArbSyncObjectSetup(OSInterface &osInterface, CL_GL_SYNC_INFO &glSyncInfo) { |
| 117 |
return pfnGlArbSyncObjectSetup(*this, osInterface, glSyncInfo); |
| 118 |
} |
| 119 |
void glArbSyncObjectCleanup(OSInterface &osInterface, CL_GL_SYNC_INFO *glSyncInfo) { |
| 120 |
pfnGlArbSyncObjectCleanup(osInterface, glSyncInfo); |
| 121 |
} |
| 122 |
void glArbSyncObjectSignal(OsContext &osContext, CL_GL_SYNC_INFO &glSyncInfo) { |
| 123 |
pfnGlArbSyncObjectSignal(osContext, glSyncInfo); |
| 124 |
} |
| 125 |
void glArbSyncObjectWaitServer(OSInterface &osInterface, CL_GL_SYNC_INFO &glSyncInfo) { |
| 126 |
pfnGlArbSyncObjectWaitServer(osInterface, glSyncInfo); |
| 127 |
} |
| 128 |
|
| 129 |
LUID getAdapterLuid(GLContext glhglrcHandle) const; |
| 130 |
|
| 131 |
// Buffer reuse |
| 132 |
std::mutex mutex; |
| 133 |
std::vector<std::pair<unsigned int, GraphicsAllocation *>> graphicsAllocationsForGlBufferReuse; |
| 134 |
|
| 135 |
protected: |
| 136 |
void updateOpenGLContext() { |
| 137 |
if (glSetSharedOCLContextState) { |
| 138 |
setSharedOCLContextState(); |
| 139 |
} |
| 140 |
} |
| 141 |
GLboolean setSharedOCLContextState(); |
| 142 |
void createBackupContext(); |
| 143 |
bool isOpenGlExtensionSupported(const unsigned char *pExtentionString); |
| 144 |
|
| 145 |
// Handles |
| 146 |
GLType glHDCType = 0; |
| 147 |
GLContext glHGLRCHandle = 0; |
| 148 |
GLContext glHGLRCHandleBkpCtx = 0; |
| 149 |
GLDisplay glHDCHandle = 0; |
| 150 |
OS_HANDLE glDeviceHandle = 0; |
| 151 |
OS_HANDLE glContextHandle = 0; |
| 152 |
|
| 153 |
// GL functions |
| 154 |
std::unique_ptr<OsLibrary> glLibrary; |
| 155 |
PFNOGLSetSharedOCLContextStateINTEL glSetSharedOCLContextState = nullptr; |
| 156 |
PFNOGLAcquireSharedBufferINTEL glAcquireSharedBuffer = nullptr; |
| 157 |
PFNOGLReleaseSharedBufferINTEL glReleaseSharedBuffer = nullptr; |
| 158 |
PFNOGLAcquireSharedRenderBufferINTEL glAcquireSharedRenderBuffer = nullptr; |
| 159 |
PFNOGLReleaseSharedRenderBufferINTEL glReleaseSharedRenderBuffer = nullptr; |
| 160 |
PFNOGLAcquireSharedTextureINTEL glAcquireSharedTexture = nullptr; |
| 161 |
PFNOGLReleaseSharedTextureINTEL glReleaseSharedTexture = nullptr; |
| 162 |
PFNOGLGetCurrentContext glGetCurrentContext = nullptr; |
| 163 |
PFNOGLGetCurrentDisplay glGetCurrentDisplay = nullptr; |
| 164 |
PFNglGetString glGetString = nullptr; |
| 165 |
PFNglGetStringi glGetStringi = nullptr; |
| 166 |
PFNglGetIntegerv glGetIntegerv = nullptr; |
| 167 |
PFNwglCreateContext pfnWglCreateContext = nullptr; |
| 168 |
PFNwglMakeCurrent wglMakeCurrent = nullptr; |
| 169 |
PFNwglShareLists pfnWglShareLists = nullptr; |
| 170 |
PFNwglDeleteContext pfnWglDeleteContext = nullptr; |
| 171 |
PFNOGLRetainSyncINTEL glRetainSync = nullptr; |
| 172 |
PFNOGLReleaseSyncINTEL glReleaseSync = nullptr; |
| 173 |
PFNOGLGetSyncivINTEL glGetSynciv = nullptr; |
| 174 |
PFNglArbSyncObjectSetup pfnGlArbSyncObjectSetup = nullptr; |
| 175 |
PFNglArbSyncObjectCleanup pfnGlArbSyncObjectCleanup = nullptr; |
| 176 |
PFNglArbSyncObjectSignal pfnGlArbSyncObjectSignal = nullptr; |
| 177 |
PFNglArbSyncObjectWaitServer pfnGlArbSyncObjectWaitServer = nullptr; |
| 178 |
PFNGLGETLUIDINTEL glGetLuid = nullptr; |
| 179 |
|
| 180 |
// support for GL_ARB_cl_event |
| 181 |
std::mutex glArbEventMutex; |
| 182 |
std::unordered_map<Event *, GlArbSyncEvent *> glArbEventMapping; |
| 183 |
}; |
| 184 |
|
| 185 |
template <typename EventType> |
| 186 |
inline auto GLSharingFunctionsWindows::getOrCreateGlArbSyncEvent(Event &baseEvent) -> decltype(EventType::create(baseEvent)) { |
| 187 |
std::lock_guard<std::mutex> lock{glArbEventMutex}; |
| 188 |
auto it = glArbEventMapping.find(&baseEvent); |
| 189 |
if (it != glArbEventMapping.end()) { |
| 190 |
return it->second; |
| 191 |
} |
| 192 |
|
| 193 |
auto arbEvent = EventType::create(baseEvent); |
| 194 |
if (nullptr == arbEvent) { |
| 195 |
return arbEvent; |
| 196 |
} |
| 197 |
glArbEventMapping[&baseEvent] = arbEvent; |
| 198 |
return arbEvent; |
| 199 |
} |
| 200 |
|
| 201 |
} // namespace NEO |