Skip to content
This repository was archived by the owner on Jan 26, 2024. It is now read-only.

Commit f4eb67b

Browse files
pghafariPayam Ghafari
authored andcommitted
SWDEV-245531 - GLInterop: linux - Buffer Interop
in progress... Change-Id: I6f2cd8ee19ffef93e4f2aa6a08e002a13722d78a
1 parent ed1d0a9 commit f4eb67b

File tree

1 file changed

+80
-6
lines changed

1 file changed

+80
-6
lines changed

src/hip_gl.cpp

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
#include <GL/gl.h>
2727
#include <GL/glext.h>
2828

29+
#ifndef _WIN32
30+
#include <GL/glx.h>
31+
#endif
32+
2933
namespace amd {
3034
static std::once_flag interopOnce;
3135
}
@@ -108,6 +112,59 @@ static inline bool getDeviceGLContext(HDC& dc, HGLRC& glrc) {
108112
}
109113
return false;
110114
}
115+
#else
116+
struct OCLGLHandle_ {
117+
static Display* display;
118+
static XVisualInfo* vInfo;
119+
static int referenceCount;
120+
GLXContext context;
121+
Window window;
122+
Colormap cmap;
123+
};
124+
125+
126+
#define DEFAULT_OPENGL "libGL.so"
127+
Display* OCLGLHandle_::display = nullptr;
128+
XVisualInfo* OCLGLHandle_::vInfo = nullptr;
129+
int OCLGLHandle_::referenceCount = 0;
130+
131+
static inline bool getDeviceGLContext(OCLGLHandle_* hGL) {
132+
133+
amd::GLFunctions* glenv_;
134+
void* h = amd::Os::loadLibrary(DEFAULT_OPENGL);
135+
136+
if (h && (glenv_ = new amd::GLFunctions(h, false))) {
137+
LogError("\n couldn't load opengl\n");
138+
return false;
139+
}
140+
141+
142+
hGL->display = XOpenDisplay(nullptr);
143+
if (hGL->display == nullptr) {
144+
printf("XOpenDisplay() failed\n");
145+
return false;
146+
}
147+
148+
if (hGL->vInfo == nullptr) {
149+
int dblBuf[] = {GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE,
150+
1, GLX_DEPTH_SIZE, 12, GLX_DOUBLEBUFFER, None};
151+
152+
hGL->vInfo = glenv_->glXChooseVisual_(hGL->display, DefaultScreen(hGL->display), dblBuf);
153+
if (hGL->vInfo == nullptr) {
154+
printf("glXChooseVisual() failed\n");
155+
return false;
156+
}
157+
}
158+
hGL->referenceCount++;
159+
160+
hGL->context = glenv_->glXCreateContext_(hGL->display, hGL->vInfo, None, True);
161+
if (hGL->context == nullptr) {
162+
printf("glXCreateContext() failed\n");
163+
return false;
164+
}
165+
166+
return true;
167+
}
111168
#endif
112169

113170
// Sets up GL context association with amd context.
@@ -132,6 +189,29 @@ void setupGLInteropOnce() {
132189
(cl_context_properties)dc,
133190
0};
134191

192+
193+
194+
#else
195+
OCLGLHandle_* hGL = new OCLGLHandle_;
196+
197+
hGL->context = nullptr;
198+
hGL->window = 0;
199+
hGL->cmap = 0;
200+
201+
if (!getDeviceGLContext(hGL)) {
202+
LogError(" Context setup failed \n");
203+
return;
204+
}
205+
206+
cl_context_properties properties[] = {CL_CONTEXT_PLATFORM,
207+
(cl_context_properties)AMD_PLATFORM,
208+
CL_GL_CONTEXT_KHR,
209+
(cl_context_properties)hGL->context,
210+
CL_GLX_DISPLAY_KHR,
211+
(cl_context_properties)hGL->display,
212+
0};
213+
#endif
214+
135215
amd::Context::Info info;
136216
if (CL_SUCCESS != amd::Context::checkProperties(properties, &info)) {
137217
LogError("Context setup failed \n");
@@ -142,12 +222,6 @@ void setupGLInteropOnce() {
142222
if (CL_SUCCESS != amdContext->create(properties)) {
143223
LogError("Context setup failed \n");
144224
}
145-
146-
#else
147-
// INTEROP_TODO: Add support for Linux
148-
LogError(" Only Windows platform is supported \n");
149-
return;
150-
#endif
151225
}
152226

153227
static inline hipError_t hipSetInteropObjects(int num_objects, void** mem_objects,

0 commit comments

Comments
 (0)