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     }
87 
88     if ( gTestSmallImages )
89     {
90         for ( imageInfo.width = 1; imageInfo.width < 13; imageInfo.width++ )
91         {
92             imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
93 
94             if (gEnablePitch)
95             {
96               rowPadding = rowPadding_default;
97               do {
98                 rowPadding++;
99                 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
100               } while ((imageInfo.rowPitch % pixelSize) != 0);
101             }
102 
103             for ( imageInfo.height = 1; imageInfo.height < 9; imageInfo.height++ )
104             {
105                 if ( gDebugTrace )
106                     log_info( "   at size %d,%d\n", (int)imageInfo.width, (int)imageInfo.height );
107 
108                 int ret = test_fill_image_size_2D( context, queue, &imageInfo, outputType, seed );
109                 if ( ret )
110                     return -1;
111             }
112         }
113     }
114     else if ( gTestMaxImages )
115     {
116         // Try a specific set of maximum sizes
117         size_t numbeOfSizes;
118         size_t sizes[100][3];
119 
120         get_max_sizes(&numbeOfSizes, 100, sizes, maxWidth, maxHeight, 1, 1, maxAllocSize, memSize, CL_MEM_OBJECT_IMAGE2D, imageInfo.format);
121 
122         for ( size_t idx = 0; idx < numbeOfSizes; idx++ )
123         {
124             imageInfo.width = sizes[ idx ][ 0 ];
125             imageInfo.height = sizes[ idx ][ 1 ];
126             imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
127 
128             if (gEnablePitch)
129             {
130               rowPadding = rowPadding_default;
131               do {
132                 rowPadding++;
133                 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
134               } while ((imageInfo.rowPitch % pixelSize) != 0);
135             }
136 
137             log_info( "Testing %d x %d\n", (int)sizes[ idx ][ 0 ], (int)sizes[ idx ][ 1 ] );
138             if ( gDebugTrace )
139                 log_info( "   at max size %d,%d\n", (int)sizes[ idx ][ 0 ], (int)sizes[ idx ][ 1 ] );
140             if ( test_fill_image_size_2D( context, queue, &imageInfo, outputType, seed ) )
141                 return -1;
142         }
143     }
144     else
145     {
146         for ( int i = 0; i < NUM_IMAGE_ITERATIONS; i++ )
147         {
148             cl_ulong size;
149             // Loop until we get a size that a) will fit in the max alloc size and b) that an allocation of that
150             // image, the result array, plus offset arrays, will fit in the global ram space
151             do
152             {
153                 imageInfo.width = (size_t)random_log_in_range( 16, (int)maxWidth / 32, seed );
154                 imageInfo.height = (size_t)random_log_in_range( 16, (int)maxHeight / 32, seed );
155 
156                 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
157 
158                 if (gEnablePitch)
159                 {
160                   rowPadding = rowPadding_default;
161                   do {
162                     rowPadding++;
163                     imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
164                   } while ((imageInfo.rowPitch % pixelSize) != 0);
165                 }
166 
167                 size = (size_t)imageInfo.rowPitch * (size_t)imageInfo.height * 4;
168             } while (  size > maxAllocSize || ( size * 3 ) > memSize );
169 
170             if ( gDebugTrace )
171                 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 );
172             int ret = test_fill_image_size_2D( context, queue, &imageInfo, outputType, seed );
173             if ( ret )
174                 return -1;
175         }
176     }
177 
178     return 0;
179 }
180