• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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       info.array_size = MAX2(1, img->array_size());
168    } else {
169       info.width0 = obj.size();
170       info.height0 = 1;
171       info.depth0 = 1;
172       info.array_size = 1;
173    }
174 
175    info.target = translate_target(obj.type());
176    info.bind = (PIPE_BIND_SAMPLER_VIEW |
177                 PIPE_BIND_COMPUTE_RESOURCE |
178                 PIPE_BIND_GLOBAL);
179 
180    if (obj.flags() & CL_MEM_USE_HOST_PTR && dev.allows_user_pointers()) {
181       // Page alignment is normally required for this, just try, hope for the
182       // best and fall back if it fails.
183       pipe = dev.pipe->resource_from_user_memory(dev.pipe, &info, obj.host_ptr());
184       if (pipe)
185          return;
186    }
187 
188    if (obj.flags() & (CL_MEM_ALLOC_HOST_PTR | CL_MEM_USE_HOST_PTR)) {
189       info.usage = PIPE_USAGE_STAGING;
190    }
191 
192    pipe = dev.pipe->resource_create(dev.pipe, &info);
193    if (!pipe)
194       throw error(CL_OUT_OF_RESOURCES);
195 
196    if (data_ptr) {
197       box rect { {{ 0, 0, 0 }}, {{ info.width0, info.height0, info.depth0 }} };
198       unsigned cpp = util_format_get_blocksize(info.format);
199 
200       if (pipe->target == PIPE_BUFFER)
201          q.pipe->buffer_subdata(q.pipe, pipe, PIPE_MAP_WRITE,
202                                 0, info.width0, data_ptr);
203       else
204          q.pipe->texture_subdata(q.pipe, pipe, 0, PIPE_MAP_WRITE,
205                                  rect, data_ptr, cpp * info.width0,
206                                  cpp * info.width0 * info.height0);
207    }
208 }
209 
root_resource(clover::device & dev,memory_obj & obj,root_resource & r)210 root_resource::root_resource(clover::device &dev, memory_obj &obj,
211                              root_resource &r) :
212    resource(dev, obj) {
213    assert(0); // XXX -- resource shared among dev and r.dev
214 }
215 
~root_resource()216 root_resource::~root_resource() {
217    pipe_resource_reference(&this->pipe, NULL);
218 }
219 
sub_resource(resource & r,const vector & offset)220 sub_resource::sub_resource(resource &r, const vector &offset) :
221    resource(r.device(), r.obj) {
222    this->pipe = r.pipe;
223    this->offset = r.offset + offset;
224 }
225 
mapping(command_queue & q,resource & r,cl_map_flags flags,bool blocking,const resource::vector & origin,const resource::vector & region)226 mapping::mapping(command_queue &q, resource &r,
227                  cl_map_flags flags, bool blocking,
228                  const resource::vector &origin,
229                  const resource::vector &region) :
230    pctx(q.pipe), pres(NULL) {
231    unsigned usage = ((flags & CL_MAP_WRITE ? PIPE_MAP_WRITE : 0 ) |
232                      (flags & CL_MAP_READ ? PIPE_MAP_READ : 0 ) |
233                      (flags & CL_MAP_WRITE_INVALIDATE_REGION ?
234                       PIPE_MAP_DISCARD_RANGE : 0) |
235                      (!blocking ? PIPE_MAP_UNSYNCHRONIZED : 0));
236 
237    p = pctx->buffer_map(pctx, r.pipe, 0, usage,
238                           box(origin + r.offset, region), &pxfer);
239    if (!p) {
240       pxfer = NULL;
241       throw error(CL_OUT_OF_RESOURCES);
242    }
243    pipe_resource_reference(&pres, r.pipe);
244 }
245 
mapping(mapping && m)246 mapping::mapping(mapping &&m) :
247    pctx(m.pctx), pxfer(m.pxfer), pres(m.pres), p(m.p) {
248    m.pctx = NULL;
249    m.pxfer = NULL;
250    m.pres = NULL;
251    m.p = NULL;
252 }
253 
~mapping()254 mapping::~mapping() {
255    if (pxfer) {
256       pctx->buffer_unmap(pctx, pxfer);
257    }
258    pipe_resource_reference(&pres, NULL);
259 }
260 
261 mapping &
operator =(mapping m)262 mapping::operator=(mapping m) {
263    std::swap(pctx, m.pctx);
264    std::swap(pxfer, m.pxfer);
265    std::swap(pres, m.pres);
266    std::swap(p, m.p);
267    return *this;
268 }
269 
270 resource::vector
pitch() const271 mapping::pitch() const
272 {
273    return {
274       util_format_get_blocksize(pres->format),
275       pxfer->stride,
276       pxfer->layer_stride,
277    };
278 }
279