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