v3d_resource.c 35.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright © 2014-2017 Broadcom
 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

25
#include "pipe/p_defines.h"
26 27 28 29 30
#include "util/u_blit.h"
#include "util/u_memory.h"
#include "util/u_format.h"
#include "util/u_inlines.h"
#include "util/u_surface.h"
31
#include "util/u_transfer_helper.h"
32
#include "util/u_upload_mgr.h"
33
#include "util/u_format_zs.h"
34 35

#include "drm_fourcc.h"
36 37 38 39
#include "v3d_screen.h"
#include "v3d_context.h"
#include "v3d_resource.h"
#include "v3d_tiling.h"
40 41
#include "broadcom/cle/v3d_packet_v33_pack.h"

42
static void
43
v3d_debug_resource_layout(struct v3d_resource *rsc, const char *caller)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
{
        if (!(V3D_DEBUG & V3D_DEBUG_SURFACE))
                return;

        struct pipe_resource *prsc = &rsc->base;

        if (prsc->target == PIPE_BUFFER) {
                fprintf(stderr,
                        "rsc %s %p (format %s), %dx%d buffer @0x%08x-0x%08x\n",
                        caller, rsc,
                        util_format_short_name(prsc->format),
                        prsc->width0, prsc->height0,
                        rsc->bo->offset,
                        rsc->bo->offset + rsc->bo->size - 1);
                return;
        }

        static const char *const tiling_descriptions[] = {
                [VC5_TILING_RASTER] = "R",
                [VC5_TILING_LINEARTILE] = "LT",
                [VC5_TILING_UBLINEAR_1_COLUMN] = "UB1",
                [VC5_TILING_UBLINEAR_2_COLUMN] = "UB2",
                [VC5_TILING_UIF_NO_XOR] = "UIF",
                [VC5_TILING_UIF_XOR] = "UIF^",
        };

        for (int i = 0; i <= prsc->last_level; i++) {
71
                struct v3d_resource_slice *slice = &rsc->slices[i];
72 73

                int level_width = slice->stride / rsc->cpp;
74
                int level_height = slice->padded_height;
75 76
                int level_depth =
                        u_minify(util_next_power_of_two(prsc->depth0), i);
77 78 79

                fprintf(stderr,
                        "rsc %s %p (format %s), %dx%d: "
80
                        "level %d (%s) %dx%dx%d -> %dx%dx%d, stride %d@0x%08x\n",
81 82 83 84 85 86
                        caller, rsc,
                        util_format_short_name(prsc->format),
                        prsc->width0, prsc->height0,
                        i, tiling_descriptions[slice->tiling],
                        u_minify(prsc->width0, i),
                        u_minify(prsc->height0, i),
87
                        u_minify(prsc->depth0, i),
88 89
                        level_width,
                        level_height,
90
                        level_depth,
91 92 93 94 95
                        slice->stride,
                        rsc->bo->offset + slice->offset);
        }
}

96
static bool
97
v3d_resource_bo_alloc(struct v3d_resource *rsc)
98
{
99
        struct pipe_resource *prsc = &rsc->base;
100
        struct pipe_screen *pscreen = prsc->screen;
101
        struct v3d_bo *bo;
102

103
        bo = v3d_bo_alloc(v3d_screen(pscreen), rsc->size, "resource");
104
        if (bo) {
105
                v3d_bo_unreference(&rsc->bo);
106
                rsc->bo = bo;
107
                v3d_debug_resource_layout(rsc, "alloc");
108 109 110 111 112 113 114
                return true;
        } else {
                return false;
        }
}

static void
115
v3d_resource_transfer_unmap(struct pipe_context *pctx,
116 117
                            struct pipe_transfer *ptrans)
{
118 119
        struct v3d_context *v3d = v3d_context(pctx);
        struct v3d_transfer *trans = v3d_transfer(ptrans);
120 121

        if (trans->map) {
122 123
                struct v3d_resource *rsc = v3d_resource(ptrans->resource);
                struct v3d_resource_slice *slice = &rsc->slices[ptrans->level];
124 125

                if (ptrans->usage & PIPE_TRANSFER_WRITE) {
126 127
                        for (int z = 0; z < ptrans->box.depth; z++) {
                                void *dst = rsc->bo->map +
128
                                        v3d_layer_offset(&rsc->base,
129 130
                                                         ptrans->level,
                                                         ptrans->box.z + z);
131
                                v3d_store_tiled_image(dst,
132 133 134 135 136 137 138 139 140
                                                      slice->stride,
                                                      (trans->map +
                                                       ptrans->stride *
                                                       ptrans->box.height * z),
                                                      ptrans->stride,
                                                      slice->tiling, rsc->cpp,
                                                      slice->padded_height,
                                                      &ptrans->box);
                        }
141 142 143 144 145
                }
                free(trans->map);
        }

