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 #include "core/memory.hpp"
24 #include "core/resource.hpp"
25 #include "util/format/u_format.h"
26
27 using namespace clover;
28
memory_obj(clover::context & ctx,cl_mem_flags flags,size_t size,void * host_ptr)29 memory_obj::memory_obj(clover::context &ctx, cl_mem_flags flags,
30 size_t size, void *host_ptr) :
31 context(ctx), _flags(flags),
32 _size(size), _host_ptr(host_ptr) {
33 if (flags & CL_MEM_COPY_HOST_PTR)
34 data.append((char *)host_ptr, size);
35 }
36
~memory_obj()37 memory_obj::~memory_obj() {
38 while (_destroy_notify.size()) {
39 _destroy_notify.top()();
40 _destroy_notify.pop();
41 }
42 }
43
44 bool
operator ==(const memory_obj & obj) const45 memory_obj::operator==(const memory_obj &obj) const {
46 return this == &obj;
47 }
48
49 void
destroy_notify(std::function<void ()> f)50 memory_obj::destroy_notify(std::function<void ()> f) {
51 _destroy_notify.push(f);
52 }
53
54 cl_mem_flags
flags() const55 memory_obj::flags() const {
56 return _flags;
57 }
58
59 size_t
size() const60 memory_obj::size() const {
61 return _size;
62 }
63
64 void *
host_ptr() const65 memory_obj::host_ptr() const {
66 return _host_ptr;
67 }
68
buffer(clover::context & ctx,cl_mem_flags flags,size_t size,void * host_ptr)69 buffer::buffer(clover::context &ctx, cl_mem_flags flags,
70 size_t size, void *host_ptr) :
71 memory_obj(ctx, flags, size, host_ptr) {
72 }
73
74 cl_mem_object_type
type() const75 buffer::type() const {
76 return CL_MEM_OBJECT_BUFFER;
77 }
78
root_buffer(clover::context & ctx,cl_mem_flags flags,size_t size,void * host_ptr)79 root_buffer::root_buffer(clover::context &ctx, cl_mem_flags flags,
80 size_t size, void *host_ptr) :
81 buffer(ctx, flags, size, host_ptr) {
82 }
83
84 resource &
resource_in(command_queue & q)85 root_buffer::resource_in(command_queue &q) {
86 const void *data_ptr = NULL;
87 if (flags() & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))
88 data_ptr = !data.empty() ? data.data() : host_ptr();
89
90 return resource(q, data_ptr);
91 }
92
93 resource &
resource_undef(command_queue & q)94 root_buffer::resource_undef(command_queue &q) {
95 return resource(q, NULL);
96 }
97
98 resource &
resource(command_queue & q,const void * data_ptr)99 root_buffer::resource(command_queue &q, const void *data_ptr) {
100 // Create a new resource if there's none for this device yet.
101 if (!resources.count(&q.device())) {
102 auto r = (!resources.empty() ?
103 new root_resource(q.device(), *this,
104 *resources.begin()->second) :
105 new root_resource(q.device(), *this, q, data_ptr));
106
107 resources.insert(std::make_pair(&q.device(),
108 std::unique_ptr<root_resource>(r)));
109 data.clear();
110 }
111
112 return *resources.find(&q.device())->second;
113 }
114
115 void
resource_out(command_queue & q)116 root_buffer::resource_out(command_queue &q) {
117 resources.erase(&q.device());
118 }
119
sub_buffer(root_buffer & parent,cl_mem_flags flags,size_t offset,size_t size)120 sub_buffer::sub_buffer(root_buffer &parent, cl_mem_flags flags,
121 size_t offset, size_t size) :
122 buffer(parent.context(), flags, size,
123 (char *)parent.host_ptr() + offset),
124 parent(parent), _offset(offset) {
125 }
126
127 resource &
resource_in(command_queue & q)128 sub_buffer::resource_in(command_queue &q) {
129 // Create a new resource if there's none for this device yet.
130 if (!resources.count(&q.device())) {
131 auto r = new sub_resource(parent().resource_in(q), {{ offset() }});
132
133 resources.insert(std::make_pair(&q.device(),
134 std::unique_ptr<sub_resource>(r)));
135 }
136
137 return *resources.find(&q.device())->second;
138 }
139
140 resource &
resource_undef(command_queue & q)141 sub_buffer::resource_undef(command_queue &q) {
142 return resource_in(q);
143 }
144
145 void
resource_out(command_queue & q)146 sub_buffer::resource_out(command_queue &q) {
147 resources.erase(&q.device());
148 }
149
150 size_t
offset() const151 sub_buffer::offset() const {
152 return _offset;
153 }
154
image(clover::context & ctx,cl_mem_flags flags,const cl_image_format * format,size_t width,size_t height,size_t depth,size_t row_pitch,size_t slice_pitch,size_t size,void * host_ptr)155 image::image(clover::context &ctx, cl_mem_flags flags,
156 const cl_image_format *format,
157 size_t width, size_t height, size_t depth,
158 size_t row_pitch, size_t slice_pitch, size_t size,
159 void *host_ptr) :
160 memory_obj(ctx, flags, size, host_ptr),
161 _format(*format), _width(width), _height(height), _depth(depth),
162 _row_pitch(row_pitch), _slice_pitch(slice_pitch) {
163 }
164
165 resource &
resource_in(command_queue & q)166 image::resource_in(command_queue &q) {
167 const void *data_ptr = !data.empty() ? data.data() : NULL;
168 return resource(q, data_ptr);
169 }
170
171 resource &
resource_undef(command_queue & q)172 image::resource_undef(command_queue &q) {
173 return resource(q, NULL);
174 }
175
176 resource &
resource(command_queue & q,const void * data_ptr)177 image::resource(command_queue &q, const void *data_ptr) {
178 // Create a new resource if there's none for this device yet.
179 if (!resources.count(&q.device())) {
180 auto r = (!resources.empty() ?
181 new root_resource(q.device(), *this,
182 *resources.begin()->second) :
183 new root_resource(q.device(), *this, q, data_ptr));
184
185 resources.insert(std::make_pair(&q.device(),
186 std::unique_ptr<root_resource>(r)));
187 data.clear();
188 }
189
190 return *resources.find(&q.device())->second;
191 }
192
193 void
resource_out(command_queue & q)194 image::resource_out(command_queue &q) {
195 resources.erase(&q.device());
196 }
197
198 cl_image_format
format() const199 image::format() const {
200 return _format;
201 }
202
203 size_t
width() const204 image::width() const {
205 return _width;
206 }
207
208 size_t
height() const209 image::height() const {
210 return _height;
211 }
212
213 size_t
depth() const214 image::depth() const {
215 return _depth;
216 }
217
218 size_t
pixel_size() const219 image::pixel_size() const {
220 return util_format_get_blocksize(translate_format(_format));
221 }
222
223 size_t
row_pitch() const224 image::row_pitch() const {
225 return _row_pitch;
226 }
227
228 size_t
slice_pitch() const229 image::slice_pitch() const {
230 return _slice_pitch;
231 }
232
image2d(clover::context & ctx,cl_mem_flags flags,const cl_image_format * format,size_t width,size_t height,size_t row_pitch,void * host_ptr)233 image2d::image2d(clover::context &ctx, cl_mem_flags flags,
234 const cl_image_format *format, size_t width,
235 size_t height, size_t row_pitch,
236 void *host_ptr) :
237 image(ctx, flags, format, width, height, 1,
238 row_pitch, 0, height * row_pitch, host_ptr) {
239 }
240
241 cl_mem_object_type
type() const242 image2d::type() const {
243 return CL_MEM_OBJECT_IMAGE2D;
244 }
245
image3d(clover::context & ctx,cl_mem_flags flags,const cl_image_format * format,size_t width,size_t height,size_t depth,size_t row_pitch,size_t slice_pitch,void * host_ptr)246 image3d::image3d(clover::context &ctx, cl_mem_flags flags,
247 const cl_image_format *format,
248 size_t width, size_t height, size_t depth,
249 size_t row_pitch, size_t slice_pitch,
250 void *host_ptr) :
251 image(ctx, flags, format, width, height, depth,
252 row_pitch, slice_pitch, depth * slice_pitch,
253 host_ptr) {
254 }
255
256 cl_mem_object_type
type() const257 image3d::type() const {
258 return CL_MEM_OBJECT_IMAGE3D;
259 }
260