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 extern int test_copy_image_generic( cl_context context, cl_command_queue queue, image_descriptor *srcImageInfo, image_descriptor *dstImageInfo,
27 const size_t sourcePos[], const size_t destPos[], const size_t regionSize[], MTdata d );
28
test_copy_image_size_2D(cl_context context,cl_command_queue queue,image_descriptor * imageInfo,MTdata d)29 int test_copy_image_size_2D( cl_context context, cl_command_queue queue, image_descriptor *imageInfo, MTdata d )
30 {
31 size_t sourcePos[ 3 ], destPos[ 3 ], regionSize[ 3 ];
32 int ret = 0, retCode;
33 size_t src_lod = 0, src_width_lod = imageInfo->width, src_row_pitch_lod;
34 size_t src_height_lod = imageInfo->height;
35 size_t dst_lod = 0, dst_width_lod = imageInfo->width, dst_row_pitch_lod;
36 size_t dst_height_lod = imageInfo->height;
37 size_t width_lod = imageInfo->width, height_lod = imageInfo->height;
38 size_t max_mip_level;
39
40 if( gTestMipmaps )
41 {
42 max_mip_level = imageInfo->num_mip_levels;
43 // Work at a random mip level
44 src_lod = (size_t)random_in_range( 0, max_mip_level ? max_mip_level - 1 : 0, d );
45 dst_lod = (size_t)random_in_range( 0, max_mip_level ? max_mip_level - 1 : 0, d );
46 src_width_lod = ( imageInfo->width >> src_lod )? ( imageInfo->width >> src_lod ) : 1;
47 dst_width_lod = ( imageInfo->width >> dst_lod )? ( imageInfo->width >> dst_lod ) : 1;
48 src_height_lod = ( imageInfo->height >> src_lod )? ( imageInfo->height >> src_lod ) : 1;
49 dst_height_lod = ( imageInfo->height >> dst_lod )? ( imageInfo->height >> dst_lod ) : 1;
50 width_lod = ( src_width_lod > dst_width_lod ) ? dst_width_lod : src_width_lod;
51 height_lod = ( src_height_lod > dst_height_lod ) ? dst_height_lod : src_height_lod;
52 src_row_pitch_lod = src_width_lod * get_pixel_size( imageInfo->format );
53 dst_row_pitch_lod = dst_width_lod * get_pixel_size( imageInfo->format );
54 }
55
56 // First, try just a full covering region
57 sourcePos[ 0 ] = sourcePos[ 1 ] = sourcePos[ 2 ] = 0;
58 destPos[ 0 ] = destPos[ 1 ] = destPos[ 2 ] = 0;
59 regionSize[ 0 ] = imageInfo->width;
60 regionSize[ 1 ] = imageInfo->height;
61 regionSize[ 2 ] = 1;
62
63 if(gTestMipmaps)
64 {
65 sourcePos[ 2 ] = src_lod;
66 destPos[ 2 ] = dst_lod;
67 regionSize[ 0 ] = width_lod;
68 regionSize[ 1 ] = height_lod;
69 }
70
71 retCode = test_copy_image_generic( context, queue, imageInfo, imageInfo, sourcePos, destPos, regionSize, d );
72 if( retCode < 0 )
73 return retCode;
74 else
75 ret += retCode;
76
77 // Now try a sampling of different random regions
78 for( int i = 0; i < 8; i++ )
79 {
80 if( gTestMipmaps )
81 {
82 // Work at a random mip level
83 src_lod = (size_t)random_in_range( 0, max_mip_level ? max_mip_level - 1 : 0, d );
84 dst_lod = (size_t)random_in_range( 0, max_mip_level ? max_mip_level - 1 : 0, d );
85 src_width_lod = ( imageInfo->width >> src_lod )? ( imageInfo->width >> src_lod ) : 1;
86 dst_width_lod = ( imageInfo->width >> dst_lod )? ( imageInfo->width >> dst_lod ) : 1;
87 src_height_lod = ( imageInfo->height >> src_lod )? ( imageInfo->height >> src_lod ) : 1;
88 dst_height_lod = ( imageInfo->height >> dst_lod )? ( imageInfo->height >> dst_lod ) : 1;
89 width_lod = ( src_width_lod > dst_width_lod ) ? dst_width_lod : src_width_lod;
90 height_lod = ( src_height_lod > dst_height_lod ) ? dst_height_lod : src_height_lod;
91 sourcePos[ 2 ] = src_lod;
92 destPos[ 2 ] = dst_lod;
93 }
94 // Pick a random size
95 regionSize[ 0 ] = ( width_lod > 8 ) ? (size_t)random_in_range( 8, (int)width_lod - 1, d ) : width_lod;
96 regionSize[ 1 ] = ( height_lod > 8 ) ? (size_t)random_in_range( 8, (int)height_lod - 1, d ) : height_lod;
97
98 // Now pick positions within valid ranges
99 sourcePos[ 0 ] = ( width_lod > regionSize[ 0 ] ) ? (size_t)random_in_range( 0, (int)( width_lod - regionSize[ 0 ] - 1 ), d ) : 0;
100 sourcePos[ 1 ] = ( height_lod > regionSize[ 1 ] ) ? (size_t)random_in_range( 0, (int)( height_lod - regionSize[ 1 ] - 1 ), d ) : 0;
101
102 destPos[ 0 ] = ( width_lod > regionSize[ 0 ] ) ? (size_t)random_in_range( 0, (int)( width_lod - regionSize[ 0 ] - 1 ), d ) : 0;
103 destPos[ 1 ] = ( height_lod > regionSize[ 1 ] ) ? (size_t)random_in_range( 0, (int)( height_lod - regionSize[ 1 ] - 1 ), d ) : 0;
104
105 // Go for it!
106 retCode = test_copy_image_generic( context, queue, imageInfo, imageInfo, sourcePos, destPos, regionSize, d );
107 if( retCode < 0 )
108 return retCode;
109 else
110 ret += retCode;
111 }
112
113 return ret;
114 }
115
test_copy_image_set_2D(cl_device_id device,cl_context context,cl_command_queue queue,cl_image_format * format)116 int test_copy_image_set_2D( cl_device_id device, cl_context context, cl_command_queue queue, cl_image_format *format )
117 {
118 size_t maxWidth, maxHeight;
119 cl_ulong maxAllocSize, memSize;
120 image_descriptor imageInfo = { 0 };
121 RandomSeed seed(gRandomSeed);
122 size_t pixelSize;
123
124 imageInfo.format = format;
125 imageInfo.type = CL_MEM_OBJECT_IMAGE2D;
126 pixelSize = get_pixel_size( imageInfo.format );
127
128 int error = clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof( maxWidth ), &maxWidth, NULL );
129 error |= clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_HEIGHT, sizeof( maxHeight ), &maxHeight, NULL );
130 error |= clGetDeviceInfo( device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof( maxAllocSize ), &maxAllocSize, NULL );
131 error |= clGetDeviceInfo( device, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof( memSize ), &memSize, NULL );
132 test_error( error, "Unable to get max image 2D size from device" );
133
134 if (memSize > (cl_ulong)SIZE_MAX) {
135 memSize = (cl_ulong)SIZE_MAX;
136 }
137
138 if( gTestSmallImages )
139 {
140 for( imageInfo.width = 1; imageInfo.width < 13; imageInfo.width++ )
141 {
142 size_t rowPadding = gEnablePitch ? 48 : 0;
143
144 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
145
146 if (gTestMipmaps)
147 imageInfo.num_mip_levels = (cl_uint) random_log_in_range(2, (int)compute_max_mip_levels(imageInfo.width, imageInfo.height, 0), seed);
148
149 if (gEnablePitch)
150 {
151 do {
152 rowPadding++;
153 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
154 } while ((imageInfo.rowPitch % pixelSize) != 0);
155 }
156
157 for( imageInfo.height = 1; imageInfo.height < 9; imageInfo.height++ )
158 {
159 if( gDebugTrace )
160 log_info( " at size %d,%d\n", (int)imageInfo.width, (int)imageInfo.height );
161
162 int ret = test_copy_image_size_2D( context, queue, &imageInfo, seed );
163 if( ret )
164 return -1;
165 }
166 }
167 }
168 else if( gTestMaxImages )
169 {
170 // Try a specific set of maximum sizes
171 size_t numbeOfSizes;
172 size_t sizes[100][3];
173
174 get_max_sizes(&numbeOfSizes, 100, sizes, maxWidth, maxHeight, 1, 1, maxAllocSize, memSize, CL_MEM_OBJECT_IMAGE2D, imageInfo.format);
175
176 for( size_t idx = 0; idx < numbeOfSizes; idx++ )
177 {
178 size_t rowPadding = gEnablePitch ? 48 : 0;
179
180 imageInfo.width = sizes[ idx ][ 0 ];
181 imageInfo.height = sizes[ idx ][ 1 ];
182 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
183
184 if (gTestMipmaps)
185 imageInfo.num_mip_levels = (cl_uint) random_log_in_range(2, (int)compute_max_mip_levels(imageInfo.width, imageInfo.height, 0), seed);
186
187 if (gEnablePitch)
188 {
189 do {
190 rowPadding++;
191 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
192 } while ((imageInfo.rowPitch % pixelSize) != 0);
193 }
194
195 log_info( "Testing %d x %d\n", (int)sizes[ idx ][ 0 ], (int)sizes[ idx ][ 1 ] );
196 if( gDebugTrace )
197 log_info( " at max size %d,%d\n", (int)sizes[ idx ][ 0 ], (int)sizes[ idx ][ 1 ] );
198 if( test_copy_image_size_2D( context, queue, &imageInfo, seed ) )
199 return -1;
200 }
201 }
202 else
203 {
204 for( int i = 0; i < NUM_IMAGE_ITERATIONS; i++ )
205 {
206 cl_ulong size;
207 size_t rowPadding = gEnablePitch ? 48 : 0;
208 // Loop until we get a size that a) will fit in the max alloc size and b) that an allocation of that
209 // image, the result array, plus offset arrays, will fit in the global ram space
210 do
211 {
212 imageInfo.width = (size_t)random_log_in_range( 16, (int)maxWidth / 32, seed );
213 imageInfo.height = (size_t)random_log_in_range( 16, (int)maxHeight / 32, seed );
214
215 if (gTestMipmaps)
216 {
217 imageInfo.num_mip_levels = (cl_uint) random_log_in_range(2, (int)compute_max_mip_levels(imageInfo.width, imageInfo.height, 0), seed);
218 imageInfo.rowPitch = imageInfo.width * get_pixel_size( imageInfo.format );
219 size = compute_mipmapped_image_size( imageInfo );
220 size = size*4;
221 }
222 else
223 {
224 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
225 if (gEnablePitch)
226 {
227 do {
228 rowPadding++;
229 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
230 } while ((imageInfo.rowPitch % pixelSize) != 0);
231 }
232
233 size = (size_t)imageInfo.rowPitch * (size_t)imageInfo.height * 4;
234 }
235 } while( size > maxAllocSize || ( size * 3 ) > memSize );
236
237 if( gDebugTrace )
238 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 );
239 int ret = test_copy_image_size_2D( context, queue, &imageInfo, seed );
240 if( ret )
241 return -1;
242 }
243 }
244
245 return 0;
246 }
247