From 65fcb6a0aa8c247804cbf54243bc76aadd2cbc17 Mon Sep 17 00:00:00 2001 From: pghafari Date: Wed, 16 Jun 2021 14:17:23 -0400 Subject: [PATCH] SWDEV-245531 - GLInterop: linux - Buffer Interop in progress... Change-Id: I6f2cd8ee19ffef93e4f2aa6a08e002a13722d78a --- hipamd/src/hip_gl.cpp | 86 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 6 deletions(-) diff --git a/hipamd/src/hip_gl.cpp b/hipamd/src/hip_gl.cpp index 45ba983e3c..4a9f3c08bf 100644 --- a/hipamd/src/hip_gl.cpp +++ b/hipamd/src/hip_gl.cpp @@ -26,6 +26,10 @@ #include #include +#ifndef _WIN32 +#include +#endif + namespace amd { static std::once_flag interopOnce; } @@ -108,6 +112,59 @@ static inline bool getDeviceGLContext(HDC& dc, HGLRC& glrc) { } return false; } +#else +struct OCLGLHandle_ { + static Display* display; + static XVisualInfo* vInfo; + static int referenceCount; + GLXContext context; + Window window; + Colormap cmap; +}; + + +#define DEFAULT_OPENGL "libGL.so" +Display* OCLGLHandle_::display = nullptr; +XVisualInfo* OCLGLHandle_::vInfo = nullptr; +int OCLGLHandle_::referenceCount = 0; + +static inline bool getDeviceGLContext(OCLGLHandle_* hGL) { + + amd::GLFunctions* glenv_; + void* h = amd::Os::loadLibrary(DEFAULT_OPENGL); + + if (h && (glenv_ = new amd::GLFunctions(h, false))) { + LogError("\n couldn't load opengl\n"); + return false; + } + + + hGL->display = XOpenDisplay(nullptr); + if (hGL->display == nullptr) { + printf("XOpenDisplay() failed\n"); + return false; + } + + if (hGL->vInfo == nullptr) { + int dblBuf[] = {GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, + 1, GLX_DEPTH_SIZE, 12, GLX_DOUBLEBUFFER, None}; + + hGL->vInfo = glenv_->glXChooseVisual_(hGL->display, DefaultScreen(hGL->display), dblBuf); + if (hGL->vInfo == nullptr) { + printf("glXChooseVisual() failed\n"); + return false; + } + } + hGL->referenceCount++; + + hGL->context = glenv_->glXCreateContext_(hGL->display, hGL->vInfo, None, True); + if (hGL->context == nullptr) { + printf("glXCreateContext() failed\n"); + return false; + } + + return true; +} #endif // Sets up GL context association with amd context. @@ -132,6 +189,29 @@ void setupGLInteropOnce() { (cl_context_properties)dc, 0}; + + +#else + OCLGLHandle_* hGL = new OCLGLHandle_; + + hGL->context = nullptr; + hGL->window = 0; + hGL->cmap = 0; + + if (!getDeviceGLContext(hGL)) { + LogError(" Context setup failed \n"); + return; + } + + cl_context_properties properties[] = {CL_CONTEXT_PLATFORM, + (cl_context_properties)AMD_PLATFORM, + CL_GL_CONTEXT_KHR, + (cl_context_properties)hGL->context, + CL_GLX_DISPLAY_KHR, + (cl_context_properties)hGL->display, + 0}; +#endif + amd::Context::Info info; if (CL_SUCCESS != amd::Context::checkProperties(properties, &info)) { LogError("Context setup failed \n"); @@ -142,12 +222,6 @@ void setupGLInteropOnce() { if (CL_SUCCESS != amdContext->create(properties)) { LogError("Context setup failed \n"); } - - #else - // INTEROP_TODO: Add support for Linux - LogError(" Only Windows platform is supported \n"); - return; -#endif } static inline hipError_t hipSetInteropObjects(int num_objects, void** mem_objects,