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 & 53 resource_in(command_queue &q) = 0; 54 virtual clover::resource & 55 resource_undef(command_queue &q) = 0; 56 virtual void resource_out(command_queue &q) = 0; 57 58 void destroy_notify(std::function<void ()> f); 59 cl_mem_flags flags() const; 60 size_t size() const; 61 void *host_ptr() const; 62 63 const intrusive_ref<clover::context> context; 64 65 private: 66 cl_mem_flags _flags; 67 size_t _size; 68 void *_host_ptr; 69 std::stack<std::function<void ()>> _destroy_notify; 70 71 protected: 72 std::string data; 73 }; 74 75 class buffer : public memory_obj { 76 protected: 77 buffer(clover::context &ctx, cl_mem_flags flags, 78 size_t size, void *host_ptr); 79 80 public: 81 virtual cl_mem_object_type type() const; 82 }; 83 84 class root_buffer : public buffer { 85 public: 86 root_buffer(clover::context &ctx, cl_mem_flags flags, 87 size_t size, void *host_ptr); 88 89 virtual clover::resource & 90 resource_in(command_queue &q); 91 virtual clover::resource & 92 resource_undef(command_queue &q); 93 virtual void 94 resource_out(command_queue &q); 95 96 private: 97 clover::resource & 98 resource(command_queue &q, const void *data_ptr); 99 100 std::map<device *, 101 std::unique_ptr<root_resource>> resources; 102 }; 103 104 class sub_buffer : public buffer { 105 public: 106 sub_buffer(root_buffer &parent, cl_mem_flags flags, 107 size_t offset, size_t size); 108 109 virtual clover::resource & 110 resource_in(command_queue &q); 111 virtual clover::resource & 112 resource_undef(command_queue &q); 113 virtual void 114 resource_out(command_queue &q); 115 size_t offset() const; 116 117 const intrusive_ref<root_buffer> parent; 118 119 private: 120 size_t _offset; 121 std::map<device *, 122 std::unique_ptr<sub_resource>> resources; 123 }; 124 125 class image : public memory_obj { 126 protected: 127 image(clover::context &ctx, cl_mem_flags flags, 128 const cl_image_format *format, 129 size_t width, size_t height, size_t depth, 130 size_t row_pitch, size_t slice_pitch, size_t size, 131 void *host_ptr); 132 133 public: 134 cl_image_format format() const; 135 size_t width() const; 136 size_t height() const; 137 size_t depth() const; 138 size_t pixel_size() const; 139 size_t row_pitch() const; 140 size_t slice_pitch() const; 141 virtual clover::resource & 142 resource_in(command_queue &q); 143 virtual clover::resource & 144 resource_undef(command_queue &q); 145 virtual void 146 resource_out(command_queue &q); 147 148 private: 149 clover::resource & 150 resource(command_queue &q, const void *data_ptr); 151 152 cl_image_format _format; 153 size_t _width; 154 size_t _height; 155 size_t _depth; 156 size_t _row_pitch; 157 size_t _slice_pitch; 158 std::map<device *, 159 std::unique_ptr<root_resource>> resources; 160 }; 161 162 class image2d : public image { 163 public: 164 image2d(clover::context &ctx, cl_mem_flags flags, 165 const cl_image_format *format, size_t width, 166 size_t height, size_t row_pitch, 167 void *host_ptr); 168 169 virtual cl_mem_object_type type() const; 170 }; 171 172 class image3d : public image { 173 public: 174 image3d(clover::context &ctx, cl_mem_flags flags, 175 const cl_image_format *format, 176 size_t width, size_t height, size_t depth, 177 size_t row_pitch, size_t slice_pitch, 178 void *host_ptr); 179 180 virtual cl_mem_object_type type() const; 181 }; 182 } 183 184 #endif 185