2 // Copyright 2012 Francisco Jerez
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:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
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.
23 #ifndef CLOVER_CORE_MEMORY_HPP
24 #define CLOVER_CORE_MEMORY_HPP
30 #include "core/object.hpp"
31 #include "core/queue.hpp"
37 class memory_obj : public ref_counter, public _cl_mem {
39 memory_obj(context &ctx, cl_mem_flags flags,
40 size_t size, void *host_ptr);
41 memory_obj(const memory_obj &obj) = delete;
44 virtual ~memory_obj();
46 virtual cl_mem_object_type type() const = 0;
47 virtual clover::resource &resource(command_queue &q) = 0;
49 void destroy_notify(std::function<void ()> f);
50 cl_mem_flags flags() const;
52 void *host_ptr() const;
60 std::function<void ()> _destroy_notify;
66 class buffer : public memory_obj {
68 buffer(context &ctx, cl_mem_flags flags,
69 size_t size, void *host_ptr);
72 virtual cl_mem_object_type type() const;
75 class root_buffer : public buffer {
77 root_buffer(context &ctx, cl_mem_flags flags,
78 size_t size, void *host_ptr);
80 virtual clover::resource &resource(command_queue &q);
84 std::unique_ptr<root_resource>> resources;
87 class sub_buffer : public buffer {
89 sub_buffer(root_buffer &parent, cl_mem_flags flags,
90 size_t offset, size_t size);
92 virtual clover::resource &resource(command_queue &q);
93 size_t offset() const;
100 std::unique_ptr<sub_resource>> resources;
103 class image : public memory_obj {
105 image(context &ctx, cl_mem_flags flags,
106 const cl_image_format *format,
107 size_t width, size_t height, size_t depth,
108 size_t row_pitch, size_t slice_pitch, size_t size,
112 virtual clover::resource &resource(command_queue &q);
113 cl_image_format format() const;
114 size_t width() const;
115 size_t height() const;
116 size_t depth() const;
117 size_t row_pitch() const;
118 size_t slice_pitch() const;
121 cl_image_format _format;
128 std::unique_ptr<root_resource>> resources;
131 class image2d : public image {
133 image2d(context &ctx, cl_mem_flags flags,
134 const cl_image_format *format, size_t width,
135 size_t height, size_t row_pitch,
138 virtual cl_mem_object_type type() const;
141 class image3d : public image {
143 image3d(context &ctx, cl_mem_flags flags,
144 const cl_image_format *format,
145 size_t width, size_t height, size_t depth,
146 size_t row_pitch, size_t slice_pitch,
149 virtual cl_mem_object_type type() const;