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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 // OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 // SOFTWARE.
21 //
22 
23 #ifndef __CORE_MEMORY_HPP__
24 #define __CORE_MEMORY_HPP__
25 
26 #include <functional>
27 #include <map>
28 #include <memory>
29 
30 #include "core/base.hpp"
31 #include "core/queue.hpp"
32 
33 namespace clover {
34    typedef struct _cl_mem memory_obj;
35 
36    class resource;
37    class sub_resource;
38 }
39 
40 struct _cl_mem : public clover::ref_counter {
41 protected:
42    _cl_mem(clover::context &ctx, cl_mem_flags flags,
43            size_t size, void *host_ptr);
44    _cl_mem(const _cl_mem &obj) = delete;
45 
46 public:
47    virtual ~_cl_mem();
48 
49    virtual cl_mem_object_type type() const = 0;
50    virtual clover::resource &resource(cl_command_queue q) = 0;
51 
52    void destroy_notify(std::function<void ()> f);
53    cl_mem_flags flags() const;
54    size_t size() const;
55    void *host_ptr() const;
56 
57    clover::context &ctx;
58 
59 private:
60    cl_mem_flags __flags;
61    size_t __size;
62    void *__host_ptr;
63    std::function<void ()> __destroy_notify;
64 
65 protected:
66    std::string data;
67 };
68 
69 namespace clover {
70    struct buffer : public memory_obj {
71    protected:
72       buffer(clover::context &ctx, cl_mem_flags flags,
73              size_t size, void *host_ptr);
74 
75    public:
76       virtual cl_mem_object_type type() const;
77    };
78 
79    struct root_buffer : public buffer {
80    public:
81       root_buffer(clover::context &ctx, cl_mem_flags flags,
82                   size_t size, void *host_ptr);
83 
84       virtual clover::resource &resource(cl_command_queue q);
85 
86    private:
87       std::map<clover::device *,
88                std::unique_ptr<clover::root_resource>> resources;
89    };
90 
91    struct sub_buffer : public buffer {
92    public:
93       sub_buffer(clover::root_buffer &parent, cl_mem_flags flags,
94                  size_t offset, size_t size);
95 
96       virtual clover::resource &resource(cl_command_queue q);
97       size_t offset() const;
98 
99       clover::root_buffer &parent;
100 
101    private:
102       size_t __offset;
103       std::map<clover::device *,
104                std::unique_ptr<clover::sub_resource>> resources;
105    };
106 
107    struct image : public memory_obj {
108    protected:
109       image(clover::context &ctx, cl_mem_flags flags,
110             const cl_image_format *format,
111             size_t width, size_t height, size_t depth,
112             size_t row_pitch, size_t slice_pitch, size_t size,
113             void *host_ptr);
114 
115    public:
116       virtual clover::resource &resource(cl_command_queue q);
117       cl_image_format format() const;
118       size_t width() const;
119       size_t height() const;
120       size_t depth() const;
121       size_t row_pitch() const;
122       size_t slice_pitch() const;
123 
124    private:
125       cl_image_format __format;
126       size_t __width;
127       size_t __height;
128       size_t __depth;
129       size_t __row_pitch;
130       size_t __slice_pitch;
131       std::map<clover::device *,
132                std::unique_ptr<clover::root_resource>> resources;
133    };
134 
135    struct image2d : public image {
136    public:
137       image2d(clover::context &ctx, cl_mem_flags flags,
138               const cl_image_format *format, size_t width,
139               size_t height, size_t row_pitch,
140               void *host_ptr);
141 
142       virtual cl_mem_object_type type() const;
143    };
144 
145    struct image3d : public image {
146    public:
147       image3d(clover::context &ctx, cl_mem_flags flags,
148               const cl_image_format *format,
149               size_t width, size_t height, size_t depth,
150               size_t row_pitch, size_t slice_pitch,
151               void *host_ptr);
152 
153       virtual cl_mem_object_type type() const;
154    };
155 }
156 
157 #endif
158