        pipe_resource_reference(&ptrans->resource, NULL);
146
        slab_free(&v3d->transfer_pool, ptrans);
147 148 149
}

static void *
150
v3d_resource_transfer_map(struct pipe_context *pctx,
151 152 153 154 155
                          struct pipe_resource *prsc,
                          unsigned level, unsigned usage,
                          const struct pipe_box *box,
                          struct pipe_transfer **pptrans)
{
156 157 158
        struct v3d_context *v3d = v3d_context(pctx);
        struct v3d_resource *rsc = v3d_resource(prsc);
        struct v3d_transfer *trans;
159 160 161 162
        struct pipe_transfer *ptrans;
        enum pipe_format format = prsc->format;
        char *buf;

163 164 165
        /* MSAA maps should have been handled by u_transfer_helper. */
        assert(prsc->nr_samples <= 1);

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
        /* Upgrade DISCARD_RANGE to WHOLE_RESOURCE if the whole resource is
         * being mapped.
         */
        if ((usage & PIPE_TRANSFER_DISCARD_RANGE) &&
            !(usage & PIPE_TRANSFER_UNSYNCHRONIZED) &&
            !(prsc->flags & PIPE_RESOURCE_FLAG_MAP_COHERENT) &&
            prsc->last_level == 0 &&
            prsc->width0 == box->width &&
            prsc->height0 == box->height &&
            prsc->depth0 == box->depth &&
            prsc->array_size == 1 &&
            rsc->bo->private) {
                usage |= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
        }

        if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
182
                if (v3d_resource_bo_alloc(rsc)) {
183 184 185 186 187
                        /* If it might be bound as one of our vertex buffers
                         * or UBOs, make sure we re-emit vertex buffer state
                         * or uniforms.
                         */
                        if (prsc->bind & PIPE_BIND_VERTEX_BUFFER)
188
                                v3d->dirty |= VC5_DIRTY_VTXBUF;
189
                        if (prsc->bind & PIPE_BIND_CONSTANT_BUFFER)
190
                                v3d->dirty |= VC5_DIRTY_CONSTBUF;
191 192 193 194
                } else {
                        /* If we failed to reallocate, flush users so that we
                         * don't violate any syncing requirements.
                         */
195
                        v3d_flush_jobs_reading_resource(v3d, prsc);
196 197 198 199 200 201 202
                }
        } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
                /* If we're writing and the buffer is being used by the CL, we
                 * have to flush the CL first.  If we're only reading, we need
                 * to flush if the CL has written our buffer.
                 */
                if (usage & PIPE_TRANSFER_WRITE)
203
                        v3d_flush_jobs_reading_resource(v3d, prsc);
204
                else
205
                        v3d_flush_jobs_writing_resource(v3d, prsc);
206 207 208 209 210 211 212
        }

        if (usage & PIPE_TRANSFER_WRITE) {
                rsc->writes++;
                rsc->initialized_buffers = ~0;
        }

213
        trans = slab_alloc(&v3d->transfer_pool);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        if (!trans)
                return NULL;

        /* XXX: Handle DONTBLOCK, DISCARD_RANGE, PERSISTENT, COHERENT. */

        /* slab_alloc_st() doesn't zero: */
        memset(trans, 0, sizeof(*trans));
        ptrans = &trans->base;

        pipe_resource_reference(&ptrans->resource, prsc);
        ptrans->level = level;
        ptrans->usage = usage;
        ptrans->box = *box;

        /* Note that the current kernel implementation is synchronous, so no
         * need to do syncing stuff here yet.
         */

        if (usage & PIPE_TRANSFER_UNSYNCHRONIZED)
233
                buf = v3d_bo_map_unsynchronized(rsc->bo);
234
        else
235
                buf = v3d_bo_map(rsc->bo);
236 237 238 239 240 241 242
        if (!buf) {
                fprintf(stderr, "Failed to map bo\n");
                goto fail;
        }

        *pptrans = ptrans;

