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