File: clCreateImage.c

package info (click to toggle)
pocl 6.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,320 kB
  • sloc: lisp: 149,513; ansic: 103,778; cpp: 54,947; python: 1,513; sh: 949; ruby: 255; pascal: 226; tcl: 180; makefile: 175; java: 72; xml: 49
file content (358 lines) | stat: -rw-r--r-- 13,460 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* OpenCL runtime library: clCreateImage()

   Copyright (c) 2012 Timo Viitanen, 2013 Ville Korhonen
   
   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:
   
   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.
   
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
*/

#include "pocl_cl.h"
#include "pocl_image_util.h"
#include "pocl_shared.h"
#include "pocl_util.h"

extern unsigned long image_c;

cl_mem
pocl_create_image_internal (cl_context context, cl_mem_flags flags,
                            const cl_image_format *image_format,
                            const cl_image_desc *image_desc, void *host_ptr,
                            cl_int *errcode_ret,
                            cl_GLenum gl_target, cl_GLint gl_miplevel,
                            cl_GLuint gl_texture,
                            CLeglDisplayKHR egl_display,
                            CLeglImageKHR egl_image)
{
    cl_mem mem = NULL;
    unsigned i, num_devices_supporting_image = 0;
    size_t size = 0;
    int errcode = CL_SUCCESS;
    int *device_image_support = NULL;
    size_t row_pitch;
    size_t slice_pitch;
    int elem_size;
    int channels;
    size_t elem_bytes;
    cl_int image_type_idx;
    cl_mem_object_type image_type;

    POCL_GOTO_ERROR_COND ((!IS_CL_OBJECT_VALID (context)), CL_INVALID_CONTEXT);

    POCL_GOTO_ERROR_COND((image_format == NULL), CL_INVALID_IMAGE_FORMAT_DESCRIPTOR);

    POCL_GOTO_ERROR_COND((image_desc == NULL), CL_INVALID_IMAGE_DESCRIPTOR);

    POCL_GOTO_ERROR_ON (
        (image_desc->num_mip_levels != 0 || image_desc->num_samples != 0),
        CL_INVALID_IMAGE_FORMAT_DESCRIPTOR,
        "Unimplemented: clCreateImage with image_desc->num_mip_levels != 0"
        " || image_desc->num_samples != 0 ");

    image_type = image_desc->image_type;
    image_type_idx = pocl_opencl_image_type_to_index (image_type);
    POCL_GOTO_ERROR_ON ((image_type_idx < 0),
                        CL_INVALID_VALUE, "unknown image type\n");

    /* CL_INVALID_IMAGE_SIZE if image dimensions specified in image_desc exceed
     * the minimum maximum image dimensions described in the table of allowed
     * values for param_name for clGetDeviceInfo FOR ALL DEVICES IN CONTEXT.
     */
    device_image_support = (int *)calloc (context->num_devices, sizeof (int));
#ifdef ENABLE_OPENGL_INTEROP
    unsigned is_gl_texture
        = (unsigned)((intptr_t)gl_target | (intptr_t)gl_miplevel
                     | (intptr_t)gl_texture);
#elif defined(ENABLE_EGL_INTEROP)
    unsigned is_gl_texture
        = (unsigned)((intptr_t)egl_display | (intptr_t)egl_image);
#else
    unsigned is_gl_texture = 0;
#endif

    for (i = 0; i < context->num_devices; i++)
      {
        cl_device_id dev = context->devices[i];
        if (!dev->image_support || (*(dev->available) == CL_FALSE))
          continue;
        else
          {
            if (pocl_check_device_supports_image (
                    dev, image_format, image_desc, image_type_idx,
                    is_gl_texture, &device_image_support[i])
                == CL_SUCCESS)
              {
                /* can't break here as we need device_image_support[]
                 * for all devices */
                ++num_devices_supporting_image;
              }
          }
      }
    POCL_GOTO_ERROR_ON ((num_devices_supporting_image == 0),
                        CL_INVALID_OPERATION,
                        "There are no devices in context that support this "
                        "image type + size \n");

    pocl_get_image_information (image_format->image_channel_order,
                                image_format->image_channel_data_type,
                                &channels, &elem_size);
    elem_bytes = (size_t)elem_size * (size_t)channels;

    row_pitch = image_desc->image_row_pitch;
    slice_pitch = image_desc->image_slice_pitch;

    /* This must be 0 if host_ptr is NULL and can be either 0 or ≥
     * image_width * size of element in bytes if host_ptr is not NULL.
     * If host_ptr is not NULL and image_row_pitch = 0, image_row_pitch
     * is calculated as image_width * size of element in bytes. If
     * image_row_pitch is not 0, it must be a multiple of the
     * image element size in bytes.
     */
    if (row_pitch == 0)
      {
        row_pitch = image_desc->image_width * elem_bytes;
      }
    else
      {
        POCL_GOTO_ERROR_COND ((row_pitch % elem_bytes), CL_INVALID_VALUE);
      }

    /* The size in bytes of each 2D slice in the 3D image or the size in bytes
     * of each image in a 1D or 2D image array. This must be 0 if host_ptr is
     * NULL. If host_ptr is not NULL, image_slice_pitch can be either 0 or ≥
     * image_row_pitch * image_height for a 2D image array or 3D image and can
     * be either 0 or ≥ image_row_pitch for a 1D image array. If host_ptr is
     * not NULL and image_slice_pitch = 0, image_slice_pitch is calculated as
     * image_row_pitch * image_height for a 2D image array or 3D image and
     * image_row_pitch for a 1D image array. If image_slice_pitch is not 0,
     * it must be a multiple of the image_row_pitch.
     */

    if (slice_pitch == 0)
      {
        if (image_desc->image_type == CL_MEM_OBJECT_IMAGE3D ||
            image_desc->image_type == CL_MEM_OBJECT_IMAGE2D_ARRAY)
          {
            slice_pitch = row_pitch * image_desc->image_height;
          }
        if (image_desc->image_type == CL_MEM_OBJECT_IMAGE1D_ARRAY)
          {
            slice_pitch = row_pitch;
          }
      }
    else
      {
        POCL_GOTO_ERROR_COND ((slice_pitch % row_pitch), CL_INVALID_VALUE);
      }

    if (image_desc->image_type == CL_MEM_OBJECT_IMAGE3D)
      size = slice_pitch * image_desc->image_depth;

    if (image_desc->image_type == CL_MEM_OBJECT_IMAGE2D)
      size = row_pitch * image_desc->image_height;

    if (image_desc->image_type == CL_MEM_OBJECT_IMAGE1D
        || image_desc->image_type == CL_MEM_OBJECT_IMAGE1D_BUFFER)
      size = row_pitch;

    if (image_desc->image_type == CL_MEM_OBJECT_IMAGE2D_ARRAY
        || image_desc->image_type == CL_MEM_OBJECT_IMAGE1D_ARRAY)
      {
        size = slice_pitch * image_desc->image_array_size;
      }

    /* Create buffer and fill in missing parts */
    if (image_desc->image_type == CL_MEM_OBJECT_IMAGE1D_BUFFER)
      {
        POCL_GOTO_ERROR_COND ((image_desc->buffer == NULL),
                              CL_INVALID_MEM_OBJECT);

        POCL_GOTO_ERROR_ON   ((image_desc->buffer->parent != NULL),
                              CL_INVALID_MEM_OBJECT,
                              "pocl does not support Image 1D"
                              " Buffer over SubBuffers\n");

        POCL_GOTO_ERROR_ON ((image_desc->buffer->size < size),
                            CL_INVALID_MEM_OBJECT,
                            "Invalid buffer sizes: image_desc->buffer->size"
                            " %zu || Required size: %zu\n",
                            image_desc->buffer->size, size);

        mem = (cl_mem) calloc (1, sizeof (struct _cl_mem));
        POCL_GOTO_ERROR_COND ((mem == NULL), CL_OUT_OF_HOST_MEMORY);
        POCL_INIT_OBJECT (mem);

        cl_mem b = image_desc->buffer;
        mem->is_image = CL_TRUE;
        mem->type = CL_MEM_OBJECT_IMAGE1D_BUFFER;
        mem->device_supports_this_image = device_image_support;
        mem->buffer = b;

        mem->size = size;
        mem->origin = 0;

        mem->context = context;
        assert (mem->context == b->context);

        mem->parent = b;
        pocl_cl_mem_inherit_flags (mem, b, flags);

        /* Retain the buffer we're referencing */
        POname (clRetainMemObject) (b);

        POCL_MSG_PRINT_MEMORY ("Created Image:  %" PRId64
                               " (%p), Refbuffer: %" PRId64 " (%p) \n\n",
                               mem->id, mem, b->id, b);
      }
    else
      {
        POCL_GOTO_ERROR_COND ((image_desc->buffer != NULL),
                              CL_INVALID_OPERATION);
        int host_ptr_is_svm = CL_FALSE;

        if ((flags & CL_MEM_USE_HOST_PTR) && host_ptr != NULL)
          {
            pocl_raw_ptr *item
                = pocl_find_raw_ptr_with_vm_ptr (context, host_ptr);
            if (item)
              {
                POCL_GOTO_ERROR_ON ((item->size < size), CL_INVALID_BUFFER_SIZE,
                                    "The provided host_ptr is SVM pointer, "
                                    "but the allocated SVM size (%zu) is smaller "
                                    "then requested size (%zu)",
                                    item->size, size);
                host_ptr_is_svm = CL_TRUE;
              }
          }

        mem = pocl_create_memobject (context, flags, size,
                                     image_desc->image_type,
                                     device_image_support,
                                     host_ptr, host_ptr_is_svm, &errcode);
        if (mem == NULL)
          goto ERROR;
      }

    mem->image_width = image_desc->image_width;
    if (image_desc->image_type == CL_MEM_OBJECT_IMAGE2D
        || image_desc->image_type == CL_MEM_OBJECT_IMAGE2D_ARRAY
        || image_desc->image_type == CL_MEM_OBJECT_IMAGE3D)
      mem->image_height = image_desc->image_height;
    else
      mem->image_height = 0;
    if (image_desc->image_type == CL_MEM_OBJECT_IMAGE3D)
      mem->image_depth = image_desc->image_depth;
    else
      mem->image_depth = 0;
    if (image_desc->image_type == CL_MEM_OBJECT_IMAGE2D_ARRAY
        || image_desc->image_type == CL_MEM_OBJECT_IMAGE1D_ARRAY)
      mem->image_array_size = image_desc->image_array_size;
    else
      mem->image_array_size = 0;
    mem->image_row_pitch = row_pitch;
    mem->image_slice_pitch = slice_pitch;
    mem->image_channel_data_type = image_format->image_channel_data_type;
    mem->image_channel_order = image_format->image_channel_order;
    mem->num_mip_levels = image_desc->num_mip_levels;
    mem->num_samples = image_desc->num_samples;
    mem->image_channels = channels;
    mem->image_elem_size = elem_size;

    TP_CREATE_IMAGE (context->id, mem->id);

    mem->is_gl_texture = is_gl_texture;
    if (is_gl_texture)
      {
        mem->is_gl_acquired = 0;
        mem->target = gl_target;
        mem->miplevel = gl_miplevel;
        mem->texture = gl_texture;
        mem->egl_display = egl_display;
        mem->egl_image = egl_image;
      }

    POCL_RETAIN_OBJECT (context);

    POCL_MSG_PRINT_MEMORY (
        "Created Image %" PRId64
        " (%p), HOST_PTR: %p, SIZE %zu RP %zu SP %zu FLAGS %u \n",
        mem->id, mem, mem->mem_host_ptr, size, mem->image_row_pitch,
        mem->image_slice_pitch, (unsigned)flags);

    POCL_ATOMIC_INC (image_c);

 ERROR:
   if (errcode != CL_SUCCESS)
     POCL_MEM_FREE (device_image_support);

   if (errcode_ret)
     {
       *errcode_ret = errcode;
     }

   return mem;
}

