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 }
206
207 // Determine types
208 if ( outputType == kInt )
209 {
210 readFormat = "i";
211 dataType = "int4";
212 }
213 else if ( outputType == kUInt )
214 {
215 readFormat = "ui";
216 dataType = "uint4";
217 }
218 else // kFloat
219 {
220 readFormat = "f";
221 dataType = (format->image_channel_order == CL_DEPTH) ? "float" : "float4";
222 }
223
224 // Construct the source
225 if(gTestReadWrite)
226 {
227 sprintf( programSrc, read_write2DArrayKernelSourcePattern,
228 (format->image_channel_order == CL_DEPTH) ? "image2d_array_depth_t" : "image2d_array_t",
229 (format->image_channel_order == CL_DEPTH) ? "image2d_array_depth_t" : "image2d_array_t",
230 dataType,
231 readFormat,
232 readFormat,
233 readFormat);
234 }
235 else
236 {
237 sprintf( programSrc, read2DArrayKernelSourcePattern,
238 (format->image_channel_order == CL_DEPTH) ? "image2d_array_depth_t" : "image2d_array_t",
239 dataType,
240 readFormat,
241 readFormat );
242 }
243
244
245 ptr = programSrc;
246 error = create_single_kernel_helper(context, &program, &kernel, 1, &ptr,
247 "sample_kernel");
248 test_error( error, "Unable to create testing kernel" );
249
250
251 // Run tests
252 if ( gTestSmallImages )
253 {
254 for ( imageInfo.width = 1; imageInfo.width < 13; imageInfo.width++ )
255 {
256 imageInfo.rowPitch = imageInfo.width * get_pixel_size( imageInfo.format );
257
258 for ( imageInfo.height = 1; imageInfo.height < 9; imageInfo.height++ )
259 {
260 imageInfo.slicePitch = imageInfo.rowPitch * imageInfo.height;
261 for ( imageInfo.arraySize = 2; imageInfo.arraySize < 9; imageInfo.arraySize++ )
262 {
263 if ( gDebugTrace )
264 log_info( " at size %d,%d,%d\n", (int)imageInfo.width, (int)imageInfo.height, (int)imageInfo.arraySize );
265 int retCode = test_read_image_2D_array( context, queue, kernel, &imageInfo, imageSampler, outputType, seed );
266 if ( retCode )
267 return retCode;
268 }
269 }
270 }
271 }
272 else if ( gTestMaxImages )
273 {
274 // Try a specific set of maximum sizes
275 size_t numbeOfSizes;
276 size_t sizes[100][3];
277
278 get_max_sizes(&numbeOfSizes, 100, sizes, maxWidth, maxHeight, 1, maxArraySize, maxAllocSize, memSize, CL_MEM_OBJECT_IMAGE2D_ARRAY, imageInfo.format);
279
280 for ( size_t idx = 0; idx < numbeOfSizes; idx++ )
281 {
282 imageInfo.width = sizes[ idx ][ 0 ];
283 imageInfo.height = sizes[ idx ][ 1 ];
284 imageInfo.arraySize = sizes[ idx ][ 2 ];
285 imageInfo.rowPitch = imageInfo.width * pixelSize;
286 imageInfo.slicePitch = imageInfo.height * imageInfo.rowPitch;
287 log_info("Testing %d x %d x %d\n", (int)sizes[ idx ][ 0 ], (int)sizes[ idx ][ 1 ], (int)sizes[ idx ][ 2 ]);
288 if ( gDebugTrace )
289 log_info( " at max size %d,%d,%d\n", (int)sizes[ idx ][ 0 ], (int)sizes[ idx ][ 1 ], (int)sizes[ idx ][ 2 ] );
290 int retCode = test_read_image_2D_array( context, queue, kernel, &imageInfo, imageSampler, outputType, seed );
291 if ( retCode )
292 return retCode;
293 }
294 }
295 else
296 {
297 for ( int i = 0; i < NUM_IMAGE_ITERATIONS; i++ )
298 {
299 cl_ulong size;
300 // Loop until we get a size that a) will fit in the max alloc size and b) that an allocation of that
301 // image, the result array, plus offset arrays, will fit in the global ram space
302 do
303 {
304 imageInfo.width = (size_t)random_log_in_range( 16, (int)maxWidth / 32, seed );
305 imageInfo.height = (size_t)random_log_in_range( 16, (int)maxHeight / 32, seed );
306 imageInfo.arraySize = (size_t)random_log_in_range( 16, (int)maxArraySize / 32, seed );
307
308 imageInfo.rowPitch = imageInfo.width * pixelSize;
309 imageInfo.slicePitch = imageInfo.rowPitch * imageInfo.height;
310
311 if ( gEnablePitch )
312 {
313 size_t extraWidth = (int)random_log_in_range( 0, 64, seed );
314 imageInfo.rowPitch += extraWidth * pixelSize;
315
316 size_t extraHeight = (int)random_log_in_range( 0, 64, seed );
317 imageInfo.slicePitch = imageInfo.rowPitch * (imageInfo.height + extraHeight);
318 }
319
320 size = (cl_ulong)imageInfo.slicePitch * (cl_ulong)imageInfo.arraySize * 4 * 4;
321 } while ( size > maxAllocSize || ( size * 3 ) > memSize );
322
323 if ( gDebugTrace )
324 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 );
325 int retCode = test_read_image_2D_array( context, queue, kernel, &imageInfo, imageSampler, outputType, seed );
326 if ( retCode )
327 return retCode;
328 }
329 }
330
331 return 0;
332 }
333