freedreno: Add a6xx backend
[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_format_rgtc.h"
31 #include "util/u_format_zs.h"
32 #include "util/u_inlines.h"
33 #include "util/u_transfer.h"
34 #include "util/u_string.h"
35 #include "util/u_surface.h"
36 #include "util/set.h"
37
38 #include "freedreno_resource.h"
39 #include "freedreno_batch_cache.h"
40 #include "freedreno_screen.h"
41 #include "freedreno_surface.h"
42 #include "freedreno_context.h"
43 #include "freedreno_query_hw.h"
44 #include "freedreno_util.h"
45
46 #include <errno.h>
47
48 /* XXX this should go away, needed for 'struct winsys_handle' */
49 #include "state_tracker/drm_driver.h"
50
51 /**
52 * Go through the entire state and see if the resource is bound
53 * anywhere. If it is, mark the relevant state as dirty. This is
54 * called on realloc_bo to ensure the neccessary state is re-
55 * emitted so the GPU looks at the new backing bo.
56 */
57 static void
58 rebind_resource(struct fd_context *ctx, struct pipe_resource *prsc)
59 {
60 /* VBOs */
61 for (unsigned i = 0; i < ctx->vtx.vertexbuf.count && !(ctx->dirty & FD_DIRTY_VTXBUF); i++) {
62 if (ctx->vtx.vertexbuf.vb[i].buffer.resource == prsc)
63 ctx->dirty |= FD_DIRTY_VTXBUF;
64 }
65
66 /* per-shader-stage resources: */
67 for (unsigned stage = 0; stage < PIPE_SHADER_TYPES; stage++) {
68 /* Constbufs.. note that constbuf[0] is normal uniforms emitted in
69 * cmdstream rather than by pointer..
70 */
71 const unsigned num_ubos = util_last_bit(ctx->constbuf[stage].enabled_mask);
72 for (unsigned i = 1; i < num_ubos; i++) {
73 if (ctx->dirty_shader[stage] & FD_DIRTY_SHADER_CONST)
74 break;
75 if (ctx->constbuf[stage].cb[i].buffer == prsc)
76 ctx->dirty_shader[stage] |= FD_DIRTY_SHADER_CONST;
77 }
78
79 /* Textures */
80 for (unsigned i = 0; i < ctx->tex[stage].num_textures; i++) {
81 if (ctx->dirty_shader[stage] & FD_DIRTY_SHADER_TEX)
82 break;
83 if (ctx->tex[stage].textures[i] && (ctx->tex[stage].textures[i]->texture == prsc))
84 ctx->dirty_shader[stage] |= FD_DIRTY_SHADER_TEX;
85 }
86
87 /* SSBOs */
88 const unsigned num_ssbos = util_last_bit(ctx->shaderbuf[stage].enabled_mask);
89 for (unsigned i = 0; i < num_ssbos; i++) {
90 if (ctx->dirty_shader[stage] & FD_DIRTY_SHADER_SSBO)
91 break;
92 if (ctx->shaderbuf[stage].sb[i].buffer == prsc)
93 ctx->dirty_shader[stage] |= FD_DIRTY_SHADER_SSBO;
94 }
95 }
96 }
97
98 static void
99 realloc_bo(struct fd_resource *rsc, uint32_t size)
100 {
101 struct fd_screen *screen = fd_screen(rsc->base.screen);
102 uint32_t flags = DRM_FREEDRENO_GEM_CACHE_WCOMBINE |
103 DRM_FREEDRENO_GEM_TYPE_KMEM; /* TODO */
104
105 /* if we start using things other than write-combine,
106 * be sure to check for PIPE_RESOURCE_FLAG_MAP_COHERENT
107 */
108
109 if (rsc->bo)
110 fd_bo_del(rsc->bo);
111
112 rsc->bo = fd_bo_new(screen->dev, size, flags);
113 util_range_set_empty(&rsc->valid_buffer_range);
114 fd_bc_invalidate_resource(rsc, true);
115 }
116
117 static void
118 do_blit(struct fd_context *ctx, const struct pipe_blit_info *blit, bool fallback)
119 {
120 /* TODO size threshold too?? */
121 if (!fallback) {
122 /* do blit on gpu: */
123 fd_blitter_pipe_begin(ctx, false, true, FD_STAGE_BLIT);
124 ctx->blit(ctx, blit);
125 fd_blitter_pipe_end(ctx);
126 } else {
127 /* do blit on cpu: */
128 util_resource_copy_region(&ctx->base,
129 blit->dst.resource, blit->dst.level, blit->dst.box.x,
130 blit->dst.box.y, blit->dst.box.z,
131 blit->src.resource, blit->src.level, &blit->src.box);
132 }
133 }
134
135 static bool
136 fd_try_shadow_resource(struct fd_context *ctx, struct fd_resource *rsc,
137 unsigned level, const struct pipe_box *box)
138 {
139 struct pipe_context *pctx = &ctx->base;
140 struct pipe_resource *prsc = &rsc->base;
141 bool fallback = false;
142
143 if (prsc->next)
144 return false;
145
146 /* TODO: somehow munge dimensions and format to copy unsupported
147 * render target format to something that is supported?
148 */
149 if (!pctx->screen->is_format_supported(pctx->screen,
150 prsc->format, prsc->target, prsc->nr_samples,
151 prsc->nr_storage_samples,
152 PIPE_BIND_RENDER_TARGET))
153 fallback = true;
154
155 /* do shadowing back-blits on the cpu for buffers: */
156 if (prsc->target == PIPE_BUFFER)
157 fallback = true;
158
159 bool whole_level = util_texrange_covers_whole_level(prsc, level,
160 box->x, box->y, box->z, box->width, box->height, box->depth);
161
162 /* TODO need to be more clever about current level */
163 if ((prsc->target >= PIPE_TEXTURE_2D) && !whole_level)
164 return false;
165
166 struct pipe_resource *pshadow =
167 pctx->screen->resource_create(pctx->screen, prsc);
168
169 if (!pshadow)
170 return false;
171
172 assert(!ctx->in_shadow);
173 ctx->in_shadow = true;
174
175 /* get rid of any references that batch-cache might have to us (which
176 * should empty/destroy rsc->batches hashset)
177 */
178 fd_bc_invalidate_resource(rsc, false);
179
180 mtx_lock(&ctx->screen->lock);
181
182 /* Swap the backing bo's, so shadow becomes the old buffer,
183 * blit from shadow to new buffer. From here on out, we
184 * cannot fail.
185 *
186 * Note that we need to do it in this order, otherwise if
187 * we go down cpu blit path, the recursive transfer_map()
188 * sees the wrong status..
189 */
190 struct fd_resource *shadow = fd_resource(pshadow);
191
192 DBG("shadow: %p (%d) -> %p (%d)\n", rsc, rsc->base.reference.count,
193 shadow, shadow->base.reference.count);
194
195 /* TODO valid_buffer_range?? */
196 swap(rsc->bo, shadow->bo);
197 swap(rsc->write_batch, shadow->write_batch);
198
199 /* at this point, the newly created shadow buffer is not referenced
200 * by any batches, but the existing rsc (probably) is. We need to
201 * transfer those references over:
202 */
203 debug_assert(shadow->batch_mask == 0);
204 struct fd_batch *batch;
205 foreach_batch(batch, &ctx->screen->batch_cache, rsc->batch_mask) {
206 struct set_entry *entry = _mesa_set_search(batch->resources, rsc);
207 _mesa_set_remove(batch->resources, entry);
208 _mesa_set_add(batch->resources, shadow);
209 }
210 swap(rsc->batch_mask, shadow->batch_mask);
211
212 mtx_unlock(&ctx->screen->lock);
213
214 struct pipe_blit_info blit = {};
215 blit.dst.resource = prsc;
216 blit.dst.format = prsc->format;
217 blit.src.resource = pshadow;
218 blit.src.format = pshadow->format;
219 blit.mask = util_format_get_mask(prsc->format);
220 blit.filter = PIPE_TEX_FILTER_NEAREST;
221
222 #define set_box(field, val) do { \
223 blit.dst.field = (val); \
224 blit.src.field = (val); \
225 } while (0)
226
227 /* blit the other levels in their entirety: */
228 for (unsigned l = 0; l <= prsc->last_level; l++) {
229 if (l == level)
230 continue;
231
232 /* just blit whole level: */
233 set_box(level, l);
234 set_box(box.width, u_minify(prsc->width0, l));
235 set_box(box.height, u_minify(prsc->height0, l));
236 set_box(box.depth, u_minify(prsc->depth0, l));
237
238 do_blit(ctx, &blit, fallback);
239 }
240
241 /* deal w/ current level specially, since we might need to split
242 * it up into a couple blits:
243 */
244 if (!whole_level) {
245 set_box(level, level);
246
247 switch (prsc->target) {
248 case PIPE_BUFFER:
249 case PIPE_TEXTURE_1D:
250 set_box(box.y, 0);
251 set_box(box.z, 0);
252 set_box(box.height, 1);
253 set_box(box.depth, 1);
254
255 if (box->x > 0) {
256 set_box(box.x, 0);
257 set_box(box.width, box->x);
258
259 do_blit(ctx, &blit, fallback);
260 }
261 if ((box->x + box->width) < u_minify(prsc->width0, level)) {
262 set_box(box.x, box->x + box->width);
263 set_box(box.width, u_minify(prsc->width0, level) - (box->x + box->width));
264
265 do_blit(ctx, &blit, fallback);
266 }
267 break;
268 case PIPE_TEXTURE_2D:
269 /* TODO */
270 default:
271 unreachable("TODO");
272 }
273 }
274
275 ctx->in_shadow = false;
276
277 pipe_resource_reference(&pshadow, NULL);
278
279 return true;
280 }
281
282 static struct fd_resource *
283 fd_alloc_staging(struct fd_context *ctx, struct fd_resource *rsc,
284 unsigned level, const struct pipe_box *box)
285 {
286 struct pipe_context *pctx = &ctx->base;
287 struct pipe_resource tmpl = rsc->base;
288
289 tmpl.width0 = box->width;
290 tmpl.height0 = box->height;
291 tmpl.depth0 = box->depth;
292 tmpl.array_size = 1;
293 tmpl.last_level = 0;
294 tmpl.bind |= PIPE_BIND_LINEAR;
295
296 struct pipe_resource *pstaging =
297 pctx->screen->resource_create(pctx->screen, &tmpl);
298 if (!pstaging)
299 return NULL;
300
301 return fd_resource(pstaging);
302 }
303
304 static void
305 fd_blit_from_staging(struct fd_context *ctx, struct fd_transfer *trans)
306 {
307 struct pipe_resource *dst = trans->base.resource;
308 struct pipe_blit_info blit = {};
309
310 blit.dst.resource = dst;
311 blit.dst.format = dst->format;
312 blit.dst.level = trans->base.level;
313 blit.dst.box = trans->base.box;
314 blit.src.resource = trans->staging_prsc;
315 blit.src.format = trans->staging_prsc->format;
316 blit.src.level = 0;
317 blit.src.box = trans->staging_box;
318 blit.mask = util_format_get_mask(trans->staging_prsc->format);
319 blit.filter = PIPE_TEX_FILTER_NEAREST;
320
321 do_blit(ctx, &blit, false);
322 }
323
324 static void
325 fd_blit_to_staging(struct fd_context *ctx, struct fd_transfer *trans)
326 {
327 struct pipe_resource *src = trans->base.resource;
328 struct pipe_blit_info blit = {};
329
330 blit.src.resource = src;
331 blit.src.format = src->format;
332 blit.src.level = trans->base.level;
333 blit.src.box = trans->base.box;
334 blit.dst.resource = trans->staging_prsc;
335 blit.dst.format = trans->staging_prsc->format;
336 blit.dst.level = 0;
337 blit.dst.box = trans->staging_box;
338 blit.mask = util_format_get_mask(trans->staging_prsc->format);
339 blit.filter = PIPE_TEX_FILTER_NEAREST;
340
341 do_blit(ctx, &blit, false);
342 }
343
344 static unsigned
345 fd_resource_layer_offset(struct fd_resource *rsc,
346 struct fd_resource_slice *slice,
347 unsigned layer)
348 {
349 if (rsc->layer_first)
350 return layer * rsc->layer_size;
351 else
352 return layer * slice->size0;
353 }
354
355 static void fd_resource_transfer_flush_region(struct pipe_context *pctx,
356 struct pipe_transfer *ptrans,
357 const struct pipe_box *box)
358 {
359 struct fd_resource *rsc = fd_resource(ptrans->resource);
360
361 if (ptrans->resource->target == PIPE_BUFFER)
362 util_range_add(&rsc->valid_buffer_range,
363 ptrans->box.x + box->x,
364 ptrans->box.x + box->x + box->width);
365 }
366
367 static void
368 flush_resource(struct fd_context *ctx, struct fd_resource *rsc, unsigned usage)
369 {
370 struct fd_batch *write_batch = NULL;
371
372 fd_batch_reference(&write_batch, rsc->write_batch);
373
374 if (usage & PIPE_TRANSFER_WRITE) {
375 struct fd_batch *batch, *batches[32] = {};
376 uint32_t batch_mask;
377
378 /* This is a bit awkward, probably a fd_batch_flush_locked()
379 * would make things simpler.. but we need to hold the lock
380 * to iterate the batches which reference this resource. So
381 * we must first grab references under a lock, then flush.
382 */
383 mtx_lock(&ctx->screen->lock);
384 batch_mask = rsc->batch_mask;
385 foreach_batch(batch, &ctx->screen->batch_cache, batch_mask)
386 fd_batch_reference(&batches[batch->idx], batch);
387 mtx_unlock(&ctx->screen->lock);
388
389 foreach_batch(batch, &ctx->screen->batch_cache, batch_mask)
390 fd_batch_flush(batch, false, false);
391
392 foreach_batch(batch, &ctx->screen->batch_cache, batch_mask) {
393 fd_batch_sync(batch);
394 fd_batch_reference(&batches[batch->idx], NULL);
395 }
396 assert(rsc->batch_mask == 0);
397 } else if (write_batch) {
398 fd_batch_flush(write_batch, true, false);
399 }
400
401 fd_batch_reference(&write_batch, NULL);
402
403 assert(!rsc->write_batch);
404 }
405
406 static void
407 fd_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
408 {
409 flush_resource(fd_context(pctx), fd_resource(prsc), PIPE_TRANSFER_READ);
410 }
411
412 static void
413 fd_resource_transfer_unmap(struct pipe_context *pctx,
414 struct pipe_transfer *ptrans)
415 {
416 struct fd_context *ctx = fd_context(pctx);
417 struct fd_resource *rsc = fd_resource(ptrans->resource);
418 struct fd_transfer *trans = fd_transfer(ptrans);
419
420 if (trans->staging_prsc) {
421 if (ptrans->usage & PIPE_TRANSFER_WRITE)
422 fd_blit_from_staging(ctx, trans);
423 pipe_resource_reference(&trans->staging_prsc, NULL);
424 }
425
426 if (!(ptrans->usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
427 fd_bo_cpu_fini(rsc->bo);
428 }
429
430 util_range_add(&rsc->valid_buffer_range,
431 ptrans->box.x,
432 ptrans->box.x + ptrans->box.width);
433
434 pipe_resource_reference(&ptrans->resource, NULL);
435 slab_free(&ctx->transfer_pool, ptrans);
436 }
437
438 static void *
439 fd_resource_transfer_map(struct pipe_context *pctx,
440 struct pipe_resource *prsc,
441 unsigned level, unsigned usage,
442 const struct pipe_box *box,
443 struct pipe_transfer **pptrans)
444 {
445 struct fd_context *ctx = fd_context(pctx);
446 struct fd_resource *rsc = fd_resource(prsc);
447 struct fd_resource_slice *slice = fd_resource_slice(rsc, level);
448 struct fd_transfer *trans;
449 struct pipe_transfer *ptrans;
450 enum pipe_format format = prsc->format;
451 uint32_t op = 0;
452 uint32_t offset;
453 char *buf;
454 int ret = 0;
455
456 DBG("prsc=%p, level=%u, usage=%x, box=%dx%d+%d,%d", prsc, level, usage,
457 box->width, box->height, box->x, box->y);
458
459 ptrans = slab_alloc(&ctx->transfer_pool);
460 if (!ptrans)
461 return NULL;
462
463 /* slab_alloc_st() doesn't zero: */
464 trans = fd_transfer(ptrans);
465 memset(trans, 0, sizeof(*trans));
466
467 pipe_resource_reference(&ptrans->resource, prsc);
468 ptrans->level = level;
469 ptrans->usage = usage;
470 ptrans->box = *box;
471 ptrans->stride = util_format_get_nblocksx(format, slice->pitch) * rsc->cpp;
472 ptrans->layer_stride = rsc->layer_first ? rsc->layer_size : slice->size0;
473
474 /* we always need a staging texture for tiled buffers:
475 *
476 * TODO we might sometimes want to *also* shadow the resource to avoid
477 * splitting a batch.. for ex, mid-frame texture uploads to a tiled
478 * texture.
479 */
480 if (rsc->tile_mode) {
481 struct fd_resource *staging_rsc;
482
483 staging_rsc = fd_alloc_staging(ctx, rsc, level, box);
484 if (staging_rsc) {
485 // TODO for PIPE_TRANSFER_READ, need to do untiling blit..
486 trans->staging_prsc = &staging_rsc->base;
487 trans->base.stride = util_format_get_nblocksx(format,
488 staging_rsc->slices[0].pitch) * staging_rsc->cpp;
489 trans->base.layer_stride = staging_rsc->layer_first ?
490 staging_rsc->layer_size : staging_rsc->slices[0].size0;
491 trans->staging_box = *box;
492 trans->staging_box.x = 0;
493 trans->staging_box.y = 0;
494 trans->staging_box.z = 0;
495
496 if (usage & PIPE_TRANSFER_READ) {
497 fd_blit_to_staging(ctx, trans);
498 fd_bo_cpu_prep(rsc->bo, ctx->pipe, DRM_FREEDRENO_PREP_READ);
499 }
500
501 buf = fd_bo_map(staging_rsc->bo);
502 offset = 0;
503
504 *pptrans = ptrans;
505
506 ctx->stats.staging_uploads++;
507
508 return buf;
509 }
510 }
511
512 if (ctx->in_shadow && !(usage & PIPE_TRANSFER_READ))
513 usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
514
515 if (usage & PIPE_TRANSFER_READ)
516 op |= DRM_FREEDRENO_PREP_READ;
517
518 if (usage & PIPE_TRANSFER_WRITE)
519 op |= DRM_FREEDRENO_PREP_WRITE;
520
521 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
522 realloc_bo(rsc, fd_bo_size(rsc->bo));
523 rebind_resource(ctx, prsc);
524 } else if ((usage & PIPE_TRANSFER_WRITE) &&
525 prsc->target == PIPE_BUFFER &&
526 !util_ranges_intersect(&rsc->valid_buffer_range,
527 box->x, box->x + box->width)) {
528 /* We are trying to write to a previously uninitialized range. No need
529 * to wait.
530 */
531 } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
532 struct fd_batch *write_batch = NULL;
533
534 /* hold a reference, so it doesn't disappear under us: */
535 fd_batch_reference(&write_batch, rsc->write_batch);
536
537 if ((usage & PIPE_TRANSFER_WRITE) && write_batch &&
538 write_batch->back_blit) {
539 /* if only thing pending is a back-blit, we can discard it: */
540 fd_batch_reset(write_batch);
541 }
542
543 /* If the GPU is writing to the resource, or if it is reading from the
544 * resource and we're trying to write to it, flush the renders.
545 */
546 bool needs_flush = pending(rsc, !!(usage & PIPE_TRANSFER_WRITE));
547 bool busy = needs_flush || (0 != fd_bo_cpu_prep(rsc->bo,
548 ctx->pipe, op | DRM_FREEDRENO_PREP_NOSYNC));
549
550 /* if we need to flush/stall, see if we can make a shadow buffer
551 * to avoid this:
552 *
553 * TODO we could go down this path !reorder && !busy_for_read
554 * ie. we only *don't* want to go down this path if the blit
555 * will trigger a flush!
556 */
557 if (ctx->screen->reorder && busy && !(usage & PIPE_TRANSFER_READ) &&
558 (usage & PIPE_TRANSFER_DISCARD_RANGE)) {
559 /* try shadowing only if it avoids a flush, otherwise staging would
560 * be better:
561 */
562 if (needs_flush && fd_try_shadow_resource(ctx, rsc, level, box)) {
563 needs_flush = busy = false;
564 rebind_resource(ctx, prsc);
565 ctx->stats.shadow_uploads++;
566 } else {
567 struct fd_resource *staging_rsc;
568
569 if (needs_flush) {
570 flush_resource(ctx, rsc, usage);
571 needs_flush = false;
572 }
573
574 /* in this case, we don't need to shadow the whole resource,
575 * since any draw that references the previous contents has
576 * already had rendering flushed for all tiles. So we can
577 * use a staging buffer to do the upload.
578 */
579 staging_rsc = fd_alloc_staging(ctx, rsc, level, box);
580 if (staging_rsc) {
581 trans->staging_prsc = &staging_rsc->base;
582 trans->base.stride = util_format_get_nblocksx(format,
583 staging_rsc->slices[0].pitch) * staging_rsc->cpp;
584 trans->base.layer_stride = staging_rsc->layer_first ?
585 staging_rsc->layer_size : staging_rsc->slices[0].size0;
586 trans->staging_box = *box;
587 trans->staging_box.x = 0;
588 trans->staging_box.y = 0;
589 trans->staging_box.z = 0;
590 buf = fd_bo_map(staging_rsc->bo);
591 offset = 0;
592
593 *pptrans = ptrans;
594
595 fd_batch_reference(&write_batch, NULL);
596
597 ctx->stats.staging_uploads++;
598
599 return buf;
600 }
601 }
602 }
603
604 if (needs_flush) {
605 flush_resource(ctx, rsc, usage);
606 needs_flush = false;
607 }
608
609 fd_batch_reference(&write_batch, NULL);
610
611 /* The GPU keeps track of how the various bo's are being used, and
612 * will wait if necessary for the proper operation to have
613 * completed.
614 */
615 if (busy) {
616 ret = fd_bo_cpu_prep(rsc->bo, ctx->pipe, op);
617 if (ret)
618 goto fail;
619 }
620 }
621
622 buf = fd_bo_map(rsc->bo);
623 offset = slice->offset +
624 box->y / util_format_get_blockheight(format) * ptrans->stride +
625 box->x / util_format_get_blockwidth(format) * rsc->cpp +
626 fd_resource_layer_offset(rsc, slice, box->z);
627
628 if (usage & PIPE_TRANSFER_WRITE)
629 rsc->valid = true;
630
631 *pptrans = ptrans;
632
633 return buf + offset;
634
635 fail:
636 fd_resource_transfer_unmap(pctx, ptrans);
637 return NULL;
638 }
639
640 static void
641 fd_resource_destroy(struct pipe_screen *pscreen,
642 struct pipe_resource *prsc)
643 {
644 struct fd_resource *rsc = fd_resource(prsc);
645 fd_bc_invalidate_resource(rsc, true);
646 if (rsc->bo)
647 fd_bo_del(rsc->bo);
648 util_range_destroy(&rsc->valid_buffer_range);
649 FREE(rsc);
650 }
651
652 static boolean
653 fd_resource_get_handle(struct pipe_screen *pscreen,
654 struct pipe_context *pctx,
655 struct pipe_resource *prsc,
656 struct winsys_handle *handle,
657 unsigned usage)
658 {
659 struct fd_resource *rsc = fd_resource(prsc);
660
661 return fd_screen_bo_get_handle(pscreen, rsc->bo,
662 rsc->slices[0].pitch * rsc->cpp, handle);
663 }
664
665 static uint32_t
666 setup_slices(struct fd_resource *rsc, uint32_t alignment, enum pipe_format format)
667 {
668 struct pipe_resource *prsc = &rsc->base;
669 struct fd_screen *screen = fd_screen(prsc->screen);
670 enum util_format_layout layout = util_format_description(format)->layout;
671 uint32_t pitchalign = screen->gmem_alignw;
672 uint32_t level, size = 0;
673 uint32_t width = prsc->width0;
674 uint32_t height = prsc->height0;
675 uint32_t depth = prsc->depth0;
676 /* in layer_first layout, the level (slice) contains just one
677 * layer (since in fact the layer contains the slices)
678 */
679 uint32_t layers_in_level = rsc->layer_first ? 1 : prsc->array_size;
680
681 for (level = 0; level <= prsc->last_level; level++) {
682 struct fd_resource_slice *slice = fd_resource_slice(rsc, level);
683 uint32_t blocks;
684
685 if (layout == UTIL_FORMAT_LAYOUT_ASTC)
686 slice->pitch = width =
687 util_align_npot(width, pitchalign * util_format_get_blockwidth(format));
688 else
689 slice->pitch = width = align(width, pitchalign);
690 slice->offset = size;
691 blocks = util_format_get_nblocks(format, width, height);
692 /* 1d array and 2d array textures must all have the same layer size
693 * for each miplevel on a3xx. 3d textures can have different layer
694 * sizes for high levels, but the hw auto-sizer is buggy (or at least
695 * different than what this code does), so as soon as the layer size
696 * range gets into range, we stop reducing it.
697 */
698 if (prsc->target == PIPE_TEXTURE_3D && (
699 level == 1 ||
700 (level > 1 && rsc->slices[level - 1].size0 > 0xf000)))
701 slice->size0 = align(blocks * rsc->cpp, alignment);
702 else if (level == 0 || rsc->layer_first || alignment == 1)
703 slice->size0 = align(blocks * rsc->cpp, alignment);
704 else
705 slice->size0 = rsc->slices[level - 1].size0;
706
707 size += slice->size0 * depth * layers_in_level;
708
709 width = u_minify(width, 1);
710 height = u_minify(height, 1);
711 depth = u_minify(depth, 1);
712 }
713
714 return size;
715 }
716
717 static uint32_t
718 slice_alignment(enum pipe_texture_target target)
719 {
720 /* on a3xx, 2d array and 3d textures seem to want their
721 * layers aligned to page boundaries:
722 */
723 switch (target) {
724 case PIPE_TEXTURE_3D:
725 case PIPE_TEXTURE_1D_ARRAY:
726 case PIPE_TEXTURE_2D_ARRAY:
727 return 4096;
728 default:
729 return 1;
730 }
731 }
732
733 /* cross generation texture layout to plug in to screen->setup_slices()..
734 * replace with generation specific one as-needed.
735 *
736 * TODO for a4xx probably can extract out the a4xx specific logic int
737 * a small fd4_setup_slices() wrapper that sets up layer_first, and then
738 * calls this.
739 */
740 uint32_t
741 fd_setup_slices(struct fd_resource *rsc)
742 {
743 uint32_t alignment;
744
745 alignment = slice_alignment(rsc->base.target);
746
747 struct fd_screen *screen = fd_screen(rsc->base.screen);
748 if (is_a4xx(screen)) {
749 switch (rsc->base.target) {
750 case PIPE_TEXTURE_3D:
751 rsc->layer_first = false;
752 break;
753 default:
754 rsc->layer_first = true;
755 alignment = 1;
756 break;
757 }
758 }
759
760 return setup_slices(rsc, alignment, rsc->base.format);
761 }
762
763 /* special case to resize query buf after allocated.. */
764 void
765 fd_resource_resize(struct pipe_resource *prsc, uint32_t sz)
766 {
767 struct fd_resource *rsc = fd_resource(prsc);
768
769 debug_assert(prsc->width0 == 0);
770 debug_assert(prsc->target == PIPE_BUFFER);
771 debug_assert(prsc->bind == PIPE_BIND_QUERY_BUFFER);
772
773 prsc->width0 = sz;
774 realloc_bo(rsc, fd_screen(prsc->screen)->setup_slices(rsc));
775 }
776
777 // TODO common helper?
778 static bool
779 has_depth(enum pipe_format format)
780 {
781 switch (format) {
782 case PIPE_FORMAT_Z16_UNORM:
783 case PIPE_FORMAT_Z32_UNORM:
784 case PIPE_FORMAT_Z32_FLOAT:
785 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
786 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
787 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
788 case PIPE_FORMAT_Z24X8_UNORM:
789 case PIPE_FORMAT_X8Z24_UNORM:
790 return true;
791 default:
792 return false;
793 }
794 }
795
796 /**
797 * Create a new texture object, using the given template info.
798 */
799 static struct pipe_resource *
800 fd_resource_create(struct pipe_screen *pscreen,
801 const struct pipe_resource *tmpl)
802 {
803 struct fd_screen *screen = fd_screen(pscreen);
804 struct fd_resource *rsc = CALLOC_STRUCT(fd_resource);
805 struct pipe_resource *prsc = &rsc->base;
806 enum pipe_format format = tmpl->format;
807 uint32_t size;
808
809 DBG("%p: target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
810 "nr_samples=%u, usage=%u, bind=%x, flags=%x", prsc,
811 tmpl->target, util_format_name(format),
812 tmpl->width0, tmpl->height0, tmpl->depth0,
813 tmpl->array_size, tmpl->last_level, tmpl->nr_samples,
814 tmpl->usage, tmpl->bind, tmpl->flags);
815
816 if (!rsc)
817 return NULL;
818
819 *prsc = *tmpl;
820
821 #define LINEAR \
822 (PIPE_BIND_SCANOUT | \
823 PIPE_BIND_LINEAR | \
824 PIPE_BIND_DISPLAY_TARGET)
825
826 if (screen->tile_mode &&
827 (tmpl->target != PIPE_BUFFER) &&
828 (tmpl->bind & PIPE_BIND_SAMPLER_VIEW) &&
829 !(tmpl->bind & LINEAR)) {
830 rsc->tile_mode = screen->tile_mode(tmpl);
831 }
832
833 pipe_reference_init(&prsc->reference, 1);
834
835 prsc->screen = pscreen;
836
837 util_range_init(&rsc->valid_buffer_range);
838
839 rsc->internal_format = format;
840 rsc->cpp = util_format_get_blocksize(format);
841 prsc->nr_samples = MAX2(1, prsc->nr_samples);
842 rsc->cpp *= prsc->nr_samples;
843
844 assert(rsc->cpp);
845
846 // XXX probably need some extra work if we hit rsc shadowing path w/ lrz..
847 if ((is_a5xx(screen) || is_a6xx(screen)) &&
848 (fd_mesa_debug & FD_DBG_LRZ) && has_depth(format)) {
849 const uint32_t flags = DRM_FREEDRENO_GEM_CACHE_WCOMBINE |
850 DRM_FREEDRENO_GEM_TYPE_KMEM; /* TODO */
851 unsigned lrz_pitch = align(DIV_ROUND_UP(tmpl->width0, 8), 32);
852 unsigned lrz_height = DIV_ROUND_UP(tmpl->height0, 8);
853 unsigned size = lrz_pitch * lrz_height * 2;
854
855 size += 0x1000; /* for GRAS_LRZ_FAST_CLEAR_BUFFER */
856
857 rsc->lrz_height = lrz_height;
858 rsc->lrz_width = lrz_pitch;
859 rsc->lrz_pitch = lrz_pitch;
860 rsc->lrz = fd_bo_new(screen->dev, size, flags);
861 }
862
863 size = screen->setup_slices(rsc);
864
865 /* special case for hw-query buffer, which we need to allocate before we
866 * know the size:
867 */
868 if (size == 0) {
869 /* note, semi-intention == instead of & */
870 debug_assert(prsc->bind == PIPE_BIND_QUERY_BUFFER);
871 return prsc;
872 }
873
874 if (rsc->layer_first) {
875 rsc->layer_size = align(size, 4096);
876 size = rsc->layer_size * prsc->array_size;
877 }
878
879 realloc_bo(rsc, size);
880 if (!rsc->bo)
881 goto fail;
882
883 return prsc;
884 fail:
885 fd_resource_destroy(pscreen, prsc);
886 return NULL;
887 }
888
889 /**
890 * Create a texture from a winsys_handle. The handle is often created in
891 * another process by first creating a pipe texture and then calling
892 * resource_get_handle.
893 */
894 static struct pipe_resource *
895 fd_resource_from_handle(struct pipe_screen *pscreen,
896 const struct pipe_resource *tmpl,
897 struct winsys_handle *handle, unsigned usage)
898 {
899 struct fd_resource *rsc = CALLOC_STRUCT(fd_resource);
900 struct fd_resource_slice *slice = &rsc->slices[0];
901 struct pipe_resource *prsc = &rsc->base;
902 uint32_t pitchalign = fd_screen(pscreen)->gmem_alignw;
903
904 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
905 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
906 tmpl->target, util_format_name(tmpl->format),
907 tmpl->width0, tmpl->height0, tmpl->depth0,
908 tmpl->array_size, tmpl->last_level, tmpl->nr_samples,
909 tmpl->usage, tmpl->bind, tmpl->flags);
910
911 if (!rsc)
912 return NULL;
913
914 *prsc = *tmpl;
915
916 pipe_reference_init(&prsc->reference, 1);
917
918 prsc->screen = pscreen;
919
920 util_range_init(&rsc->valid_buffer_range);
921
922 rsc->bo = fd_screen_bo_from_handle(pscreen, handle);
923 if (!rsc->bo)
924 goto fail;
925
926 prsc->nr_samples = MAX2(1, prsc->nr_samples);
927 rsc->internal_format = tmpl->format;
928 rsc->cpp = prsc->nr_samples * util_format_get_blocksize(tmpl->format);
929 slice->pitch = handle->stride / rsc->cpp;
930 slice->offset = handle->offset;
931 slice->size0 = handle->stride * prsc->height0;
932
933 if ((slice->pitch < align(prsc->width0, pitchalign)) ||
934 (slice->pitch & (pitchalign - 1)))
935 goto fail;
936
937 assert(rsc->cpp);
938
939 return prsc;
940
941 fail:
942 fd_resource_destroy(pscreen, prsc);
943 return NULL;
944 }
945
946 /**
947 * _copy_region using pipe (3d engine)
948 */
949 static bool
950 fd_blitter_pipe_copy_region(struct fd_context *ctx,
951 struct pipe_resource *dst,
952 unsigned dst_level,
953 unsigned dstx, unsigned dsty, unsigned dstz,
954 struct pipe_resource *src,
955 unsigned src_level,
956 const struct pipe_box *src_box)
957 {
958 /* not until we allow rendertargets to be buffers */
959 if (dst->target == PIPE_BUFFER || src->target == PIPE_BUFFER)
960 return false;
961
962 if (!util_blitter_is_copy_supported(ctx->blitter, dst, src))
963 return false;
964
965 /* TODO we could discard if dst box covers dst level fully.. */
966 fd_blitter_pipe_begin(ctx, false, false, FD_STAGE_BLIT);
967 util_blitter_copy_texture(ctx->blitter,
968 dst, dst_level, dstx, dsty, dstz,
969 src, src_level, src_box);
970 fd_blitter_pipe_end(ctx);
971
972 return true;
973 }
974
975 /**
976 * Copy a block of pixels from one resource to another.
977 * The resource must be of the same format.
978 * Resources with nr_samples > 1 are not allowed.
979 */
980 static void
981 fd_resource_copy_region(struct pipe_context *pctx,
982 struct pipe_resource *dst,
983 unsigned dst_level,
984 unsigned dstx, unsigned dsty, unsigned dstz,
985 struct pipe_resource *src,
986 unsigned src_level,
987 const struct pipe_box *src_box)
988 {
989 struct fd_context *ctx = fd_context(pctx);
990
991 /* TODO if we have 2d core, or other DMA engine that could be used
992 * for simple copies and reasonably easily synchronized with the 3d
993 * core, this is where we'd plug it in..
994 */
995
996 /* try blit on 3d pipe: */
997 if (fd_blitter_pipe_copy_region(ctx,
998 dst, dst_level, dstx, dsty, dstz,
999 src, src_level, src_box))
1000 return;
1001
1002 /* else fallback to pure sw: */
1003 util_resource_copy_region(pctx,
1004 dst, dst_level, dstx, dsty, dstz,
1005 src, src_level, src_box);
1006 }
1007
1008 bool
1009 fd_render_condition_check(struct pipe_context *pctx)
1010 {
1011 struct fd_context *ctx = fd_context(pctx);
1012
1013 if (!ctx->cond_query)
1014 return true;
1015
1016 union pipe_query_result res = { 0 };
1017 bool wait =
1018 ctx->cond_mode != PIPE_RENDER_COND_NO_WAIT &&
1019 ctx->cond_mode != PIPE_RENDER_COND_BY_REGION_NO_WAIT;
1020
1021 if (pctx->get_query_result(pctx, ctx->cond_query, wait, &res))
1022 return (bool)res.u64 != ctx->cond_cond;
1023
1024 return true;
1025 }
1026
1027 /**
1028 * Optimal hardware path for blitting pixels.
1029 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
1030 */
1031 static void
1032 fd_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
1033 {
1034 struct fd_context *ctx = fd_context(pctx);
1035 struct pipe_blit_info info = *blit_info;
1036 bool discard = false;
1037
1038 if (info.render_condition_enable && !fd_render_condition_check(pctx))
1039 return;
1040
1041 if (!info.scissor_enable && !info.alpha_blend) {
1042 discard = util_texrange_covers_whole_level(info.dst.resource,
1043 info.dst.level, info.dst.box.x, info.dst.box.y,
1044 info.dst.box.z, info.dst.box.width,
1045 info.dst.box.height, info.dst.box.depth);
1046 }
1047
1048 if (util_try_blit_via_copy_region(pctx, &info)) {
1049 return; /* done */
1050 }
1051
1052 if (info.mask & PIPE_MASK_S) {
1053 DBG("cannot blit stencil, skipping");
1054 info.mask &= ~PIPE_MASK_S;
1055 }
1056
1057 if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
1058 DBG("blit unsupported %s -> %s",
1059 util_format_short_name(info.src.resource->format),
1060 util_format_short_name(info.dst.resource->format));
1061 return;
1062 }
1063
1064 fd_blitter_pipe_begin(ctx, info.render_condition_enable, discard, FD_STAGE_BLIT);
1065 ctx->blit(ctx, &info);
1066 fd_blitter_pipe_end(ctx);
1067 }
1068
1069 void
1070 fd_blitter_pipe_begin(struct fd_context *ctx, bool render_cond, bool discard,
1071 enum fd_render_stage stage)
1072 {
1073 util_blitter_save_fragment_constant_buffer_slot(ctx->blitter,
1074 ctx->constbuf[PIPE_SHADER_FRAGMENT].cb);
1075 util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vtx.vertexbuf.vb);
1076 util_blitter_save_vertex_elements(ctx->blitter, ctx->vtx.vtx);
1077 util_blitter_save_vertex_shader(ctx->blitter, ctx->prog.vp);
1078 util_blitter_save_so_targets(ctx->blitter, ctx->streamout.num_targets,
1079 ctx->streamout.targets);
1080 util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer);
1081 util_blitter_save_viewport(ctx->blitter, &ctx->viewport);
1082 util_blitter_save_scissor(ctx->blitter, &ctx->scissor);
1083 util_blitter_save_fragment_shader(ctx->blitter, ctx->prog.fp);
1084 util_blitter_save_blend(ctx->blitter, ctx->blend);
1085 util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa);
1086 util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref);
1087 util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask);
1088 util_blitter_save_framebuffer(ctx->blitter,
1089 ctx->batch ? &ctx->batch->framebuffer : NULL);
1090 util_blitter_save_fragment_sampler_states(ctx->blitter,
1091 ctx->tex[PIPE_SHADER_FRAGMENT].num_samplers,
1092 (void **)ctx->tex[PIPE_SHADER_FRAGMENT].samplers);
1093 util_blitter_save_fragment_sampler_views(ctx->blitter,
1094 ctx->tex[PIPE_SHADER_FRAGMENT].num_textures,
1095 ctx->tex[PIPE_SHADER_FRAGMENT].textures);
1096 if (!render_cond)
1097 util_blitter_save_render_condition(ctx->blitter,
1098 ctx->cond_query, ctx->cond_cond, ctx->cond_mode);
1099
1100 if (ctx->batch)
1101 fd_batch_set_stage(ctx->batch, stage);
1102
1103 ctx->in_blit = discard;
1104 }
1105
1106 void
1107 fd_blitter_pipe_end(struct fd_context *ctx)
1108 {
1109 if (ctx->batch)
1110 fd_batch_set_stage(ctx->batch, FD_STAGE_NULL);
1111 ctx->in_blit = false;
1112 }
1113
1114 static void
1115 fd_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
1116 {
1117 struct fd_resource *rsc = fd_resource(prsc);
1118
1119 /*
1120 * TODO I guess we could track that the resource is invalidated and
1121 * use that as a hint to realloc rather than stall in _transfer_map(),
1122 * even in the non-DISCARD_WHOLE_RESOURCE case?
1123 */
1124
1125 if (rsc->write_batch) {
1126 struct fd_batch *batch = rsc->write_batch;
1127 struct pipe_framebuffer_state *pfb = &batch->framebuffer;
1128
1129 if (pfb->zsbuf && pfb->zsbuf->texture == prsc)
1130 batch->resolve &= ~(FD_BUFFER_DEPTH | FD_BUFFER_STENCIL);
1131
1132 for (unsigned i = 0; i < pfb->nr_cbufs; i++) {
1133 if (pfb->cbufs[i] && pfb->cbufs[i]->texture == prsc) {
1134 batch->resolve &= ~(PIPE_CLEAR_COLOR0 << i);
1135 }
1136 }
1137 }
1138
1139 rsc->valid = false;
1140 }
1141
1142 static enum pipe_format
1143 fd_resource_get_internal_format(struct pipe_resource *prsc)
1144 {
1145 return fd_resource(prsc)->internal_format;
1146 }
1147
1148 static void
1149 fd_resource_set_stencil(struct pipe_resource *prsc,
1150 struct pipe_resource *stencil)
1151 {
1152 fd_resource(prsc)->stencil = fd_resource(stencil);
1153 }
1154
1155 static struct pipe_resource *
1156 fd_resource_get_stencil(struct pipe_resource *prsc)
1157 {
1158 struct fd_resource *rsc = fd_resource(prsc);
1159 if (rsc->stencil)
1160 return &rsc->stencil->base;
1161 return NULL;
1162 }
1163
1164 static const struct u_transfer_vtbl transfer_vtbl = {
1165 .resource_create = fd_resource_create,
1166 .resource_destroy = fd_resource_destroy,
1167 .transfer_map = fd_resource_transfer_map,
1168 .transfer_flush_region = fd_resource_transfer_flush_region,
1169 .transfer_unmap = fd_resource_transfer_unmap,
1170 .get_internal_format = fd_resource_get_internal_format,
1171 .set_stencil = fd_resource_set_stencil,
1172 .get_stencil = fd_resource_get_stencil,
1173 };
1174
1175 void
1176 fd_resource_screen_init(struct pipe_screen *pscreen)
1177 {
1178 struct fd_screen *screen = fd_screen(pscreen);
1179 bool fake_rgtc = screen->gpu_id < 400;
1180
1181 pscreen->resource_create = u_transfer_helper_resource_create;
1182 pscreen->resource_from_handle = fd_resource_from_handle;
1183 pscreen->resource_get_handle = fd_resource_get_handle;
1184 pscreen->resource_destroy = u_transfer_helper_resource_destroy;
1185
1186 pscreen->transfer_helper = u_transfer_helper_create(&transfer_vtbl,
1187 true, fake_rgtc, true);
1188
1189 if (!screen->setup_slices)
1190 screen->setup_slices = fd_setup_slices;
1191 }
1192
1193 void
1194 fd_resource_context_init(struct pipe_context *pctx)
1195 {
1196 pctx->transfer_map = u_transfer_helper_transfer_map;
1197 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
1198 pctx->transfer_unmap = u_transfer_helper_transfer_unmap;
1199 pctx->buffer_subdata = u_default_buffer_subdata;
1200 pctx->texture_subdata = u_default_texture_subdata;
1201 pctx->create_surface = fd_create_surface;
1202 pctx->surface_destroy = fd_surface_destroy;
1203 pctx->resource_copy_region = fd_resource_copy_region;
1204 pctx->blit = fd_blit;
1205 pctx->flush_resource = fd_flush_resource;
1206 pctx->invalidate_resource = fd_invalidate_resource;
1207 }