xref: /aosp_15_r20/external/OpenCL-CTS/test_conformance/images/clFillImage/test_fill_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 // Defined in test_fill_2D_3D.cpp
19 extern int test_fill_image_generic( cl_context context, cl_command_queue queue, image_descriptor *imageInfo,
20                                     const size_t origin[], const size_t region[], ExplicitType outputType, MTdata d );
21 
22 
test_fill_image_size_2D(cl_context context,cl_command_queue queue,image_descriptor * imageInfo,ExplicitType outputType,MTdata d)23 int test_fill_image_size_2D( cl_context context, cl_command_queue queue, image_descriptor *imageInfo, ExplicitType outputType, MTdata d )
24 {
25     size_t origin[ 3 ], region[ 3 ];
26     int ret = 0, retCode;
27 
28     // First, try just a full covering region fill
29     origin[ 0 ] = origin[ 1 ] = origin[ 2 ] = 0;
30     region[ 0 ] = imageInfo->width;
31     region[ 1 ] = imageInfo->height;
32     region[ 2 ] = 1;
33 
34     retCode = test_fill_image_generic( context, queue, imageInfo, origin, region, outputType, d );
35     if ( retCode < 0 )
36         return retCode;
37     else
38         ret += retCode;
39 
40     // Now try a sampling of different random regions
41     for ( int i = 0; i < 8; i++ )
42     {
43         // Pick a random size
44         region[ 0 ] = ( imageInfo->width > 8 ) ? (size_t)random_in_range( 8, (int)imageInfo->width - 1, d ) : imageInfo->width;
45         region[ 1 ] = ( imageInfo->height > 8 ) ? (size_t)random_in_range( 8, (int)imageInfo->height - 1, d ) : imageInfo->height;
46 
47         // Now pick positions within valid ranges
48         origin[ 0 ] = ( imageInfo->width > region[ 0 ] ) ? (size_t)random_in_range( 0, (int)( imageInfo->width - region[ 0 ] - 1 ), d ) : 0;
49         origin[ 1 ] = ( imageInfo->height > region[ 1 ] ) ? (size_t)random_in_range( 0, (int)( imageInfo->height - region[ 1 ] - 1 ), d ) : 0;
50 
51         // Go for it!
52         retCode = test_fill_image_generic( context, queue, imageInfo, origin, region, outputType, d );
53         if ( retCode < 0 )
54             return retCode;
55         else
56             ret += retCode;
57     }
58 
59     return ret;
60 }
61 
62 
test_fill_image_set_2D(cl_device_id device,cl_context context,cl_command_queue queue,cl_image_format * format,ExplicitType outputType)63 int test_fill_image_set_2D( cl_device_id device, cl_context context, cl_command_queue queue, cl_image_format *format, ExplicitType outputType )
64 {
65     size_t maxWidth, maxHeight;
66     cl_ulong maxAllocSize, memSize;
67     image_descriptor imageInfo = { 0 };
68     RandomSeed seed(gRandomSeed);
69     const size_t rowPadding_default = 48;
70     size_t rowPadding = gEnablePitch ? rowPadding_default : 0;
71     size_t pixelSize;
72 
73     memset(&imageInfo, 0x0, sizeof(image_descriptor));
74     imageInfo.type = CL_MEM_OBJECT_IMAGE2D;
75     imageInfo.format = format;
76     pixelSize = get_pixel_size( imageInfo.format );
77 
78     int error = clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof( maxWidth ), &maxWidth, NULL );
79     error |= clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_HEIGHT, sizeof( maxHeight ), &maxHeight, NULL );
80     error |= clGetDeviceInfo( device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof( maxAllocSize ), &maxAllocSize, NULL );
81     error |= clGetDeviceInfo( device, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof( memSize ), &memSize, NULL );
82     test_error( error, "Unable to get max image 2D size from device" );
83 
84     if (memSize > (cl_ulong)SIZE_MAX) {
85       memSize = (cl_ulong)SIZE_MAX;
86       maxAllocSize = (cl_ulong)SIZE_MAX;
87     }
88 
89     if ( gTestSmallImages )
90     {
91         for ( imageInfo.width = 1; imageInfo.width < 13; imageInfo.width++ )
92         {
93             imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
94 
95             if (gEnablePitch)
96             {
97               rowPadding = rowPadding_default;
98               do {
99                 rowPadding++;
100                 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
101               } while ((imageInfo.rowPitch % pixelSize) != 0);
102             }
103 
104             for ( imageInfo.height = 1; imageInfo.height < 9; imageInfo.height++ )
105             {
106                 if ( gDebugTrace )
107                     log_info( "   at size %d,%d\n", (int)imageInfo.width, (int)imageInfo.height );
108 
109                 int ret = test_fill_image_size_2D( context, queue, &imageInfo, outputType, seed );
110                 if ( ret )
111                     return -1;
112             }
113         }
114     }
115     else if ( gTestMaxImages )
116     {
117         // Try a specific set of maximum sizes
118         size_t numbeOfSizes;
119         size_t sizes[100][3];
120 
121         get_max_sizes(&numbeOfSizes, 100, sizes, maxWidth, maxHeight, 1, 1, maxAllocSize, memSize, CL_MEM_OBJECT_IMAGE2D, imageInfo.format);
122 
123         for ( size_t idx = 0; idx < numbeOfSizes; idx++ )
124         {
125             imageInfo.width = sizes[ idx ][ 0 ];
126             imageInfo.height = sizes[ idx ][ 1 ];
127             imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
128 
129             if (gEnablePitch)
130             {
131               rowPadding = rowPadding_default;
132               do {
133                 rowPadding++;
134                 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
135               } while ((imageInfo.rowPitch % pixelSize) != 0);
136             }
137 
138             log_info( "Testing %d x %d\n", (int)sizes[ idx ][ 0 ], (int)sizes[ idx ][ 1 ] );
139             if ( gDebugTrace )
140                 log_info( "   at max size %d,%d\n", (int)sizes[ idx ][ 0 ], (int)sizes[ idx ][ 1 ] );
141             if ( test_fill_image_size_2D( context, queue, &imageInfo, outputType, seed ) )
142                 return -1;
143         }
144     }
145     else
146     {
147         for ( int i = 0; i < NUM_IMAGE_ITERATIONS; i++ )
148         {
149             cl_ulong size;
150             // Loop until we get a size that a) will fit in the max alloc size and b) that an allocation of that
151             // image, the result array, plus offset arrays, will fit in the global ram space
152             do
153             {
154                 imageInfo.width = (size_t)random_log_in_range( 16, (int)maxWidth / 32, seed );
155                 imageInfo.height = (size_t)random_log_in_range( 16, (int)maxHeight / 32, seed );
156 
157                 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
158 
159                 if (gEnablePitch)
160                 {
161                   rowPadding = rowPadding_default;
162                   do {
163                     rowPadding++;
164                     imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
165                   } while ((imageInfo.rowPitch % pixelSize) != 0);
166                 }
167 
168                 size = (size_t)imageInfo.rowPitch * (size_t)imageInfo.height * 4;
169             } while (  size > maxAllocSize || ( size * 3 ) > memSize );
170 
171             if ( gDebugTrace )
172                 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 );
173             int ret = test_fill_image_size_2D( context, queue, &imageInfo, outputType, seed );
174             if ( ret )
175                 return -1;
176         }
177     }
178 
179     return 0;
180 }
181