From 86ac9c8bb57fffa21517bcf3c068ff4dd6f2d01f Mon Sep 17 00:00:00 2001 From: pghafari Date: Mon, 4 Oct 2021 13:34:09 -0400 Subject: [PATCH] SWDEV-245531 - GLInterop: removing GL dependency Change-Id: I58133e330f5f5e90965fc05003c2d901d4ae6382 [ROCm/clr commit: 1cb0641831c13eeaf431e2744797abfc6c12fd9e] --- .../clr/rocclr/platform/command_utils.hpp | 4 + projects/clr/rocclr/platform/context.cpp | 80 +++++++++++++------ 2 files changed, 61 insertions(+), 23 deletions(-) diff --git a/projects/clr/rocclr/platform/command_utils.hpp b/projects/clr/rocclr/platform/command_utils.hpp index 388f20e4ef..254485bc95 100644 --- a/projects/clr/rocclr/platform/command_utils.hpp +++ b/projects/clr/rocclr/platform/command_utils.hpp @@ -31,4 +31,8 @@ #define ROCCLR_STREAM_WAIT_VALUE_AND 0x2 #define ROCCLR_STREAM_WAIT_VALUE_NOR 0x3 +#define ROCCLR_HIP_GL_CONTEXT_KHR 0x2100 +#define ROCCLR_HIP_GLX_DISPLAY_KHR 0x2101 +#define ROCCLR_HIP_WGL_HDC_KHR 0x2102 + #endif // COMMAND_UTILS_HPP_ \ No newline at end of file diff --git a/projects/clr/rocclr/platform/context.cpp b/projects/clr/rocclr/platform/context.cpp index 27450cb213..50e1eafef3 100644 --- a/projects/clr/rocclr/platform/context.cpp +++ b/projects/clr/rocclr/platform/context.cpp @@ -169,21 +169,25 @@ int Context::checkProperties(const cl_context_properties* properties, Context::I #if defined(__linux__) case CL_GLX_DISPLAY_KHR: #endif // linux + if (p->ptr == NULL) { + return CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR; + } + case ROCCLR_HIP_GLX_DISPLAY_KHR: + case ROCCLR_HIP_WGL_HDC_KHR: info->hDev_[GLDeviceKhrIdx] = p->ptr; - + break; #if defined(__APPLE__) || defined(__MACOSX) case CL_CGL_SHAREGROUP_KHR: Unimplemented(); break; #endif //__APPLE__ || MACOS - case CL_GL_CONTEXT_KHR: if (p->ptr == NULL) { return CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR; } - if (p->name == CL_GL_CONTEXT_KHR) { - info->hCtx_ = p->ptr; - } + //skip the null case in the case of hip-gl, it will be initialized in create + case ROCCLR_HIP_GL_CONTEXT_KHR: + info->hCtx_ = p->ptr; info->flags_ |= GLDeviceKhr; break; case CL_CONTEXT_PLATFORM: @@ -223,6 +227,49 @@ int Context::create(const intptr_t* properties) { ::memcpy(properties_, properties, info().propertiesSize_); } + // if the context passed in is null, it's the GL interop case and we need to get the current context + if (info_.hCtx_ == nullptr) { + if (info_.flags_ & GLDeviceKhr) { + // Init context for GL interop + if (glenv_ == NULL) { + HMODULE h = (HMODULE)Os::loadLibrary( +#ifdef _WIN32 + "OpenGL32.dll" +#else //!_WIN32 + "libGL.so.1" +#endif //!_WIN32 + ); + if (h && (glenv_ = new GLFunctions(h, (info_.flags_ & Flags::EGLDeviceKhr) != 0))) { +#ifdef _WIN32 + info_.hCtx_ = (void*)glenv_->wglGetCurrentContext_(); + info_.hDev_[GLDeviceKhrIdx] = (void*)glenv_->wglGetCurrentDC_(); + +#else + info_.hCtx_ = (void*)glenv_->glXGetCurrentContext_(); + info_.hDev_[GLDeviceKhrIdx] = (void*)glenv_->glXGetCurrentDisplay_(); +#endif + } + } + + struct Element { + intptr_t name; + void* ptr; + }* p = reinterpret_cast(properties_); + while (p->name != 0) { + switch (p->name) { + case ROCCLR_HIP_GLX_DISPLAY_KHR: + case ROCCLR_HIP_WGL_HDC_KHR: + p->ptr = info_.hDev_[GLDeviceKhrIdx]; + break; + case ROCCLR_HIP_GL_CONTEXT_KHR: + p->ptr = info_.hCtx_; + break; + } + p++; + } + } + } + // Check if OCL context can be associated with any external device if (info_.flags_ & (D3D10DeviceKhr | D3D11DeviceKhr | GLDeviceKhr | D3D9DeviceKhr | D3D9DeviceEXKhr | D3D9DeviceVAKhr)) { @@ -247,24 +294,11 @@ int Context::create(const intptr_t* properties) { } } else { if (info_.flags_ & GLDeviceKhr) { - // Init context for GL interop - if (glenv_ == NULL) { - HMODULE h = (HMODULE)Os::loadLibrary( -#ifdef _WIN32 - "OpenGL32.dll" -#else //!_WIN32 - "libGL.so.1" -#endif //!_WIN32 - ); - - if (h && (glenv_ = new GLFunctions(h, (info_.flags_ & Flags::EGLDeviceKhr) != 0))) { - if (!glenv_->init(reinterpret_cast(info_.hDev_[GLDeviceKhrIdx]), - reinterpret_cast(info_.hCtx_))) { - delete glenv_; - glenv_ = NULL; - result = CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR; - } - } + if (!glenv_->init(reinterpret_cast(info_.hDev_[GLDeviceKhrIdx]), + reinterpret_cast(info_.hCtx_))) { + delete glenv_; + glenv_ = NULL; + result = CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR; } } }