243 244 245 246 247 248 249 250
        /* Our load/store routines work on entire compressed blocks. */
        ptrans->box.x /= util_format_get_blockwidth(format);
        ptrans->box.y /= util_format_get_blockheight(format);
        ptrans->box.width = DIV_ROUND_UP(ptrans->box.width,
                                         util_format_get_blockwidth(format));
        ptrans->box.height = DIV_ROUND_UP(ptrans->box.height,
                                          util_format_get_blockheight(format));

251
        struct v3d_resource_slice *slice = &rsc->slices[level];
252 253 254 255 256 257 258 259 260 261 262 263 264
        if (rsc->tiled) {
                /* No direct mappings of tiled, since we need to manually
                 * tile/untile.
                 */
                if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
                        return NULL;

                ptrans->stride = ptrans->box.width * rsc->cpp;
                ptrans->layer_stride = ptrans->stride * ptrans->box.height;

                trans->map = malloc(ptrans->layer_stride * ptrans->box.depth);

                if (usage & PIPE_TRANSFER_READ) {
265 266
                        for (int z = 0; z < ptrans->box.depth; z++) {
                                void *src = rsc->bo->map +
267
                                        v3d_layer_offset(&rsc->base,
268 269
                                                         ptrans->level,
                                                         ptrans->box.z + z);
270
                                v3d_load_tiled_image((trans->map +
271 272 273 274 275 276 277
                                                      ptrans->stride *
                                                      ptrans->box.height * z),
                                                     ptrans->stride,
                                                     src,
                                                     slice->stride,
                                                     slice->tiling, rsc->cpp,
                                                     slice->padded_height,
278
                                             &ptrans->box);
279
                        }
280 281 282 283 284 285 286
                }
                return trans->map;
        } else {
                ptrans->stride = slice->stride;
                ptrans->layer_stride = ptrans->stride;

                return buf + slice->offset +
287 288
                        ptrans->box.y * ptrans->stride +
                        ptrans->box.x * rsc->cpp +
289 290 291 292 293
                        ptrans->box.z * rsc->cube_map_stride;
        }


fail:
294
        v3d_resource_transfer_unmap(pctx, ptrans);
295 296 297 298
        return NULL;
}

static void
299
v3d_resource_destroy(struct pipe_screen *pscreen,
300 301
                     struct pipe_resource *prsc)
{
302
        struct v3d_resource *rsc = v3d_resource(prsc);
303

304
        v3d_bo_unreference(&rsc->bo);
305 306 307 308
        free(rsc);
}

static boolean
309
v3d_resource_get_handle(struct pipe_screen *pscreen,
310
                        struct pipe_context *pctx,
311
                        struct pipe_resource *prsc,
312 313
                        struct winsys_handle *whandle,
                        unsigned usage)
314
{
315 316
        struct v3d_resource *rsc = v3d_resource(prsc);
        struct v3d_bo *bo = rsc->bo;
317 318 319 320 321 322 323 324 325 326

        whandle->stride = rsc->slices[0].stride;

        /* If we're passing some reference to our BO out to some other part of
         * the system, then we can't do any optimizations about only us being
         * the ones seeing it (like BO caching).
         */
        bo->private = false;

        switch (whandle->type) {
327
        case WINSYS_HANDLE_TYPE_SHARED:
328
                return v3d_bo_flink(bo, &whandle->handle);
329
        case WINSYS_HANDLE_TYPE_KMS:
330 331
                whandle->handle = bo->handle;
                return TRUE;
332
        case WINSYS_HANDLE_TYPE_FD:
333
                whandle->handle = v3d_bo_get_dmabuf(bo);
334 335 336 337 338 339
                return whandle->handle != -1;
        }

        return FALSE;
}

340 341 342 343 344
#define PAGE_UB_ROWS (VC5_UIFCFG_PAGE_SIZE / VC5_UIFBLOCK_ROW_SIZE)
#define PAGE_UB_ROWS_TIMES_1_5 ((PAGE_UB_ROWS * 3) >> 1)
#define PAGE_CACHE_UB_ROWS (VC5_PAGE_CACHE_SIZE / VC5_UIFBLOCK_ROW_SIZE)
#define PAGE_CACHE_MINUS_1_5_UB_ROWS (PAGE_CACHE_UB_ROWS - PAGE_UB_ROWS_TIMES_1_5)

345 346 347 348 349 350 351 352
/**
 * Computes the HW's UIFblock padding for a given height/cpp.
 *
 * The goal of the padding is to keep pages of the same color (bank number) at
 * least half a page away from each other vertically when crossing between
 * between columns of UIF blocks.
 */
