freedreno: add fd_transfer to wrap around pipe_transfer
[mesa.git] / src / gallium / drivers / freedreno / freedreno_resource.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include "util/u_format.h"
30 #include "util/u_inlines.h"
31 #include "util/u_transfer.h"
32 #include "util/u_string.h"
33 #include "util/u_surface.h"
34
35 #include "freedreno_resource.h"
36 #include "freedreno_screen.h"
37 #include "freedreno_surface.h"
38 #include "freedreno_context.h"
39 #include "freedreno_query_hw.h"
40 #include "freedreno_util.h"
41
42 #include <errno.h>
43
44 static void
45 fd_invalidate_resource(struct fd_context *ctx, struct pipe_resource *prsc)
46 {
47 int i;
48
49 /* Go through the entire state and see if the resource is bound
50 * anywhere. If it is, mark the relevant state as dirty. This is called on
51 * realloc_bo.
52 */
53
54 /* Constbufs */
55 for (i = 1; i < PIPE_MAX_CONSTANT_BUFFERS && !(ctx->dirty & FD_DIRTY_CONSTBUF); i++) {
56 if (ctx->constbuf[PIPE_SHADER_VERTEX].cb[i].buffer == prsc)
57 ctx->dirty |= FD_DIRTY_CONSTBUF;
58 if (ctx->constbuf[PIPE_SHADER_FRAGMENT].cb[i].buffer == prsc)
59 ctx->dirty |= FD_DIRTY_CONSTBUF;
60 }
61
62 /* VBOs */
63 for (i = 0; i < ctx->vtx.vertexbuf.count && !(ctx->dirty & FD_DIRTY_VTXBUF); i++) {
64 if (ctx->vtx.vertexbuf.vb[i].buffer == prsc)
65 ctx->dirty |= FD_DIRTY_VTXBUF;
66 }
67
68 /* Index buffer */
69 if (ctx->indexbuf.buffer == prsc)
70 ctx->dirty |= FD_DIRTY_INDEXBUF;
71
72 /* Textures */
73 for (i = 0; i < ctx->verttex.num_textures && !(ctx->dirty & FD_DIRTY_VERTTEX); i++) {
74 if (ctx->verttex.textures[i]->texture == prsc)
75 ctx->dirty |= FD_DIRTY_VERTTEX;
76 }
77 for (i = 0; i < ctx->fragtex.num_textures && !(ctx->dirty & FD_DIRTY_FRAGTEX); i++) {
78 if (ctx->fragtex.textures[i]->texture == prsc)
79 ctx->dirty |= FD_DIRTY_FRAGTEX;
80 }
81 }
82
83 static void
84 realloc_bo(struct fd_resource *rsc, uint32_t size)
85 {
86 struct fd_screen *screen = fd_screen(rsc->base.b.screen);
87 uint32_t flags = DRM_FREEDRENO_GEM_CACHE_WCOMBINE |
88 DRM_FREEDRENO_GEM_TYPE_KMEM; /* TODO */
89
90 /* if we start using things other than write-combine,
91 * be sure to check for PIPE_RESOURCE_FLAG_MAP_COHERENT
92 */
93
94 if (rsc->bo)
95 fd_bo_del(rsc->bo);
96
97 rsc->bo = fd_bo_new(screen->dev, size, flags);
98 rsc->timestamp = 0;
99 rsc->dirty = rsc->reading = false;
100 list_delinit(&rsc->list);
101 util_range_set_empty(&rsc->valid_buffer_range);
102 }
103
104 static void fd_resource_transfer_flush_region(struct pipe_context *pctx,
105 struct pipe_transfer *ptrans,
106 const struct pipe_box *box)
107 {
108 struct fd_resource *rsc = fd_resource(ptrans->resource);
109
110 if (ptrans->resource->target == PIPE_BUFFER)
111 util_range_add(&rsc->valid_buffer_range,
112 ptrans->box.x + box->x,
113 ptrans->box.x + box->x + box->width);
114 }
115
116 static void
117 fd_resource_transfer_unmap(struct pipe_context *pctx,
118 struct pipe_transfer *ptrans)
119 {
120 struct fd_context *ctx = fd_context(pctx);
121 struct fd_resource *rsc = fd_resource(ptrans->resource);
122 if (!(ptrans->usage & PIPE_TRANSFER_UNSYNCHRONIZED))
123 fd_bo_cpu_fini(rsc->bo);
124
125 util_range_add(&rsc->valid_buffer_range,
126 ptrans->box.x,
127 ptrans->box.x + ptrans->box.width);
128
129 pipe_resource_reference(&ptrans->resource, NULL);
130 util_slab_free(&ctx->transfer_pool, ptrans);
131 }
132
133 static void *
134 fd_resource_transfer_map(struct pipe_context *pctx,
135 struct pipe_resource *prsc,
136 unsigned level, unsigned usage,
137 const struct pipe_box *box,
138 struct pipe_transfer **pptrans)
139 {
140 struct fd_context *ctx = fd_context(pctx);
141 struct fd_resource *rsc = fd_resource(prsc);
142 struct fd_resource_slice *slice = fd_resource_slice(rsc, level);
143 struct fd_transfer *trans;
144 struct pipe_transfer *ptrans;
145 enum pipe_format format = prsc->format;
146 uint32_t op = 0;
147 uint32_t offset;
148 char *buf;
149 int ret = 0;
150
151 DBG("prsc=%p, level=%u, usage=%x", prsc, level, usage);
152
153 ptrans = util_slab_alloc(&ctx->transfer_pool);
154 if (!ptrans)
155 return NULL;
156
157 /* util_slab_alloc() doesn't zero: */
158 trans = fd_transfer(ptrans);
159 memset(trans, 0, sizeof(*trans));
160
161 pipe_resource_reference(&ptrans->resource, prsc);
162 ptrans->level = level;
163 ptrans->usage = usage;
164 ptrans->box = *box;
165 ptrans->stride = slice->pitch * rsc->cpp;
166 ptrans->layer_stride = slice->size0;
167
168 if (usage & PIPE_TRANSFER_READ)
169 op |= DRM_FREEDRENO_PREP_READ;
170
171 if (usage & PIPE_TRANSFER_WRITE)
172 op |= DRM_FREEDRENO_PREP_WRITE;
173
174 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
175 realloc_bo(rsc, fd_bo_size(rsc->bo));
176 fd_invalidate_resource(ctx, prsc);
177 } else if ((usage & PIPE_TRANSFER_WRITE) &&
178 prsc->target == PIPE_BUFFER &&
179 !util_ranges_intersect(&rsc->valid_buffer_range,
180 box->x, box->x + box->width)) {
181 /* We are trying to write to a previously uninitialized range. No need
182 * to wait.
183 */
184 } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
185 /* If the GPU is writing to the resource, or if it is reading from the
186 * resource and we're trying to write to it, flush the renders.
187 */
188 if (rsc->dirty ||
189 ((ptrans->usage & PIPE_TRANSFER_WRITE) && rsc->reading))
190 fd_context_render(pctx);
191
192 /* The GPU keeps track of how the various bo's are being used, and
193 * will wait if necessary for the proper operation to have
194 * completed.
195 */
196 ret = fd_bo_cpu_prep(rsc->bo, ctx->screen->pipe, op);
197 if (ret)
198 goto fail;
199 }
200
201 buf = fd_bo_map(rsc->bo);
202 if (!buf) {
203 fd_resource_transfer_unmap(pctx, ptrans);
204 return NULL;
205 }
206
207 *pptrans = ptrans;
208
209 if (rsc->layer_first) {
210 offset = slice->offset +
211 box->y / util_format_get_blockheight(format) * ptrans->stride +
212 box->x / util_format_get_blockwidth(format) * rsc->cpp +
213 box->z * rsc->layer_size;
214 } else {
215 offset = slice->offset +
216 box->y / util_format_get_blockheight(format) * ptrans->stride +
217 box->x / util_format_get_blockwidth(format) * rsc->cpp +
218 box->z * slice->size0;
219 }
220
221 return buf + offset;
222
223 fail:
224 fd_resource_transfer_unmap(pctx, ptrans);
225 return NULL;
226 }
227
228 static void
229 fd_resource_destroy(struct pipe_screen *pscreen,
230 struct pipe_resource *prsc)
231 {
232 struct fd_resource *rsc = fd_resource(prsc);
233 if (rsc->bo)
234 fd_bo_del(rsc->bo);
235 list_delinit(&rsc->list);
236 util_range_destroy(&rsc->valid_buffer_range);
237 FREE(rsc);
238 }
239
240 static boolean
241 fd_resource_get_handle(struct pipe_screen *pscreen,
242 struct pipe_resource *prsc,
243 struct winsys_handle *handle)
244 {
245 struct fd_resource *rsc = fd_resource(prsc);
246
247 return fd_screen_bo_get_handle(pscreen, rsc->bo,
248 rsc->slices[0].pitch * rsc->cpp, handle);
249 }
250
251
252 static const struct u_resource_vtbl fd_resource_vtbl = {
253 .resource_get_handle = fd_resource_get_handle,
254 .resource_destroy = fd_resource_destroy,
255 .transfer_map = fd_resource_transfer_map,
256 .transfer_flush_region = fd_resource_transfer_flush_region,
257 .transfer_unmap = fd_resource_transfer_unmap,
258 .transfer_inline_write = u_default_transfer_inline_write,
259 };
260
261 static uint32_t
262 setup_slices(struct fd_resource *rsc, uint32_t alignment)
263 {
264 struct pipe_resource *prsc = &rsc->base.b;
265 uint32_t level, size = 0;
266 uint32_t width = prsc->width0;
267 uint32_t height = prsc->height0;
268 uint32_t depth = prsc->depth0;
269 /* in layer_first layout, the level (slice) contains just one
270 * layer (since in fact the layer contains the slices)
271 */
272 uint32_t layers_in_level = rsc->layer_first ? 1 : prsc->array_size;
273
274 for (level = 0; level <= prsc->last_level; level++) {
275 struct fd_resource_slice *slice = fd_resource_slice(rsc, level);
276
277 slice->pitch = width = align(width, 32);
278 slice->offset = size;
279 /* 1d array and 2d array textures must all have the same layer size
280 * for each miplevel on a3xx. 3d textures can have different layer
281 * sizes for high levels, but the hw auto-sizer is buggy (or at least
282 * different than what this code does), so as soon as the layer size
283 * range gets into range, we stop reducing it.
284 */
285 if (prsc->target == PIPE_TEXTURE_3D && (
286 level == 1 ||
287 (level > 1 && rsc->slices[level - 1].size0 > 0xf000)))
288 slice->size0 = align(slice->pitch * height * rsc->cpp, alignment);
289 else if (level == 0 || rsc->layer_first || alignment == 1)
290 slice->size0 = align(slice->pitch * height * rsc->cpp, alignment);
291 else
292 slice->size0 = rsc->slices[level - 1].size0;
293
294 size += slice->size0 * depth * layers_in_level;
295
296 width = u_minify(width, 1);
297 height = u_minify(height, 1);
298 depth = u_minify(depth, 1);
299 }
300
301 return size;
302 }
303
304 static uint32_t
305 slice_alignment(struct pipe_screen *pscreen, const struct pipe_resource *tmpl)
306 {
307 /* on a3xx, 2d array and 3d textures seem to want their
308 * layers aligned to page boundaries:
309 */
310 switch (tmpl->target) {
311 case PIPE_TEXTURE_3D:
312 case PIPE_TEXTURE_1D_ARRAY:
313 case PIPE_TEXTURE_2D_ARRAY:
314 return 4096;
315 default:
316 return 1;
317 }
318 }
319
320 /**
321 * Create a new texture object, using the given template info.
322 */
323 static struct pipe_resource *
324 fd_resource_create(struct pipe_screen *pscreen,
325 const struct pipe_resource *tmpl)
326 {
327 struct fd_resource *rsc = CALLOC_STRUCT(fd_resource);
328 struct pipe_resource *prsc = &rsc->base.b;
329 uint32_t size;
330
331 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
332 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
333 tmpl->target, util_format_name(tmpl->format),
334 tmpl->width0, tmpl->height0, tmpl->depth0,
335 tmpl->array_size, tmpl->last_level, tmpl->nr_samples,
336 tmpl->usage, tmpl->bind, tmpl->flags);
337
338 if (!rsc)
339 return NULL;
340
341 *prsc = *tmpl;
342
343 pipe_reference_init(&prsc->reference, 1);
344 list_inithead(&rsc->list);
345 prsc->screen = pscreen;
346
347 util_range_init(&rsc->valid_buffer_range);
348
349 rsc->base.vtbl = &fd_resource_vtbl;
350 rsc->cpp = util_format_get_blocksize(tmpl->format);
351
352 assert(rsc->cpp);
353
354 if (is_a4xx(fd_screen(pscreen))) {
355 switch (tmpl->target) {
356 case PIPE_TEXTURE_3D:
357 /* TODO 3D_ARRAY? */
358 rsc->layer_first = false;
359 break;
360 default:
361 rsc->layer_first = true;
362 break;
363 }
364 }
365
366 size = setup_slices(rsc, slice_alignment(pscreen, tmpl));
367
368 if (rsc->layer_first) {
369 rsc->layer_size = align(size, 4096);
370 size = rsc->layer_size * prsc->array_size;
371 }
372
373 realloc_bo(rsc, size);
374 if (!rsc->bo)
375 goto fail;
376
377 return prsc;
378 fail:
379 fd_resource_destroy(pscreen, prsc);
380 return NULL;
381 }
382
383 /**
384 * Create a texture from a winsys_handle. The handle is often created in
385 * another process by first creating a pipe texture and then calling
386 * resource_get_handle.
387 */
388 static struct pipe_resource *
389 fd_resource_from_handle(struct pipe_screen *pscreen,
390 const struct pipe_resource *tmpl,
391 struct winsys_handle *handle)
392 {
393 struct fd_resource *rsc = CALLOC_STRUCT(fd_resource);
394 struct fd_resource_slice *slice = &rsc->slices[0];
395 struct pipe_resource *prsc = &rsc->base.b;
396
397 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
398 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
399 tmpl->target, util_format_name(tmpl->format),
400 tmpl->width0, tmpl->height0, tmpl->depth0,
401 tmpl->array_size, tmpl->last_level, tmpl->nr_samples,
402 tmpl->usage, tmpl->bind, tmpl->flags);
403
404 if (!rsc)
405 return NULL;
406
407 *prsc = *tmpl;
408
409 pipe_reference_init(&prsc->reference, 1);
410 list_inithead(&rsc->list);
411 prsc->screen = pscreen;
412
413 util_range_init(&rsc->valid_buffer_range);
414
415 rsc->bo = fd_screen_bo_from_handle(pscreen, handle, &slice->pitch);
416 if (!rsc->bo)
417 goto fail;
418
419 rsc->base.vtbl = &fd_resource_vtbl;
420 rsc->cpp = util_format_get_blocksize(tmpl->format);
421 slice->pitch /= rsc->cpp;
422
423 assert(rsc->cpp);
424
425 return prsc;
426
427 fail:
428 fd_resource_destroy(pscreen, prsc);
429 return NULL;
430 }
431
432 static void fd_blitter_pipe_begin(struct fd_context *ctx);
433 static void fd_blitter_pipe_end(struct fd_context *ctx);
434
435 /**
436 * _copy_region using pipe (3d engine)
437 */
438 static bool
439 fd_blitter_pipe_copy_region(struct fd_context *ctx,
440 struct pipe_resource *dst,
441 unsigned dst_level,
442 unsigned dstx, unsigned dsty, unsigned dstz,
443 struct pipe_resource *src,
444 unsigned src_level,
445 const struct pipe_box *src_box)
446 {
447 /* not until we allow rendertargets to be buffers */
448 if (dst->target == PIPE_BUFFER || src->target == PIPE_BUFFER)
449 return false;
450
451 if (!util_blitter_is_copy_supported(ctx->blitter, dst, src))
452 return false;
453
454 fd_blitter_pipe_begin(ctx);
455 util_blitter_copy_texture(ctx->blitter,
456 dst, dst_level, dstx, dsty, dstz,
457 src, src_level, src_box);
458 fd_blitter_pipe_end(ctx);
459
460 return true;
461 }
462
463 /**
464 * Copy a block of pixels from one resource to another.
465 * The resource must be of the same format.
466 * Resources with nr_samples > 1 are not allowed.
467 */
468 static void
469 fd_resource_copy_region(struct pipe_context *pctx,
470 struct pipe_resource *dst,
471 unsigned dst_level,
472 unsigned dstx, unsigned dsty, unsigned dstz,
473 struct pipe_resource *src,
474 unsigned src_level,
475 const struct pipe_box *src_box)
476 {
477 struct fd_context *ctx = fd_context(pctx);
478
479 /* TODO if we have 2d core, or other DMA engine that could be used
480 * for simple copies and reasonably easily synchronized with the 3d
481 * core, this is where we'd plug it in..
482 */
483
484 /* try blit on 3d pipe: */
485 if (fd_blitter_pipe_copy_region(ctx,
486 dst, dst_level, dstx, dsty, dstz,
487 src, src_level, src_box))
488 return;
489
490 /* else fallback to pure sw: */
491 util_resource_copy_region(pctx,
492 dst, dst_level, dstx, dsty, dstz,
493 src, src_level, src_box);
494 }
495
496 /**
497 * Optimal hardware path for blitting pixels.
498 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
499 */
500 static void
501 fd_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
502 {
503 struct fd_context *ctx = fd_context(pctx);
504 struct pipe_blit_info info = *blit_info;
505
506 if (info.src.resource->nr_samples > 1 &&
507 info.dst.resource->nr_samples <= 1 &&
508 !util_format_is_depth_or_stencil(info.src.resource->format) &&
509 !util_format_is_pure_integer(info.src.resource->format)) {
510 DBG("color resolve unimplemented");
511 return;
512 }
513
514 if (util_try_blit_via_copy_region(pctx, &info)) {
515 return; /* done */
516 }
517
518 if (info.mask & PIPE_MASK_S) {
519 DBG("cannot blit stencil, skipping");
520 info.mask &= ~PIPE_MASK_S;
521 }
522
523 if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
524 DBG("blit unsupported %s -> %s",
525 util_format_short_name(info.src.resource->format),
526 util_format_short_name(info.dst.resource->format));
527 return;
528 }
529
530 fd_blitter_pipe_begin(ctx);
531 util_blitter_blit(ctx->blitter, &info);
532 fd_blitter_pipe_end(ctx);
533 }
534
535 static void
536 fd_blitter_pipe_begin(struct fd_context *ctx)
537 {
538 util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vtx.vertexbuf.vb);
539 util_blitter_save_vertex_elements(ctx->blitter, ctx->vtx.vtx);
540 util_blitter_save_vertex_shader(ctx->blitter, ctx->prog.vp);
541 util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer);
542 util_blitter_save_viewport(ctx->blitter, &ctx->viewport);
543 util_blitter_save_scissor(ctx->blitter, &ctx->scissor);
544 util_blitter_save_fragment_shader(ctx->blitter, ctx->prog.fp);
545 util_blitter_save_blend(ctx->blitter, ctx->blend);
546 util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa);
547 util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref);
548 util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask);
549 util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer);
550 util_blitter_save_fragment_sampler_states(ctx->blitter,
551 ctx->fragtex.num_samplers,
552 (void **)ctx->fragtex.samplers);
553 util_blitter_save_fragment_sampler_views(ctx->blitter,
554 ctx->fragtex.num_textures, ctx->fragtex.textures);
555
556 fd_hw_query_set_stage(ctx, ctx->ring, FD_STAGE_BLIT);
557 }
558
559 static void
560 fd_blitter_pipe_end(struct fd_context *ctx)
561 {
562 fd_hw_query_set_stage(ctx, ctx->ring, FD_STAGE_NULL);
563 }
564
565 static void
566 fd_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
567 {
568 struct fd_resource *rsc = fd_resource(prsc);
569
570 if (rsc->dirty)
571 fd_context_render(pctx);
572 }
573
574 void
575 fd_resource_screen_init(struct pipe_screen *pscreen)
576 {
577 pscreen->resource_create = fd_resource_create;
578 pscreen->resource_from_handle = fd_resource_from_handle;
579 pscreen->resource_get_handle = u_resource_get_handle_vtbl;
580 pscreen->resource_destroy = u_resource_destroy_vtbl;
581 }
582
583 void
584 fd_resource_context_init(struct pipe_context *pctx)
585 {
586 pctx->transfer_map = u_transfer_map_vtbl;
587 pctx->transfer_flush_region = u_transfer_flush_region_vtbl;
588 pctx->transfer_unmap = u_transfer_unmap_vtbl;
589 pctx->transfer_inline_write = u_transfer_inline_write_vtbl;
590 pctx->create_surface = fd_create_surface;
591 pctx->surface_destroy = fd_surface_destroy;
592 pctx->resource_copy_region = fd_resource_copy_region;
593 pctx->blit = fd_blit;
594 pctx->flush_resource = fd_flush_resource;
595 }