• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <float.h>
18 
19 #if defined( __APPLE__ )
20     #include <signal.h>
21     #include <sys/signal.h>
22     #include <setjmp.h>
23 #endif
24 
25 extern bool gTestReadWrite;
26 
27 const char *read2DKernelSourcePattern =
28 "__kernel void sample_kernel( read_only %s input, sampler_t sampler, __global int *results )\n"
29 "{\n"
30 "   int tidX = get_global_id(0), tidY = get_global_id(1);\n"
31 "   int offset = tidY*get_image_width(input) + tidX;\n"
32 "   int2 coords = (int2)(tidX, tidY);\n"
33 "   %s clr = read_image%s( input, coords );\n"
34 "   int4 test = (clr != read_image%s( input, sampler, coords ));\n"
35 "   if ( test.x || test.y || test.z || test.w )\n"
36 "      results[offset] = -1;\n"
37 "   else\n"
38 "      results[offset] = 0;\n"
39 "}";
40 
41 
42 const char *read_write2DKernelSourcePattern =
43 "__kernel void sample_kernel( read_only %s read_only_image, read_write %s read_write_image, sampler_t sampler, __global int *results )\n"
44 "{\n"
45 "   int tidX = get_global_id(0), tidY = get_global_id(1);\n"
46 "   int offset = tidY*get_image_width(read_only_image) + tidX;\n"
47 "   int2 coords = (int2)(tidX, tidY);\n"
48 "   %s clr = read_image%s( read_only_image, sampler, coords );\n"
49 "   write_image%s(read_write_image, coords, clr);\n"
50 "   atomic_work_item_fence(CLK_IMAGE_MEM_FENCE, memory_order_acq_rel, memory_scope_work_item);\n"
51 "   int4 test = (clr != read_image%s( read_write_image, coords ));\n"
52 "   if ( test.x || test.y || test.z || test.w )\n"
53 "      results[offset] = -1;\n"
54 "   else\n"
55 "      results[offset] = 0;\n"
56 "}";
test_read_image_2D(cl_context context,cl_command_queue queue,cl_kernel kernel,image_descriptor * imageInfo,image_sampler_data * imageSampler,ExplicitType outputType,MTdata d)57 int test_read_image_2D( cl_context context, cl_command_queue queue, cl_kernel kernel,
58                         image_descriptor *imageInfo, image_sampler_data *imageSampler,
59                         ExplicitType outputType, MTdata d )
60 {
61     int error;
62     size_t threads[2];
63     cl_sampler actualSampler;
64 
65     // generate_random_image_data allocates with malloc, so we use a MallocDataBuffer here
66     BufferOwningPtr<char> imageValues;
67     generate_random_image_data( imageInfo, imageValues, d );
68 
69     if ( gDebugTrace )
70         log_info( " - Creating image %d by %d...\n", (int)imageInfo->width, (int)imageInfo->height );
71 
72     // Construct testing sources
73     cl_mem read_only_image, read_write_image;
74     cl_image_desc image_desc;
75 
76     memset(&image_desc, 0x0, sizeof(cl_image_desc));
77     image_desc.image_type = CL_MEM_OBJECT_IMAGE2D;
78     image_desc.image_width = imageInfo->width;
79     image_desc.image_height = imageInfo->height;
80     image_desc.image_row_pitch = ( gEnablePitch ? imageInfo->rowPitch : 0 );
81     image_desc.num_mip_levels = 0;
82     read_only_image = clCreateImage( context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, imageInfo->format,
83                                        &image_desc, imageValues, &error );
84     if ( error != CL_SUCCESS )
85     {
86         log_error( "ERROR: Unable to create 2D read_only image of size %d x %d pitch %d (%s)\n", (int)imageInfo->width, (int)imageInfo->height, (int)imageInfo->rowPitch, IGetErrorString( error ) );
87         return error;
88     }
89 
90     if(gTestReadWrite)
91     {
92         read_write_image = clCreateImage(context,
93                                          CL_MEM_READ_WRITE,
94                                          imageInfo->format,
95                                          &image_desc,
96                                          NULL,
97                                          &error );
98         if ( error != CL_SUCCESS )
99         {
100             log_error( "ERROR: Unable to create 2D read_write image of size %d x %d pitch %d (%s)\n",
101                         (int)imageInfo->width,
102                         (int)imageInfo->height,
103                         (int)imageInfo->rowPitch,
104                         IGetErrorString( error ) );
105             return error;
106         }
107     }
108 
109     if ( gDebugTrace )
110         log_info( " - Creating kernel arguments...\n" );
111 
112     // Create sampler to use
113     actualSampler = clCreateSampler( context, CL_FALSE, CL_ADDRESS_NONE, CL_FILTER_NEAREST, &error );
114     test_error( error, "Unable to create image sampler" );
115 
116     // Create results buffer
117     cl_mem results = clCreateBuffer( context, 0, imageInfo->width * imageInfo->height * sizeof(cl_int), NULL, &error);
118     test_error( error, "Unable to create results buffer" );
119 
120     size_t resultValuesSize = imageInfo->width * imageInfo->height * sizeof(cl_int);
121     BufferOwningPtr<int> resultValues(malloc( resultValuesSize ));
122     memset( resultValues, 0xff, resultValuesSize );
123     clEnqueueWriteBuffer( queue, results, CL_TRUE, 0, resultValuesSize, resultValues, 0, NULL, NULL );
124 
125     // Set arguments
126     int idx = 0;
127     error = clSetKernelArg( kernel, idx++, sizeof( cl_mem ), &read_only_image );
128     test_error( error, "Unable to set kernel arguments" );
129     if(gTestReadWrite)
130     {
131         error = clSetKernelArg( kernel, idx++, sizeof( cl_mem ), &read_write_image );
132         test_error( error, "Unable to set kernel arguments" );
133     }
134     error = clSetKernelArg( kernel, idx++, sizeof( cl_sampler ), &actualSampler );
135     test_error( error, "Unable to set kernel arguments" );
136     error = clSetKernelArg( kernel, idx++, sizeof( cl_mem ), &results );
137     test_error( error, "Unable to set kernel arguments" );
138 
139     // Run the kernel
140     threads[0] = (size_t)imageInfo->width;
141     threads[1] = (size_t)imageInfo->height;
142     error = clEnqueueNDRangeKernel( queue, kernel, 2, NULL, threads, NULL, 0, NULL, NULL );
143     test_error( error, "Unable to run kernel" );
144 
145     if ( gDebugTrace )
146         log_info( "    reading results, %ld kbytes\n", (unsigned long)( imageInfo->width * imageInfo->height * sizeof(cl_int) / 1024 ) );
147 
148     error = clEnqueueReadBuffer( queue, results, CL_TRUE, 0, resultValuesSize, resultValues, 0, NULL, NULL );
149     test_error( error, "Unable to read results from kernel" );
150     if ( gDebugTrace )
151         log_info( "    results read\n" );
152 
153     // Check for non-zero comps
154     bool allZeroes = true;
155     for ( size_t ic = 0; ic < imageInfo->width * imageInfo->height; ++ic )
156     {
157         if ( resultValues[ic] ) {
158             allZeroes = false;
159             break;
160         }
161     }
162     if ( !allZeroes )
163     {
164         log_error( " Sampler-less reads differ from reads with sampler.\n" );
165         return -1;
166     }
167 
168     clReleaseSampler(actualSampler);
169     clReleaseMemObject(results);
170     clReleaseMemObject(read_only_image);
171     if(gTestReadWrite)
172     {
173         clReleaseMemObject(read_write_image);
174     }
175 
176     return 0;
177 }
178 
test_read_image_set_2D(cl_device_id device,cl_context context,cl_command_queue queue,const cl_image_format * format,image_sampler_data * imageSampler,ExplicitType outputType)179 int test_read_image_set_2D(cl_device_id device, cl_context context,
180                            cl_command_queue queue,
181                            const cl_image_format *format,
182                            image_sampler_data *imageSampler,
183                            ExplicitType outputType)
184 {
185     char programSrc[10240];
186     const char *ptr;
187     const char *readFormat;
188     const char *dataType;
189     clProgramWrapper program;
190     clKernelWrapper kernel;
191     RandomSeed seed( gRandomSeed );
192     int error;
193 
194     // Get our operating params
195     size_t maxWidth, maxHeight;
196     cl_ulong maxAllocSize, memSize;
197     image_descriptor imageInfo = { 0 };
198     size_t pixelSize;
199 
200     if (gTestReadWrite && checkForReadWriteImageSupport(device))
201     {
202         return TEST_SKIPPED_ITSELF;
203     }
204 
205     imageInfo.format = format;
206     imageInfo.depth = imageInfo.arraySize = imageInfo.slicePitch = 0;
207     imageInfo.type = CL_MEM_OBJECT_IMAGE2D;
208     pixelSize = get_pixel_size( imageInfo.format );
209 
210     error = clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof( maxWidth ), &maxWidth, NULL );
211     error |= clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_HEIGHT, sizeof( maxHeight ), &maxHeight, NULL );
212     error |= clGetDeviceInfo( device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof( maxAllocSize ), &maxAllocSize, NULL );
213     error |= clGetDeviceInfo( device, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof( memSize ), &memSize, NULL );
214     test_error( error, "Unable to get max image 2D size from device" );
215 
216     if (memSize > (cl_ulong)SIZE_MAX) {
217       memSize = (cl_ulong)SIZE_MAX;
218     }
219 
220     // Determine types
221     if ( outputType == kInt )
222     {
223         readFormat = "i";
224         dataType = "int4";
225     }
226     else if ( outputType == kUInt )
227     {
228         readFormat = "ui";
229         dataType = "uint4";
230     }
231     else // kFloat
232     {
233         readFormat = "f";
234         dataType = (format->image_channel_order == CL_DEPTH) ? "float" : "float4";
235     }
236 
237     if(gTestReadWrite)
238     {
239         sprintf(programSrc,
240                 read_write2DKernelSourcePattern,
241                 (format->image_channel_order == CL_DEPTH) ? "image2d_depth_t" : "image2d_t",
242                 (format->image_channel_order == CL_DEPTH) ? "image2d_depth_t" : "image2d_t",
243                 dataType,
244                 readFormat,
245                 readFormat,
246                 readFormat);
247     }
248     else
249     {
250         sprintf(programSrc,
251                 read2DKernelSourcePattern,
252                 (format->image_channel_order == CL_DEPTH) ? "image2d_depth_t" : "image2d_t",
253                 dataType,
254                 readFormat,
255                 readFormat );
256     }
257 
258     ptr = programSrc;
259     error = create_single_kernel_helper(context, &program, &kernel, 1, &ptr,
260                                         "sample_kernel");
261     test_error( error, "Unable to create testing kernel" );
262 
263     if ( gTestSmallImages )
264     {
265         for ( imageInfo.width = 1; imageInfo.width < 13; imageInfo.width++ )
266         {
267             imageInfo.rowPitch = imageInfo.width * pixelSize;
268             for ( imageInfo.height = 1; imageInfo.height < 9; imageInfo.height++ )
269             {
270                 if ( gDebugTrace )
271                     log_info( "   at size %d,%d\n", (int)imageInfo.width, (int)imageInfo.height );
272 
273                 int retCode = test_read_image_2D( context, queue, kernel, &imageInfo, imageSampler, outputType, seed );
274                 if ( retCode )
275                     return retCode;
276             }
277         }
278     }
279     else if ( gTestMaxImages )
280     {
281         // Try a specific set of maximum sizes
282         size_t numbeOfSizes;
283         size_t sizes[100][3];
284 
285         get_max_sizes(&numbeOfSizes, 100, sizes, maxWidth, maxHeight, 1, 1, maxAllocSize, memSize, CL_MEM_OBJECT_IMAGE2D, imageInfo.format);
286 
287         for ( size_t idx = 0; idx < numbeOfSizes; idx++ )
288         {
289             imageInfo.width = sizes[ idx ][ 0 ];
290             imageInfo.height = sizes[ idx ][ 1 ];
291             imageInfo.rowPitch = imageInfo.width * pixelSize;
292             log_info("Testing %d x %d\n", (int)sizes[ idx ][ 0 ], (int)sizes[ idx ][ 1 ]);
293             if ( gDebugTrace )
294                 log_info( "   at max size %d,%d\n", (int)sizes[ idx ][ 0 ], (int)sizes[ idx ][ 1 ] );
295             int retCode = test_read_image_2D( context, queue, kernel, &imageInfo, imageSampler, outputType, seed );
296             if ( retCode )
297                 return retCode;
298         }
299     }
300     else
301     {
302         for ( int i = 0; i < NUM_IMAGE_ITERATIONS; i++ )
303         {
304             cl_ulong size;
305             // Loop until we get a size that a) will fit in the max alloc size and b) that an allocation of that
306             // image, the result array, plus offset arrays, will fit in the global ram space
307             do
308             {
309                 imageInfo.width = (size_t)random_log_in_range( 16, (int)maxWidth / 32, seed );
310                 imageInfo.height = (size_t)random_log_in_range( 16, (int)maxHeight / 32, seed );
311 
312                 imageInfo.rowPitch = imageInfo.width * pixelSize;
313                 if ( gEnablePitch )
314                 {
315                     size_t extraWidth = (int)random_log_in_range( 0, 64, seed );
316                     imageInfo.rowPitch += extraWidth * pixelSize;
317                 }
318 
319                 size = (size_t)imageInfo.rowPitch * (size_t)imageInfo.height * 4;
320             } while (  size > maxAllocSize || ( size * 3 ) > memSize );
321 
322             if ( gDebugTrace )
323                 log_info( "   at size %d,%d (row pitch %d) out of %d,%d\n", (int)imageInfo.width, (int)imageInfo.height, (int)imageInfo.rowPitch, (int)maxWidth, (int)maxHeight );
324             int retCode = test_read_image_2D( context, queue, kernel, &imageInfo, imageSampler, outputType, seed );
325             if ( retCode )
326                 return retCode;
327         }
328     }
329 
330     return 0;
331 }
332