static uint32_t
353
v3d_get_ub_pad(struct v3d_resource *rsc, uint32_t height)
354
{
355
        uint32_t utile_h = v3d_utile_height(rsc->cpp);
356 357 358
        uint32_t uif_block_h = utile_h * 2;
        uint32_t height_ub = height / uif_block_h;

359
        uint32_t height_offset_in_pc = height_ub % PAGE_CACHE_UB_ROWS;
360 361 362 363 364 365

        /* For the perfectly-aligned-for-UIF-XOR case, don't add any pad. */
        if (height_offset_in_pc == 0)
                return 0;

        /* Try padding up to where we're offset by at least half a page. */
366
        if (height_offset_in_pc < PAGE_UB_ROWS_TIMES_1_5) {
367
                /* If we fit entirely in the page cache, don't pad. */
368
                if (height_ub < PAGE_CACHE_UB_ROWS)
369 370
                        return 0;
                else
371
                        return PAGE_UB_ROWS_TIMES_1_5 - height_offset_in_pc;
372 373 374 375 376
        }

        /* If we're close to being aligned to page cache size, then round up
         * and rely on XOR.
         */
377 378
        if (height_offset_in_pc > PAGE_CACHE_MINUS_1_5_UB_ROWS)
                return PAGE_CACHE_UB_ROWS - height_offset_in_pc;
379 380 381 382 383 384 385

        /* Otherwise, we're far enough away (top and bottom) to not need any
         * padding.
         */
        return 0;
}

