// // Copyright (c) 2017 The Khronos Group Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #include "harness/compat.h" #include #include #include #include #include "procs.h" #include "harness/testHarness.h" #include "harness/errorHelpers.h" //--- the code for the kernel executables static const char *readKernelCode[] = { "__kernel void testReadf(read_only image2d_t srcimg, __global float4 *dst)\n" "{\n" " int tid_x = get_global_id(0);\n" " int tid_y = get_global_id(1);\n" " int indx = tid_y * get_image_width(srcimg) + tid_x;\n" " float4 color;\n" "\n" " const sampler_t sampler = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST | CLK_NORMALIZED_COORDS_FALSE;\n" " color = read_imagef(srcimg, sampler, (int2)(tid_x, tid_y));\n" " dst[indx].x = color.x;\n" " dst[indx].y = color.y;\n" " dst[indx].z = color.z;\n" " dst[indx].w = color.w;\n" "\n" "}\n", "__kernel void testReadi(read_only image2d_t srcimg, __global uchar4 *dst)\n" "{\n" " int tid_x = get_global_id(0);\n" " int tid_y = get_global_id(1);\n" " int indx = tid_y * get_image_width(srcimg) + tid_x;\n" " int4 color;\n" "\n" " const sampler_t sampler = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST | CLK_NORMALIZED_COORDS_FALSE;\n" " color = read_imagei(srcimg, sampler, (int2)(tid_x, tid_y));\n" " uchar4 dst_write;\n" " dst_write.x = (uchar)color.x;\n" " dst_write.y = (uchar)color.y;\n" " dst_write.z = (uchar)color.z;\n" " dst_write.w = (uchar)color.w;\n" " dst[indx] = dst_write;\n" "\n" "}\n", "__kernel void testReadui(read_only image2d_t srcimg, __global uchar4 *dst)\n" "{\n" " int tid_x = get_global_id(0);\n" " int tid_y = get_global_id(1);\n" " int indx = tid_y * get_image_width(srcimg) + tid_x;\n" " uint4 color;\n" "\n" " const sampler_t sampler = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST | CLK_NORMALIZED_COORDS_FALSE;\n" " color = read_imageui(srcimg, sampler, (int2)(tid_x, tid_y));\n" " uchar4 dst_write;\n" " dst_write.x = (uchar)color.x;\n" " dst_write.y = (uchar)color.y;\n" " dst_write.z = (uchar)color.z;\n" " dst_write.w = (uchar)color.w;\n" " dst[indx] = dst_write;\n" "\n" "}\n", "__kernel void testWritef(__global uchar *src, write_only image2d_t dstimg)\n" "{\n" " int tid_x = get_global_id(0);\n" " int tid_y = get_global_id(1);\n" " int indx = tid_y * get_image_width(dstimg) + tid_x;\n" " float4 color;\n" "\n" " indx *= 4;\n" " color = (float4)((float)src[indx+0], (float)src[indx+1], (float)src[indx+2], (float)src[indx+3]);\n" " color /= (float4)(255.f, 255.f, 255.f, 255.f);\n" " write_imagef(dstimg, (int2)(tid_x, tid_y), color);\n" "\n" "}\n", "__kernel void testWritei(__global char *src, write_only image2d_t dstimg)\n" "{\n" " int tid_x = get_global_id(0);\n" " int tid_y = get_global_id(1);\n" " int indx = tid_y * get_image_width(dstimg) + tid_x;\n" " int4 color;\n" "\n" " indx *= 4;\n" " color.x = (int)src[indx+0];\n" " color.y = (int)src[indx+1];\n" " color.z = (int)src[indx+2];\n" " color.w = (int)src[indx+3];\n" " write_imagei(dstimg, (int2)(tid_x, tid_y), color);\n" "\n" "}\n", "__kernel void testWriteui(__global uchar *src, write_only image2d_t dstimg)\n" "{\n" " int tid_x = get_global_id(0);\n" " int tid_y = get_global_id(1);\n" " int indx = tid_y * get_image_width(dstimg) + tid_x;\n" " uint4 color;\n" "\n" " indx *= 4;\n" " color.x = (uint)src[indx+0];\n" " color.y = (uint)src[indx+1];\n" " color.z = (uint)src[indx+2];\n" " color.w = (uint)src[indx+3];\n" " write_imageui(dstimg, (int2)(tid_x, tid_y), color);\n" "\n" "}\n", "__kernel void testReadWriteff(read_only image2d_t srcimg, write_only image2d_t dstimg)\n" "{\n" " int tid_x = get_global_id(0);\n" " int tid_y = get_global_id(1);\n" " float4 color;\n" "\n" " color = read_imagef(srcimg, CLK_DEFAULT_SAMPLER, (int2)(tid_x, tid_y));\n" " write_imagef(dstimg, (int2)(tid_x, tid_y), color);\n" "\n" "}\n", "__kernel void testReadWriteii(read_only image2d_t srcimg, write_only image2d_t dstimg)\n" "{\n" " int tid_x = get_global_id(0);\n" " int tid_y = get_global_id(1);\n" " int4 color;\n" "\n" " color = read_imagei(srcimg, CLK_DEFAULT_SAMPLER, (int2)(tid_x, tid_y));\n" " write_imagei(dstimg, (int2)(tid_x, tid_y), color);\n" "\n" "}\n", "__kernel void testReadWriteuiui(read_only image2d_t srcimg, write_only image2d_t dstimg)\n" "{\n" " int tid_x = get_global_id(0);\n" " int tid_y = get_global_id(1);\n" " uint4 color;\n" "\n" " color = read_imageui(srcimg, CLK_DEFAULT_SAMPLER, (int2)(tid_x, tid_y));\n" " write_imageui(dstimg, (int2)(tid_x, tid_y), color);\n" "\n" "}\n", "__kernel void testReadWritefi(read_only image2d_t srcimg, write_only image2d_t dstimg)\n" "{\n" " int tid_x = get_global_id(0);\n" " int tid_y = get_global_id(1);\n" " float4 colorf;\n" " int4 colori;\n" "\n" " const sampler_t sampler = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST | CLK_NORMALIZED_COORDS_FALSE;\n" " colorf = read_imagef(srcimg, sampler, (int2)(tid_x, tid_y));\n" // since we are going from unsigned to signed, be sure to convert // values greater 0.5 to negative values " if( colorf.x >= 0.5f )\n" " colori.x = (int)( ( colorf.x - 1.f ) * 255.f );\n" " else\n" " colori.x = (int)( colorf.x * 255.f );\n" " if( colorf.y >= 0.5f )\n" " colori.y = (int)( ( colorf.y - 1.f ) * 255.f );\n" " else\n" " colori.y = (int)( colorf.y * 255.f );\n" " if( colorf.z >= 0.5f )\n" " colori.z = (int)( ( colorf.z - 1.f ) * 255.f );\n" " else\n" " colori.z = (int)( colorf.z * 255.f );\n" " if( colorf.w >= 0.5f )\n" " colori.w = (int)( ( colorf.w - 1.f ) * 255.f );\n" " else\n" " colori.w = (int)( colorf.w * 255.f );\n" " write_imagei(dstimg, (int2)(tid_x, tid_y), colori);\n" "\n" "}\n", "__kernel void testReadWritefui(read_only image2d_t srcimg, write_only image2d_t dstimg)\n" "{\n" " int tid_x = get_global_id(0);\n" " int tid_y = get_global_id(1);\n" " float4 colorf;\n" " uint4 colorui;\n" "\n" " const sampler_t sampler = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST | CLK_NORMALIZED_COORDS_FALSE;\n" " colorf = read_imagef(srcimg, sampler, (int2)(tid_x, tid_y));\n" " colorui.x = (uint)( colorf.x * 255.f );\n" " colorui.y = (uint)( colorf.y * 255.f );\n" " colorui.z = (uint)( colorf.z * 255.f );\n" " colorui.w = (uint)( colorf.w * 255.f );\n" " write_imageui(dstimg, (int2)(tid_x, tid_y), colorui);\n" "\n" "}\n", "__kernel void testReadWriteif(read_only image2d_t srcimg, write_only image2d_t dstimg)\n" "{\n" " int tid_x = get_global_id(0);\n" " int tid_y = get_global_id(1);\n" " int4 colori;\n" " float4 colorf;\n" "\n" // since we are going from signed to unsigned, we need to adjust the rgba values from // from the signed image to add 256 to the signed image values less than 0. " const sampler_t sampler = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST | CLK_NORMALIZED_COORDS_FALSE;\n" " colori = read_imagei(srcimg, sampler, (int2)(tid_x, tid_y));\n" " if( colori.x < 0 )\n" " colorf.x = ( (float)colori.x + 256.f ) / 255.f;\n" " else\n" " colorf.x = (float)colori.x / 255.f;\n" " if( colori.y < 0 )\n" " colorf.y = ( (float)colori.y + 256.f ) / 255.f;\n" " else\n" " colorf.y = (float)colori.y / 255.f;\n" " if( colori.z < 0 )\n" " colorf.z = ( (float)colori.z + 256.f ) / 255.f;\n" " else\n" " colorf.z = (float)colori.z / 255.f;\n" " if( colori.w < 0 )\n" " colorf.w = ( (float)colori.w + 256.f ) / 255.f;\n" " else\n" " colorf.w = (float)colori.w / 255.f;\n" " write_imagef(dstimg, (int2)(tid_x, tid_y), colorf);\n" "\n" "}\n", "__kernel void testReadWriteiui(read_only image2d_t srcimg, write_only image2d_t dstimg)\n" "{\n" " int tid_x = get_global_id(0);\n" " int tid_y = get_global_id(1);\n" " int4 colori;\n" " uint4 colorui;\n" "\n" " const sampler_t sampler = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST | CLK_NORMALIZED_COORDS_FALSE;\n" " colori = read_imagei(srcimg, sampler, (int2)(tid_x, tid_y));\n" // since we are going from signed to unsigned, we need to adjust the rgba values from // from the signed image to add 256 to the signed image values less than 0. " if( colori.x < 0 )\n" " colorui.x = (uint)( colori.x + 256 );\n" " else\n" " colorui.x = (uint)colori.x;\n" " if( colori.y < 0 )\n" " colorui.y = (uint)( colori.y + 256 );\n" " else\n" " colorui.y = (uint)colori.y;\n" " if( colori.z < 0 )\n" " colorui.z = (uint)( colori.z + 256 );\n" " else\n" " colorui.z = (uint)colori.z;\n" " if( colori.w < 0 )\n" " colorui.w = (uint)( colori.w + 256 );\n" " else\n" " colorui.w = (uint)colori.w;\n" " write_imageui(dstimg, (int2)(tid_x, tid_y), colorui);\n" "\n" "}\n", "__kernel void testReadWriteuif(read_only image2d_t srcimg, write_only image2d_t dstimg)\n" "{\n" " int tid_x = get_global_id(0);\n" " int tid_y = get_global_id(1);\n" " uint4 colorui;\n" " float4 colorf;\n" "\n" " const sampler_t sampler = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST | CLK_NORMALIZED_COORDS_FALSE;\n" " colorui = read_imageui(srcimg, sampler, (int2)(tid_x, tid_y));\n" " colorf.x = (float)colorui.x / 255.f;\n" " colorf.y = (float)colorui.y / 255.f;\n" " colorf.z = (float)colorui.z / 255.f;\n" " colorf.w = (float)colorui.w / 255.f;\n" " write_imagef(dstimg, (int2)(tid_x, tid_y), colorf);\n" "\n" "}\n", "__kernel void testReadWriteuii(read_only image2d_t srcimg, write_only image2d_t dstimg)\n" "{\n" " int tid_x = get_global_id(0);\n" " int tid_y = get_global_id(1);\n" " uint4 colorui;\n" " int4 colori;\n" "\n" " const sampler_t sampler = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST | CLK_NORMALIZED_COORDS_FALSE;\n" " colorui = read_imageui(srcimg, sampler, (int2)(tid_x, tid_y));\n" // since we are going from unsigned to signed, be sure to convert // values greater 0.5 to negative values " if( colorui.x >= 128U )\n" " colori.x = (int)colorui.x - 256;\n" " else\n" " colori.x = (int)colorui.x;\n" " if( colorui.y >= 128U )\n" " colori.y = (int)colorui.y - 256;\n" " else\n" " colori.y = (int)colorui.y;\n" " if( colorui.z >= 128U )\n" " colori.z = (int)colorui.z - 256;\n" " else\n" " colori.z = (int)colorui.z;\n" " if( colorui.w >= 128U )\n" " colori.w = (int)colorui.w - 256;\n" " else\n" " colori.w = (int)colorui.w;\n" " write_imagei(dstimg, (int2)(tid_x, tid_y), colori);\n" "\n" "}\n" }; static const char *readKernelName[] = { "testReadf", "testReadi", "testReadui", "testWritef", "testWritei", "testWriteui", "testReadWriteff", "testReadWriteii", "testReadWriteuiui", "testReadWritefi", "testReadWritefui", "testReadWriteif", "testReadWriteiui", "testReadWriteuif", "testReadWriteuii" }; static cl_uchar *generateImage( int n, MTdata d ) { cl_uchar *ptr = (cl_uchar *)malloc( n * sizeof( cl_uchar ) ); int i; for( i = 0; i < n; i++ ){ ptr[i] = (cl_uchar)genrand_int32(d); } return ptr; } static char *generateSignedImage( int n, MTdata d ) { char *ptr = (char *)malloc( n * sizeof( char ) ); int i; for( i = 0; i < n; i++ ){ ptr[i] = (char)genrand_int32(d); } return ptr; } static int verifyImage( cl_uchar *image, cl_uchar *outptr, int w, int h ) { int i; for( i = 0; i < w * h * 4; i++ ){ if( outptr[i] != image[i] ){ log_error("Image verification failed at offset %d. Actual value=%d, expected value=%d\n", i, outptr[i], image[i]); return -1; } } return 0; } static int verifyImageFloat ( cl_double *refptr, cl_float *outptr, int w, int h ) { int i; for (i=0; i