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