Added comparability verification for GL associated devices query (#2231)

Fixes #1485 according to work plan from issue description.
This commit is contained in:
Marcin Hajder
2025-06-10 17:41:20 +02:00
committed by GitHub
parent f209922722
commit 3233d2089f
8 changed files with 285 additions and 92 deletions

View File

@@ -13,12 +13,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef _setup_h
#define _setup_h
#ifndef _gl_setup_h
#define _gl_setup_h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include "gl_headers.h"
#ifdef __APPLE__
#include <OpenCL/opencl.h>
@@ -36,13 +37,11 @@ class GLEnvironment
GLEnvironment() {}
virtual ~GLEnvironment() {}
virtual int Init( int *argc, char **argv, int use_opengl_32 ) = 0;
virtual int Init(int *argc, char **argv, int use_opengl_32) = 0;
virtual cl_context CreateCLContext( void ) = 0;
virtual int SupportsCLGLInterop( cl_device_type device_type) = 0;
static GLEnvironment * Instance( void );
static GLEnvironment *Instance(void);
};
#endif // _setup_h
#endif // _gl_setup_h

View File

@@ -1,6 +1,6 @@
//
// Copyright (c) 2017 The Khronos Group Inc.
//
// Copyright (c) 2024 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
@@ -25,65 +25,74 @@ private:
public:
OSXGLEnvironment()
{
mCGLContext = NULL;
mIsGlutInit = false;
mCGLContext = NULL;
mShareGroup = NULL;
mPlatform = NULL;
}
virtual int Init( int *argc, char **argv, int use_opengl_32 )
int Init(int *argc, char **argv, int use_opengl_32) override
{
if (!use_opengl_32)
{
if (!use_opengl_32) {
if (!mIsGlutInit)
{
// Create a GLUT window to render into
glutInit(argc, argv);
glutInitWindowSize(512, 512);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("OpenCL <-> OpenGL Test");
mIsGlutInit = true;
}
}
else {
CGLPixelFormatAttribute attribs[] = {
kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute)kCGLOGLPVersion_3_2_Core,
kCGLPFAAllowOfflineRenderers,
kCGLPFANoRecovery,
kCGLPFAAccelerated,
kCGLPFADoubleBuffer,
(CGLPixelFormatAttribute)0
};
CGLError err;
CGLPixelFormatObj pix;
GLint npix;
err = CGLChoosePixelFormat (attribs, &pix, &npix);
if(err != kCGLNoError)
{
log_error("Failed to choose pixel format\n");
return -1;
}
err = CGLCreateContext(pix, NULL, &mCGLContext);
if(err != kCGLNoError)
{
log_error("Failed to create GL context\n");
return -1;
}
CGLSetCurrentContext(mCGLContext);
}
return 0;
if (!mIsGlutInit)
{
// Create a GLUT window to render into
glutInit(argc, argv);
glutInitWindowSize(512, 512);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("OpenCL <-> OpenGL Test");
mIsGlutInit = true;
}
}
virtual cl_context CreateCLContext( void )
else
{
CGLPixelFormatAttribute attribs[] = {
kCGLPFAOpenGLProfile,
(CGLPixelFormatAttribute)kCGLOGLPVersion_3_2_Core,
kCGLPFAAllowOfflineRenderers,
kCGLPFANoRecovery,
kCGLPFAAccelerated,
kCGLPFADoubleBuffer,
(CGLPixelFormatAttribute)0
};
CGLError err;
CGLPixelFormatObj pix;
GLint npix;
err = CGLChoosePixelFormat(attribs, &pix, &npix);
if (err != kCGLNoError)
{
log_error("Failed to choose pixel format\n");
return -1;
}
err = CGLCreateContext(pix, NULL, &mCGLContext);
if (err != kCGLNoError)
{
log_error("Failed to create GL context\n");
return -1;
}
CGLSetCurrentContext(mCGLContext);
}
return 0;
}
cl_context CreateCLContext(void) override
{
int error;
if( mCGLContext == NULL )
mCGLContext = CGLGetCurrentContext();
CGLShareGroupObj share_group = CGLGetShareGroup(mCGLContext);
cl_context_properties properties[] = { CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)share_group, 0 };
mShareGroup = CGLGetShareGroup(mCGLContext);
cl_context_properties properties[] = {
CL_CONTEXT_PLATFORM, (cl_context_properties)mPlatform,
CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
(cl_context_properties)mShareGroup, 0
};
cl_context context = clCreateContext(properties, 0, 0, 0, 0, &error);
if (error) {
print_error(error, "clCreateContext failed");
@@ -108,16 +117,24 @@ public:
return context;
}
virtual int SupportsCLGLInterop( cl_device_type device_type )
int SupportsCLGLInterop(cl_device_type device_type) override
{
int found_valid_device = 0;
cl_device_id devices[64];
cl_uint num_of_devices;
int error;
error = clGetDeviceIDs(NULL, device_type, 64, devices, &num_of_devices);
error = clGetPlatformIDs(1, &mPlatform, NULL);
if (error)
{
print_error(error, "clGetPlatformIDs failed");
return 0;
}
error =
clGetDeviceIDs(mPlatform, device_type, 64, devices, &num_of_devices);
if (error) {
print_error(error, "clGetDeviceIDs failed");
return -1;
return 0;
}
for (int i=0; i<(int)num_of_devices; i++) {
@@ -131,13 +148,11 @@ public:
return found_valid_device;
}
virtual ~OSXGLEnvironment()
{
CGLDestroyContext( mCGLContext );
}
CGLContextObj mCGLContext;
virtual ~OSXGLEnvironment() { CGLDestroyContext(mCGLContext); }
CGLContextObj mCGLContext;
CGLShareGroupObj mShareGroup;
cl_platform_id mPlatform;
};
GLEnvironment * GLEnvironment::Instance( void )

