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 #include <float.h>
18
19 #if defined( __APPLE__ )
20 #include <signal.h>
21 #include <sys/signal.h>
22 #include <setjmp.h>
23 #endif
24
25 #define MAX_ERR 0.005f
26 #define MAX_HALF_LINEAR_ERR 0.3f
27
28 extern bool gDebugTrace, gTestSmallImages, gEnablePitch, gTestMaxImages, gDeviceLt20;
29
30 #define MAX_TRIES 1
31 #define MAX_CLAMPED 1
32
33 const char *read1DBufferKernelSourcePattern =
34 "__kernel void sample_kernel( read_only image1d_buffer_t inputA, read_only image1d_t inputB, sampler_t sampler, __global int *results )\n"
35 "{\n"
36 " int tidX = get_global_id(0);\n"
37 " int offset = tidX;\n"
38 " %s clr = read_image%s( inputA, tidX );\n"
39 " int4 test = (clr != read_image%s( inputB, sampler, tidX ));\n"
40 " if ( test.x || test.y || test.z || test.w )\n"
41 " results[offset] = -1;\n"
42 " else\n"
43 " results[offset] = 0;\n"
44 "}";
45
46
test_read_image_1D_buffer(cl_context context,cl_command_queue queue,cl_kernel kernel,image_descriptor * imageInfo,image_sampler_data * imageSampler,ExplicitType outputType,MTdata d)47 int test_read_image_1D_buffer( cl_context context, cl_command_queue queue, cl_kernel kernel,
48 image_descriptor *imageInfo, image_sampler_data *imageSampler,
49 ExplicitType outputType, MTdata d )
50 {
51 int error;
52 size_t threads[2];
53 cl_sampler actualSampler;
54
55 BufferOwningPtr<char> imageValues;
56 generate_random_image_data( imageInfo, imageValues, d );
57
58 if ( gDebugTrace )
59 log_info( " - Creating 1D image from buffer %d ...\n", (int)imageInfo->width );
60
61 // Construct testing sources
62 cl_mem image[2];
63 cl_image_desc image_desc;
64
65 cl_mem imageBuffer = clCreateBuffer( context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, imageInfo->rowPitch, imageValues, &error);
66 if ( error != CL_SUCCESS )
67 {
68 log_error( "ERROR: Unable to create buffer of size %d bytes (%s)\n", (int)imageInfo->rowPitch, IGetErrorString( error ) );
69 return error;
70 }
71
72 memset(&image_desc, 0x0, sizeof(cl_image_desc));
73 image_desc.image_type = CL_MEM_OBJECT_IMAGE1D_BUFFER;
74 image_desc.image_width = imageInfo->width;
75 image_desc.mem_object = imageBuffer;
76 image[0] = clCreateImage( context, CL_MEM_READ_ONLY, imageInfo->format,
77 &image_desc, NULL, &error );
78 if ( error != CL_SUCCESS )
79 {
80 log_error( "ERROR: Unable to create IMAGE1D_BUFFER of size %d pitch %d (%s)\n", (int)imageInfo->width, (int)imageInfo->rowPitch, IGetErrorString( error ) );
81 return error;
82 }
83
84 cl_mem ret = NULL;
85 error = clGetMemObjectInfo(image[0], CL_MEM_ASSOCIATED_MEMOBJECT, sizeof(ret), &ret, NULL);
86 if ( error != CL_SUCCESS )
87 {
88 log_error( "ERROR: Unable to query CL_MEM_ASSOCIATED_MEMOBJECT\n", IGetErrorString( error ) );
89 return error;
90 }
91
92 if (ret != imageBuffer) {
93 log_error("ERROR: clGetImageInfo for CL_IMAGE_BUFFER returned wrong value\n");
94 return -1;
95 }
96
97 memset(&image_desc, 0x0, sizeof(cl_image_desc));
98 image_desc.image_type = CL_MEM_OBJECT_IMAGE1D;
99 image_desc.image_width = imageInfo->width;
100 image[1] = clCreateImage( context, CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR, imageInfo->format, &image_desc, imageValues, &error );
101 if ( error != CL_SUCCESS )
102 {
103 log_error( "ERROR: Unable to create IMAGE1D of size %d pitch %d (%s)\n", (int)imageInfo->width, (int)imageInfo->rowPitch, IGetErrorString( error ) );
104 return error;
105 }
106
107 if ( gDebugTrace )
108 log_info( " - Creating kernel arguments...\n" );
109
110 // Create sampler to use
111 actualSampler = clCreateSampler( context, CL_FALSE, CL_ADDRESS_NONE, CL_FILTER_NEAREST, &error );
112 test_error( error, "Unable to create image sampler" );
113
114 // Create results buffer
115 cl_mem results = clCreateBuffer( context, 0, imageInfo->width * sizeof(cl_int), NULL, &error);
116 test_error( error, "Unable to create results buffer" );
117
118 size_t resultValuesSize = imageInfo->width * sizeof(cl_int);
119 BufferOwningPtr<int> resultValues(malloc( resultValuesSize ));
120 memset( resultValues, 0xff, resultValuesSize );
121 clEnqueueWriteBuffer( queue, results, CL_TRUE, 0, resultValuesSize, resultValues, 0, NULL, NULL );
122
123 // Set arguments
124 int idx = 0;
125 error = clSetKernelArg( kernel, idx++, sizeof( cl_mem ), &image[0] );
126 test_error( error, "Unable to set kernel arguments" );
127 error = clSetKernelArg( kernel, idx++, sizeof( cl_mem ), &image[1] );
128 test_error( error, "Unable to set kernel arguments" );
129 error = clSetKernelArg( kernel, idx++, sizeof( cl_sampler ), &actualSampler );
130 test_error( error, "Unable to set kernel arguments" );
131 error = clSetKernelArg( kernel, idx++, sizeof( cl_mem ), &results );
132 test_error( error, "Unable to set kernel arguments" );
133
134 // Run the kernel
135 threads[0] = (size_t)imageInfo->width;
136 error = clEnqueueNDRangeKernel( queue, kernel, 1, NULL, threads, NULL, 0, NULL, NULL );
137 test_error( error, "Unable to run kernel" );
138
139 if ( gDebugTrace )
140 log_info( " reading results, %ld kbytes\n", (unsigned long)( imageInfo->width * sizeof(cl_int) / 1024 ) );
141
142 error = clEnqueueReadBuffer( queue, results, CL_TRUE, 0, resultValuesSize, resultValues, 0, NULL, NULL );
143 test_error( error, "Unable to read results from kernel" );
144 if ( gDebugTrace )
145 log_info( " results read\n" );
146
147 // Check for non-zero comps
148 bool allZeroes = true;
149 for ( size_t ic = 0; ic < imageInfo->width; ++ic )
150 {
151 if ( resultValues[ic] ) {
152 allZeroes = false;
153 break;
154 }
155 }
156 if ( !allZeroes )
157 {
158 log_error( " Sampler-less reads differ from reads with sampler.\n" );
159 return -1;
160 }
161
162 clReleaseSampler(actualSampler);
163 clReleaseMemObject(results);
164 clReleaseMemObject(image[0]);
165 clReleaseMemObject(image[1]);
166 clReleaseMemObject(imageBuffer);
167 return 0;
168 }
169
test_read_image_set_1D_buffer(cl_device_id device,cl_context context,cl_command_queue queue,cl_image_format * format,image_sampler_data * imageSampler,ExplicitType outputType)170 int test_read_image_set_1D_buffer( cl_device_id device, cl_context context, cl_command_queue queue, cl_image_format *format, image_sampler_data *imageSampler,
171 ExplicitType outputType )
172 {
173 char programSrc[10240];
174 const char *ptr;
175 const char *readFormat;
176 const char *dataType;
177 clProgramWrapper program;
178 clKernelWrapper kernel;
179 RandomSeed seed( gRandomSeed );
180 int error;
181
182 // Get our operating params
183 size_t maxWidth, maxWidth1D;
184 cl_ulong maxAllocSize, memSize;
185 image_descriptor imageInfo = { 0 };
186 size_t pixelSize;
187
188 if (format->image_channel_order == CL_RGB || format->image_channel_order == CL_RGBx)
189 {
190 switch (format->image_channel_data_type)
191 {
192 case CL_UNORM_INT8:
193 case CL_UNORM_INT16:
194 case CL_SNORM_INT8:
195 case CL_SNORM_INT16:
196 case CL_HALF_FLOAT:
197 case CL_FLOAT:
198 case CL_SIGNED_INT8:
199 case CL_SIGNED_INT16:
200 case CL_SIGNED_INT32:
201 case CL_UNSIGNED_INT8:
202 case CL_UNSIGNED_INT16:
203 case CL_UNSIGNED_INT32:
204 case CL_UNORM_INT_101010:
205 log_info( "Skipping image format: %s %s\n", GetChannelOrderName( format->image_channel_order ),
206 GetChannelTypeName( format->image_channel_data_type ));
207 return 0;
208 default:
209 break;
210 }
211 }
212
213 imageInfo.format = format;
214 imageInfo.height = imageInfo.depth = imageInfo.arraySize = imageInfo.slicePitch = 0;
215 imageInfo.type = CL_MEM_OBJECT_IMAGE1D;
216 pixelSize = get_pixel_size( imageInfo.format );
217
218 error = clGetDeviceInfo( device, CL_DEVICE_IMAGE_MAX_BUFFER_SIZE, sizeof( maxWidth ), &maxWidth, NULL );
219 error |= clGetDeviceInfo( device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof( maxAllocSize ), &maxAllocSize, NULL );
220 error |= clGetDeviceInfo( device, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof( memSize ), &memSize, NULL );
221 error |= clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof( maxWidth ), &maxWidth1D, NULL );
222 test_error( error, "Unable to get max image 1D buffer size from device" );
223
224 if (memSize > (cl_ulong)SIZE_MAX) {
225 memSize = (cl_ulong)SIZE_MAX;
226 }
227
228 // note: image_buffer test uses image1D for results validation.
229 // So the test can't use the biggest possible size for image_buffer if it's bigger than the max image1D size
230 maxWidth = (maxWidth > maxWidth1D) ? maxWidth1D : maxWidth;
231 // Determine types
232 if ( outputType == kInt )
233 {
234 readFormat = "i";
235 dataType = "int4";
236 }
237 else if ( outputType == kUInt )
238 {
239 readFormat = "ui";
240 dataType = "uint4";
241 }
242 else // kFloat
243 {
244 readFormat = "f";
245 dataType = "float4";
246 }
247
248 sprintf( programSrc, read1DBufferKernelSourcePattern, dataType,
249 readFormat,
250 readFormat );
251
252 ptr = programSrc;
253 error = create_single_kernel_helper_with_build_options( context, &program, &kernel, 1, &ptr, "sample_kernel", gDeviceLt20 ? "" : "-cl-std=CL2.0" );
254 test_error( error, "Unable to create testing kernel" );
255
256 if ( gTestSmallImages )
257 {
258 for ( imageInfo.width = 1; imageInfo.width < 13; imageInfo.width++ )
259 {
260 imageInfo.rowPitch = imageInfo.width * pixelSize;
261 {
262 if ( gDebugTrace )
263 log_info( " at size %d\n", (int)imageInfo.width );
264
265 int retCode = test_read_image_1D_buffer( context, queue, kernel, &imageInfo, imageSampler, outputType, seed );
266 if ( retCode )
267 return retCode;
268 }
269 }
270 }
271 else if ( gTestMaxImages )
272 {
273 // Try a specific set of maximum sizes
274 size_t numbeOfSizes;
275 size_t sizes[100][3];
276
277 get_max_sizes(&numbeOfSizes, 100, sizes, maxWidth, 1, 1, 1, maxAllocSize, memSize, CL_MEM_OBJECT_IMAGE1D, imageInfo.format);
278
279 for ( size_t idx = 0; idx < numbeOfSizes; idx++ )
280 {
281 imageInfo.width = sizes[ idx ][ 0 ];
282 imageInfo.rowPitch = imageInfo.width * pixelSize;
283 log_info("Testing %d\n", (int)sizes[ idx ][ 0 ]);
284 if ( gDebugTrace )
285 log_info( " at max size %d\n", (int)sizes[ idx ][ 0 ] );
286 int retCode = test_read_image_1D_buffer( context, queue, kernel, &imageInfo, imageSampler, outputType, seed );
287 if ( retCode )
288 return retCode;
289 }
290 }
291 else
292 {
293 for ( int i = 0; i < NUM_IMAGE_ITERATIONS; i++ )
294 {
295 cl_ulong size;
296 // Loop until we get a size that a) will fit in the max alloc size and b) that an allocation of that
297 // image, the result array, plus offset arrays, will fit in the global ram space
298 do
299 {
300 imageInfo.width = (size_t)random_log_in_range( 16, (int)maxWidth / 32, seed );
301 imageInfo.rowPitch = imageInfo.width * pixelSize;
302 size = (size_t)imageInfo.rowPitch * 4;
303 } while ( size > maxAllocSize || ( size * 3 ) > memSize );
304
305 if ( gDebugTrace )
306 log_info( " at size %d (row pitch %d) out of %d\n", (int)imageInfo.width, (int)imageInfo.rowPitch, (int)maxWidth );
307 int retCode = test_read_image_1D_buffer( context, queue, kernel, &imageInfo, imageSampler, outputType, seed );
308 if ( retCode )
309 return retCode;
310 }
311 }
312
313 return 0;
314 }
315
316
317