386
static void
387
v3d_setup_slices(struct v3d_resource *rsc)
388
{
389
        struct pipe_resource *prsc = &rsc->base;
390 391
        uint32_t width = prsc->width0;
        uint32_t height = prsc->height0;
392
        uint32_t depth = prsc->depth0;
393 394 395 396 397 398 399 400
        /* Note that power-of-two padding is based on level 1.  These are not
         * equivalent to just util_next_power_of_two(dimension), because at a
         * level 0 dimension of 9, the level 1 power-of-two padded value is 4,
         * not 8.
         */
        uint32_t pot_width = 2 * util_next_power_of_two(u_minify(width, 1));
        uint32_t pot_height = 2 * util_next_power_of_two(u_minify(height, 1));
        uint32_t pot_depth = 2 * util_next_power_of_two(u_minify(depth, 1));
401
        uint32_t offset = 0;
402 403
        uint32_t utile_w = v3d_utile_width(rsc->cpp);
        uint32_t utile_h = v3d_utile_height(rsc->cpp);
404 405
        uint32_t uif_block_w = utile_w * 2;
        uint32_t uif_block_h = utile_h * 2;
406 407
        uint32_t block_width = util_format_get_blockwidth(prsc->format);
        uint32_t block_height = util_format_get_blockheight(prsc->format);
408 409 410 411
        bool msaa = prsc->nr_samples > 1;
        /* MSAA textures/renderbuffers are always laid out as single-level
         * UIF.
         */
412
        bool uif_top = msaa;
413 414

        for (int i = prsc->last_level; i >= 0; i--) {
415
                struct v3d_resource_slice *slice = &rsc->slices[i];
416

417
                uint32_t level_width, level_height, level_depth;
418 419 420 421 422 423 424
                if (i < 2) {
                        level_width = u_minify(width, i);
                        level_height = u_minify(height, i);
                } else {
                        level_width = u_minify(pot_width, i);
                        level_height = u_minify(pot_height, i);
                }
425 426 427 428
                if (i < 1)
                        level_depth = u_minify(depth, i);
                else
                        level_depth = u_minify(pot_depth, i);
429

430 431 432 433 434
                if (msaa) {
                        level_width *= 2;
                        level_height *= 2;
                }

435 436 437
                level_width = DIV_ROUND_UP(level_width, block_width);
                level_height = DIV_ROUND_UP(level_height, block_height);

438 439
                if (!rsc->tiled) {
                        slice->tiling = VC5_TILING_RASTER;
440 441
                        if (prsc->target == PIPE_TEXTURE_1D)
                                level_width = align(level_width, 64 / rsc->cpp);
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
                } else {
                        if ((i != 0 || !uif_top) &&
                            (level_width <= utile_w ||
                             level_height <= utile_h)) {
                                slice->tiling = VC5_TILING_LINEARTILE;
                                level_width = align(level_width, utile_w);
                                level_height = align(level_height, utile_h);
                        } else if ((i != 0 || !uif_top) &&
                                   level_width <= uif_block_w) {
                                slice->tiling = VC5_TILING_UBLINEAR_1_COLUMN;
                                level_width = align(level_width, uif_block_w);
                                level_height = align(level_height, uif_block_h);
                        } else if ((i != 0 || !uif_top) &&
                                   level_width <= 2 * uif_block_w) {
                                slice->tiling = VC5_TILING_UBLINEAR_2_COLUMN;
                                level_width = align(level_width, 2 * uif_block_w);
                                level_height = align(level_height, uif_block_h);
                        } else {
460 461 462 463
                                /* We align the width to a 4-block column of
                                 * UIF blocks, but we only align height to UIF
                                 * blocks.
                                 */
464 465 466
                                level_width = align(level_width,
                                                    4 * uif_block_w);
                                level_height = align(level_height,
467
                                                     uif_block_h);
468

469
                                slice->ub_pad = v3d_get_ub_pad(rsc,
470 471
                                                               level_height);
                                level_height += slice->ub_pad * uif_block_h;
472 473 474 475 476 477 478 479 480 481 482 483 484

                                /* If the padding set us to to be aligned to
                                 * the page cache size, then the HW will use
                                 * the XOR bit on odd columns to get us
                                 * perfectly misaligned
                                 */
                                if ((level_height / uif_block_h) %
                                    (VC5_PAGE_CACHE_SIZE /
                                     VC5_UIFBLOCK_ROW_SIZE) == 0) {
                                        slice->tiling = VC5_TILING_UIF_XOR;
                                } else {
                                        slice->tiling = VC5_TILING_UIF_NO_XOR;
                                }
485 486 487 488
                        }
                }

                slice->offset = offset;
489
                slice->stride = level_width * rsc->cpp;
490
                slice->padded_height = level_height;
491 492
                slice->size = level_height * slice->stride;

493
                uint32_t slice_total_size = slice->size * level_depth;
494

495 496 497 498 499 500 501 502
                /* The HW aligns level 1's base to a page if any of level 1 or
                 * below could be UIF XOR.  The lower levels then inherit the
                 * alignment for as long as necesary, thanks to being power of
                 * two aligned.
                 */
                if (i == 1 &&
                    level_width > 4 * uif_block_w &&
                    level_height > PAGE_CACHE_MINUS_1_5_UB_ROWS * uif_block_h) {
503 504
                        slice_total_size = align(slice_total_size,
                                                 VC5_UIFCFG_PAGE_SIZE);
505
                }
506 507 508

                offset += slice_total_size;

509
        }
510
        rsc->size = offset;
511 512 513 514 515 516 517 518 519 520 521 522

        /* UIF/UBLINEAR levels need to be aligned to UIF-blocks, and LT only
         * needs to be aligned to utile boundaries.  Since tiles are laid out
         * from small to big in memory, we need to align the later UIF slices
         * to UIF blocks, if they were preceded by non-UIF-block-aligned LT
         * slices.
         *
         * We additionally align to 4k, which improves UIF XOR performance.
         */
        uint32_t page_align_offset = (align(rsc->slices[0].offset, 4096) -
                                      rsc->slices[0].offset);
        if (page_align_offset) {
523
                rsc->size += page_align_offset;
524 525 526 527
                for (int i = 0; i <= prsc->last_level; i++)
                        rsc->slices[i].offset += page_align_offset;
        }

528 529 530
        /* Arrays and cube textures have a stride which is the distance from
         * one full mipmap tree to the next (64b aligned).  For 3D textures,
         * we need to program the stride between slices of miplevel 0.
531
         */
532 533 534 535 536 537 538 539 540 541
        if (prsc->target != PIPE_TEXTURE_3D) {
                rsc->cube_map_stride = align(rsc->slices[0].offset +
                                             rsc->slices[0].size, 64);
                rsc->size += rsc->cube_map_stride * (prsc->array_size - 1);
        } else {
                rsc->cube_map_stride = rsc->slices[0].size;
        }
}

uint32_t
542
v3d_layer_offset(struct pipe_resource *prsc, uint32_t level, uint32_t layer)
543
{
544 545
        struct v3d_resource *rsc = v3d_resource(prsc);
        struct v3d_resource_slice *slice = &rsc->slices[level];
546 547 548 549 550

        if (prsc->target == PIPE_TEXTURE_3D)
                return slice->offset + layer * slice->size;
        else
                return slice->offset + layer * rsc->cube_map_stride;
551 552
}

