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