b8e257db6dc1be2c221a0dc10b6aeddc59a47bf1
[mesa.git] / src / gallium / frontends / clover / core / resource.cpp
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:
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
41 operator const pipe_box *() {
42 return &pipe;
43 }
44
45 protected:
46 pipe_box pipe;
47 };
48 }
49
50 resource::resource(clover::device &dev, memory_obj &obj) :
51 device(dev), obj(obj), pipe(NULL), offset() {
52 }
53
54 resource::~resource() {
55 }
56
57 void
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
68 resource::clear(command_queue &q, const size_t origin, const size_t size,
69 const void *pattern, const size_t pattern_size) {
70 auto p = offset[0] + origin;
71
72 q.pipe->clear_buffer(q.pipe, pipe, p, size, pattern, pattern_size);
73 }
74
75 void *
76 resource::add_map(command_queue &q, cl_map_flags flags, bool blocking,
77 const vector &origin, const vector &region) {
78 maps.emplace_back(q, *this, flags, blocking, origin, region);
79 return maps.back();
80 }
81
82 void
83 resource::del_map(void *p) {
84 erase_if([&](const mapping &m) {
85 return static_cast<void *>(m) == p;
86 }, maps);
87 }
88
89 unsigned
90 resource::map_count() const {
91 return maps.size();
92 }
93
94 pipe_sampler_view *
95 resource::bind_sampler_view(command_queue &q) {
96 pipe_sampler_view info;
97
98 u_sampler_view_default_template(&info, pipe, pipe->format);
99 return q.pipe->create_sampler_view(q.pipe, pipe, &info);
100 }
101
102 void
103 resource::unbind_sampler_view(command_queue &q,
104 pipe_sampler_view *st) {
105 q.pipe->sampler_view_destroy(q.pipe, st);
106 }
107
108 pipe_surface *
109 resource::bind_surface(command_queue &q, bool rw) {
110 pipe_surface info {};
111
112 info.format = pipe->format;
113 info.writable = rw;
114
115 if (pipe->target == PIPE_BUFFER)
116 info.u.buf.last_element = pipe->width0 - 1;
117
118 return q.pipe->create_surface(q.pipe, pipe, &info);
119 }
120
121 void
122 resource::unbind_surface(command_queue &q, pipe_surface *st) {
123 q.pipe->surface_destroy(q.pipe, st);
124 }
125
126 root_resource::root_resource(clover::device &dev, memory_obj &obj,
127 command_queue &q, const std::string &data) :
128 resource(dev, obj) {
129 pipe_resource info {};
130 const bool user_ptr_support = dev.pipe->get_param(dev.pipe,
131 PIPE_CAP_RESOURCE_FROM_USER_MEMORY);
132
133 if (image *img = dynamic_cast<image *>(&obj)) {
134 info.format = translate_format(img->format());
135 info.width0 = img->width();
136 info.height0 = img->height();
137 info.depth0 = img->depth();
138 } else {
139 info.width0 = obj.size();
140 info.height0 = 1;
141 info.depth0 = 1;
142 }
143
144 info.array_size = 1;
145 info.target = translate_target(obj.type());
146 info.bind = (PIPE_BIND_SAMPLER_VIEW |
147 PIPE_BIND_COMPUTE_RESOURCE |
148 PIPE_BIND_GLOBAL);
149
150 if (obj.flags() & CL_MEM_USE_HOST_PTR && user_ptr_support) {
151 // Page alignment is normally required for this, just try, hope for the
152 // best and fall back if it fails.
153 pipe = dev.pipe->resource_from_user_memory(dev.pipe, &info, obj.host_ptr());
154 if (pipe)
155 return;
156 }
157
158 if (obj.flags() & (CL_MEM_ALLOC_HOST_PTR | CL_MEM_USE_HOST_PTR)) {
159 info.usage = PIPE_USAGE_STAGING;
160 }
161
162 pipe = dev.pipe->resource_create(dev.pipe, &info);
163 if (!pipe)
164 throw error(CL_OUT_OF_RESOURCES);
165
166 if (obj.flags() & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR)) {
167 const void *data_ptr = !data.empty() ? data.data() : obj.host_ptr();
168 box rect { {{ 0, 0, 0 }}, {{ info.width0, info.height0, info.depth0 }} };
169 unsigned cpp = util_format_get_blocksize(info.format);
170
171 if (pipe->target == PIPE_BUFFER)
172 q.pipe->buffer_subdata(q.pipe, pipe, PIPE_TRANSFER_WRITE,
173 0, info.width0, data_ptr);
174 else
175 q.pipe->texture_subdata(q.pipe, pipe, 0, PIPE_TRANSFER_WRITE,
176 rect, data_ptr, cpp * info.width0,
177 cpp * info.width0 * info.height0);
178 }
179 }
180
181 root_resource::root_resource(clover::device &dev, memory_obj &obj,
182 root_resource &r) :
183 resource(dev, obj) {
184 assert(0); // XXX -- resource shared among dev and r.dev
185 }
186
187 root_resource::~root_resource() {
188 pipe_resource_reference(&this->pipe, NULL);
189 }
190
191 sub_resource::sub_resource(resource &r, const vector &offset) :
192 resource(r.device(), r.obj) {
193 this->pipe = r.pipe;
194 this->offset = r.offset + offset;
195 }
196
197 mapping::mapping(command_queue &q, resource &r,
198 cl_map_flags flags, bool blocking,
199 const resource::vector &origin,
200 const resource::vector &region) :
201 pctx(q.pipe), pres(NULL) {
202 unsigned usage = ((flags & CL_MAP_WRITE ? PIPE_TRANSFER_WRITE : 0 ) |
203 (flags & CL_MAP_READ ? PIPE_TRANSFER_READ : 0 ) |
204 (flags & CL_MAP_WRITE_INVALIDATE_REGION ?
205 PIPE_TRANSFER_DISCARD_RANGE : 0) |
206 (!blocking ? PIPE_TRANSFER_UNSYNCHRONIZED : 0));
207
208 p = pctx->transfer_map(pctx, r.pipe, 0, usage,
209 box(origin + r.offset, region), &pxfer);
210 if (!p) {
211 pxfer = NULL;
212 throw error(CL_OUT_OF_RESOURCES);
213 }
214 pipe_resource_reference(&pres, r.pipe);
215 }
216
217 mapping::mapping(mapping &&m) :
218 pctx(m.pctx), pxfer(m.pxfer), pres(m.pres), p(m.p) {
219 m.pctx = NULL;
220 m.pxfer = NULL;
221 m.pres = NULL;
222 m.p = NULL;
223 }
224
225 mapping::~mapping() {
226 if (pxfer) {
227 pctx->transfer_unmap(pctx, pxfer);
228 }
229 pipe_resource_reference(&pres, NULL);
230 }
231
232 mapping &
233 mapping::operator=(mapping m) {
234 std::swap(pctx, m.pctx);
235 std::swap(pxfer, m.pxfer);
236 std::swap(pres, m.pres);
237 std::swap(p, m.p);
238 return *this;
239 }