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