• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
129 
130     if( gTestSmallImages )
131     {
132         for( imageInfo.width = 1; imageInfo.width < 13; imageInfo.width++ )
133         {
134       size_t rowPadding = gEnablePitch ? 48 : 0;
135 
136             imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
137 
138             if (gTestMipmaps)
139                 imageInfo.num_mip_levels = (cl_uint) random_log_in_range(2, (int)compute_max_mip_levels(imageInfo.width, imageInfo.height, 0), seed);
140 
141             if (gEnablePitch)
142             {
143                 do {
144                     rowPadding++;
145                     imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
146                 } while ((imageInfo.rowPitch % pixelSize) != 0);
147             }
148 
149             for( imageInfo.height = 1; imageInfo.height < 9; imageInfo.height++ )
150             {
151                 if( gDebugTrace )
152                     log_info( "   at size %d,%d\n", (int)imageInfo.width, (int)imageInfo.height );
153 
154                 int ret = test_copy_image_size_2D( context, queue, &imageInfo, seed );
155                 if( ret )
156                     return -1;
157             }
158         }
159     }
160     else if( gTestMaxImages )
161     {
162         // Try a specific set of maximum sizes
163         size_t numbeOfSizes;
164         size_t sizes[100][3];
165 
166         get_max_sizes(&numbeOfSizes, 100, sizes, maxWidth, maxHeight, 1, 1, maxAllocSize, memSize, CL_MEM_OBJECT_IMAGE2D, imageInfo.format);
167 
168         for( size_t idx = 0; idx < numbeOfSizes; idx++ )
169         {
170       size_t rowPadding = gEnablePitch ? 48 : 0;
171 
172             imageInfo.width = sizes[ idx ][ 0 ];
173             imageInfo.height = sizes[ idx ][ 1 ];
174             imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
175 
176             if (gTestMipmaps)
177                 imageInfo.num_mip_levels = (cl_uint) random_log_in_range(2, (int)compute_max_mip_levels(imageInfo.width, imageInfo.height, 0), seed);
178 
179             if (gEnablePitch)
180             {
181                 do {
182                     rowPadding++;
183                     imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
184                 } while ((imageInfo.rowPitch % pixelSize) != 0);
185             }
186 
187             log_info( "Testing %d x %d\n", (int)sizes[ idx ][ 0 ], (int)sizes[ idx ][ 1 ] );
188             if( gDebugTrace )
189                 log_info( "   at max size %d,%d\n", (int)sizes[ idx ][ 0 ], (int)sizes[ idx ][ 1 ] );
190             if( test_copy_image_size_2D( context, queue, &imageInfo, seed ) )
191                 return -1;
192         }
193     }
194     else
195     {
196         for( int i = 0; i < NUM_IMAGE_ITERATIONS; i++ )
197         {
198             cl_ulong size;
199       size_t rowPadding = gEnablePitch ? 48 : 0;
200             // Loop until we get a size that a) will fit in the max alloc size and b) that an allocation of that
201             // image, the result array, plus offset arrays, will fit in the global ram space
202             do
203             {
204                 imageInfo.width = (size_t)random_log_in_range( 16, (int)maxWidth / 32, seed );
205                 imageInfo.height = (size_t)random_log_in_range( 16, (int)maxHeight / 32, seed );
206 
207         if (gTestMipmaps)
208         {
209           imageInfo.num_mip_levels = (cl_uint) random_log_in_range(2, (int)compute_max_mip_levels(imageInfo.width, imageInfo.height, 0), seed);
210           imageInfo.rowPitch = imageInfo.width * get_pixel_size( imageInfo.format );
211           size = compute_mipmapped_image_size( imageInfo );
212           size = size*4;
213         }
214         else
215         {
216           imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
217           if (gEnablePitch)
218           {
219             do {
220               rowPadding++;
221               imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
222             } while ((imageInfo.rowPitch % pixelSize) != 0);
223           }
224 
225           size = (size_t)imageInfo.rowPitch * (size_t)imageInfo.height * 4;
226         }
227             } while(  size > maxAllocSize || ( size * 3 ) > memSize );
228 
229             if( gDebugTrace )
230                 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 );
231             int ret = test_copy_image_size_2D( context, queue, &imageInfo, seed );
232             if( ret )
233                 return -1;
234         }
235     }
236 
237     return 0;
238 }
239