• 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 #include <float.h>
18 
19 extern bool gTestReadWrite;
20 
21 const char *read3DKernelSourcePattern =
22 "__kernel void sample_kernel( read_only image3d_t input, sampler_t sampler, __global int *results )\n"
23 "{\n"
24 "   int tidX = get_global_id(0), tidY = get_global_id(1), tidZ = get_global_id(2);\n"
25 "   int offset = tidZ*get_image_width(input)*get_image_height(input) + tidY*get_image_width(input) + tidX;\n"
26 "   int4 coords = (int4)( tidX, tidY, tidZ, 0 );\n"
27 "   %s clr = read_image%s( input, coords );\n"
28 "   int4 test = (clr != read_image%s( input, sampler, coords ));\n"
29 "   if ( test.x || test.y || test.z || test.w )\n"
30 "      results[offset] = -1;\n"
31 "   else\n"
32 "      results[offset] = 0;\n"
33 "}";
34 
35 const char *read_write3DKernelSourcePattern =
36 "__kernel void sample_kernel( read_only image3d_t read_only_image, read_write image3d_t read_write_image, sampler_t sampler, __global int *results )\n"
37 "{\n"
38 "   int tidX = get_global_id(0), tidY = get_global_id(1), tidZ = get_global_id(2);\n"
39 "   int offset = tidZ*get_image_width(read_only_image)*get_image_height(read_only_image) + tidY*get_image_width(read_only_image) + tidX;\n"
40 "   int4 coords = (int4)( tidX, tidY, tidZ, 0 );\n"
41 "   %s clr = read_image%s( read_only_image, sampler, coords );\n"
42 "   write_image%s(read_write_image, coords, clr);\n"
43 "   atomic_work_item_fence(CLK_IMAGE_MEM_FENCE, memory_order_acq_rel, memory_scope_work_item);\n"
44 "   int4 test = (clr != read_image%s( read_write_image, coords ));\n"
45 "   if ( test.x || test.y || test.z || test.w )\n"
46 "      results[offset] = -1;\n"
47 "   else\n"
48 "      results[offset] = 0;\n"
49 "}";
test_read_image_3D(cl_context context,cl_command_queue queue,cl_kernel kernel,image_descriptor * imageInfo,image_sampler_data * imageSampler,ExplicitType outputType,MTdata d)50 int test_read_image_3D( cl_context context, cl_command_queue queue, cl_kernel kernel,
51                         image_descriptor *imageInfo, image_sampler_data *imageSampler,
52                         ExplicitType outputType, MTdata d )
53 {
54     int error;
55     size_t threads[3];
56     cl_sampler actualSampler;
57 
58     BufferOwningPtr<char> imageValues;
59     generate_random_image_data( imageInfo, imageValues, d );
60     // Don't use clEnqueueWriteImage; just use copy host ptr to get the data in
61     cl_image_desc image_desc;
62     cl_mem read_only_image, read_write_image;
63 
64     memset(&image_desc, 0x0, sizeof(cl_image_desc));
65     image_desc.image_type = CL_MEM_OBJECT_IMAGE3D;
66     image_desc.image_width = imageInfo->width;
67     image_desc.image_height = imageInfo->height;
68     image_desc.image_depth = imageInfo->depth;
69     image_desc.image_row_pitch = ( gEnablePitch ? imageInfo->rowPitch : 0 );
70     image_desc.image_slice_pitch = ( gEnablePitch ? imageInfo->slicePitch : 0 );
71     image_desc.num_mip_levels = 0;
72     read_only_image = clCreateImage( context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, imageInfo->format,
73                                        &image_desc, imageValues, &error );
74     if ( error != CL_SUCCESS )
75     {
76         log_error( "ERROR: Unable to create read_only 3D image of size %d x %d x %d (pitch %d, %d ) (%s)", (int)imageInfo->width, (int)imageInfo->height, (int)imageInfo->depth, (int)imageInfo->rowPitch, (int)imageInfo->slicePitch, IGetErrorString( error ) );
77         return error;
78     }
79 
80     if(gTestReadWrite)
81     {
82         read_write_image = clCreateImage(context,
83                                         CL_MEM_READ_WRITE,
84                                         imageInfo->format,
85                                         &image_desc,
86                                         NULL,
87                                         &error);
88         if ( error != CL_SUCCESS )
89         {
90             log_error( "ERROR: Unable to create read_write 3D image of size %d x %d x %d (pitch %d, %d ) (%s)", (int)imageInfo->width, (int)imageInfo->height, (int)imageInfo->depth, (int)imageInfo->rowPitch, (int)imageInfo->slicePitch, IGetErrorString( error ) );
91             return error;
92         }
93     }
94 
95     // Create sampler to use
96     actualSampler = clCreateSampler( context, CL_FALSE, CL_ADDRESS_NONE, CL_FILTER_NEAREST, &error );
97     test_error( error, "Unable to create image sampler" );
98 
99     // Create results buffer
100     cl_mem results = clCreateBuffer( context, 0, imageInfo->width * imageInfo->height * imageInfo->depth * sizeof(cl_int), NULL, &error);
101     test_error( error, "Unable to create results buffer" );
102 
103     size_t resultValuesSize = imageInfo->width * imageInfo->height * imageInfo->depth * sizeof(cl_int);
104     BufferOwningPtr<int> resultValues(malloc( resultValuesSize ));
105     memset( resultValues, 0xff, resultValuesSize );
106     clEnqueueWriteBuffer( queue, results, CL_TRUE, 0, resultValuesSize, resultValues, 0, NULL, NULL );
107 
108     // Set arguments
109     int idx = 0;
110     error = clSetKernelArg( kernel, idx++, sizeof( cl_mem ), &read_only_image );
111     test_error( error, "Unable to set kernel arguments" );
112     if(gTestReadWrite)
113     {
114         error = clSetKernelArg( kernel, idx++, sizeof( cl_mem ), &read_write_image );
115         test_error( error, "Unable to set kernel arguments" );
116     }
117     error = clSetKernelArg( kernel, idx++, sizeof( cl_sampler ), &actualSampler );
118     test_error( error, "Unable to set kernel arguments" );
119     error = clSetKernelArg( kernel, idx++, sizeof( cl_mem ), &results );
120     test_error( error, "Unable to set kernel arguments" );
121 
122     // Figure out thread dimensions
123     threads[0] = (size_t)imageInfo->width;
124     threads[1] = (size_t)imageInfo->height;
125     threads[2] = (size_t)imageInfo->depth;
126 
127     // Run the kernel
128     error = clEnqueueNDRangeKernel( queue, kernel, 3, NULL, threads, NULL, 0, NULL, NULL );
129     test_error( error, "Unable to run kernel" );
130 
131     if ( gDebugTrace )
132         log_info( "    reading results, %ld kbytes\n", (unsigned long)( imageInfo->width * imageInfo->height * imageInfo->depth * sizeof(cl_int) / 1024 ) );
133 
134     // Get results
135     error = clEnqueueReadBuffer( queue, results, CL_TRUE, 0, resultValuesSize, resultValues, 0, NULL, NULL );
136     test_error( error, "Unable to read results from kernel" );
137     if ( gDebugTrace )
138         log_info( "    results read\n" );
139 
140     // Check for non-zero comps
141     bool allZeroes = true;
142     size_t ic;
143     for ( ic = 0; ic < imageInfo->width * imageInfo->height * imageInfo->depth; ++ic )
144     {
145         if ( resultValues[ic] ) {
146             allZeroes = false;
147             break;
148         }
149     }
150     if ( !allZeroes )
151     {
152         log_error( " Sampler-less reads differ from reads with sampler at index %lu.\n", ic );
153         return -1;
154     }
155 
156     clReleaseSampler(actualSampler);
157     clReleaseMemObject(results);
158     clReleaseMemObject(read_only_image);
159     if(gTestReadWrite)
160     {
161         clReleaseMemObject(read_write_image);
162     }
163 
164     return 0;
165 }
166 
test_read_image_set_3D(cl_device_id device,cl_context context,cl_command_queue queue,const cl_image_format * format,image_sampler_data * imageSampler,ExplicitType outputType)167 int test_read_image_set_3D(cl_device_id device, cl_context context,
168                            cl_command_queue queue,
169                            const cl_image_format *format,
170                            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     RandomSeed seed( gRandomSeed );
178 
179     int error;
180 
181     if (gTestReadWrite && checkForReadWriteImageSupport(device))
182     {
183         return TEST_SKIPPED_ITSELF;
184     }
185 
186     clProgramWrapper program;
187     clKernelWrapper kernel;
188 
189     // Get operating parameters
190     size_t maxWidth, maxHeight, maxDepth;
191     cl_ulong maxAllocSize, memSize;
192     image_descriptor imageInfo = { 0 };
193     size_t pixelSize;
194 
195     imageInfo.format = format;
196     imageInfo.arraySize = 0;
197     imageInfo.type = CL_MEM_OBJECT_IMAGE3D;
198     pixelSize = get_pixel_size( imageInfo.format );
199 
200     error = clGetDeviceInfo( device, CL_DEVICE_IMAGE3D_MAX_WIDTH, sizeof( maxWidth ), &maxWidth, NULL );
201     error |= clGetDeviceInfo( device, CL_DEVICE_IMAGE3D_MAX_HEIGHT, sizeof( maxHeight ), &maxHeight, NULL );
202     error |= clGetDeviceInfo( device, CL_DEVICE_IMAGE3D_MAX_DEPTH, sizeof( maxDepth ), &maxDepth, NULL );
203     error |= clGetDeviceInfo( device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof( maxAllocSize ), &maxAllocSize, NULL );
204     error |= clGetDeviceInfo( device, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof( memSize ), &memSize, NULL );
205     test_error( error, "Unable to get max image 3D size from device" );
206 
207     if (memSize > (cl_ulong)SIZE_MAX) {
208       memSize = (cl_ulong)SIZE_MAX;
209       maxAllocSize = (cl_ulong)SIZE_MAX;
210     }
211 
212     // Determine types
213     if ( outputType == kInt )
214     {
215         readFormat = "i";
216         dataType = "int4";
217     }
218     else if ( outputType == kUInt )
219     {
220         readFormat = "ui";
221         dataType = "uint4";
222     }
223     else // kFloat
224     {
225         readFormat = "f";
226         dataType = "float4";
227     }
228 
229     // Construct the source
230     if(gTestReadWrite)
231     {
232         sprintf( programSrc,
233                  read_write3DKernelSourcePattern,
234                  dataType,
235                  readFormat,
236                  readFormat,
237                  readFormat);
238     }
239     else
240     {
241         sprintf( programSrc,
242                  read3DKernelSourcePattern,
243                  dataType,
244                  readFormat,
245                  readFormat );
246     }
247 
248 
249     ptr = programSrc;
250     error = create_single_kernel_helper(context, &program, &kernel, 1, &ptr,
251                                         "sample_kernel");
252     test_error( error, "Unable to create testing kernel" );
253 
254 
255     // Run tests
256     if ( gTestSmallImages )
257     {
258         for ( imageInfo.width = 1; imageInfo.width < 13; imageInfo.width++ )
259         {
260             imageInfo.rowPitch = imageInfo.width * pixelSize;
261 
262             for ( imageInfo.height = 1; imageInfo.height < 9; imageInfo.height++ )
263             {
264                 imageInfo.slicePitch = imageInfo.rowPitch * imageInfo.height;
265                 for ( imageInfo.depth = 2; imageInfo.depth < 9; imageInfo.depth++ )
266                 {
267                     if ( gDebugTrace )
268                         log_info( "   at size %d,%d,%d\n", (int)imageInfo.width, (int)imageInfo.height, (int)imageInfo.depth );
269                     int retCode = test_read_image_3D( context, queue, kernel, &imageInfo, imageSampler, outputType, seed );
270                     if ( retCode )
271                         return retCode;
272                 }
273             }
274         }
275     }
276     else if ( gTestMaxImages )
277     {
278         // Try a specific set of maximum sizes
279         size_t numbeOfSizes;
280         size_t sizes[100][3];
281 
282         get_max_sizes(&numbeOfSizes, 100, sizes, maxWidth, maxHeight, maxDepth, 1, maxAllocSize, memSize, CL_MEM_OBJECT_IMAGE3D, imageInfo.format);
283 
284         for ( size_t idx = 0; idx < numbeOfSizes; idx++ )
285         {
286             imageInfo.width = sizes[ idx ][ 0 ];
287             imageInfo.height = sizes[ idx ][ 1 ];
288             imageInfo.depth = sizes[ idx ][ 2 ];
289             imageInfo.rowPitch = imageInfo.width * pixelSize;
290             imageInfo.slicePitch = imageInfo.height * imageInfo.rowPitch;
291             log_info("Testing %d x %d x %d\n", (int)sizes[ idx ][ 0 ], (int)sizes[ idx ][ 1 ], (int)sizes[ idx ][ 2 ]);
292             if ( gDebugTrace )
293                 log_info( "   at max size %d,%d,%d\n", (int)sizes[ idx ][ 0 ], (int)sizes[ idx ][ 1 ], (int)sizes[ idx ][ 2 ] );
294             int retCode = test_read_image_3D( context, queue, kernel, &imageInfo, imageSampler, outputType, seed );
295             if ( retCode )
296                 return retCode;
297         }
298     }
299     else
300     {
301         for ( int i = 0; i < NUM_IMAGE_ITERATIONS; i++ )
302         {
303             cl_ulong size;
304             // Loop until we get a size that a) will fit in the max alloc size and b) that an allocation of that
305             // image, the result array, plus offset arrays, will fit in the global ram space
306             do
307             {
308                 imageInfo.width = (size_t)random_log_in_range( 16, (int)maxWidth / 32, seed );
309                 imageInfo.height = (size_t)random_log_in_range( 16, (int)maxHeight / 32, seed );
310                 imageInfo.depth = (size_t)random_log_in_range( 16, (int)maxDepth / 32, seed );
311 
312                 imageInfo.rowPitch = imageInfo.width * pixelSize;
313                 imageInfo.slicePitch = imageInfo.rowPitch * imageInfo.height;
314 
315                 if ( gEnablePitch )
316                 {
317                     size_t extraWidth = (int)random_log_in_range( 0, 64, seed );
318                     imageInfo.rowPitch += extraWidth * pixelSize;
319 
320                     size_t extraHeight = (int)random_log_in_range( 0, 64, seed );
321                     imageInfo.slicePitch = imageInfo.rowPitch * (imageInfo.height + extraHeight);
322                 }
323 
324                 size = (cl_ulong)imageInfo.slicePitch * (cl_ulong)imageInfo.depth * 4 * 4;
325             } while (  size > maxAllocSize || ( size * 3 ) > memSize );
326 
327             if ( gDebugTrace )
328                 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 );
329             int retCode = test_read_image_3D( context, queue, kernel, &imageInfo, imageSampler, outputType, seed );
330             if ( retCode )
331                 return retCode;
332         }
333     }
334 
335     return 0;
336 }
337