1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include "setup.h"
17 #include "harness/errorHelpers.h"
18 #include <OpenGL/CGLDevice.h>
19
20 class OSXGLEnvironment : public GLEnvironment
21 {
22 public:
OSXGLEnvironment()23 OSXGLEnvironment()
24 {
25 mCGLContext = NULL;
26 }
27
Init(int * argc,char ** argv,int use_opengl_32)28 virtual int Init( int *argc, char **argv, int use_opengl_32 )
29 {
30 if (!use_opengl_32) {
31
32 // Create a GLUT window to render into
33 glutInit( argc, argv );
34 glutInitWindowSize( 512, 512 );
35 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
36 glutCreateWindow( "OpenCL <-> OpenGL Test" );
37 }
38
39 else {
40
41 CGLPixelFormatAttribute attribs[] = {
42 kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute)kCGLOGLPVersion_3_2_Core,
43 kCGLPFAAllowOfflineRenderers,
44 kCGLPFANoRecovery,
45 kCGLPFAAccelerated,
46 kCGLPFADoubleBuffer,
47 (CGLPixelFormatAttribute)0
48 };
49
50 CGLError err;
51 CGLPixelFormatObj pix;
52 GLint npix;
53 err = CGLChoosePixelFormat (attribs, &pix, &npix);
54 if(err != kCGLNoError)
55 {
56 log_error("Failed to choose pixel format\n");
57 return -1;
58 }
59 err = CGLCreateContext(pix, NULL, &mCGLContext);
60 if(err != kCGLNoError)
61 {
62 log_error("Failed to create GL context\n");
63 return -1;
64 }
65 CGLSetCurrentContext(mCGLContext);
66 }
67
68 return 0;
69 }
70
CreateCLContext(void)71 virtual cl_context CreateCLContext( void )
72 {
73 int error;
74
75 if( mCGLContext == NULL )
76 mCGLContext = CGLGetCurrentContext();
77
78 CGLShareGroupObj share_group = CGLGetShareGroup(mCGLContext);
79 cl_context_properties properties[] = { CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)share_group, 0 };
80 cl_context context = clCreateContext(properties, 0, 0, 0, 0, &error);
81 if (error) {
82 print_error(error, "clCreateContext failed");
83 return NULL;
84 }
85
86 // Verify that all devices in the context support the required extension
87 cl_device_id devices[64];
88 size_t size_out;
89 error = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(devices), devices, &size_out);
90 if (error) {
91 print_error(error, "clGetContextInfo failed");
92 return NULL;
93 }
94
95 for (int i=0; i<(int)(size_out/sizeof(cl_device_id)); i++) {
96 if (!is_extension_available(devices[i], "cl_APPLE_gl_sharing")) {
97 log_error("Device %d does not support required extension cl_APPLE_gl_sharing.\n", i);
98 return NULL;
99 }
100 }
101 return context;
102 }
103
SupportsCLGLInterop(cl_device_type device_type)104 virtual int SupportsCLGLInterop( cl_device_type device_type )
105 {
106 int found_valid_device = 0;
107 cl_device_id devices[64];
108 cl_uint num_of_devices;
109 int error;
110 error = clGetDeviceIDs(NULL, device_type, 64, devices, &num_of_devices);
111 if (error) {
112 print_error(error, "clGetDeviceIDs failed");
113 return -1;
114 }
115
116 for (int i=0; i<(int)num_of_devices; i++) {
117 if (!is_extension_available(devices[i], "cl_APPLE_gl_sharing")) {
118 log_info("Device %d of %d does not support required extension cl_APPLE_gl_sharing.\n", i, num_of_devices);
119 } else {
120 log_info("Device %d of %d does support required extension cl_APPLE_gl_sharing.\n", i, num_of_devices);
121 found_valid_device = 1;
122 }
123 }
124 return found_valid_device;
125 }
126
~OSXGLEnvironment()127 virtual ~OSXGLEnvironment()
128 {
129 CGLDestroyContext( mCGLContext );
130 }
131
132 CGLContextObj mCGLContext;
133
134 };
135
Instance(void)136 GLEnvironment * GLEnvironment::Instance( void )
137 {
138 static OSXGLEnvironment * env = NULL;
139 if( env == NULL )
140 env = new OSXGLEnvironment();
141 return env;
142 }
143