Clover Git
OpenCL 1.1 software implementation

api_memory.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Denis Steckelmacher <steckdenis@yahoo.fr>
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *     * Redistributions of source code must retain the above copyright
00008  *       notice, this list of conditions and the following disclaimer.
00009  *     * Redistributions in binary form must reproduce the above copyright
00010  *       notice, this list of conditions and the following disclaimer in the
00011  *       documentation and/or other materials provided with the distribution.
00012  *     * Neither the name of the copyright holder nor the
00013  *       names of its contributors may be used to endorse or promote products
00014  *       derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00020  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00033 #include "CL/cl.h"
00034 #include <core/memobject.h>
00035 #include <core/context.h>
00036 
00037 #include <cstring>
00038 
00039 // Memory Object APIs
00040 cl_mem
00041 clCreateBuffer(cl_context   context,
00042                cl_mem_flags flags,
00043                size_t       size,
00044                void *       host_ptr,
00045                cl_int *     errcode_ret)
00046 {
00047     cl_int dummy_errcode;
00048 
00049     if (!errcode_ret)
00050         errcode_ret = &dummy_errcode;
00051 
00052     if (!context->isA(Coal::Object::T_Context))
00053     {
00054         *errcode_ret = CL_INVALID_CONTEXT;
00055         return 0;
00056     }
00057 
00058     *errcode_ret = CL_SUCCESS;
00059 
00060     Coal::Buffer *buf = new Coal::Buffer(context, size, host_ptr, flags,
00061                                          errcode_ret);
00062 
00063     if (*errcode_ret != CL_SUCCESS || (*errcode_ret = buf->init()) != CL_SUCCESS)
00064     {
00065         delete buf;
00066         return 0;
00067     }
00068 
00069     return (cl_mem)buf;
00070 }
00071 
00072 cl_mem
00073 clCreateSubBuffer(cl_mem                buffer,
00074                   cl_mem_flags          flags,
00075                   cl_buffer_create_type buffer_create_type,
00076                   const void *          buffer_create_info,
00077                   cl_int *              errcode_ret)
00078 {
00079     cl_int dummy_errcode;
00080 
00081     if (!errcode_ret)
00082         errcode_ret = &dummy_errcode;
00083 
00084     if (!buffer->isA(Coal::Object::T_MemObject))
00085     {
00086         *errcode_ret = CL_INVALID_MEM_OBJECT;
00087         return 0;
00088     }
00089 
00090     Coal::MemObject *memobject = (Coal::MemObject *)buffer;
00091     cl_buffer_region *region = (cl_buffer_region *)buffer_create_info;
00092 
00093     // NOTE: Is it right ? Couldn't we create SubBuffers of images ?
00094     if (memobject->type() != Coal::MemObject::Buffer)
00095     {
00096         *errcode_ret = CL_INVALID_MEM_OBJECT;
00097         return 0;
00098     }
00099 
00100     if (buffer_create_type != CL_BUFFER_CREATE_TYPE_REGION)
00101     {
00102         *errcode_ret = CL_INVALID_VALUE;
00103         return 0;
00104     }
00105 
00106     if (!buffer_create_info)
00107     {
00108         *errcode_ret = CL_INVALID_VALUE;
00109         return 0;
00110     }
00111 
00112     *errcode_ret = CL_SUCCESS;
00113 
00114     Coal::SubBuffer *buf = new Coal::SubBuffer((Coal::Buffer *)buffer,
00115                                                region->origin, region->size,
00116                                                flags, errcode_ret);
00117 
00118     if (*errcode_ret != CL_SUCCESS || (*errcode_ret = buf->init()) != CL_SUCCESS)
00119     {
00120         delete buf;
00121         return 0;
00122     }
00123 
00124     return (cl_mem)buf;
00125 }
00126 
00127 cl_mem
00128 clCreateImage2D(cl_context              context,
00129                 cl_mem_flags            flags,
00130                 const cl_image_format * image_format,
00131                 size_t                  image_width,
00132                 size_t                  image_height,
00133                 size_t                  image_row_pitch,
00134                 void *                  host_ptr,
00135                 cl_int *                errcode_ret)
00136 {
00137     cl_int dummy_errcode;
00138 
00139     if (!errcode_ret)
00140         errcode_ret = &dummy_errcode;
00141 
00142     if (!context->isA(Coal::Object::T_Context))
00143     {
00144         *errcode_ret = CL_INVALID_CONTEXT;
00145         return 0;
00146     }
00147 
00148     *errcode_ret = CL_SUCCESS;
00149 
00150     Coal::Image2D *image = new Coal::Image2D(context, image_width, image_height,
00151                                              image_row_pitch, image_format,
00152                                              host_ptr, flags, errcode_ret);
00153 
00154     if (*errcode_ret != CL_SUCCESS || (*errcode_ret = image->init()) != CL_SUCCESS)
00155     {
00156         delete image;
00157         return 0;
00158     }
00159 
00160     return (cl_mem)image;
00161 }
00162 
00163 cl_mem
00164 clCreateImage3D(cl_context              context,
00165                 cl_mem_flags            flags,
00166                 const cl_image_format * image_format,
00167                 size_t                  image_width,
00168                 size_t                  image_height,
00169                 size_t                  image_depth,
00170                 size_t                  image_row_pitch,
00171                 size_t                  image_slice_pitch,
00172                 void *                  host_ptr,
00173                 cl_int *                errcode_ret)
00174 {
00175     cl_int dummy_errcode;
00176 
00177     if (!errcode_ret)
00178         errcode_ret = &dummy_errcode;
00179 
00180     if (!context->isA(Coal::Object::T_Context))
00181     {
00182         *errcode_ret = CL_INVALID_CONTEXT;
00183         return 0;
00184     }
00185 
00186     *errcode_ret = CL_SUCCESS;
00187 
00188     Coal::Image3D *image = new Coal::Image3D(context, image_width, image_height,
00189                                              image_depth, image_row_pitch,
00190                                              image_slice_pitch, image_format,
00191                                              host_ptr, flags, errcode_ret);
00192 
00193     if (*errcode_ret != CL_SUCCESS || (*errcode_ret = image->init()) != CL_SUCCESS)
00194     {
00195         delete image;
00196         return 0;
00197     }
00198 
00199     return (cl_mem)image;
00200 }
00201 
00202 cl_int
00203 clRetainMemObject(cl_mem memobj)
00204 {
00205     if (!memobj->isA(Coal::Object::T_MemObject))
00206         return CL_INVALID_MEM_OBJECT;
00207 
00208     memobj->reference();
00209 
00210     return CL_SUCCESS;
00211 }
00212 
00213 cl_int
00214 clReleaseMemObject(cl_mem memobj)
00215 {
00216     if (!memobj->isA(Coal::Object::T_MemObject))
00217         return CL_INVALID_MEM_OBJECT;
00218 
00219     if (memobj->dereference())
00220         delete memobj;
00221 
00222     return CL_SUCCESS;
00223 }
00224 
00225 static cl_image_format supported_formats[] = {
00226     { CL_RGBA, CL_UNORM_INT8 },
00227     { CL_RGBA, CL_UNORM_INT16 },
00228     { CL_RGBA, CL_SNORM_INT8 },
00229     { CL_RGBA, CL_SNORM_INT16 },
00230     { CL_RGBA, CL_SIGNED_INT8 },
00231     { CL_RGBA, CL_SIGNED_INT16 },
00232     { CL_RGBA, CL_SIGNED_INT32 },
00233     { CL_RGBA, CL_UNSIGNED_INT8 },
00234     { CL_RGBA, CL_UNSIGNED_INT16 },
00235     { CL_RGBA, CL_UNSIGNED_INT32 },
00236     { CL_RGBA, CL_FLOAT },
00237 
00238     { CL_ARGB, CL_UNORM_INT8 },
00239     { CL_ARGB, CL_SNORM_INT8 },
00240     { CL_ARGB, CL_SIGNED_INT8 },
00241     { CL_ARGB, CL_UNSIGNED_INT8 },
00242 
00243     { CL_BGRA, CL_UNORM_INT8 },
00244     { CL_BGRA, CL_SNORM_INT8 },
00245     { CL_BGRA, CL_SIGNED_INT8 },
00246     { CL_BGRA, CL_UNSIGNED_INT8 },
00247 
00248     { CL_RGB, CL_UNORM_SHORT_565 },
00249     { CL_RGB, CL_UNORM_SHORT_555 },
00250     { CL_RGB, CL_UNORM_INT_101010 },
00251 
00252     { CL_RGBx, CL_UNORM_SHORT_565 },
00253     { CL_RGBx, CL_UNORM_SHORT_555 },
00254     { CL_RGBx, CL_UNORM_INT_101010 },
00255 
00256     { CL_RG, CL_UNORM_INT8 },
00257     { CL_RG, CL_UNORM_INT16 },
00258     { CL_RG, CL_SNORM_INT8 },
00259     { CL_RG, CL_SNORM_INT16 },
00260     { CL_RG, CL_SIGNED_INT8 },
00261     { CL_RG, CL_SIGNED_INT16 },
00262     { CL_RG, CL_SIGNED_INT32 },
00263     { CL_RG, CL_UNSIGNED_INT8 },
00264     { CL_RG, CL_UNSIGNED_INT16 },
00265     { CL_RG, CL_UNSIGNED_INT32 },
00266     { CL_RG, CL_FLOAT },
00267 
00268     { CL_RGx, CL_UNORM_INT8 },
00269     { CL_RGx, CL_UNORM_INT16 },
00270     { CL_RGx, CL_SNORM_INT8 },
00271     { CL_RGx, CL_SNORM_INT16 },
00272     { CL_RGx, CL_SIGNED_INT8 },
00273     { CL_RGx, CL_SIGNED_INT16 },
00274     { CL_RGx, CL_SIGNED_INT32 },
00275     { CL_RGx, CL_UNSIGNED_INT8 },
00276     { CL_RGx, CL_UNSIGNED_INT16 },
00277     { CL_RGx, CL_UNSIGNED_INT32 },
00278     { CL_RGx, CL_FLOAT },
00279 
00280     { CL_RA, CL_UNORM_INT8 },
00281     { CL_RA, CL_UNORM_INT16 },
00282     { CL_RA, CL_SNORM_INT8 },
00283     { CL_RA, CL_SNORM_INT16 },
00284     { CL_RA, CL_SIGNED_INT8 },
00285     { CL_RA, CL_SIGNED_INT16 },
00286     { CL_RA, CL_SIGNED_INT32 },
00287     { CL_RA, CL_UNSIGNED_INT8 },
00288     { CL_RA, CL_UNSIGNED_INT16 },
00289     { CL_RA, CL_UNSIGNED_INT32 },
00290     { CL_RA, CL_FLOAT },
00291 
00292     { CL_R, CL_UNORM_INT8 },
00293     { CL_R, CL_UNORM_INT16 },
00294     { CL_R, CL_SNORM_INT8 },
00295     { CL_R, CL_SNORM_INT16 },
00296     { CL_R, CL_SIGNED_INT8 },
00297     { CL_R, CL_SIGNED_INT16 },
00298     { CL_R, CL_SIGNED_INT32 },
00299     { CL_R, CL_UNSIGNED_INT8 },
00300     { CL_R, CL_UNSIGNED_INT16 },
00301     { CL_R, CL_UNSIGNED_INT32 },
00302     { CL_R, CL_FLOAT },
00303 
00304     { CL_Rx, CL_UNORM_INT8 },
00305     { CL_Rx, CL_UNORM_INT16 },
00306     { CL_Rx, CL_SNORM_INT8 },
00307     { CL_Rx, CL_SNORM_INT16 },
00308     { CL_Rx, CL_SIGNED_INT8 },
00309     { CL_Rx, CL_SIGNED_INT16 },
00310     { CL_Rx, CL_SIGNED_INT32 },
00311     { CL_Rx, CL_UNSIGNED_INT8 },
00312     { CL_Rx, CL_UNSIGNED_INT16 },
00313     { CL_Rx, CL_UNSIGNED_INT32 },
00314     { CL_Rx, CL_FLOAT },
00315 
00316     { CL_A, CL_UNORM_INT8 },
00317     { CL_A, CL_UNORM_INT16 },
00318     { CL_A, CL_SNORM_INT8 },
00319     { CL_A, CL_SNORM_INT16 },
00320     { CL_A, CL_SIGNED_INT8 },
00321     { CL_A, CL_SIGNED_INT16 },
00322     { CL_A, CL_SIGNED_INT32 },
00323     { CL_A, CL_UNSIGNED_INT8 },
00324     { CL_A, CL_UNSIGNED_INT16 },
00325     { CL_A, CL_UNSIGNED_INT32 },
00326     { CL_A, CL_FLOAT },
00327 
00328     { CL_LUMINANCE, CL_UNORM_INT8 },
00329     { CL_LUMINANCE, CL_UNORM_INT16 },
00330     { CL_LUMINANCE, CL_SNORM_INT8 },
00331     { CL_LUMINANCE, CL_SNORM_INT16 },
00332     { CL_LUMINANCE, CL_FLOAT },
00333 
00334     { CL_INTENSITY, CL_UNORM_INT8 },
00335     { CL_INTENSITY, CL_UNORM_INT16 },
00336     { CL_INTENSITY, CL_SNORM_INT8 },
00337     { CL_INTENSITY, CL_SNORM_INT16 },
00338     { CL_INTENSITY, CL_FLOAT }
00339 };
00340 
00341 #define MIN(a, b) ((a) < (b) ? (a) : (b))
00342 
00343 cl_int
00344 clGetSupportedImageFormats(cl_context           context,
00345                            cl_mem_flags         flags,
00346                            cl_mem_object_type   image_type,
00347                            cl_uint              num_entries,
00348                            cl_image_format *    image_formats,
00349                            cl_uint *            num_image_formats)
00350 {
00351     if (!context->isA(Coal::Object::T_Context))
00352         return CL_INVALID_CONTEXT;
00353 
00354     (void) flags;
00355     (void) image_type;
00356 
00357     if (!num_entries && image_formats)
00358         return CL_INVALID_VALUE;
00359 
00360     if (image_formats)
00361     {
00362         std::memcpy(image_formats, supported_formats,
00363                     MIN(num_entries * sizeof(cl_image_format),
00364                         sizeof(supported_formats)));
00365     }
00366 
00367     if (num_image_formats)
00368         *num_image_formats = sizeof(supported_formats) / sizeof(cl_image_format);
00369 
00370     return CL_SUCCESS;
00371 }
00372 
00373 cl_int
00374 clGetMemObjectInfo(cl_mem           memobj,
00375                    cl_mem_info      param_name,
00376                    size_t           param_value_size,
00377                    void *           param_value,
00378                    size_t *         param_value_size_ret)
00379 {
00380     if (!memobj->isA(Coal::Object::T_MemObject))
00381         return CL_INVALID_MEM_OBJECT;
00382 
00383     return memobj->info(param_name, param_value_size, param_value,
00384                         param_value_size_ret);
00385 }
00386 
00387 cl_int
00388 clGetImageInfo(cl_mem           image,
00389                cl_image_info    param_name,
00390                size_t           param_value_size,
00391                void *           param_value,
00392                size_t *         param_value_size_ret)
00393 {
00394     if (!image->isA(Coal::Object::T_MemObject) ||
00395             (image->type() != Coal::MemObject::Image2D &&
00396              image->type() != Coal::MemObject::Image3D))
00397         return CL_INVALID_MEM_OBJECT;
00398 
00399     Coal::Image2D *image2d = (Coal::Image2D *)image;
00400 
00401     return image2d->imageInfo(param_name, param_value_size, param_value,
00402                               param_value_size_ret);
00403 }
00404 
00405 cl_int
00406 clSetMemObjectDestructorCallback(cl_mem memobj,
00407                                  void   (CL_CALLBACK *pfn_notify)(cl_mem memobj,
00408                                                                   void *user_data),
00409                                  void * user_data)
00410 {
00411     if (!memobj->isA(Coal::Object::T_MemObject))
00412         return CL_INVALID_MEM_OBJECT;
00413 
00414     memobj->setDestructorCallback(pfn_notify, user_data);
00415 
00416     return CL_SUCCESS;
00417 }
00418 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines