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 "testBase.h"
17 #include "common.h"
18 
19 #if defined( __APPLE__ )
20     #include <OpenGL/glu.h>
21 #else
22     #include <GL/glu.h>
23     #include <CL/cl_gl.h>
24 #endif
25 
26 extern int supportsHalf(cl_context context, bool* supports_half);
27 
test_image_info(cl_context context,cl_command_queue queue,GLenum glTarget,GLuint glTexture,size_t imageWidth,size_t imageHeight,size_t imageDepth,cl_image_format * outFormat,ExplicitType * outType,void ** outResultBuffer)28 static int test_image_info( cl_context context, cl_command_queue queue,
29   GLenum glTarget, GLuint glTexture, size_t imageWidth, size_t imageHeight,
30   size_t imageDepth, cl_image_format *outFormat, ExplicitType *outType,
31   void **outResultBuffer )
32 {
33   clMemWrapper streams[ 2 ];
34 
35   int error;
36 
37   // Create a CL image from the supplied GL texture
38   streams[ 0 ] = (*clCreateFromGLTexture_ptr)( context, CL_MEM_READ_ONLY,
39     glTarget, 0, glTexture, &error );
40   if( error != CL_SUCCESS )
41   {
42     print_error( error, "Unable to create CL image from GL texture" );
43     GLint fmt;
44     glGetTexLevelParameteriv( glTarget, 0, GL_TEXTURE_INTERNAL_FORMAT, &fmt );
45     log_error( "    Supplied GL texture was format %s\n", GetGLFormatName( fmt ) );
46     return error;
47   }
48 
49   // Determine data type and format that CL came up with
50   error = clGetImageInfo( streams[ 0 ], CL_IMAGE_FORMAT,
51     sizeof( cl_image_format ), outFormat, NULL );
52   test_error( error, "Unable to get CL image format" );
53 
54   cl_gl_object_type object_type;
55   switch (glTarget) {
56     case GL_TEXTURE_1D:
57       object_type = CL_GL_OBJECT_TEXTURE1D;
58       break;
59     case GL_TEXTURE_BUFFER:
60       object_type = CL_GL_OBJECT_TEXTURE_BUFFER;
61       break;
62     case GL_TEXTURE_1D_ARRAY:
63       object_type = CL_GL_OBJECT_TEXTURE1D_ARRAY;
64       break;
65     case GL_TEXTURE_2D:
66     case GL_TEXTURE_RECTANGLE_EXT:
67     case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
68     case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
69     case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
70     case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
71     case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
72     case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
73       object_type = CL_GL_OBJECT_TEXTURE2D;
74       break;
75     case GL_TEXTURE_2D_ARRAY:
76       object_type = CL_GL_OBJECT_TEXTURE2D_ARRAY;
77       break;
78     case GL_TEXTURE_3D:
79       object_type = CL_GL_OBJECT_TEXTURE3D;
80       break;
81     default:
82       log_error("Unsupported texture target.");
83       return 1;
84   }
85 
86   return CheckGLObjectInfo(streams[0], object_type, glTexture, glTarget, 0);
87 }
88 
test_image_format_get_info(cl_context context,cl_command_queue queue,size_t width,size_t height,size_t depth,GLenum target,struct format * fmt,MTdata data)89 static int test_image_format_get_info(
90     cl_context context, cl_command_queue queue,
91     size_t width, size_t height, size_t depth,
92     GLenum target, struct format* fmt, MTdata data)
93 {
94   int error = 0;
95 
96   // If we're testing a half float format, then we need to determine the
97   // rounding mode of this machine.  Punt if we fail to do so.
98 
99   if( fmt->type == kHalf )
100   {
101     if( DetectFloatToHalfRoundingMode(queue) )
102       return 0;
103     bool supports_half = false;
104     error = supportsHalf(context, &supports_half);
105     if( error != 0 )
106       return error;
107     if (!supports_half) return 0;
108   }
109 
110   size_t w = width, h = height, d = depth;
111 
112   // Unpack the format and use it, along with the target, to create an
113   // appropriate GL texture.
114 
115   GLenum gl_fmt          = fmt->formattype;
116   GLenum gl_internal_fmt = fmt->internal;
117   GLenum gl_type         = fmt->datatype;
118   ExplicitType type      = fmt->type;
119 
120   glTextureWrapper texture;
121   glBufferWrapper glbuf;
122 
123   // If we're testing a half float format, then we need to determine the
124   // rounding mode of this machine.  Punt if we fail to do so.
125 
126   if( fmt->type == kHalf )
127     if( DetectFloatToHalfRoundingMode(queue) )
128       return 1;
129 
130   // Use the correct texture creation function depending on the target, and
131   // adjust width, height, depth as appropriate so subsequent size calculations
132   // succeed.
133 
134   switch (target) {
135     case GL_TEXTURE_1D:
136       h = 1; d = 1;
137       CreateGLTexture1D( width, target, gl_fmt,
138         gl_internal_fmt, gl_type, type, &texture, &error, false, data );
139       break;
140     case GL_TEXTURE_BUFFER:
141       h = 1; d = 1;
142       CreateGLTextureBuffer( width, target, gl_fmt,
143         gl_internal_fmt, gl_type, type, &texture, &glbuf, &error, false, data );
144       break;
145     case GL_TEXTURE_1D_ARRAY:
146       d = 1;
147       CreateGLTexture1DArray( width, height, target, gl_fmt,
148         gl_internal_fmt, gl_type, type, &texture, &error, false, data );
149       break;
150     case GL_TEXTURE_RECTANGLE_EXT:
151     case GL_TEXTURE_2D:
152     case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
153     case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
154     case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
155     case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
156     case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
157     case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
158       d = 1;
159       CreateGLTexture2D( width, height, target, gl_fmt,
160         gl_internal_fmt, gl_type, type, &texture, &error, false, data );
161       break;
162     case GL_TEXTURE_2D_ARRAY:
163       CreateGLTexture2DArray( width, height, depth, target, gl_fmt,
164         gl_internal_fmt, gl_type, type, &texture, &error, false, data );
165       break;
166     case GL_TEXTURE_3D:
167       d = 1;
168       CreateGLTexture3D( width, height, depth, target, gl_fmt,
169         gl_internal_fmt, gl_type, type, &texture, &error, data, false );
170       break;
171     default:
172       log_error("Unsupported texture target.\n");
173       return 1;
174   }
175 
176   if ( error == -2 ) {
177     log_info("OpenGL texture couldn't be created, because a texture is too big. Skipping test.\n");
178     return 0;
179   }
180 
181   if ( error != 0 ) {
182     if ((gl_fmt == GL_RGBA_INTEGER_EXT) && (!CheckGLIntegerExtensionSupport())) {
183       log_info("OpenGL version does not support GL_RGBA_INTEGER_EXT. "
184         "Skipping test.\n");
185       return 0;
186     } else {
187       return error;
188     }
189   }
190 
191   cl_image_format clFormat;
192   ExplicitType actualType;
193   char *outBuffer;
194 
195   // Perform the info check:
196   return test_image_info( context, queue, target, texture, w, h, d, &clFormat,
197     &actualType, (void **)&outBuffer );
198 }
199 
test_images_get_info_common(cl_device_id device,cl_context context,cl_command_queue queue,struct format * formats,size_t nformats,GLenum * targets,size_t ntargets,sizevec_t * sizes,size_t nsizes)200 int test_images_get_info_common( cl_device_id device, cl_context context,
201   cl_command_queue queue, struct format* formats, size_t nformats,
202   GLenum *targets, size_t ntargets, sizevec_t *sizes, size_t nsizes )
203 {
204   int error = 0;
205   RandomSeed seed(gRandomSeed);
206 
207   // First, ensure this device supports images.
208 
209   if (checkForImageSupport(device)) {
210     log_info("Device does not support images.  Skipping test.\n");
211     return 0;
212   }
213 
214   size_t fidx, tidx, sidx;
215 
216   // Test each format on every target, every size.
217 
218   for ( fidx = 0; fidx < nformats; fidx++ ) {
219     for ( tidx = 0; tidx < ntargets; tidx++ ) {
220 
221       if ( formats[ fidx ].datatype == GL_UNSIGNED_INT_2_10_10_10_REV )
222       {
223         // Check if the RGB 101010 format is supported
224         if ( is_rgb_101010_supported( context, targets[ tidx ] ) == 0 )
225           break; // skip
226       }
227 
228       log_info( "Testing image info for GL format %s : %s : %s : %s\n",
229         GetGLTargetName( targets[ tidx ] ),
230         GetGLFormatName( formats[ fidx ].internal ),
231         GetGLBaseFormatName( formats[ fidx ].formattype ),
232         GetGLTypeName( formats[ fidx ].datatype ) );
233 
234       for ( sidx = 0; sidx < nsizes; sidx++ ) {
235 
236         // Test this format + size:
237 
238         if ( test_image_format_get_info(context, queue,
239                                         sizes[sidx].width, sizes[sidx].height, sizes[sidx].depth,
240                                         targets[tidx], &formats[fidx], seed) )
241         {
242           // We land here in the event of test failure.
243 
244           log_error( "ERROR: Image info test failed for %s : %s : %s : %s\n\n",
245             GetGLTargetName( targets[ tidx ] ),
246             GetGLFormatName( formats[ fidx ].internal ),
247             GetGLBaseFormatName( formats[ fidx ].formattype ),
248             GetGLTypeName( formats[ fidx ].datatype ) );
249           error++;
250 
251           // Skip the other sizes for this format.
252 
253           break;
254         }
255       }
256     }
257   }
258 
259   return error;
260 }
261