View File

@@ -1,6 +1,6 @@
//
// Copyright (c) 2017 The Khronos Group Inc.
//
// Copyright (c) 2024 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
@@ -42,14 +42,19 @@ private:
cl_platform_id m_platform;
bool m_is_glut_init;
HGLRC m_hGLRC;
HDC m_hDC;
public:
WGLEnvironment()
{
m_device_count = 0;
m_platform = 0;
m_is_glut_init = false;
m_hGLRC = 0;
m_hDC = 0;
}
virtual int Init( int *argc, char **argv, int use_opengl_32 )
int Init(int *argc, char **argv, int use_opengl_32) override
{
if (!m_is_glut_init)
{
@@ -64,21 +69,25 @@ public:
return 0;
}
virtual cl_context CreateCLContext( void )
cl_context CreateCLContext(void) override
{
HGLRC hGLRC = wglGetCurrentContext();
HDC hDC = wglGetCurrentDC();
m_hGLRC = wglGetCurrentContext();
m_hDC = wglGetCurrentDC();
cl_context_properties properties[] = {
CL_CONTEXT_PLATFORM, (cl_context_properties) m_platform,
CL_GL_CONTEXT_KHR, (cl_context_properties) hGLRC,
CL_WGL_HDC_KHR, (cl_context_properties) hDC,
CL_CONTEXT_PLATFORM,
(cl_context_properties)m_platform,
CL_GL_CONTEXT_KHR,
(cl_context_properties)m_hGLRC,
CL_WGL_HDC_KHR,
(cl_context_properties)m_hDC,
0
};
cl_device_id devices[MAX_DEVICES];
size_t dev_size;
cl_int status;
if (!hGLRC || !hDC) {
if (!m_hGLRC || !m_hDC)
{
print_error(CL_INVALID_CONTEXT, "No GL context bound");
return 0;
}
@@ -155,7 +164,7 @@ public:
return clCreateContext(properties, 1, &ctxDevice, NULL, NULL, &status);
}
virtual int SupportsCLGLInterop( cl_device_type device_type )
int SupportsCLGLInterop(cl_device_type device_type) override
{
cl_device_id devices[MAX_DEVICES];
cl_uint num_of_devices;

View File

@@ -28,13 +28,21 @@ private:
cl_uint m_device_count;
bool m_glut_init;
cl_platform_id m_platform;
GLXContext m_context;
Display *m_dpy;
public:
X11GLEnvironment()
{
m_device_count = 0;
m_glut_init = false;
m_platform = 0;
m_context = 0;
m_dpy = nullptr;
}
virtual int Init( int *argc, char **argv, int use_opencl_32 )
int Init(int *argc, char **argv, int use_opencl_32) override
{
// Create a GLUT window to render into
if (!m_glut_init)
@@ -49,19 +57,24 @@ public:
return 0;
}
virtual cl_context CreateCLContext( void )
cl_context CreateCLContext(void) override
{
GLXContext context = glXGetCurrentContext();
Display *dpy = glXGetCurrentDisplay();
m_context = glXGetCurrentContext();
m_dpy = glXGetCurrentDisplay();
cl_context_properties properties[] = {
CL_GL_CONTEXT_KHR, (cl_context_properties) context,
CL_GLX_DISPLAY_KHR, (cl_context_properties) dpy,
CL_CONTEXT_PLATFORM,
(cl_context_properties)m_platform,
CL_GL_CONTEXT_KHR,
(cl_context_properties)m_context,
CL_GLX_DISPLAY_KHR,
(cl_context_properties)m_dpy,
0
};
cl_int status;
if (!context || !dpy) {
if (!m_context || !m_dpy)
{
print_error(CL_INVALID_CONTEXT, "No GL context bound");
return 0;
}
@@ -69,19 +82,19 @@ public:
return clCreateContext(properties, 1, m_devices, NULL, NULL, &status);
}
virtual int SupportsCLGLInterop( cl_device_type device_type )
int SupportsCLGLInterop(cl_device_type device_type) override
{
int found_valid_device = 0;
cl_platform_id platform;
cl_device_id devices[64];
cl_uint num_of_devices;
int error;
error = clGetPlatformIDs(1, &platform, NULL);
error = clGetPlatformIDs(1, &m_platform, NULL);
if (error) {
print_error(error, "clGetPlatformIDs failed");
return -1;
}
error = clGetDeviceIDs(platform, device_type, 64, devices, &num_of_devices);
error = clGetDeviceIDs(m_platform, device_type, 64, devices,
&num_of_devices);
// If this platform doesn't have any of the requested device_type (namely GPUs) then return 0
if (error == CL_DEVICE_NOT_FOUND)
return 0;