xref: /aosp_15_r20/external/OpenCL-CTS/test_conformance/images/clCopyImage/test_copy_2D.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 
18 extern int test_copy_image_generic( cl_context context, cl_command_queue queue, image_descriptor *srcImageInfo, image_descriptor *dstImageInfo,
19                                    const size_t sourcePos[], const size_t destPos[], const size_t regionSize[], MTdata d );
20 
test_copy_image_size_2D(cl_context context,cl_command_queue queue,image_descriptor * imageInfo,MTdata d)21 int test_copy_image_size_2D( cl_context context, cl_command_queue queue, image_descriptor *imageInfo, MTdata d )
22 {
23     size_t sourcePos[ 3 ], destPos[ 3 ], regionSize[ 3 ];
24     int ret = 0, retCode;
25     size_t src_lod = 0, src_width_lod = imageInfo->width, src_row_pitch_lod;
26     size_t src_height_lod = imageInfo->height;
27     size_t dst_lod = 0, dst_width_lod = imageInfo->width, dst_row_pitch_lod;
28     size_t dst_height_lod = imageInfo->height;
29     size_t width_lod = imageInfo->width, height_lod = imageInfo->height;
30     size_t max_mip_level;
31 
32     if( gTestMipmaps )
33     {
34         max_mip_level = imageInfo->num_mip_levels;
35         // Work at a random mip level
36         src_lod = (size_t)random_in_range( 0, max_mip_level ? max_mip_level - 1 : 0, d );
37         dst_lod = (size_t)random_in_range( 0, max_mip_level ? max_mip_level - 1 : 0, d );
38         src_width_lod = ( imageInfo->width >> src_lod )? ( imageInfo->width >> src_lod ) : 1;
39         dst_width_lod = ( imageInfo->width >> dst_lod )? ( imageInfo->width >> dst_lod ) : 1;
40         src_height_lod = ( imageInfo->height >> src_lod )? ( imageInfo->height >> src_lod ) : 1;
41         dst_height_lod = ( imageInfo->height >> dst_lod )? ( imageInfo->height >> dst_lod ) : 1;
42         width_lod  = ( src_width_lod > dst_width_lod ) ? dst_width_lod : src_width_lod;
43         height_lod  = ( src_height_lod > dst_height_lod ) ? dst_height_lod : src_height_lod;
44         src_row_pitch_lod = src_width_lod * get_pixel_size( imageInfo->format );
45         dst_row_pitch_lod = dst_width_lod * get_pixel_size( imageInfo->format );
46     }
47 
48     // First, try just a full covering region
49     sourcePos[ 0 ] = sourcePos[ 1 ] = sourcePos[ 2 ] = 0;
50     destPos[ 0 ] = destPos[ 1 ] = destPos[ 2 ] = 0;
51     regionSize[ 0 ] = imageInfo->width;
52     regionSize[ 1 ] = imageInfo->height;
53     regionSize[ 2 ] = 1;
54 
55     if(gTestMipmaps)
56     {
57         sourcePos[ 2 ] = src_lod;
58         destPos[ 2 ] = dst_lod;
59         regionSize[ 0 ] = width_lod;
60         regionSize[ 1 ] = height_lod;
61     }
62 
63     retCode = test_copy_image_generic( context, queue, imageInfo, imageInfo, sourcePos, destPos, regionSize, d );
64     if( retCode < 0 )
65         return retCode;
66     else
67         ret += retCode;
68 
69     // Now try a sampling of different random regions
70     for( int i = 0; i < 8; i++ )
71     {
72         if( gTestMipmaps )
73         {
74             // Work at a random mip level
75             src_lod = (size_t)random_in_range( 0, max_mip_level ? max_mip_level - 1 : 0, d );
76             dst_lod = (size_t)random_in_range( 0, max_mip_level ? max_mip_level - 1 : 0, d );
77             src_width_lod = ( imageInfo->width >> src_lod )? ( imageInfo->width >> src_lod ) : 1;
78             dst_width_lod = ( imageInfo->width >> dst_lod )? ( imageInfo->width >> dst_lod ) : 1;
79             src_height_lod = ( imageInfo->height >> src_lod )? ( imageInfo->height >> src_lod ) : 1;
80             dst_height_lod = ( imageInfo->height >> dst_lod )? ( imageInfo->height >> dst_lod ) : 1;
81             width_lod  = ( src_width_lod > dst_width_lod ) ? dst_width_lod : src_width_lod;
82             height_lod  = ( src_height_lod > dst_height_lod ) ? dst_height_lod : src_height_lod;
83             sourcePos[ 2 ] = src_lod;
84             destPos[ 2 ] = dst_lod;
85         }
86         // Pick a random size
87         regionSize[ 0 ] = ( width_lod > 8 ) ? (size_t)random_in_range( 8, (int)width_lod - 1, d ) : width_lod;
88         regionSize[ 1 ] = ( height_lod > 8 ) ? (size_t)random_in_range( 8, (int)height_lod - 1, d ) : height_lod;
89 
90         // Now pick positions within valid ranges
91         sourcePos[ 0 ] = ( width_lod > regionSize[ 0 ] ) ? (size_t)random_in_range( 0, (int)( width_lod - regionSize[ 0 ] - 1 ), d ) : 0;
92         sourcePos[ 1 ] = ( height_lod > regionSize[ 1 ] ) ? (size_t)random_in_range( 0, (int)( height_lod - regionSize[ 1 ] - 1 ), d ) : 0;
93 
94         destPos[ 0 ] = ( width_lod > regionSize[ 0 ] ) ? (size_t)random_in_range( 0, (int)( width_lod - regionSize[ 0 ] - 1 ), d ) : 0;
95         destPos[ 1 ] = ( height_lod > regionSize[ 1 ] ) ? (size_t)random_in_range( 0, (int)( height_lod - regionSize[ 1 ] - 1 ), d ) : 0;
96 
97         // Go for it!
98         retCode = test_copy_image_generic( context, queue, imageInfo, imageInfo, sourcePos, destPos, regionSize, d );
99         if( retCode < 0 )
100             return retCode;
101         else
102             ret += retCode;
103     }
104 
105     return ret;
106 }
107 
test_copy_image_set_2D(cl_device_id device,cl_context context,cl_command_queue queue,cl_image_format * format)108 int test_copy_image_set_2D( cl_device_id device, cl_context context, cl_command_queue queue, cl_image_format *format )
109 {
110     size_t maxWidth, maxHeight;
111     cl_ulong maxAllocSize, memSize;
112     image_descriptor imageInfo = { 0 };
113     RandomSeed seed(gRandomSeed);
114     size_t pixelSize;
115 
116     imageInfo.format = format;
117     imageInfo.type = CL_MEM_OBJECT_IMAGE2D;
118     pixelSize = get_pixel_size( imageInfo.format );
119 
120     int error = clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof( maxWidth ), &maxWidth, NULL );
121     error |= clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_HEIGHT, sizeof( maxHeight ), &maxHeight, NULL );
122     error |= clGetDeviceInfo( device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof( maxAllocSize ), &maxAllocSize, NULL );
123     error |= clGetDeviceInfo( device, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof( memSize ), &memSize, NULL );
124     test_error( error, "Unable to get max image 2D size from device" );
125 
126     if (memSize > (cl_ulong)SIZE_MAX) {
127         memSize = (cl_ulong)SIZE_MAX;
128         maxAllocSize = (cl_ulong)SIZE_MAX;
129     }
130 
131     if( gTestSmallImages )
132     {
133         for( imageInfo.width = 1; imageInfo.width < 13; imageInfo.width++ )
134         {
135       size_t rowPadding = gEnablePitch ? 48 : 0;
136 
137             imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
138 
139             if (gTestMipmaps)
140                 imageInfo.num_mip_levels = (cl_uint) random_log_in_range(2, (int)compute_max_mip_levels(imageInfo.width, imageInfo.height, 0), seed);
141 
142             if (gEnablePitch)
143             {
144                 do {
145                     rowPadding++;
146                     imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
147                 } while ((imageInfo.rowPitch % pixelSize) != 0);
148             }
149 
150             for( imageInfo.height = 1; imageInfo.height < 9; imageInfo.height++ )
151             {
152                 if( gDebugTrace )
153                     log_info( "   at size %d,%d\n", (int)imageInfo.width, (int)imageInfo.height );
154 
155                 int ret = test_copy_image_size_2D( context, queue, &imageInfo, seed );
156                 if( ret )
157                     return -1;
158             }
159         }
160     }
161     else if( gTestMaxImages )
162     {
163         // Try a specific set of maximum sizes
164         size_t numbeOfSizes;
165         size_t sizes[100][3];
166 
167         get_max_sizes(&numbeOfSizes, 100, sizes, maxWidth, maxHeight, 1, 1, maxAllocSize, memSize, CL_MEM_OBJECT_IMAGE2D, imageInfo.format);
168 
169         for( size_t idx = 0; idx < numbeOfSizes; idx++ )
170         {
171       size_t rowPadding = gEnablePitch ? 48 : 0;
172 
173             imageInfo.width = sizes[ idx ][ 0 ];
174             imageInfo.height = sizes[ idx ][ 1 ];
175             imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
176 
177             if (gTestMipmaps)
178                 imageInfo.num_mip_levels = (cl_uint) random_log_in_range(2, (int)compute_max_mip_levels(imageInfo.width, imageInfo.height, 0), seed);
179 
180             if (gEnablePitch)
181             {
182                 do {
183                     rowPadding++;
184                     imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
185                 } while ((imageInfo.rowPitch % pixelSize) != 0);
186             }
187 
188             log_info( "Testing %d x %d\n", (int)sizes[ idx ][ 0 ], (int)sizes[ idx ][ 1 ] );
189             if( gDebugTrace )
190                 log_info( "   at max size %d,%d\n", (int)sizes[ idx ][ 0 ], (int)sizes[ idx ][ 1 ] );
191             if( test_copy_image_size_2D( context, queue, &imageInfo, seed ) )
192                 return -1;
193         }
194     }
195     else
196     {
197         for( int i = 0; i < NUM_IMAGE_ITERATIONS; i++ )
198         {
199             cl_ulong size;
200       size_t rowPadding = gEnablePitch ? 48 : 0;
201             // Loop until we get a size that a) will fit in the max alloc size and b) that an allocation of that
202             // image, the result array, plus offset arrays, will fit in the global ram space
203             do
204             {
205                 imageInfo.width = (size_t)random_log_in_range( 16, (int)maxWidth / 32, seed );
206                 imageInfo.height = (size_t)random_log_in_range( 16, (int)maxHeight / 32, seed );
207 
208         if (gTestMipmaps)
209         {
210           imageInfo.num_mip_levels = (cl_uint) random_log_in_range(2, (int)compute_max_mip_levels(imageInfo.width, imageInfo.height, 0), seed);
211           imageInfo.rowPitch = imageInfo.width * get_pixel_size( imageInfo.format );
212           size = compute_mipmapped_image_size( imageInfo );
213           size = size*4;
214         }
215         else
216         {
217           imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
218           if (gEnablePitch)
219           {
220             do {
221               rowPadding++;
222               imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
223             } while ((imageInfo.rowPitch % pixelSize) != 0);
224           }
225 
226           size = (size_t)imageInfo.rowPitch * (size_t)imageInfo.height * 4;
227         }
228             } while(  size > maxAllocSize || ( size * 3 ) > memSize );
229 
230             if( gDebugTrace )
231                 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 );
232             int ret = test_copy_image_size_2D( context, queue, &imageInfo, seed );
233             if( ret )
234                 return -1;
235         }
236     }
237 
238     return 0;
239 }
240