553 554
static struct v3d_resource *
v3d_resource_setup(struct pipe_screen *pscreen,
555 556
                   const struct pipe_resource *tmpl)
{
557 558
        struct v3d_screen *screen = v3d_screen(pscreen);
        struct v3d_resource *rsc = CALLOC_STRUCT(v3d_resource);
559 560
        if (!rsc)
                return NULL;
561
        struct pipe_resource *prsc = &rsc->base;
562 563 564 565 566 567

        *prsc = *tmpl;

        pipe_reference_init(&prsc->reference, 1);
        prsc->screen = pscreen;

568
        if (prsc->nr_samples <= 1 ||
569
            screen->devinfo.ver >= 40 ||
570
            util_format_is_depth_or_stencil(prsc->format)) {
571 572 573
                rsc->cpp = util_format_get_blocksize(prsc->format);
                if (screen->devinfo.ver < 40 && prsc->nr_samples > 1)
                        rsc->cpp *= prsc->nr_samples;
574
        } else {
575
                assert(v3d_rt_format_supported(&screen->devinfo, prsc->format));
576
                uint32_t output_image_format =
577
                        v3d_get_rt_format(&screen->devinfo, prsc->format);
578 579
                uint32_t internal_type;
                uint32_t internal_bpp;
580
                v3d_get_internal_type_bpp_for_output_format(&screen->devinfo,
581
                                                            output_image_format,
582 583 584
                                                            &internal_type,
                                                            &internal_bpp);
                switch (internal_bpp) {
585
                case V3D_INTERNAL_BPP_32:
586 587
                        rsc->cpp = 4;
                        break;
588
                case V3D_INTERNAL_BPP_64:
589 590
                        rsc->cpp = 8;
                        break;
591
                case V3D_INTERNAL_BPP_128:
592 593 594 595
                        rsc->cpp = 16;
                        break;
                }
        }
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615

        assert(rsc->cpp);

        return rsc;
}

static bool
find_modifier(uint64_t needle, const uint64_t *haystack, int count)
{
        int i;

        for (i = 0; i < count; i++) {
                if (haystack[i] == needle)
                        return true;
        }

        return false;
}

static struct pipe_resource *
616
v3d_resource_create_with_modifiers(struct pipe_screen *pscreen,
617 618 619 620 621
                                   const struct pipe_resource *tmpl,
                                   const uint64_t *modifiers,
                                   int count)
{
        bool linear_ok = find_modifier(DRM_FORMAT_MOD_LINEAR, modifiers, count);
622
        struct v3d_resource *rsc = v3d_resource_setup(pscreen, tmpl);
623
        struct pipe_resource *prsc = &rsc->base;
624 625 626 627 628 629 630 631 632 633 634 635
        /* Use a tiled layout if we can, for better 3D performance. */
        bool should_tile = true;

        /* VBOs/PBOs are untiled (and 1 height). */
        if (tmpl->target == PIPE_BUFFER)
                should_tile = false;

        /* Cursors are always linear, and the user can request linear as well.
         */
        if (tmpl->bind & (PIPE_BIND_LINEAR | PIPE_BIND_CURSOR))
                should_tile = false;

636 637 638 639 640
        /* 1D and 1D_ARRAY textures are always raster-order. */
        if (tmpl->target == PIPE_TEXTURE_1D ||
            tmpl->target == PIPE_TEXTURE_1D_ARRAY)
                should_tile = false;

641 642 643
        /* Scanout BOs for simulator need to be linear for interaction with
         * i965.
         */
644
        if (using_v3d_simulator &&
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
            tmpl->bind & (PIPE_BIND_SHARED | PIPE_BIND_SCANOUT))
                should_tile = false;

        /* No user-specified modifier; determine our own. */
        if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
                linear_ok = true;
                rsc->tiled = should_tile;
        } else if (should_tile &&
                   find_modifier(DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED,
                                 modifiers, count)) {
                rsc->tiled = true;
        } else if (linear_ok) {
                rsc->tiled = false;
        } else {
                fprintf(stderr, "Unsupported modifier requested\n");
                return NULL;
        }

663 664
        rsc->internal_format = prsc->format;

665 666
        v3d_setup_slices(rsc);
        if (!v3d_resource_bo_alloc(rsc))
667 668 669 670
                goto fail;

        return prsc;
fail:
671
        v3d_resource_destroy(pscreen, prsc);
672 673 674 675
        return NULL;
}

struct pipe_resource *
676
v3d_resource_create(struct pipe_screen *pscreen,
677 678 679
                    const struct pipe_resource *tmpl)
{
        const uint64_t mod = DRM_FORMAT_MOD_INVALID;
680
        return v3d_resource_create_with_modifiers(pscreen, tmpl, &mod, 1);
681 682 683
}

