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 // Defined in test_fill_2D_3D.cpp
19 extern int test_fill_image_generic( cl_context context, cl_command_queue queue, image_descriptor *imageInfo,
20 const size_t origin[], const size_t region[], ExplicitType outputType, MTdata d );
21
22
test_fill_image_2D_array(cl_context context,cl_command_queue queue,image_descriptor * imageInfo,ExplicitType outputType,MTdata d)23 static int test_fill_image_2D_array( cl_context context, cl_command_queue queue, image_descriptor *imageInfo, ExplicitType outputType, MTdata d )
24 {
25 size_t origin[ 3 ], region[ 3 ];
26 int ret = 0, retCode;
27
28 // First, try just a full covering region
29 origin[ 0 ] = origin[ 1 ] = origin[ 2 ] = 0;
30 region[ 0 ] = imageInfo->width;
31 region[ 1 ] = imageInfo->height;
32 region[ 2 ] = imageInfo->arraySize;
33
34 retCode = test_fill_image_generic( context, queue, imageInfo, origin, region, outputType, d );
35 if ( retCode < 0 )
36 return retCode;
37 else
38 ret += retCode;
39
40 // Now try a sampling of different random regions
41 for ( int i = 0; i < 8; i++ )
42 {
43 // Pick a random size
44 region[ 0 ] = ( imageInfo->width > 8 ) ? (size_t)random_in_range( 8, (int)imageInfo->width - 1, d ) : imageInfo->width;
45 region[ 1 ] = ( imageInfo->height > 8 ) ? (size_t)random_in_range( 8, (int)imageInfo->height - 1, d ) : imageInfo->height;
46 region[ 2 ] = ( imageInfo->arraySize > 8 ) ? (size_t)random_in_range( 8, (int)imageInfo->arraySize - 1, d ) : imageInfo->arraySize;
47
48 // Now pick positions within valid ranges
49 origin[ 0 ] = ( imageInfo->width > region[ 0 ] ) ? (size_t)random_in_range( 0, (int)( imageInfo->width - region[ 0 ] - 1 ), d ) : 0;
50 origin[ 1 ] = ( imageInfo->height > region[ 1 ] ) ? (size_t)random_in_range( 0, (int)( imageInfo->height - region[ 1 ] - 1 ), d ) : 0;
51 origin[ 2 ] = ( imageInfo->arraySize > region[ 2 ] ) ? (size_t)random_in_range( 0, (int)( imageInfo->arraySize - region[ 2 ] - 1 ), d ) : 0;
52
53 // Go for it!
54 retCode = test_fill_image_generic( context, queue, imageInfo, origin, region, outputType, d );
55 if ( retCode < 0 )
56 return retCode;
57 else
58 ret += retCode;
59 }
60
61 return ret;
62 }
63
64
test_fill_image_set_2D_array(cl_device_id device,cl_context context,cl_command_queue queue,cl_image_format * format,ExplicitType outputType)65 int test_fill_image_set_2D_array( cl_device_id device, cl_context context, cl_command_queue queue, cl_image_format *format, ExplicitType outputType )
66 {
67 size_t maxWidth, maxHeight, maxArraySize;
68 cl_ulong maxAllocSize, memSize;
69 image_descriptor imageInfo = { 0 };
70 RandomSeed seed( gRandomSeed );
71 const size_t rowPadding_default = 80;
72 size_t rowPadding = gEnablePitch ? rowPadding_default : 0;
73 size_t slicePadding = gEnablePitch ? 3 : 0;
74 size_t pixelSize;
75
76 memset(&imageInfo, 0x0, sizeof(image_descriptor));
77 imageInfo.type = CL_MEM_OBJECT_IMAGE2D_ARRAY;
78 imageInfo.format = format;
79 pixelSize = get_pixel_size( imageInfo.format );
80
81 int error = clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof( maxWidth ), &maxWidth, NULL );
82 error |= clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_HEIGHT, sizeof( maxHeight ), &maxHeight, NULL );
83 error |= clGetDeviceInfo( device, CL_DEVICE_IMAGE_MAX_ARRAY_SIZE, sizeof( maxArraySize ), &maxArraySize, NULL );
84 error |= clGetDeviceInfo( device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof( maxAllocSize ), &maxAllocSize, NULL );
85 error |= clGetDeviceInfo( device, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof( memSize ), &memSize, NULL );
86 test_error( error, "Unable to get max image 2D array size from device" );
87
88 if (memSize > (cl_ulong)SIZE_MAX) {
89 memSize = (cl_ulong)SIZE_MAX;
90 maxAllocSize = (cl_ulong)SIZE_MAX;
91 }
92
93 if ( gTestSmallImages )
94 {
95 for ( imageInfo.width = 1; imageInfo.width < 13; imageInfo.width++ )
96 {
97 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
98
99 if (gEnablePitch)
100 {
101 rowPadding = rowPadding_default;
102 do {
103 rowPadding++;
104 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
105 } while ((imageInfo.rowPitch % pixelSize) != 0);
106 }
107
108 for ( imageInfo.height = 1; imageInfo.height < 9; imageInfo.height++ )
109 {
110 imageInfo.slicePitch = imageInfo.rowPitch * (imageInfo.height + slicePadding);
111 for ( imageInfo.arraySize = 2; imageInfo.arraySize < 9; imageInfo.arraySize++ )
112 {
113 if ( gDebugTrace )
114 log_info( " at size %d,%d,%d\n", (int)imageInfo.width, (int)imageInfo.height, (int)imageInfo.arraySize );
115 int ret = test_fill_image_2D_array( context, queue, &imageInfo, outputType, seed );
116 if ( ret )
117 return -1;
118 }
119 }
120 }
121 }
122 else if ( gTestMaxImages )
123 {
124 // Try a specific set of maximum sizes
125 size_t numbeOfSizes;
126 size_t sizes[100][3];
127 get_max_sizes(&numbeOfSizes, 100, sizes, maxWidth, maxHeight, 1, maxArraySize, maxAllocSize, memSize, CL_MEM_OBJECT_IMAGE2D_ARRAY, imageInfo.format);
128
129 for ( size_t idx = 0; idx < numbeOfSizes; idx++ )
130 {
131 imageInfo.width = sizes[ idx ][ 0 ];
132 imageInfo.height = sizes[ idx ][ 1 ];
133 imageInfo.arraySize = sizes[ idx ][ 2 ];
134 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
135
136 if (gEnablePitch)
137 {
138 rowPadding = rowPadding_default;
139 do {
140 rowPadding++;
141 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
142 } while ((imageInfo.rowPitch % pixelSize) != 0);
143 }
144
145 imageInfo.slicePitch = imageInfo.rowPitch * (imageInfo.height + slicePadding);
146
147 // Loop until we get a size that a) will fit in the max alloc size and b) that an allocation of that
148 // image, the result array, plus offset arrays, will fit in the global ram space
149 cl_ulong size = (cl_ulong)imageInfo.slicePitch * (cl_ulong)imageInfo.arraySize * 4 * 4;
150
151 while (size > maxAllocSize || (size * 3) > memSize) {
152 if (imageInfo.arraySize == 1) {
153 // arraySize cannot be 0.
154 break;
155 }
156 imageInfo.arraySize--;
157 size = (cl_ulong)imageInfo.slicePitch * (cl_ulong)imageInfo.arraySize * 4 * 4;
158 }
159
160 while (size > maxAllocSize || (size * 3) > memSize) {
161 imageInfo.height--;
162 imageInfo.slicePitch = imageInfo.height * imageInfo.rowPitch;
163 size = (cl_ulong)imageInfo.slicePitch * (cl_ulong)imageInfo.arraySize * 4 * 4;
164 }
165
166 log_info( "Testing %d x %d x %d\n", (int)imageInfo.width, (int)imageInfo.height, (int)imageInfo.arraySize);
167
168 if ( test_fill_image_2D_array( context, queue, &imageInfo, outputType, seed ) )
169 return -1;
170 }
171 }
172 else
173 {
174 for ( int i = 0; i < NUM_IMAGE_ITERATIONS; i++ )
175 {
176 cl_ulong size;
177 // Loop until we get a size that a) will fit in the max alloc size and b) that an allocation of that
178 // image, the result array, plus offset arrays, will fit in the global ram space
179 do
180 {
181 imageInfo.width = (size_t)random_log_in_range( 16, (int)maxWidth / 64, seed );
182 imageInfo.height = (size_t)random_log_in_range( 16, (int)maxHeight / 64, seed );
183 imageInfo.arraySize = (size_t)random_log_in_range( 16, (int)maxArraySize / 32,seed );
184
185 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
186
187 if (gEnablePitch)
188 {
189 rowPadding = rowPadding_default;
190 do {
191 rowPadding++;
192 imageInfo.rowPitch = imageInfo.width * pixelSize + rowPadding;
193 } while ((imageInfo.rowPitch % pixelSize) != 0);
194 }
195
196 imageInfo.slicePitch = imageInfo.rowPitch * (imageInfo.height + slicePadding);
197
198 size = (cl_ulong)imageInfo.slicePitch * (cl_ulong)imageInfo.arraySize * 4 * 4;
199 } while ( size > maxAllocSize || ( size * 3 ) > memSize );
200
201 if ( gDebugTrace )
202 log_info( " at size %d,%d,%d (pitch %d,%d) out of %d,%d,%d\n", (int)imageInfo.width, (int)imageInfo.height, (int)imageInfo.arraySize, (int)imageInfo.rowPitch, (int)imageInfo.slicePitch, (int)maxWidth, (int)maxHeight, (int)maxArraySize );
203 int ret = test_fill_image_2D_array( context, queue, &imageInfo, outputType, seed );
204 if ( ret )
205 return -1;
206 }
207 }
208
209 return 0;
210 }
211