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