Home | History | Annotate | Download | only in core
      1 //
      2 // Copyright 2012 Francisco Jerez
      3 //
      4 // Permission is hereby granted, free of charge, to any person obtaining a
      5 // copy of this software and associated documentation files (the "Software"),
      6 // to deal in the Software without restriction, including without limitation
      7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8 // and/or sell copies of the Software, and to permit persons to whom the
      9 // Software is furnished to do so, subject to the following conditions:
     10 //
     11 // The above copyright notice and this permission notice shall be included in
     12 // all copies or substantial portions of the Software.
     13 //
     14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     20 // OTHER DEALINGS IN THE SOFTWARE.
     21 //
     22 
     23 #ifndef CLOVER_CORE_MEMORY_HPP
     24 #define CLOVER_CORE_MEMORY_HPP
     25 
     26 #include <functional>
     27 #include <map>
     28 #include <memory>
     29 #include <stack>
     30 
     31 #include "core/object.hpp"
     32 #include "core/queue.hpp"
     33 #include "core/resource.hpp"
     34 
     35 namespace clover {
     36    class memory_obj : public ref_counter, public _cl_mem {
     37    protected:
     38       memory_obj(clover::context &ctx, cl_mem_flags flags,
     39                  size_t size, void *host_ptr);
     40 
     41       memory_obj(const memory_obj &obj) = delete;
     42       memory_obj &
     43       operator=(const memory_obj &obj) = delete;
     44 
     45    public:
     46       virtual ~memory_obj();
     47 
     48       bool
     49       operator==(const memory_obj &obj) const;
     50 
     51       virtual cl_mem_object_type type() const = 0;
     52       virtual clover::resource &resource(command_queue &q) = 0;
     53 
     54       void destroy_notify(std::function<void ()> f);
     55       cl_mem_flags flags() const;
     56       size_t size() const;
     57       void *host_ptr() const;
     58 
     59       const intrusive_ref<clover::context> context;
     60 
     61    private:
     62       cl_mem_flags _flags;
     63       size_t _size;
     64       void *_host_ptr;
     65       std::stack<std::function<void ()>> _destroy_notify;
     66 
     67    protected:
     68       std::string data;
     69    };
     70 
     71    class buffer : public memory_obj {
     72    protected:
     73       buffer(clover::context &ctx, cl_mem_flags flags,
     74              size_t size, void *host_ptr);
     75 
     76    public:
     77       virtual cl_mem_object_type type() const;
     78    };
     79 
     80    class root_buffer : public buffer {
     81    public:
     82       root_buffer(clover::context &ctx, cl_mem_flags flags,
     83                   size_t size, void *host_ptr);
     84 
     85       virtual clover::resource &resource(command_queue &q);
     86 
     87    private:
     88       std::map<device *,
     89                std::unique_ptr<root_resource>> resources;
     90    };
     91 
     92    class sub_buffer : public buffer {
     93    public:
     94       sub_buffer(root_buffer &parent, cl_mem_flags flags,
     95                  size_t offset, size_t size);
     96 
     97       virtual clover::resource &resource(command_queue &q);
     98       size_t offset() const;
     99 
    100       const intrusive_ref<root_buffer> parent;
    101 
    102    private:
    103       size_t _offset;
    104       std::map<device *,
    105                std::unique_ptr<sub_resource>> resources;
    106    };
    107 
    108    class image : public memory_obj {
    109    protected:
    110       image(clover::context &ctx, cl_mem_flags flags,
    111             const cl_image_format *format,
    112             size_t width, size_t height, size_t depth,
    113             size_t row_pitch, size_t slice_pitch, size_t size,
    114             void *host_ptr);
    115 
    116    public:
    117       virtual clover::resource &resource(command_queue &q);
    118       cl_image_format format() const;
    119       size_t width() const;
    120       size_t height() const;
    121       size_t depth() const;
    122       size_t pixel_size() const;
    123       size_t row_pitch() const;
    124       size_t slice_pitch() const;
    125 
    126    private:
    127       cl_image_format _format;
    128       size_t _width;
    129       size_t _height;
    130       size_t _depth;
    131       size_t _row_pitch;
    132       size_t _slice_pitch;
    133       std::map<device *,
    134                std::unique_ptr<root_resource>> resources;
    135    };
    136 
    137    class image2d : public image {
    138    public:
    139       image2d(clover::context &ctx, cl_mem_flags flags,
    140               const cl_image_format *format, size_t width,
    141               size_t height, size_t row_pitch,
    142               void *host_ptr);
    143 
    144       virtual cl_mem_object_type type() const;
    145    };
    146 
    147    class image3d : public image {
    148    public:
    149       image3d(clover::context &ctx, cl_mem_flags flags,
    150               const cl_image_format *format,
    151               size_t width, size_t height, size_t depth,
    152               size_t row_pitch, size_t slice_pitch,
    153               void *host_ptr);
    154 
    155       virtual cl_mem_object_type type() const;
    156    };
    157 }
    158 
    159 #endif
    160