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