• 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 
test_read_image_2D(cl_context context,cl_command_queue queue,image_descriptor * imageInfo,MTdata d,cl_mem_flags flags)18 int test_read_image_2D(cl_context context, cl_command_queue queue,
19                        image_descriptor *imageInfo, MTdata d,
20                        cl_mem_flags flags)
21 {
22     int error;
23 
24     clMemWrapper image;
25 
26     // Generate some data to test against
27     BufferOwningPtr<char> imageValues;
28     generate_random_image_data( imageInfo, imageValues, d );
29 
30     if( gDebugTrace )
31     {
32         log_info( " - Creating %s image %d by %d...\n", gTestMipmaps?"mipmapped":"", (int)imageInfo->width, (int)imageInfo->height );
33         if( gTestMipmaps )
34             log_info( " with %llu mip levels\n", (unsigned long long) imageInfo->num_mip_levels );
35     }
36 
37     // Construct testing sources
38     if(!gTestMipmaps)
39     {
40         image =
41             create_image_2d(context, flags, imageInfo->format, imageInfo->width,
42                             imageInfo->height, 0, NULL, &error);
43         if( image == NULL )
44         {
45             log_error( "ERROR: Unable to create 2D image of size %d x %d (%s)", (int)imageInfo->width, (int)imageInfo->height, IGetErrorString( error ) );
46             return -1;
47         }
48     }
49     else
50     {
51         cl_image_desc image_desc = {0};
52         image_desc.image_type = CL_MEM_OBJECT_IMAGE2D;
53         image_desc.image_width = imageInfo->width;
54         image_desc.image_height = imageInfo->height;
55         image_desc.num_mip_levels = imageInfo->num_mip_levels;
56 
57         image = clCreateImage(context, flags, imageInfo->format, &image_desc,
58                               NULL, &error);
59         if( error != CL_SUCCESS )
60         {
61             log_error( "ERROR: Unable to create %d level mipmapped 2D image of size %d x %d (pitch %d ) (%s)",(int)imageInfo->num_mip_levels, (int)imageInfo->width, (int)imageInfo->height, (int)imageInfo->rowPitch, IGetErrorString( error ) );
62             return error;
63         }
64     }
65     if( gDebugTrace )
66         log_info( " - Writing image...\n" );
67 
68     size_t origin[ 3 ] = { 0, 0, 0 };
69     size_t region[ 3 ] = { 0, 0, 1 };
70     size_t fullImageSize;
71     if( gTestMipmaps )
72     {
73         fullImageSize = (size_t)compute_mipmapped_image_size( *imageInfo );
74     }
75     else
76     {
77         fullImageSize = imageInfo->height * imageInfo->rowPitch;
78     }
79     BufferOwningPtr<char> resultValues(malloc(fullImageSize));
80     size_t imgValMipLevelOffset = 0;
81 
82     for( size_t lod = 0; (gTestMipmaps && lod < imageInfo->num_mip_levels) || (!gTestMipmaps && lod < 1); lod++)
83     {
84         origin[2] = lod;
85         size_t width_lod, height_lod, row_pitch_lod;
86 
87         width_lod = (imageInfo->width >> lod) ? (imageInfo->width >> lod) : 1;
88         height_lod = (imageInfo->height >> lod) ? (imageInfo->height >> lod) : 1;
89         row_pitch_lod = gTestMipmaps ? (width_lod * get_pixel_size( imageInfo->format )): imageInfo->rowPitch;
90 
91         region[0] = width_lod;
92         region[1] = height_lod;
93 
94         if ( gDebugTrace && gTestMipmaps) {
95             log_info(" - Working at mipLevel :%llu\n", (unsigned long long)lod);
96         }
97         error = clEnqueueWriteImage(queue, image, CL_FALSE,
98                                     origin, region, ( gEnablePitch ? row_pitch_lod : 0 ), 0,
99                                    (char*)imageValues + imgValMipLevelOffset, 0, NULL, NULL);
100         if (error != CL_SUCCESS) {
101             log_error( "ERROR: Unable to write to 2D image of size %d x %d \n", (int)width_lod, (int)height_lod );
102             return -1;
103         }
104 
105         // To verify, we just read the results right back and see whether they match the input
106         if( gDebugTrace ) {
107             log_info( " - Initing result array...\n" );
108         }
109 
110         // Note: we read back without any pitch, to verify pitch actually WORKED
111         size_t scanlineSize = width_lod * get_pixel_size( imageInfo->format );
112         size_t imageSize = scanlineSize * height_lod;
113         memset( resultValues, 0xff, imageSize );
114 
115         if( gDebugTrace )
116             log_info( " - Reading results...\n" );
117 
118         error = clEnqueueReadImage( queue, image, CL_TRUE, origin, region, 0, 0, resultValues, 0, NULL, NULL );
119         test_error( error, "Unable to read image values" );
120 
121         // Verify scanline by scanline, since the pitches are different
122         char *sourcePtr = (char *)imageValues + imgValMipLevelOffset;
123         char *destPtr = resultValues;
124 
125         for( size_t y = 0; y < height_lod; y++ )
126         {
127             if( memcmp( sourcePtr, destPtr, scanlineSize ) != 0 )
128             {
129                 if(gTestMipmaps)
130                 {
131                     log_error("At mip level %llu\n",(unsigned long long) lod);
132                 }
133                 log_error( "ERROR: Scanline %d did not verify for image size %d,%d pitch %d (extra %d bytes)\n", (int)y, (int)width_lod, (int)height_lod, (int)row_pitch_lod, (int)row_pitch_lod - (int)width_lod * (int)get_pixel_size( imageInfo->format ) );
134 
135                 log_error( "First few values: \n" );
136                 log_error( " Input: " );
137                 uint32_t *s = (uint32_t *)sourcePtr;
138                 uint32_t *d = (uint32_t *)destPtr;
139                 for( int q = 0; q < 12; q++ )
140                     log_error( "%08x ", s[ q ] );
141                 log_error( "\nOutput: " );
142                 for( int q = 0; q < 12; q++ )
143                     log_error( "%08x ", d[ q ] );
144                 log_error( "\n" );
145 
146                 int outX, outY;
147                 int offset = (int)get_pixel_size( imageInfo->format ) * (int)( width_lod - 16 );
148                 if( offset < 0 )
149                     offset = 0;
150                 int foundCount = debug_find_vector_in_image( (char*)imageValues + imgValMipLevelOffset, imageInfo, destPtr + offset, get_pixel_size( imageInfo->format ), &outX, &outY, NULL );
151                 if( foundCount > 0 )
152                 {
153                     int returnedOffset = ( (int)y * (int)width_lod + offset / (int)get_pixel_size( imageInfo->format ) ) - ( outY * (int)width_lod + outX );
154 
155                     if( memcmp( sourcePtr + returnedOffset * get_pixel_size( imageInfo->format ), destPtr, get_pixel_size( imageInfo->format ) * 8 ) == 0 )
156                         log_error( "       Values appear to be offsetted by %d\n", returnedOffset );
157                     else
158                         log_error( "       Calculated offset is %d but unable to verify\n", returnedOffset );
159                 }
160                 else
161                 {
162                     log_error( "      Unable to determine offset\n" );
163                 }
164                 return -1;
165             }
166             sourcePtr += row_pitch_lod;
167             destPtr += scanlineSize;
168         }
169         imgValMipLevelOffset += width_lod * height_lod * get_pixel_size( imageInfo->format );
170     }
171     return 0;
172 }
173 
test_read_image_set_2D(cl_device_id device,cl_context context,cl_command_queue queue,cl_image_format * format,cl_mem_flags flags)174 int test_read_image_set_2D(cl_device_id device, cl_context context,
175                            cl_command_queue queue, cl_image_format *format,
176                            cl_mem_flags flags)
177 {
178     size_t maxWidth, maxHeight;
179     cl_ulong maxAllocSize, memSize;
180     image_descriptor imageInfo = { 0 };
181     RandomSeed  seed( gRandomSeed );
182     size_t pixelSize;
183 
184     imageInfo.type = CL_MEM_OBJECT_IMAGE2D;
185     imageInfo.format = format;
186     imageInfo.depth = imageInfo.slicePitch = 0;
187     pixelSize = get_pixel_size( imageInfo.format );
188 
189     int error = clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof( maxWidth ), &maxWidth, NULL );
190     error |= clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_HEIGHT, sizeof( maxHeight ), &maxHeight, NULL );
191     error |= clGetDeviceInfo( device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof( maxAllocSize ), &maxAllocSize, NULL );
192     error |= clGetDeviceInfo( device, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof( memSize ), &memSize, NULL );
193     test_error( error, "Unable to get max image 2D size from device" );
194 
195     if (memSize > (cl_ulong)SIZE_MAX) {
196         memSize = (cl_ulong)SIZE_MAX;
197         maxAllocSize = (cl_ulong)SIZE_MAX;
198     }
199 
200     if( gTestSmallImages )
201     {
202         for( imageInfo.width = 1; imageInfo.width < 13; imageInfo.width++ )
203         {
204             imageInfo.rowPitch = imageInfo.width * pixelSize;
205             for( imageInfo.height = 1; imageInfo.height < 9; imageInfo.height++ )
206             {
207                 if (gTestMipmaps)
208                     imageInfo.num_mip_levels = (cl_uint) random_log_in_range(2, (int)compute_max_mip_levels(imageInfo.width, imageInfo.height, 0), seed);
209 
210                 if( gDebugTrace )
211                     log_info( "   at size %d,%d\n", (int)imageInfo.width, (int)imageInfo.height );
212 
213                 int ret =
214                     test_read_image_2D(context, queue, &imageInfo, seed, flags);
215                 if( ret )
216                     return -1;
217             }
218         }
219     }
220     else if( gTestMaxImages )
221     {
222         // Try a specific set of maximum sizes
223         size_t numbeOfSizes;
224         size_t sizes[100][3];
225 
226         get_max_sizes(&numbeOfSizes, 100, sizes, maxWidth, maxHeight, 1, 1, maxAllocSize, memSize, CL_MEM_OBJECT_IMAGE2D, imageInfo.format);
227 
228         for( size_t idx = 0; idx < numbeOfSizes; idx++ )
229         {
230             imageInfo.width = sizes[idx][0];
231             imageInfo.height = sizes[idx][1];
232             imageInfo.rowPitch = imageInfo.width * pixelSize;
233 
234             if (gTestMipmaps)
235                 imageInfo.num_mip_levels = (cl_uint) random_log_in_range(2, (int)compute_max_mip_levels(imageInfo.width, imageInfo.height, 0), seed);
236 
237             log_info("Testing %d x %d\n", (int)imageInfo.width, (int)imageInfo.height);
238             if( gDebugTrace )
239                 log_info( "   at max size %d,%d\n", (int)maxWidth, (int)maxHeight );
240             if (test_read_image_2D(context, queue, &imageInfo, seed, flags))
241                 return -1;
242         }
243     }
244     else
245     {
246         for( int i = 0; i < NUM_IMAGE_ITERATIONS; i++ )
247         {
248             cl_ulong size;
249             // Loop until we get a size that a) will fit in the max alloc size and b) that an allocation of that
250             // image, the result array, plus offset arrays, will fit in the global ram space
251             do
252             {
253                 imageInfo.width = (size_t)random_log_in_range( 16, (int)maxWidth / 32, seed );
254                 imageInfo.height = (size_t)random_log_in_range( 16, (int)maxHeight / 32, seed );
255                 if (gTestMipmaps)
256                 {
257                     imageInfo.num_mip_levels = (cl_uint) random_log_in_range(2, (int)compute_max_mip_levels(imageInfo.width, imageInfo.height, 0), seed);
258                     imageInfo.rowPitch = imageInfo.width * get_pixel_size( imageInfo.format );
259                     size = compute_mipmapped_image_size( imageInfo );
260                 }
261                 else
262                 {
263                     imageInfo.rowPitch = imageInfo.width * pixelSize;
264                     if( gEnablePitch )
265                     {
266                         size_t extraWidth = (int)random_log_in_range( 0, 64, seed );
267                         imageInfo.rowPitch += extraWidth * pixelSize;
268                     }
269 
270                     size = (size_t)imageInfo.rowPitch * (size_t)imageInfo.height * 4;
271                 }
272             } while(  size > maxAllocSize || ( size / 3 ) > memSize );
273 
274             if( gDebugTrace )
275                 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 );
276             int ret =
277                 test_read_image_2D(context, queue, &imageInfo, seed, flags);
278             if( ret )
279                 return -1;
280         }
281     }
282 
283     return 0;
284 }
285