CL_API_ENTRY cl_mem CL_API_CALL POname (clCreateImage) (
    cl_context context, cl_mem_flags flags,
    const cl_image_format *image_format, const cl_image_desc *image_desc,
    void *host_ptr, cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_2
{
  return pocl_create_image_internal (context, flags, image_format, image_desc,
                                     host_ptr, errcode_ret,
                                     0, 0, 0, NULL, NULL);
}
POsym (clCreateImage)

CL_API_ENTRY cl_mem CL_API_CALL
POname(clCreateImageWithProperties)(cl_context                context,
                            const cl_mem_properties * properties,
                            cl_mem_flags              flags,
                            const cl_image_format *   image_format,
                            const cl_image_desc *     image_desc,
                            void *                    host_ptr,
                            cl_int *                  errcode_ret)
CL_API_SUFFIX__VERSION_3_0
{
  int errcode;

  /* pocl doesn't support any extra properties ATM */
  POCL_GOTO_ERROR_ON ((properties && properties[0] != 0), CL_INVALID_PROPERTY,
                      "PoCL doesn't support any properties on images yet\n");

  cl_mem mem_ret = POname (clCreateImage) (context, flags, image_format,
                                           image_desc, host_ptr, errcode_ret);

  if (mem_ret == NULL)
    return NULL;

  if (properties && properties[0] == 0)
    {
      mem_ret->num_properties = 1;
      mem_ret->properties[0] = 0;
    }

  return mem_ret;

ERROR:
  if (errcode_ret)
    {
      *errcode_ret = errcode;
    }
  return NULL;
}
POsym (clCreateImageWithProperties)