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/resource.hpp"
24 #include "core/memory.hpp"
25 #include "pipe/p_screen.h"
26 #include "util/u_sampler.h"
27 #include "util/format/u_format.h"
28 #include "util/u_inlines.h"
29 #include "util/u_resource.h"
30 
31 using namespace clover;
32 
33 namespace {
34    class box {
35    public:
box(const resource::vector & origin,const resource::vector & size)36       box(const resource::vector &origin, const resource::vector &size) :
37         pipe({ (int)origin[0], (int16_t)origin[1],
38                (int16_t)origin[2], (int)size[0],
39                (int16_t)size[1], (int16_t)size[2] }) {
40       }
41 
operator const pipe_box*()42       operator const pipe_box *() {
43          return &pipe;
44       }
45 
46    protected:
47       pipe_box pipe;
48    };
49 }
50 
resource(clover::device & dev,memory_obj & obj)51 resource::resource(clover::device &dev, memory_obj &obj) :
52    device(dev), obj(obj), pipe(NULL), offset() {
53 }
54 
~resource()55 resource::~resource() {
56 }
57 
58 void
copy(command_queue & q,const vector & origin,const vector & region,resource & src_res,const vector & src_origin)59 resource::copy(command_queue &q, const vector &origin, const vector &region,
60                resource &src_res, const vector &src_origin) {
61    auto p = offset + origin;
62 
63    q.pipe->resource_copy_region(q.pipe, pipe, 0, p[0], p[1], p[2],
64                                 src_res.pipe, 0,
65                                 box(src_res.offset + src_origin, region));
66 }
67 
68 void
clear(command_queue & q,const vector & origin,const vector & region,const std::string & data)69 resource::clear(command_queue &q, const vector &origin, const vector &region,
70                 const std::string &data) {
71    auto from = offset + origin;
72 
73    if (pipe->target == PIPE_BUFFER) {
74       q.pipe->clear_buffer(q.pipe, pipe, from[0], region[0], data.data(), data.size());
75    } else {
76       std::string texture_data;
77       texture_data.reserve(util_format_get_blocksize(pipe->format));
78       util_format_pack_rgba(pipe->format, &texture_data[0], data.data(), 1);
79       q.pipe->clear_texture(q.pipe, pipe, 0, box(from, region), texture_data.data());
80    }
81 }
82 
83 mapping *
add_map(command_queue & q,cl_map_flags flags,bool blocking,const vector & origin,const vector & region)84 resource::add_map(command_queue &q, cl_map_flags flags, bool blocking,
85                   const vector &origin, const vector &region) {
86    maps.emplace_back(q, *this, flags, blocking, origin, region);
87    return &maps.back();
88 }
89 
90 void
del_map(void * p)91 resource::del_map(void *p) {
92    erase_if([&](const mapping &b) {
93          return static_cast<void *>(b) == p;
94       }, maps);
95 }
96 
97 unsigned
map_count() const98 resource::map_count() const {
99    return maps.size();
100 }
101 
102 pipe_sampler_view *
bind_sampler_view(command_queue & q)103 resource::bind_sampler_view(command_queue &q) {
104    pipe_sampler_view info;
105 
106    u_sampler_view_default_template(&info, pipe, pipe->format);
107    return q.pipe->create_sampler_view(q.pipe, pipe, &info);
108 }
109 
110 void
unbind_sampler_view(command_queue & q,pipe_sampler_view * st)111 resource::unbind_sampler_view(command_queue &q,
112                               pipe_sampler_view *st) {
113    q.pipe->sampler_view_destroy(q.pipe, st);
114 }
115 
116 pipe_image_view
create_image_view(command_queue & q)117 resource::create_image_view(command_queue &q) {
118    pipe_image_view view;
119    view.resource = pipe;
120    view.format = pipe->format;
121    view.access = 0;
122    view.shader_access = PIPE_IMAGE_ACCESS_WRITE;
123 
124    if (pipe->target == PIPE_BUFFER) {
125       view.u.buf.offset = 0;
126       view.u.buf.size = obj.size();
127    } else {
128       view.u.tex.first_layer = 0;
129       if (util_texture_is_array(pipe->target))
130          view.u.tex.last_layer = pipe->array_size - 1;
131       else
132          view.u.tex.last_layer = 0;
133       view.u.tex.level = 0;
134    }
135 
136    return view;
137 }
138 
139 pipe_surface *
bind_surface(command_queue & q,bool rw)140 resource::bind_surface(command_queue &q, bool rw) {
141    pipe_surface info {};
142 
143    info.format = pipe->format;
144    info.writable = rw;
145 
146    if (pipe->target == PIPE_BUFFER)
147       info.u.buf.last_element = pipe->width0 - 1;
148 
149    return q.pipe->create_surface(q.pipe, pipe, &info);
150 }
151 
152 void
unbind_surface(command_queue & q,pipe_surface * st)153 resource::unbind_surface(command_queue &q, pipe_surface *st) {
154    q.pipe->surface_destroy(q.pipe, st);
155 }
156 
root_resource(clover::device & dev,memory_obj & obj,command_queue & q,const void * data_ptr)157 root_resource::root_resource(clover::device &dev, memory_obj &obj,
158                              command_queue &q, const void *data_ptr) :
159    resource(dev, obj) {
160    pipe_resource info {};
161 
162    if (image *img = dynamic_cast<image *>(&obj)) {
163       info.format = translate_format(img->format());
164       info.width0 = img->width();
165       info.height0 = img->height();
166       info.depth0 = img->depth();
167    } else {
168       info.width0 = obj.size();
169       info.height0 = 1;
170       info.depth0 = 1;
171    }
172 
173    info.array_size = 1;
174    info.target = translate_target(obj.type());
175    info.bind = (PIPE_BIND_SAMPLER_VIEW |
176                 PIPE_BIND_COMPUTE_RESOURCE |
177                 PIPE_BIND_GLOBAL);
178 
179    if (obj.flags() & CL_MEM_USE_HOST_PTR && dev.allows_user_pointers()) {
180       // Page alignment is normally required for this, just try, hope for the
181       // best and fall back if it fails.
182       pipe = dev.pipe->resource_from_user_memory(dev.pipe, &info, obj.host_ptr());
183       if (pipe)
184          return;
185    }
186 
187    if (obj.flags() & (CL_MEM_ALLOC_HOST_PTR | CL_MEM_USE_HOST_PTR)) {
188       info.usage = PIPE_USAGE_STAGING;
189    }
190 
191    pipe = dev.pipe->resource_create(dev.pipe, &info);
192    if (!pipe)
193       throw error(CL_OUT_OF_RESOURCES);
194 
195    if (data_ptr) {
196       box rect { {{ 0, 0, 0 }}, {{ info.width0, info.height0, info.depth0 }} };
197       unsigned cpp = util_format_get_blocksize(info.format);
198 
199       if (pipe->target == PIPE_BUFFER)
200          q.pipe->buffer_subdata(q.pipe, pipe, PIPE_MAP_WRITE,
201                                 0, info.width0, data_ptr);
202       else
203          q.pipe->texture_subdata(q.pipe, pipe, 0, PIPE_MAP_WRITE,
204                                  rect, data_ptr, cpp * info.width0,
205                                  cpp * info.width0 * info.height0);
206    }
207 }
208 
root_resource(clover::device & dev,memory_obj & obj,root_resource & r)209 root_resource::root_resource(clover::device &dev, memory_obj &obj,
210                              root_resource &r) :
211    resource(dev, obj) {
212    assert(0); // XXX -- resource shared among dev and r.dev
213 }
214 
~root_resource()215 root_resource::~root_resource() {
216    pipe_resource_reference(&this->pipe, NULL);
217 }
218 
sub_resource(resource & r,const vector & offset)219 sub_resource::sub_resource(resource &r, const vector &offset) :
220    resource(r.device(), r.obj) {
221    this->pipe = r.pipe;
222    this->offset = r.offset + offset;
223 }
224 
mapping(command_queue & q,resource & r,cl_map_flags flags,bool blocking,const resource::vector & origin,const resource::vector & region)225 mapping::mapping(command_queue &q, resource &r,
226                  cl_map_flags flags, bool blocking,
227                  const resource::vector &origin,
228                  const resource::vector &region) :
229    pctx(q.pipe), pres(NULL) {
230    unsigned usage = ((flags & CL_MAP_WRITE ? PIPE_MAP_WRITE : 0 ) |
231                      (flags & CL_MAP_READ ? PIPE_MAP_READ : 0 ) |
232                      (flags & CL_MAP_WRITE_INVALIDATE_REGION ?
233                       PIPE_MAP_DISCARD_RANGE : 0) |
234                      (!blocking ? PIPE_MAP_UNSYNCHRONIZED : 0));
235 
236    p = pctx->buffer_map(pctx, r.pipe, 0, usage,
237                           box(origin + r.offset, region), &pxfer);
238    if (!p) {
239       pxfer = NULL;
240       throw error(CL_OUT_OF_RESOURCES);
241    }
242    pipe_resource_reference(&pres, r.pipe);
243 }
244 
mapping(mapping && m)245 mapping::mapping(mapping &&m) :
246    pctx(m.pctx), pxfer(m.pxfer), pres(m.pres), p(m.p) {
247    m.pctx = NULL;
248    m.pxfer = NULL;
249    m.pres = NULL;
250    m.p = NULL;
251 }
252 
~mapping()253 mapping::~mapping() {
254    if (pxfer) {
255       pctx->buffer_unmap(pctx, pxfer);
256    }
257    pipe_resource_reference(&pres, NULL);
258 }
259 
260 mapping &
operator =(mapping m)261 mapping::operator=(mapping m) {
262    std::swap(pctx, m.pctx);
263    std::swap(pxfer, m.pxfer);
264    std::swap(pres, m.pres);
265    std::swap(p, m.p);
266    return *this;
267 }
268 
269 resource::vector
pitch() const270 mapping::pitch() const
271 {
272    return {
273       util_format_get_blocksize(pres->format),
274       pxfer->stride,
275       pxfer->layer_stride,
276    };
277 }
278