blob: e0f151c2df3cdb948d1394c4ca173d7031a30897 [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/resource.hpp"
Francisco Jerezc4578d22014-02-18 15:07:11 +010024#include "core/memory.hpp"
Francisco Jerezc6db1b32012-04-20 16:56:19 +020025#include "pipe/p_screen.h"
26#include "util/u_sampler.h"
Eric Anholt882ca6d2019-06-27 15:05:31 -070027#include "util/format/u_format.h"
Jan Vesely14b543b2017-03-01 19:45:03 -050028#include "util/u_inlines.h"
Francisco Jerezc6db1b32012-04-20 16:56:19 +020029
30using namespace clover;
31
32namespace {
33 class box {
34 public:
Francisco Jerez7d617692013-10-06 13:49:05 -070035 box(const resource::vector &origin, const resource::vector &size) :
Aaron Watry4d0399f2017-04-04 20:16:02 -050036 pipe({ (int)origin[0], (int16_t)origin[1],
37 (int16_t)origin[2], (int)size[0],
38 (int16_t)size[1], (int16_t)size[2] }) {
Francisco Jerezc6db1b32012-04-20 16:56:19 +020039 }
40
41 operator const pipe_box *() {
42 return &pipe;
43 }
44
45 protected:
46 pipe_box pipe;
47 };
48}
49
Francisco Jerezc4578d22014-02-18 15:07:11 +010050resource::resource(clover::device &dev, memory_obj &obj) :
51 device(dev), obj(obj), pipe(NULL), offset() {
Francisco Jerezc6db1b32012-04-20 16:56:19 +020052}
53
54resource::~resource() {
55}
56
57void
Francisco Jerez7d617692013-10-06 13:49:05 -070058resource::copy(command_queue &q, const vector &origin, const vector &region,
59 resource &src_res, const vector &src_origin) {
60 auto p = offset + origin;
Francisco Jerezc6db1b32012-04-20 16:56:19 +020061
62 q.pipe->resource_copy_region(q.pipe, pipe, 0, p[0], p[1], p[2],
63 src_res.pipe, 0,
64 box(src_res.offset + src_origin, region));
65}
66
Serge Martindad042b2020-05-03 13:56:15 +020067void
Serge Martind7d66e32020-05-09 12:29:39 +020068resource::clear(command_queue &q, const vector &origin, const vector &region,
69 const std::string &data) {
70 auto from = offset + origin;
Serge Martindad042b2020-05-03 13:56:15 +020071
Serge Martind7d66e32020-05-09 12:29:39 +020072 if (pipe->target == PIPE_BUFFER) {
73 q.pipe->clear_buffer(q.pipe, pipe, from[0], region[0], data.data(), data.size());
74 } else {
75 std::string texture_data;
76 texture_data.reserve(util_format_get_blocksize(pipe->format));
77 util_format_pack_rgba(pipe->format, &texture_data[0], data.data(), 1);
78 q.pipe->clear_texture(q.pipe, pipe, 0, box(from, region), texture_data.data());
79 }
Serge Martindad042b2020-05-03 13:56:15 +020080}
81
Karol Herbstf2bdb692020-10-15 01:19:57 +020082mapping *
Francisco Jerezc6db1b32012-04-20 16:56:19 +020083resource::add_map(command_queue &q, cl_map_flags flags, bool blocking,
Francisco Jerez7d617692013-10-06 13:49:05 -070084 const vector &origin, const vector &region) {
Francisco Jerezc6db1b32012-04-20 16:56:19 +020085 maps.emplace_back(q, *this, flags, blocking, origin, region);
Karol Herbstf2bdb692020-10-15 01:19:57 +020086 return &maps.back();
Francisco Jerezc6db1b32012-04-20 16:56:19 +020087}
88
89void
90resource::del_map(void *p) {
Francisco Jerez1d741e32013-09-15 23:07:10 -070091 erase_if([&](const mapping &m) {
Francisco Jerez7d617692013-10-06 13:49:05 -070092 return static_cast<void *>(m) == p;
93 }, maps);
Francisco Jerezc6db1b32012-04-20 16:56:19 +020094}
95
96unsigned
97resource::map_count() const {
98 return maps.size();
99}
100
101pipe_sampler_view *
Francisco Jerezd6f7afc2013-10-01 12:00:51 -0700102resource::bind_sampler_view(command_queue &q) {
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200103 pipe_sampler_view info;
104
105 u_sampler_view_default_template(&info, pipe, pipe->format);
106 return q.pipe->create_sampler_view(q.pipe, pipe, &info);
107}
108
109void
Francisco Jerezd6f7afc2013-10-01 12:00:51 -0700110resource::unbind_sampler_view(command_queue &q,
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200111 pipe_sampler_view *st) {
112 q.pipe->sampler_view_destroy(q.pipe, st);
113}
114
Karol Herbst3aead712020-10-07 23:08:43 +0200115pipe_image_view
116resource::create_image_view(command_queue &q) {
117 pipe_image_view view;
118 view.resource = pipe;
119 view.format = pipe->format;
120 view.access = 0;
121 view.shader_access = PIPE_IMAGE_ACCESS_WRITE;
122
123 if (pipe->target == PIPE_BUFFER) {
124 view.u.buf.offset = 0;
125 view.u.buf.size = obj.size();
126 } else {
127 view.u.tex.first_layer = 0;
128 view.u.tex.last_layer = 0;
129 view.u.tex.level = 0;
130 }
131
132 return view;
133}
134
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200135pipe_surface *
Francisco Jerezd6f7afc2013-10-01 12:00:51 -0700136resource::bind_surface(command_queue &q, bool rw) {
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200137 pipe_surface info {};
138
139 info.format = pipe->format;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200140 info.writable = rw;
141
142 if (pipe->target == PIPE_BUFFER)
143 info.u.buf.last_element = pipe->width0 - 1;
144
145 return q.pipe->create_surface(q.pipe, pipe, &info);
146}
147
148void
Francisco Jerezd6f7afc2013-10-01 12:00:51 -0700149resource::unbind_surface(command_queue &q, pipe_surface *st) {
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200150 q.pipe->surface_destroy(q.pipe, st);
151}
152
Francisco Jerezc4578d22014-02-18 15:07:11 +0100153root_resource::root_resource(clover::device &dev, memory_obj &obj,
Serge Martinc0f03f62020-05-09 08:11:16 +0200154 command_queue &q, const void *data_ptr) :
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200155 resource(dev, obj) {
156 pipe_resource info {};
157
158 if (image *img = dynamic_cast<image *>(&obj)) {
159 info.format = translate_format(img->format());
160 info.width0 = img->width();
161 info.height0 = img->height();
162 info.depth0 = img->depth();
163 } else {
164 info.width0 = obj.size();
Francisco Jerez60e7b082012-05-04 15:02:21 +0200165 info.height0 = 1;
166 info.depth0 = 1;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200167 }
168
Zoltan Gilianaa46fba2015-07-27 11:27:12 +0200169 info.array_size = 1;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200170 info.target = translate_target(obj.type());
171 info.bind = (PIPE_BIND_SAMPLER_VIEW |
172 PIPE_BIND_COMPUTE_RESOURCE |
Marek Olšák5981ab52016-09-07 21:24:08 +0200173 PIPE_BIND_GLOBAL);
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200174
Karol Herbstc0f7f832020-05-05 15:09:50 +0200175 if (obj.flags() & CL_MEM_USE_HOST_PTR && dev.allows_user_pointers()) {
Grigori Goronzyf972b222015-05-19 09:28:30 +0200176 // Page alignment is normally required for this, just try, hope for the
177 // best and fall back if it fails.
178 pipe = dev.pipe->resource_from_user_memory(dev.pipe, &info, obj.host_ptr());
179 if (pipe)
180 return;
181 }
182
183 if (obj.flags() & (CL_MEM_ALLOC_HOST_PTR | CL_MEM_USE_HOST_PTR)) {
Grigori Goronzy5c495e82015-05-12 02:22:12 +0200184 info.usage = PIPE_USAGE_STAGING;
185 }
186
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200187 pipe = dev.pipe->resource_create(dev.pipe, &info);
188 if (!pipe)
189 throw error(CL_OUT_OF_RESOURCES);
190
Serge Martinc0f03f62020-05-09 08:11:16 +0200191 if (data_ptr) {
Francisco Jerez7d617692013-10-06 13:49:05 -0700192 box rect { {{ 0, 0, 0 }}, {{ info.width0, info.height0, info.depth0 }} };
Francisco Jerez60e7b082012-05-04 15:02:21 +0200193 unsigned cpp = util_format_get_blocksize(info.format);
194
Marek Olšák1ffe77e2016-07-16 21:19:48 +0200195 if (pipe->target == PIPE_BUFFER)
Marek Olšák22253e62020-07-01 08:16:12 -0400196 q.pipe->buffer_subdata(q.pipe, pipe, PIPE_MAP_WRITE,
Marek Olšák1ffe77e2016-07-16 21:19:48 +0200197 0, info.width0, data_ptr);
198 else
Marek Olšák22253e62020-07-01 08:16:12 -0400199 q.pipe->texture_subdata(q.pipe, pipe, 0, PIPE_MAP_WRITE,
Marek Olšák1ffe77e2016-07-16 21:19:48 +0200200 rect, data_ptr, cpp * info.width0,
201 cpp * info.width0 * info.height0);
Francisco Jerez60e7b082012-05-04 15:02:21 +0200202 }
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200203}
204
Francisco Jerezc4578d22014-02-18 15:07:11 +0100205root_resource::root_resource(clover::device &dev, memory_obj &obj,
Francisco Jerezd6f7afc2013-10-01 12:00:51 -0700206 root_resource &r) :
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200207 resource(dev, obj) {
208 assert(0); // XXX -- resource shared among dev and r.dev
209}
210
211root_resource::~root_resource() {
Jan Vesely14b543b2017-03-01 19:45:03 -0500212 pipe_resource_reference(&this->pipe, NULL);
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200213}
214
Francisco Jerez7d617692013-10-06 13:49:05 -0700215sub_resource::sub_resource(resource &r, const vector &offset) :
Francisco Jerezc4578d22014-02-18 15:07:11 +0100216 resource(r.device(), r.obj) {
Francisco Jerez7d617692013-10-06 13:49:05 -0700217 this->pipe = r.pipe;
218 this->offset = r.offset + offset;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200219}
220
221mapping::mapping(command_queue &q, resource &r,
222 cl_map_flags flags, bool blocking,
Francisco Jerez7d617692013-10-06 13:49:05 -0700223 const resource::vector &origin,
224 const resource::vector &region) :
Jan Vesely14b543b2017-03-01 19:45:03 -0500225 pctx(q.pipe), pres(NULL) {
Marek Olšák22253e62020-07-01 08:16:12 -0400226 unsigned usage = ((flags & CL_MAP_WRITE ? PIPE_MAP_WRITE : 0 ) |
227 (flags & CL_MAP_READ ? PIPE_MAP_READ : 0 ) |
Bruno Jiménezec737782014-08-07 16:34:53 +0200228 (flags & CL_MAP_WRITE_INVALIDATE_REGION ?
Marek Olšák22253e62020-07-01 08:16:12 -0400229 PIPE_MAP_DISCARD_RANGE : 0) |
230 (!blocking ? PIPE_MAP_UNSYNCHRONIZED : 0));
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200231
Marek Olšák369e4682012-10-08 04:06:42 +0200232 p = pctx->transfer_map(pctx, r.pipe, 0, usage,
233 box(origin + r.offset, region), &pxfer);
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200234 if (!p) {
Marek Olšák369e4682012-10-08 04:06:42 +0200235 pxfer = NULL;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200236 throw error(CL_OUT_OF_RESOURCES);
237 }
Jan Vesely14b543b2017-03-01 19:45:03 -0500238 pipe_resource_reference(&pres, r.pipe);
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200239}
240
241mapping::mapping(mapping &&m) :
Jan Vesely14b543b2017-03-01 19:45:03 -0500242 pctx(m.pctx), pxfer(m.pxfer), pres(m.pres), p(m.p) {
Francisco Jerez1d741e32013-09-15 23:07:10 -0700243 m.pctx = NULL;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200244 m.pxfer = NULL;
Jan Vesely14b543b2017-03-01 19:45:03 -0500245 m.pres = NULL;
Francisco Jerez1d741e32013-09-15 23:07:10 -0700246 m.p = NULL;
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200247}
248
249mapping::~mapping() {
250 if (pxfer) {
251 pctx->transfer_unmap(pctx, pxfer);
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200252 }
Jan Vesely14b543b2017-03-01 19:45:03 -0500253 pipe_resource_reference(&pres, NULL);
Francisco Jerezc6db1b32012-04-20 16:56:19 +0200254}
Francisco Jerez7d617692013-10-06 13:49:05 -0700255
256mapping &
257mapping::operator=(mapping m) {
258 std::swap(pctx, m.pctx);
259 std::swap(pxfer, m.pxfer);
Jan Vesely14b543b2017-03-01 19:45:03 -0500260 std::swap(pres, m.pres);
Francisco Jerez7d617692013-10-06 13:49:05 -0700261 std::swap(p, m.p);
262 return *this;
263}
Jason Ekstrandafa5c2e2020-08-31 16:43:40 -0500264
265resource::vector
266mapping::pitch() const
267{
268 return {
269 util_format_get_blocksize(pres->format),
270 pxfer->stride,
271 pxfer->layer_stride,
272 };
273}