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