static struct pipe_resource *
684
v3d_resource_from_handle(struct pipe_screen *pscreen,
685 686 687 688
                         const struct pipe_resource *tmpl,
                         struct winsys_handle *whandle,
                         unsigned usage)
{
689 690
        struct v3d_screen *screen = v3d_screen(pscreen);
        struct v3d_resource *rsc = v3d_resource_setup(pscreen, tmpl);
691
        struct pipe_resource *prsc = &rsc->base;
692
        struct v3d_resource_slice *slice = &rsc->slices[0];
693 694 695 696 697 698

        if (!rsc)
                return NULL;

        switch (whandle->modifier) {
        case DRM_FORMAT_MOD_LINEAR:
699
        case DRM_FORMAT_MOD_INVALID:
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
                rsc->tiled = false;
                break;
        /* XXX: UIF */
        default:
                fprintf(stderr,
                        "Attempt to import unsupported modifier 0x%llx\n",
                        (long long)whandle->modifier);
                goto fail;
        }

        if (whandle->offset != 0) {
                fprintf(stderr,
                        "Attempt to import unsupported winsys offset %u\n",
                        whandle->offset);
                goto fail;
        }

        switch (whandle->type) {
718
        case WINSYS_HANDLE_TYPE_SHARED:
719
                rsc->bo = v3d_bo_open_name(screen,
720 721
                                           whandle->handle, whandle->stride);
                break;
722
        case WINSYS_HANDLE_TYPE_FD:
723
                rsc->bo = v3d_bo_open_dmabuf(screen,
724 725 726 727 728 729 730 731 732 733 734 735
                                             whandle->handle, whandle->stride);
                break;
        default:
                fprintf(stderr,
                        "Attempt to import unsupported handle type %d\n",
                        whandle->type);
                goto fail;
        }

        if (!rsc->bo)
                goto fail;

736 737
        rsc->internal_format = prsc->format;

738 739
        v3d_setup_slices(rsc);
        v3d_debug_resource_layout(rsc, "import");
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758

        if (whandle->stride != slice->stride) {
                static bool warned = false;
                if (!warned) {
                        warned = true;
                        fprintf(stderr,
                                "Attempting to import %dx%d %s with "
                                "unsupported stride %d instead of %d\n",
                                prsc->width0, prsc->height0,
                                util_format_short_name(prsc->format),
                                whandle->stride,
                                slice->stride);
                }
                goto fail;
        }

        return prsc;

fail:
759
        v3d_resource_destroy(pscreen, prsc);
760 761 762 763
        return NULL;
}

static struct pipe_surface *
764
v3d_create_surface(struct pipe_context *pctx,
765 766 767
                   struct pipe_resource *ptex,
                   const struct pipe_surface *surf_tmpl)
{
768 769 770 771
        struct v3d_context *v3d = v3d_context(pctx);
        struct v3d_screen *screen = v3d->screen;
        struct v3d_surface *surface = CALLOC_STRUCT(v3d_surface);
        struct v3d_resource *rsc = v3d_resource(ptex);
772 773 774 775 776 777 778 779

        if (!surface)
                return NULL;

        assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);

        struct pipe_surface *psurf = &surface->base;
        unsigned level = surf_tmpl->u.tex.level;
780
        struct v3d_resource_slice *slice = &rsc->slices[level];
781 782 783 784 785 786 787 788 789 790 791 792

        pipe_reference_init(&psurf->reference, 1);
        pipe_resource_reference(&psurf->texture, ptex);

        psurf->context = pctx;
        psurf->format = surf_tmpl->format;
        psurf->width = u_minify(ptex->width0, level);
        psurf->height = u_minify(ptex->height0, level);
        psurf->u.tex.level = level;
        psurf->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
        psurf->u.tex.last_layer = surf_tmpl->u.tex.last_layer;

793
        surface->offset = v3d_layer_offset(ptex, level,
794
                                           psurf->u.tex.first_layer);
795
        surface->tiling = slice->tiling;
796

797
        surface->format = v3d_get_rt_format(&screen->devinfo, psurf->format);
