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

@@ -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 )