etnaviv: slim down resource waiting
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_transfer.c
1 /*
2 * Copyright (c) 2012-2015 Etnaviv Project
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, sub license,
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 (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Wladimir J. van der Laan <laanwj@gmail.com>
25 */
26
27 #include "etnaviv_transfer.h"
28 #include "etnaviv_clear_blit.h"
29 #include "etnaviv_context.h"
30 #include "etnaviv_debug.h"
31
32 #include "pipe/p_defines.h"
33 #include "pipe/p_format.h"
34 #include "pipe/p_screen.h"
35 #include "pipe/p_state.h"
36 #include "util/u_format.h"
37 #include "util/u_inlines.h"
38 #include "util/u_memory.h"
39 #include "util/u_surface.h"
40 #include "util/u_transfer.h"
41
42 /* Compute offset into a 1D/2D/3D buffer of a certain box.
43 * This box must be aligned to the block width and height of the
44 * underlying format. */
45 static inline size_t
46 etna_compute_offset(enum pipe_format format, const struct pipe_box *box,
47 size_t stride, size_t layer_stride)
48 {
49 return box->z * layer_stride +
50 box->y / util_format_get_blockheight(format) * stride +
51 box->x / util_format_get_blockwidth(format) *
52 util_format_get_blocksize(format);
53 }
54
55 static void
56 etna_transfer_unmap(struct pipe_context *pctx, struct pipe_transfer *ptrans)
57 {
58 struct etna_context *ctx = etna_context(pctx);
59 struct etna_transfer *trans = etna_transfer(ptrans);
60 struct etna_resource *rsc = etna_resource(ptrans->resource);
61
62 /* XXX
63 * When writing to a resource that is already in use, replace the resource
64 * with a completely new buffer
65 * and free the old one using a fenced free.
66 * The most tricky case to implement will be: tiled or supertiled surface,
67 * partial write, target not aligned to 4/64. */
68 assert(ptrans->level <= rsc->base.last_level);
69
70 if (rsc->texture && !etna_resource_newer(rsc, etna_resource(rsc->texture)))
71 rsc = etna_resource(rsc->texture); /* switch to using the texture resource */
72
73 if (trans->rsc)
74 etna_bo_cpu_fini(etna_resource(trans->rsc)->bo);
75
76 if (ptrans->usage & PIPE_TRANSFER_WRITE) {
77 if (trans->rsc) {
78 /* We have a temporary resource due to either tile status or
79 * tiling format. Write back the updated buffer contents.
80 * FIXME: we need to invalidate the tile status. */
81 etna_copy_resource(pctx, ptrans->resource, trans->rsc, ptrans->level,
82 trans->rsc->last_level);
83 } else if (trans->staging) {
84 /* map buffer object */
85 struct etna_resource_level *res_level = &rsc->levels[ptrans->level];
86 void *mapped = etna_bo_map(rsc->bo) + res_level->offset;
87
88 if (rsc->layout == ETNA_LAYOUT_LINEAR || rsc->layout == ETNA_LAYOUT_TILED) {
89 if (rsc->layout == ETNA_LAYOUT_TILED && !util_format_is_compressed(rsc->base.format)) {
90 etna_texture_tile(
91 mapped + ptrans->box.z * res_level->layer_stride,
92 trans->staging, ptrans->box.x, ptrans->box.y,
93 res_level->stride, ptrans->box.width, ptrans->box.height,
94 ptrans->stride, util_format_get_blocksize(rsc->base.format));
95 } else { /* non-tiled or compressed format */
96 util_copy_box(mapped, rsc->base.format, res_level->stride,
97 res_level->layer_stride, ptrans->box.x,
98 ptrans->box.y, ptrans->box.z, ptrans->box.width,
99 ptrans->box.height, ptrans->box.depth,
100 trans->staging, ptrans->stride,
101 ptrans->layer_stride, 0, 0, 0 /* src x,y,z */);
102 }
103 } else {
104 BUG("unsupported tiling %i", rsc->layout);
105 }
106
107 FREE(trans->staging);
108 }
109
110 rsc->seqno++;
111
112 if (rsc->base.bind & PIPE_BIND_SAMPLER_VIEW) {
113 ctx->dirty |= ETNA_DIRTY_TEXTURE_CACHES;
114 }
115 }
116
117 if (!trans->rsc)
118 etna_bo_cpu_fini(rsc->bo);
119
120 pipe_resource_reference(&trans->rsc, NULL);
121 pipe_resource_reference(&ptrans->resource, NULL);
122 slab_free(&ctx->transfer_pool, trans);
123 }
124
125 static void *
126 etna_transfer_map(struct pipe_context *pctx, struct pipe_resource *prsc,
127 unsigned level,
128 unsigned usage,
129 const struct pipe_box *box,
130 struct pipe_transfer **out_transfer)
131 {
132 struct etna_context *ctx = etna_context(pctx);
133 struct etna_resource *rsc = etna_resource(prsc);
134 struct etna_transfer *trans;
135 struct pipe_transfer *ptrans;
136 enum pipe_format format = prsc->format;
137
138 trans = slab_alloc(&ctx->transfer_pool);
139 if (!trans)
140 return NULL;
141
142 /* slab_alloc() doesn't zero */
143 memset(trans, 0, sizeof(*trans));
144
145 ptrans = &trans->base;
146 pipe_resource_reference(&ptrans->resource, prsc);
147 ptrans->level = level;
148 ptrans->usage = usage;
149 ptrans->box = *box;
150
151 assert(level <= prsc->last_level);
152
153 if (rsc->texture && !etna_resource_newer(rsc, etna_resource(rsc->texture))) {
154 /* We have a texture resource which is the same age or newer than the
155 * render resource. Use the texture resource, which avoids bouncing
156 * pixels between the two resources, and we can de-tile it in s/w. */
157 rsc = etna_resource(rsc->texture);
158 } else if (rsc->ts_bo ||
159 (rsc->layout != ETNA_LAYOUT_LINEAR &&
160 util_format_get_blocksize(format) > 1 &&
161 /* HALIGN 4 resources are incompatible with the resolve engine,
162 * so fall back to using software to detile this resource. */
163 rsc->halign != TEXTURE_HALIGN_FOUR)) {
164 /* If the surface has tile status, we need to resolve it first.
165 * The strategy we implement here is to use the RS to copy the
166 * depth buffer, filling in the "holes" where the tile status
167 * indicates that it's clear. We also do this for tiled
168 * resources, but only if the RS can blit them. */
169 if (usage & PIPE_TRANSFER_MAP_DIRECTLY) {
170 slab_free(&ctx->transfer_pool, trans);
171 BUG("unsupported transfer flags %#x with tile status/tiled layout", usage);
172 return NULL;
173 }
174
175 if (prsc->depth0 > 1) {
176 slab_free(&ctx->transfer_pool, trans);
177 BUG("resource has depth >1 with tile status");
178 return NULL;
179 }
180
181 struct pipe_resource templ = *prsc;
182 templ.nr_samples = 0;
183 templ.bind = PIPE_BIND_RENDER_TARGET;
184
185 trans->rsc = etna_resource_alloc(pctx->screen, ETNA_LAYOUT_LINEAR, &templ);
186 if (!trans->rsc) {
187 slab_free(&ctx->transfer_pool, trans);
188 return NULL;
189 }
190
191 etna_copy_resource(pctx, trans->rsc, prsc, level, trans->rsc->last_level);
192
193 /* Switch to using the temporary resource instead */
194 rsc = etna_resource(trans->rsc);
195 }
196
197 struct etna_resource_level *res_level = &rsc->levels[level];
198
199 /* Always sync if we have the temporary resource. The PIPE_TRANSFER_READ
200 * case could be optimised if we knew whether the resource has outstanding
201 * rendering. */
202 if ((usage & PIPE_TRANSFER_READ || trans->rsc) &&
203 rsc->status & ETNA_PENDING_WRITE)
204 pctx->flush(pctx, NULL, 0);
205
206 /* XXX we don't handle PIPE_TRANSFER_FLUSH_EXPLICIT; this flag can be ignored
207 * when mapping in-place,
208 * but when not in place we need to fire off the copy operation in
209 * transfer_flush_region (currently
210 * a no-op) instead of unmap. Need to handle this to support
211 * ARB_map_buffer_range extension at least.
212 */
213 /* XXX we don't take care of current operations on the resource; which can
214 be, at some point in the pipeline
215 which is not yet executed:
216
217 - bound as surface
218 - bound through vertex buffer
219 - bound through index buffer
220 - bound in sampler view
221 - used in clear_render_target / clear_depth_stencil operation
222 - used in blit
223 - used in resource_copy_region
224
225 How do other drivers record this information over course of the rendering
226 pipeline?
227 Is it necessary at all? Only in case we want to provide a fast path and
228 map the resource directly
229 (and for PIPE_TRANSFER_MAP_DIRECTLY) and we don't want to force a sync.
230 We also need to know whether the resource is in use to determine if a sync
231 is needed (or just do it
232 always, but that comes at the expense of performance).
233
234 A conservative approximation without too much overhead would be to mark
235 all resources that have
236 been bound at some point as busy. A drawback would be that accessing
237 resources that have
238 been bound but are no longer in use for a while still carry a performance
239 penalty. On the other hand,
240 the program could be using PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE or
241 PIPE_TRANSFER_UNSYNCHRONIZED to
242 avoid this in the first place...
243
244 A) We use an in-pipe copy engine, and queue the copy operation after unmap
245 so that the copy
246 will be performed when all current commands have been executed.
247 Using the RS is possible, not sure if always efficient. This can also
248 do any kind of tiling for us.
249 Only possible when PIPE_TRANSFER_DISCARD_RANGE is set.
250 B) We discard the entire resource (or at least, the mipmap level) and
251 allocate new memory for it.
252 Only possible when mapping the entire resource or
253 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE is set.
254 */
255
256 /* No need to allocate a buffer for copying if the resource is not in use,
257 * and no tiling is needed, can just return a direct pointer.
258 */
259 bool in_place = rsc->layout == ETNA_LAYOUT_LINEAR ||
260 (rsc->layout == ETNA_LAYOUT_TILED &&
261 util_format_is_compressed(prsc->format));
262
263 /* Ignore PIPE_TRANSFER_UNSYNCHRONIZED and PIPE_TRANSFER_DONTBLOCK here.
264 * It appears that Gallium operates the index/vertex buffers in a
265 * circular fashion, and the CPU can catch up with the GPU and starts
266 * overwriting yet-to-be-processed entries, causing rendering corruption. */
267 uint32_t prep_flags = 0;
268
269 if (usage & PIPE_TRANSFER_READ)
270 prep_flags |= DRM_ETNA_PREP_READ;
271 if (usage & PIPE_TRANSFER_WRITE)
272 prep_flags |= DRM_ETNA_PREP_WRITE;
273
274 if (etna_bo_cpu_prep(rsc->bo, prep_flags))
275 goto fail_prep;
276
277 /* map buffer object */
278 void *mapped = etna_bo_map(rsc->bo);
279 if (!mapped)
280 goto fail;
281
282 *out_transfer = ptrans;
283
284 if (in_place) {
285 ptrans->stride = res_level->stride;
286 ptrans->layer_stride = res_level->layer_stride;
287
288 return mapped + res_level->offset +
289 etna_compute_offset(prsc->format, box, res_level->stride,
290 res_level->layer_stride);
291 } else {
292 unsigned divSizeX = util_format_get_blockwidth(format);
293 unsigned divSizeY = util_format_get_blockheight(format);
294
295 /* No direct mappings of tiled, since we need to manually
296 * tile/untile.
297 */
298 if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
299 goto fail;
300
301 mapped += res_level->offset;
302 ptrans->stride = align(box->width, divSizeX) * util_format_get_blocksize(format); /* row stride in bytes */
303 ptrans->layer_stride = align(box->height, divSizeY) * ptrans->stride;
304 size_t size = ptrans->layer_stride * box->depth;
305
306 trans->staging = MALLOC(size);
307 if (!trans->staging)
308 goto fail;
309
310 if (usage & PIPE_TRANSFER_READ) {
311 /* untile or copy resource for reading */
312 if (rsc->layout == ETNA_LAYOUT_LINEAR || rsc->layout == ETNA_LAYOUT_TILED) {
313 if (rsc->layout == ETNA_LAYOUT_TILED && !util_format_is_compressed(rsc->base.format)) {
314 etna_texture_untile(trans->staging,
315 mapped + ptrans->box.z * res_level->layer_stride,
316 ptrans->box.x, ptrans->box.y, res_level->stride,
317 ptrans->box.width, ptrans->box.height, ptrans->stride,
318 util_format_get_blocksize(rsc->base.format));
319 } else { /* non-tiled or compressed format */
320 util_copy_box(trans->staging, rsc->base.format, ptrans->stride,
321 ptrans->layer_stride, 0, 0, 0, /* dst x,y,z */
322 ptrans->box.width, ptrans->box.height,
323 ptrans->box.depth, mapped, res_level->stride,
324 res_level->layer_stride, ptrans->box.x,
325 ptrans->box.y, ptrans->box.z);
326 }
327 } else /* TODO supertiling */
328 {
329 BUG("unsupported tiling %i for reading", rsc->layout);
330 }
331 }
332
333 return trans->staging;
334 }
335
336 fail:
337 etna_bo_cpu_fini(rsc->bo);
338 fail_prep:
339 etna_transfer_unmap(pctx, ptrans);
340 return NULL;
341 }
342
343 static void
344 etna_transfer_flush_region(struct pipe_context *pctx,
345 struct pipe_transfer *transfer,
346 const struct pipe_box *box)
347 {
348 /* NOOP for now */
349 }
350
351 void
352 etna_transfer_init(struct pipe_context *pctx)
353 {
354 pctx->transfer_map = etna_transfer_map;
355 pctx->transfer_flush_region = etna_transfer_flush_region;
356 pctx->transfer_unmap = etna_transfer_unmap;
357 pctx->buffer_subdata = u_default_buffer_subdata;
358 pctx->texture_subdata = u_default_texture_subdata;
359 }