798 799 800 801

        if (util_format_is_depth_or_stencil(psurf->format)) {
                switch (psurf->format) {
                case PIPE_FORMAT_Z16_UNORM:
802
                        surface->internal_type = V3D_INTERNAL_TYPE_DEPTH_16;
803 804 805
                        break;
                case PIPE_FORMAT_Z32_FLOAT:
                case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
806
                        surface->internal_type = V3D_INTERNAL_TYPE_DEPTH_32F;
807 808
                        break;
                default:
809
                        surface->internal_type = V3D_INTERNAL_TYPE_DEPTH_24;
810 811 812
                }
        } else {
                uint32_t bpp, type;
813
                v3d_get_internal_type_bpp_for_output_format(&screen->devinfo,
814
                                                            surface->format,
815 816 817 818 819
                                                            &type, &bpp);
                surface->internal_type = type;
                surface->internal_bpp = bpp;
        }

820 821 822
        if (surface->tiling == VC5_TILING_UIF_NO_XOR ||
            surface->tiling == VC5_TILING_UIF_XOR) {
                surface->padded_height_of_output_image_in_uif_blocks =
823
                        (slice->padded_height /
824
                         (2 * v3d_utile_height(rsc->cpp)));
825
        }
826

827 828
        if (rsc->separate_stencil) {
                surface->separate_stencil =
829
                        v3d_create_surface(pctx, &rsc->separate_stencil->base,
830
                                           surf_tmpl);
831 832
        }

833 834 835 836
        return &surface->base;
}

static void
837
v3d_surface_destroy(struct pipe_context *pctx, struct pipe_surface *psurf)
838
{
839
        struct v3d_surface *surf = v3d_surface(psurf);
840 841 842 843

        if (surf->separate_stencil)
                pipe_surface_reference(&surf->separate_stencil, NULL);

844 845 846 847 848
        pipe_resource_reference(&psurf->texture, NULL);
        FREE(psurf);
}

static void
849
v3d_flush_resource(struct pipe_context *pctx, struct pipe_resource *resource)
850 851 852 853 854 855
{
        /* All calls to flush_resource are followed by a flush of the context,
         * so there's nothing to do.
         */
}

856
static enum pipe_format
857
v3d_resource_get_internal_format(struct pipe_resource *prsc)
858
{
859
        return v3d_resource(prsc)->internal_format;
860 861 862
}

static void
863
v3d_resource_set_stencil(struct pipe_resource *prsc,
864 865
                         struct pipe_resource *stencil)
{
866
        v3d_resource(prsc)->separate_stencil = v3d_resource(stencil);
867 868 869
}

static struct pipe_resource *
870
v3d_resource_get_stencil(struct pipe_resource *prsc)
871
{
872
        struct v3d_resource *rsc = v3d_resource(prsc);
873 874 875 876 877

        return &rsc->separate_stencil->base;
}

static const struct u_transfer_vtbl transfer_vtbl = {
878 879 880 881
        .resource_create          = v3d_resource_create,
        .resource_destroy         = v3d_resource_destroy,
        .transfer_map             = v3d_resource_transfer_map,
        .transfer_unmap           = v3d_resource_transfer_unmap,
882
        .transfer_flush_region    = u_default_transfer_flush_region,
883 884 885
        .get_internal_format      = v3d_resource_get_internal_format,
        .set_stencil              = v3d_resource_set_stencil,
        .get_stencil              = v3d_resource_get_stencil,
886 887
};

888
void
889
v3d_resource_screen_init(struct pipe_screen *pscreen)
890 891
{
        pscreen->resource_create_with_modifiers =
892
                v3d_resource_create_with_modifiers;
893
        pscreen->resource_create = u_transfer_helper_resource_create;
894 895
        pscreen->resource_from_handle = v3d_resource_from_handle;
        pscreen->resource_get_handle = v3d_resource_get_handle;
896 897
        pscreen->resource_destroy = u_transfer_helper_resource_destroy;
        pscreen->transfer_helper = u_transfer_helper_create(&transfer_vtbl,
898
                                                            true, true, true);
899 900 901
}

void
902
v3d_resource_context_init(struct pipe_context *pctx)
903
{
904 905 906
        pctx->transfer_map = u_transfer_helper_transfer_map;
        pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
        pctx->transfer_unmap = u_transfer_helper_transfer_unmap;
907 908
        pctx->buffer_subdata = u_default_buffer_subdata;
        pctx->texture_subdata = u_default_texture_subdata;
909 910
        pctx->create_surface = v3d_create_surface;
        pctx->surface_destroy = v3d_surface_destroy;
911
        pctx->resource_copy_region = util_resource_copy_region;
912 913
        pctx->blit = v3d_blit;
        pctx->flush_resource = v3d_flush_resource;
914
}