• 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 
30 using namespace clover;
31 
32 namespace {
33    class box {
34    public:
box(const resource::vector & origin,const resource::vector & size)35       box(const resource::vector &origin, const resource::vector &size) :
36         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] }) {
39       }
40 
operator const pipe_box*()41       operator const pipe_box *() {
42          return &pipe;
43       }
44 
45    protected:
46       pipe_box pipe;
47    };
48 }
49 
resource(clover::device & dev,memory_obj & obj)50 resource::resource(clover::device &dev, memory_obj &obj) :
51    device(dev), obj(obj), pipe(NULL), offset() {
52 }
53 
~resource()54 resource::~resource() {
55 }
56 
57 void
copy(command_queue & q,const vector & origin,const vector & region,resource & src_res,const vector & src_origin)58 resource::copy(command_queue &q, const vector &origin, const vector &region,
59                resource &src_res, const vector &src_origin) {
60    auto p = offset + origin;
61 
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 
67 void
clear(command_queue & q,const vector & origin,const vector & region,const std::string & data)68 resource::clear(command_queue &q, const vector &origin, const vector &region,
69                 const std::string &data) {
70    auto from = offset + origin;
71 
72    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    }
80 }
81 
82 mapping *
add_map(command_queue & q,cl_map_flags flags,bool blocking,const vector & origin,const vector & region)83 resource::add_map(command_queue &q, cl_map_flags flags, bool blocking,
84                   const vector &origin, const vector &region) {
85    maps.emplace_back(q, *this, flags, blocking, origin, region);
86    return &maps.back();
87 }
88 
89 void
del_map(void * p)90 resource::del_map(void *p) {
91    erase_if([&](const mapping &m) {
92          return static_cast<void *>(m) == p;
93       }, maps);
94 }
95 
96 unsigned
map_count() const97 resource::map_count() const {
98    return maps.size();
99 }
100 
101 pipe_sampler_view *
bind_sampler_view(command_queue & q)102 resource::bind_sampler_view(command_queue &q) {
103    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 
109 void
unbind_sampler_view(command_queue & q,pipe_sampler_view * st)110 resource::unbind_sampler_view(command_queue &q,
111                               pipe_sampler_view *st) {
112    q.pipe->sampler_view_destroy(q.pipe, st);
113 }
114 
115 pipe_image_view
create_image_view(command_queue & q)116 resource::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 
135 pipe_surface *
bind_surface(command_queue & q,bool rw)136 resource::bind_surface(command_queue &q, bool rw) {
137    pipe_surface info {};
138 
139    info.format = pipe->format;
140    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 
148 void
unbind_surface(command_queue & q,pipe_surface * st)149 resource::unbind_surface(command_queue &q, pipe_surface *st) {
150    q.pipe->surface_destroy(q.pipe, st);
151 }
152 
root_resource(clover::device & dev,memory_obj & obj,command_queue & q,const void * data_ptr)153 root_resource::root_resource(clover::device &dev, memory_obj &obj,
154                              command_queue &q, const void *data_ptr) :
155    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();
165       info.height0 = 1;
166       info.depth0 = 1;
167    }
168 
169    info.array_size = 1;
170    info.target = translate_target(obj.type());
171    info.bind = (PIPE_BIND_SAMPLER_VIEW |
172                 PIPE_BIND_COMPUTE_RESOURCE |
173                 PIPE_BIND_GLOBAL);
174 
175    if (obj.flags() & CL_MEM_USE_HOST_PTR && dev.allows_user_pointers()) {
176       // 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)) {
184       info.usage = PIPE_USAGE_STAGING;
185    }
186 
187    pipe = dev.pipe->resource_create(dev.pipe, &info);
188    if (!pipe)
189       throw error(CL_OUT_OF_RESOURCES);
190 
191    if (data_ptr) {
192       box rect { {{ 0, 0, 0 }}, {{ info.width0, info.height0, info.depth0 }} };
193       unsigned cpp = util_format_get_blocksize(info.format);
194 
195       if (pipe->target == PIPE_BUFFER)
196          q.pipe->buffer_subdata(q.pipe, pipe, PIPE_MAP_WRITE,
197                                 0, info.width0, data_ptr);
198       else
199          q.pipe->texture_subdata(q.pipe, pipe, 0, PIPE_MAP_WRITE,
200                                  rect, data_ptr, cpp * info.width0,
201                                  cpp * info.width0 * info.height0);
202    }
203 }
204 
root_resource(clover::device & dev,memory_obj & obj,root_resource & r)205 root_resource::root_resource(clover::device &dev, memory_obj &obj,
206                              root_resource &r) :
207    resource(dev, obj) {
208    assert(0); // XXX -- resource shared among dev and r.dev
209 }
210 
~root_resource()211 root_resource::~root_resource() {
212    pipe_resource_reference(&this->pipe, NULL);
213 }
214 
sub_resource(resource & r,const vector & offset)215 sub_resource::sub_resource(resource &r, const vector &offset) :
216    resource(r.device(), r.obj) {
217    this->pipe = r.pipe;
218    this->offset = r.offset + offset;
219 }
220 
mapping(command_queue & q,resource & r,cl_map_flags flags,bool blocking,const resource::vector & origin,const resource::vector & region)221 mapping::mapping(command_queue &q, resource &r,
222                  cl_map_flags flags, bool blocking,
223                  const resource::vector &origin,
224                  const resource::vector &region) :
225    pctx(q.pipe), pres(NULL) {
226    unsigned usage = ((flags & CL_MAP_WRITE ? PIPE_MAP_WRITE : 0 ) |
227                      (flags & CL_MAP_READ ? PIPE_MAP_READ : 0 ) |
228                      (flags & CL_MAP_WRITE_INVALIDATE_REGION ?
229                       PIPE_MAP_DISCARD_RANGE : 0) |
230                      (!blocking ? PIPE_MAP_UNSYNCHRONIZED : 0));
231 
232    p = pctx->transfer_map(pctx, r.pipe, 0, usage,
233                           box(origin + r.offset, region), &pxfer);
234    if (!p) {
235       pxfer = NULL;
236       throw error(CL_OUT_OF_RESOURCES);
237    }
238    pipe_resource_reference(&pres, r.pipe);
239 }
240 
mapping(mapping && m)241 mapping::mapping(mapping &&m) :
242    pctx(m.pctx), pxfer(m.pxfer), pres(m.pres), p(m.p) {
243    m.pctx = NULL;
244    m.pxfer = NULL;
245    m.pres = NULL;
246    m.p = NULL;
247 }
248 
~mapping()249 mapping::~mapping() {
250    if (pxfer) {
251       pctx->transfer_unmap(pctx, pxfer);
252    }
253    pipe_resource_reference(&pres, NULL);
254 }
255 
256 mapping &
operator =(mapping m)257 mapping::operator=(mapping m) {
258    std::swap(pctx, m.pctx);
259    std::swap(pxfer, m.pxfer);
260    std::swap(pres, m.pres);
261    std::swap(p, m.p);
262    return *this;
263 }
264 
265 resource::vector
pitch() const266 mapping::pitch() const
267 {
268    return {
269       util_format_get_blocksize(pres->format),
270       pxfer->stride,
271       pxfer->layer_stride,
272    };
273 }
274