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 #ifndef test_conformance_clImageHelper_h 17 #define test_conformance_clImageHelper_h 18 19 #ifdef __APPLE__ 20 #include <OpenCL/opencl.h> 21 #else 22 #include <CL/cl.h> 23 #endif 24 25 #include <stdio.h> 26 #include "errorHelpers.h" 27 28 29 // helper function to replace clCreateImage2D , to make the existing code use 30 // the functions of version 1.2 and veriosn 1.1 respectively 31 create_image_2d(cl_context context,cl_mem_flags flags,const cl_image_format * image_format,size_t image_width,size_t image_height,size_t image_row_pitch,void * host_ptr,cl_int * errcode_ret)32 static inline cl_mem create_image_2d (cl_context context, 33 cl_mem_flags flags, 34 const cl_image_format *image_format, 35 size_t image_width, 36 size_t image_height, 37 size_t image_row_pitch, 38 void *host_ptr, 39 cl_int *errcode_ret) 40 { 41 cl_mem mImage = NULL; 42 43 #ifdef CL_VERSION_1_2 44 cl_image_desc image_desc_dest; 45 image_desc_dest.image_type = CL_MEM_OBJECT_IMAGE2D;; 46 image_desc_dest.image_width = image_width; 47 image_desc_dest.image_height = image_height; 48 image_desc_dest.image_depth= 0;// not usedfor 2d 49 image_desc_dest.image_array_size = 0;// not used for 2d 50 image_desc_dest.image_row_pitch = image_row_pitch; 51 image_desc_dest.image_slice_pitch = 0; 52 image_desc_dest.num_mip_levels = 0; 53 image_desc_dest.num_samples = 0; 54 image_desc_dest.mem_object = NULL;// no image type of CL_MEM_OBJECT_IMAGE1D_BUFFER in CL_VERSION_1_1, so always is NULL 55 mImage = clCreateImage( context, flags, image_format, &image_desc_dest, host_ptr, errcode_ret ); 56 if (errcode_ret && (*errcode_ret)) { 57 // Log an info message and rely on the calling function to produce an error 58 // if necessary. 59 log_info("clCreateImage failed (%d)\n", *errcode_ret); 60 } 61 62 #else 63 mImage = clCreateImage2D( context, flags, image_format, image_width, image_height, image_row_pitch, host_ptr, errcode_ret ); 64 if (errcode_ret && (*errcode_ret)) { 65 // Log an info message and rely on the calling function to produce an error 66 // if necessary. 67 log_info("clCreateImage2D failed (%d)\n", *errcode_ret); 68 } 69 #endif 70 71 return mImage; 72 } 73 74 // helper function to replace clCreateImage2D , to make the existing code use 75 // the functions of version 1.2 and veriosn 1.1 respectively 76 create_image_2d_buffer(cl_context context,cl_mem_flags flags,const cl_image_format * image_format,size_t image_width,size_t image_height,size_t image_row_pitch,cl_mem buffer,cl_int * errcode_ret)77 static inline cl_mem create_image_2d_buffer (cl_context context, 78 cl_mem_flags flags, 79 const cl_image_format *image_format, 80 size_t image_width, 81 size_t image_height, 82 size_t image_row_pitch, 83 cl_mem buffer, 84 cl_int *errcode_ret) 85 { 86 cl_mem mImage = NULL; 87 88 cl_image_desc image_desc_dest; 89 image_desc_dest.image_type = CL_MEM_OBJECT_IMAGE2D;; 90 image_desc_dest.image_width = image_width; 91 image_desc_dest.image_height = image_height; 92 image_desc_dest.image_depth= 0;// not usedfor 2d 93 image_desc_dest.image_array_size = 0;// not used for 2d 94 image_desc_dest.image_row_pitch = image_row_pitch; 95 image_desc_dest.image_slice_pitch = 0; 96 image_desc_dest.num_mip_levels = 0; 97 image_desc_dest.num_samples = 0; 98 image_desc_dest.mem_object = buffer; 99 mImage = clCreateImage( context, flags, image_format, &image_desc_dest, NULL, errcode_ret ); 100 if (errcode_ret && (*errcode_ret)) { 101 // Log an info message and rely on the calling function to produce an error 102 // if necessary. 103 log_info("clCreateImage failed (%d)\n", *errcode_ret); 104 } 105 106 return mImage; 107 } 108 109 110 create_image_3d(cl_context context,cl_mem_flags flags,const cl_image_format * image_format,size_t image_width,size_t image_height,size_t image_depth,size_t image_row_pitch,size_t image_slice_pitch,void * host_ptr,cl_int * errcode_ret)111 static inline cl_mem create_image_3d (cl_context context, 112 cl_mem_flags flags, 113 const cl_image_format *image_format, 114 size_t image_width, 115 size_t image_height, 116 size_t image_depth, 117 size_t image_row_pitch, 118 size_t image_slice_pitch, 119 void *host_ptr, 120 cl_int *errcode_ret) 121 { 122 cl_mem mImage; 123 124 #ifdef CL_VERSION_1_2 125 cl_image_desc image_desc; 126 image_desc.image_type = CL_MEM_OBJECT_IMAGE3D; 127 image_desc.image_width = image_width; 128 image_desc.image_height = image_height; 129 image_desc.image_depth = image_depth; 130 image_desc.image_array_size = 0;// not used for one image 131 image_desc.image_row_pitch = image_row_pitch; 132 image_desc.image_slice_pitch = image_slice_pitch; 133 image_desc.num_mip_levels = 0; 134 image_desc.num_samples = 0; 135 image_desc.mem_object = NULL; // no image type of CL_MEM_OBJECT_IMAGE1D_BUFFER in CL_VERSION_1_1, so always is NULL 136 mImage = clCreateImage( context, 137 flags, 138 image_format, 139 &image_desc, 140 host_ptr, 141 errcode_ret ); 142 if (errcode_ret && (*errcode_ret)) { 143 // Log an info message and rely on the calling function to produce an error 144 // if necessary. 145 log_info("clCreateImage failed (%d)\n", *errcode_ret); 146 } 147 148 #else 149 mImage = clCreateImage3D( context, 150 flags, image_format, 151 image_width, 152 image_height, 153 image_depth, 154 image_row_pitch, 155 image_slice_pitch, 156 host_ptr, 157 errcode_ret ); 158 if (errcode_ret && (*errcode_ret)) { 159 // Log an info message and rely on the calling function to produce an error 160 // if necessary. 161 log_info("clCreateImage3D failed (%d)\n", *errcode_ret); 162 } 163 #endif 164 165 return mImage; 166 } 167 create_image_2d_array(cl_context context,cl_mem_flags flags,const cl_image_format * image_format,size_t image_width,size_t image_height,size_t image_array_size,size_t image_row_pitch,size_t image_slice_pitch,void * host_ptr,cl_int * errcode_ret)168 static inline cl_mem create_image_2d_array (cl_context context, 169 cl_mem_flags flags, 170 const cl_image_format *image_format, 171 size_t image_width, 172 size_t image_height, 173 size_t image_array_size, 174 size_t image_row_pitch, 175 size_t image_slice_pitch, 176 void *host_ptr, 177 cl_int *errcode_ret) 178 { 179 cl_mem mImage; 180 181 cl_image_desc image_desc; 182 image_desc.image_type = CL_MEM_OBJECT_IMAGE2D_ARRAY; 183 image_desc.image_width = image_width; 184 image_desc.image_height = image_height; 185 image_desc.image_depth = 1; 186 image_desc.image_array_size = image_array_size; 187 image_desc.image_row_pitch = image_row_pitch; 188 image_desc.image_slice_pitch = image_slice_pitch; 189 image_desc.num_mip_levels = 0; 190 image_desc.num_samples = 0; 191 image_desc.mem_object = NULL; 192 mImage = clCreateImage( context, 193 flags, 194 image_format, 195 &image_desc, 196 host_ptr, 197 errcode_ret ); 198 if (errcode_ret && (*errcode_ret)) { 199 // Log an info message and rely on the calling function to produce an error 200 // if necessary. 201 log_info("clCreateImage failed (%d)\n", *errcode_ret); 202 } 203 204 return mImage; 205 } 206 create_image_1d_array(cl_context context,cl_mem_flags flags,const cl_image_format * image_format,size_t image_width,size_t image_array_size,size_t image_row_pitch,size_t image_slice_pitch,void * host_ptr,cl_int * errcode_ret)207 static inline cl_mem create_image_1d_array (cl_context context, 208 cl_mem_flags flags, 209 const cl_image_format *image_format, 210 size_t image_width, 211 size_t image_array_size, 212 size_t image_row_pitch, 213 size_t image_slice_pitch, 214 void *host_ptr, 215 cl_int *errcode_ret) 216 { 217 cl_mem mImage; 218 219 cl_image_desc image_desc; 220 image_desc.image_type = CL_MEM_OBJECT_IMAGE1D_ARRAY; 221 image_desc.image_width = image_width; 222 image_desc.image_height = 1; 223 image_desc.image_depth = 1; 224 image_desc.image_array_size = image_array_size; 225 image_desc.image_row_pitch = image_row_pitch; 226 image_desc.image_slice_pitch = image_slice_pitch; 227 image_desc.num_mip_levels = 0; 228 image_desc.num_samples = 0; 229 image_desc.mem_object = NULL; 230 mImage = clCreateImage( context, 231 flags, 232 image_format, 233 &image_desc, 234 host_ptr, 235 errcode_ret ); 236 if (errcode_ret && (*errcode_ret)) { 237 // Log an info message and rely on the calling function to produce an error 238 // if necessary. 239 log_info("clCreateImage failed (%d)\n", *errcode_ret); 240 } 241 242 return mImage; 243 } 244 create_image_1d(cl_context context,cl_mem_flags flags,const cl_image_format * image_format,size_t image_width,size_t image_row_pitch,void * host_ptr,cl_mem buffer,cl_int * errcode_ret)245 static inline cl_mem create_image_1d (cl_context context, 246 cl_mem_flags flags, 247 const cl_image_format *image_format, 248 size_t image_width, 249 size_t image_row_pitch, 250 void *host_ptr, 251 cl_mem buffer, 252 cl_int *errcode_ret) 253 { 254 cl_mem mImage; 255 256 cl_image_desc image_desc; 257 image_desc.image_type = buffer ? CL_MEM_OBJECT_IMAGE1D_BUFFER: CL_MEM_OBJECT_IMAGE1D; 258 image_desc.image_width = image_width; 259 image_desc.image_height = 1; 260 image_desc.image_depth = 1; 261 image_desc.image_row_pitch = image_row_pitch; 262 image_desc.image_slice_pitch = 0; 263 image_desc.num_mip_levels = 0; 264 image_desc.num_samples = 0; 265 image_desc.mem_object = buffer; 266 mImage = clCreateImage( context, 267 flags, 268 image_format, 269 &image_desc, 270 host_ptr, 271 errcode_ret ); 272 if (errcode_ret && (*errcode_ret)) { 273 // Log an info message and rely on the calling function to produce an error 274 // if necessary. 275 log_info("clCreateImage failed (%d)\n", *errcode_ret); 276 } 277 278 return mImage; 279 } 280 281 282 #endif 283