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