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