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