blob: d20dbadd3a2b71c68338cb8f0026570988408fe6 [file] [log] [blame]
Francisco Jerezc6db1b32012-04-20 16:56:19 +02001//
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
Kenneth Graunkef0cb66b2013-04-21 13:52:08 -070017// 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.
Francisco Jerezc6db1b32012-04-20 16:56:19 +020021//
22
23#include "core/memory.hpp"
24#include "core/resource.hpp"
Eric Anholt882ca6d2019-06-27 15:05:31 -070025#include "util/format/u_format.h"
Francisco Jerezc6db1b32012-04-20 16:56:19 +020026
27using namespace clover;
28
Francisco Jerezc4578d22014-02-18 15:07:11 +010029memory_obj::memory_obj(clover::context &ctx, cl_mem_flags flags,
Francisco Jerezd6f7afc2013-10-01 12:00:51 -070030 size_t size, void *host_ptr) :
Francisco Jerezc4578d22014-02-18 15:07:11 +010031 context(ctx), _flags(flags),
Tom Stellardc6d98012014-09-22 10:00:39 -040032 _size(size), _host_ptr(host_ptr) {
Grigori Goronzyf972b222015-05-19 09:28:30 +020033 if (flags & CL_MEM_COPY_HOST_PTR)
Francisco Jerez60e7b082012-05-04 15:02:21 +020034 data.append((char *)host_ptr, size);
Francisco Jerezc6db1b32012-04-20 16:56:19 +020035}
36
Francisco Jerezd6f7afc2013-10-01 12:00:51 -070037memory_obj::~memory_obj() {
Tom Stellardc6d98012014-09-22 10:00:39 -040038 while (_destroy_notify.size()) {
39 _destroy_notify.top()();
40 _destroy_notify.pop();
41 }
Francisco Jerezc6db1b32012-04-20 16:56:19 +020042}
43
Francisco Jerez369419f2013-09-16 21:11:16 -070044bool
45memory_obj::operator==(const memory_obj &obj) const {
46 return this == &obj;
47}
48
Francisco Jerezc6db1b32012-04-20 16:56:19 +020049void
Francisco Jerezd6f7afc2013-10-01 12:00:51 -070050memory_obj::destroy_notify(std::function<void ()> f) {
Tom Stellardc6d98012014-09-22 10:00:39 -040051 _destroy_notify.push(f);
Francisco Jerezc6db1b32012-04-20 16:56:19 +020052}
53
54cl_mem_flags
Francisco Jerezd6f7afc2013-10-01 12:00:51 -070055memory_obj::flags() const {
Francisco Jerez8e14b822013-09-17 23:13:48 -070056 return _flags;
Francisco Jerezc6db1b32012-04-20 16:56:19 +020057}
58
59size_t
Francisco Jerezd6f7afc2013-10-01 12:00:51 -070060memory_obj::size() const {
Francisco Jerez8e14b822013-09-17 23:13:48 -070061 return _size;
Francisco Jerezc6db1b32012-04-20 16:56:19 +020062}
63
64void *
Francisco Jerezd6f7afc2013-10-01 12:00:51 -070065memory_obj::host_ptr() const {
Francisco Jerez8e14b822013-09-17 23:13:48 -070066 return _host_ptr;
Francisco Jerezc6db1b32012-04-20 16:56:19 +020067}
68
Francisco Jerezc4578d22014-02-18 15:07:11 +010069buffer::buffer(clover::context &ctx, cl_mem_flags flags,
Francisco Jerezc6db1b32012-04-20 16:56:19 +020070 size_t size, void *host_ptr) :
71 memory_obj(ctx, flags, size, host_ptr) {
72}
73
74cl_mem_object_type
75buffer::type() const {
76 return CL_MEM_OBJECT_BUFFER;
77}
78
Francisco Jerezc4578d22014-02-18 15:07:11 +010079root_buffer::root_buffer(clover::context &ctx, cl_mem_flags flags,
Francisco Jerezc6db1b32012-04-20 16:56:19 +020080 size_t size, void *host_ptr) :
81 buffer(ctx, flags, size, host_ptr) {
82}
83
Francisco Jerezd6f7afc2013-10-01 12:00:51 -070084resource &
Serge Martinc0f03f62020-05-09 08:11:16 +020085root_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
93resource &
94root_buffer::resource_undef(command_queue &q) {
95 return resource(q, NULL);
96}
97
98resource &
99root_buffer::resource(command_queue &q, const void *data_ptr) {
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200100 // Create a new resource if there's none for this device yet.
Francisco Jerezc4578d22014-02-18 15:07:11 +0100101 if (!resources.count(&q.device())) {
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200102 auto r = (!resources.empty() ?
Francisco Jerezc4578d22014-02-18 15:07:11 +0100103 new root_resource(q.device(), *this,
104 *resources.begin()->second) :
Serge Martinc0f03f62020-05-09 08:11:16 +0200105 new root_resource(q.device(), *this, q, data_ptr));
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200106
Francisco Jerezc4578d22014-02-18 15:07:11 +0100107 resources.insert(std::make_pair(&q.device(),
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200108 std::unique_ptr<root_resource>(r)));
109 data.clear();
110 }
111
Francisco Jerezc4578d22014-02-18 15:07:11 +0100112 return *resources.find(&q.device())->second;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200113}
114
Serge Martinc0f03f62020-05-09 08:11:16 +0200115void
116root_buffer::resource_out(command_queue &q) {
117 resources.erase(&q.device());
118}
119
Francisco Jerezd6f7afc2013-10-01 12:00:51 -0700120sub_buffer::sub_buffer(root_buffer &parent, cl_mem_flags flags,
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200121 size_t offset, size_t size) :
Francisco Jerezc4578d22014-02-18 15:07:11 +0100122 buffer(parent.context(), flags, size,
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200123 (char *)parent.host_ptr() + offset),
Francisco Jerez8e14b822013-09-17 23:13:48 -0700124 parent(parent), _offset(offset) {
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200125}
126
Francisco Jerezd6f7afc2013-10-01 12:00:51 -0700127resource &
Serge Martinc0f03f62020-05-09 08:11:16 +0200128sub_buffer::resource_in(command_queue &q) {
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200129 // Create a new resource if there's none for this device yet.
Francisco Jerezc4578d22014-02-18 15:07:11 +0100130 if (!resources.count(&q.device())) {
Serge Martinc0f03f62020-05-09 08:11:16 +0200131 auto r = new sub_resource(parent().resource_in(q), {{ offset() }});
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200132
Francisco Jerezc4578d22014-02-18 15:07:11 +0100133 resources.insert(std::make_pair(&q.device(),
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200134 std::unique_ptr<sub_resource>(r)));
135 }
136
Francisco Jerezc4578d22014-02-18 15:07:11 +0100137 return *resources.find(&q.device())->second;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200138}
139
Serge Martinc0f03f62020-05-09 08:11:16 +0200140resource &
141sub_buffer::resource_undef(command_queue &q) {
142 return resource_in(q);
143}
144
145void
146sub_buffer::resource_out(command_queue &q) {
147 resources.erase(&q.device());
148}
149
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200150size_t
151sub_buffer::offset() const {
Francisco Jerez8e14b822013-09-17 23:13:48 -0700152 return _offset;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200153}
154
Francisco Jerezc4578d22014-02-18 15:07:11 +0100155image::image(clover::context &ctx, cl_mem_flags flags,
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200156 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),
Francisco Jerez8e14b822013-09-17 23:13:48 -0700161 _format(*format), _width(width), _height(height), _depth(depth),
162 _row_pitch(row_pitch), _slice_pitch(slice_pitch) {
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200163}
164
Francisco Jerezd6f7afc2013-10-01 12:00:51 -0700165resource &
Serge Martinc0f03f62020-05-09 08:11:16 +0200166image::resource_in(command_queue &q) {
167 const void *data_ptr = !data.empty() ? data.data() : NULL;
168 return resource(q, data_ptr);
169}
170
171resource &
172image::resource_undef(command_queue &q) {
173 return resource(q, NULL);
174}
175
176resource &
177image::resource(command_queue &q, const void *data_ptr) {
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200178 // Create a new resource if there's none for this device yet.
Francisco Jerezc4578d22014-02-18 15:07:11 +0100179 if (!resources.count(&q.device())) {
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200180 auto r = (!resources.empty() ?
Francisco Jerezc4578d22014-02-18 15:07:11 +0100181 new root_resource(q.device(), *this,
182 *resources.begin()->second) :
Serge Martinc0f03f62020-05-09 08:11:16 +0200183 new root_resource(q.device(), *this, q, data_ptr));
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200184
Francisco Jerezc4578d22014-02-18 15:07:11 +0100185 resources.insert(std::make_pair(&q.device(),
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200186 std::unique_ptr<root_resource>(r)));
187 data.clear();
188 }
189
Francisco Jerezc4578d22014-02-18 15:07:11 +0100190 return *resources.find(&q.device())->second;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200191}
192
Serge Martinc0f03f62020-05-09 08:11:16 +0200193void
194image::resource_out(command_queue &q) {
195 resources.erase(&q.device());
196}
197
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200198cl_image_format
199image::format() const {
Francisco Jerez8e14b822013-09-17 23:13:48 -0700200 return _format;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200201}
202
203size_t
204image::width() const {
Francisco Jerez8e14b822013-09-17 23:13:48 -0700205 return _width;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200206}
207
208size_t
209image::height() const {
Francisco Jerez8e14b822013-09-17 23:13:48 -0700210 return _height;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200211}
212
213size_t
214image::depth() const {
Francisco Jerez8e14b822013-09-17 23:13:48 -0700215 return _depth;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200216}
217
218size_t
Francisco Jerezadefa842013-10-18 16:25:36 -0700219image::pixel_size() const {
220 return util_format_get_blocksize(translate_format(_format));
221}
222
223size_t
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200224image::row_pitch() const {
Francisco Jerez8e14b822013-09-17 23:13:48 -0700225 return _row_pitch;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200226}
227
228size_t
229image::slice_pitch() const {
Francisco Jerez8e14b822013-09-17 23:13:48 -0700230 return _slice_pitch;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200231}
232
Francisco Jerezc4578d22014-02-18 15:07:11 +0100233image2d::image2d(clover::context &ctx, cl_mem_flags flags,
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200234 const cl_image_format *format, size_t width,
235 size_t height, size_t row_pitch,
236 void *host_ptr) :
Zoltan Gilianaa46fba2015-07-27 11:27:12 +0200237 image(ctx, flags, format, width, height, 1,
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200238 row_pitch, 0, height * row_pitch, host_ptr) {
239}
240
241cl_mem_object_type
242image2d::type() const {
243 return CL_MEM_OBJECT_IMAGE2D;
244}
245
Francisco Jerezc4578d22014-02-18 15:07:11 +0100246image3d::image3d(clover::context &ctx, cl_mem_flags flags,
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200247 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
256cl_mem_object_type
257image3d::type() const {
258 return CL_MEM_OBJECT_IMAGE3D;
259}