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