v3d: expose OES_geometry_shader
[mesa.git] / src / gallium / drivers / v3d / v3d_resource.c
1 /*
2 * Copyright © 2014-2017 Broadcom
3 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include "pipe/p_defines.h"
26 #include "util/u_blit.h"
27 #include "util/u_memory.h"
28 #include "util/format/u_format.h"
29 #include "util/u_inlines.h"
30 #include "util/u_surface.h"
31 #include "util/u_transfer_helper.h"
32 #include "util/u_upload_mgr.h"
33 #include "util/format/u_format_zs.h"
34 #include "util/u_drm.h"
35
36 #include "drm-uapi/drm_fourcc.h"
37 #include "v3d_screen.h"
38 #include "v3d_context.h"
39 #include "v3d_resource.h"
40 #include "v3d_tiling.h"
41 #include "broadcom/cle/v3d_packet_v33_pack.h"
42
43 static void
44 v3d_debug_resource_layout(struct v3d_resource *rsc, const char *caller)
45 {
46 if (!(V3D_DEBUG & V3D_DEBUG_SURFACE))
47 return;
48
49 struct pipe_resource *prsc = &rsc->base;
50
51 if (prsc->target == PIPE_BUFFER) {
52 fprintf(stderr,
53 "rsc %s %p (format %s), %dx%d buffer @0x%08x-0x%08x\n",
54 caller, rsc,
55 util_format_short_name(prsc->format),
56 prsc->width0, prsc->height0,
57 rsc->bo->offset,
58 rsc->bo->offset + rsc->bo->size - 1);
59 return;
60 }
61
62 static const char *const tiling_descriptions[] = {
63 [VC5_TILING_RASTER] = "R",
64 [VC5_TILING_LINEARTILE] = "LT",
65 [VC5_TILING_UBLINEAR_1_COLUMN] = "UB1",
66 [VC5_TILING_UBLINEAR_2_COLUMN] = "UB2",
67 [VC5_TILING_UIF_NO_XOR] = "UIF",
68 [VC5_TILING_UIF_XOR] = "UIF^",
69 };
70
71 for (int i = 0; i <= prsc->last_level; i++) {
72 struct v3d_resource_slice *slice = &rsc->slices[i];
73
74 int level_width = slice->stride / rsc->cpp;
75 int level_height = slice->padded_height;
76 int level_depth =
77 u_minify(util_next_power_of_two(prsc->depth0), i);
78
79 fprintf(stderr,
80 "rsc %s %p (format %s), %dx%d: "
81 "level %d (%s) %dx%dx%d -> %dx%dx%d, stride %d@0x%08x\n",
82 caller, rsc,
83 util_format_short_name(prsc->format),
84 prsc->width0, prsc->height0,
85 i, tiling_descriptions[slice->tiling],
86 u_minify(prsc->width0, i),
87 u_minify(prsc->height0, i),
88 u_minify(prsc->depth0, i),
89 level_width,
90 level_height,
91 level_depth,
92 slice->stride,
93 rsc->bo->offset + slice->offset);
94 }
95 }
96
97 static bool
98 v3d_resource_bo_alloc(struct v3d_resource *rsc)
99 {
100 struct pipe_resource *prsc = &rsc->base;
101 struct pipe_screen *pscreen = prsc->screen;
102 struct v3d_bo *bo;
103
104 bo = v3d_bo_alloc(v3d_screen(pscreen), rsc->size, "resource");
105 if (bo) {
106 v3d_bo_unreference(&rsc->bo);
107 rsc->bo = bo;
108 v3d_debug_resource_layout(rsc, "alloc");
109 return true;
110 } else {
111 return false;
112 }
113 }
114
115 static void
116 v3d_resource_transfer_unmap(struct pipe_context *pctx,
117 struct pipe_transfer *ptrans)
118 {
119 struct v3d_context *v3d = v3d_context(pctx);
120 struct v3d_transfer *trans = v3d_transfer(ptrans);
121
122 if (trans->map) {
123 struct v3d_resource *rsc = v3d_resource(ptrans->resource);
124 struct v3d_resource_slice *slice = &rsc->slices[ptrans->level];
125
126 if (ptrans->usage & PIPE_TRANSFER_WRITE) {
127 for (int z = 0; z < ptrans->box.depth; z++) {
128 void *dst = rsc->bo->map +
129 v3d_layer_offset(&rsc->base,
130 ptrans->level,
131 ptrans->box.z + z);
132 v3d_store_tiled_image(dst,
133 slice->stride,
134 (trans->map +
135 ptrans->stride *
136 ptrans->box.height * z),
137 ptrans->stride,
138 slice->tiling, rsc->cpp,
139 slice->padded_height,
140 &ptrans->box);
141 }
142 }
143 free(trans->map);
144 }
145
146 pipe_resource_reference(&ptrans->resource, NULL);
147 slab_free(&v3d->transfer_pool, ptrans);
148 }
149
150 static void
151 v3d_map_usage_prep(struct pipe_context *pctx,
152 struct pipe_resource *prsc,
153 unsigned usage)
154 {
155 struct v3d_context *v3d = v3d_context(pctx);
156 struct v3d_resource *rsc = v3d_resource(prsc);
157
158 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
159 if (v3d_resource_bo_alloc(rsc)) {
160 /* If it might be bound as one of our vertex buffers
161 * or UBOs, make sure we re-emit vertex buffer state
162 * or uniforms.
163 */
164 if (prsc->bind & PIPE_BIND_VERTEX_BUFFER)
165 v3d->dirty |= VC5_DIRTY_VTXBUF;
166 if (prsc->bind & PIPE_BIND_CONSTANT_BUFFER)
167 v3d->dirty |= VC5_DIRTY_CONSTBUF;
168 } else {
169 /* If we failed to reallocate, flush users so that we
170 * don't violate any syncing requirements.
171 */
172 v3d_flush_jobs_reading_resource(v3d, prsc,
173 V3D_FLUSH_DEFAULT);
174 }
175 } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
176 /* If we're writing and the buffer is being used by the CL, we
177 * have to flush the CL first. If we're only reading, we need
178 * to flush if the CL has written our buffer.
179 */
180 if (usage & PIPE_TRANSFER_WRITE)
181 v3d_flush_jobs_reading_resource(v3d, prsc,
182 V3D_FLUSH_ALWAYS);
183 else
184 v3d_flush_jobs_writing_resource(v3d, prsc,
185 V3D_FLUSH_ALWAYS);
186 }
187
188 if (usage & PIPE_TRANSFER_WRITE) {
189 rsc->writes++;
190 rsc->initialized_buffers = ~0;
191 }
192 }
193
194 static void *
195 v3d_resource_transfer_map(struct pipe_context *pctx,
196 struct pipe_resource *prsc,
197 unsigned level, unsigned usage,
198 const struct pipe_box *box,
199 struct pipe_transfer **pptrans)
200 {
201 struct v3d_context *v3d = v3d_context(pctx);
202 struct v3d_resource *rsc = v3d_resource(prsc);
203 struct v3d_transfer *trans;
204 struct pipe_transfer *ptrans;
205 enum pipe_format format = prsc->format;
206 char *buf;
207
208 /* MSAA maps should have been handled by u_transfer_helper. */
209 assert(prsc->nr_samples <= 1);
210
211 /* Upgrade DISCARD_RANGE to WHOLE_RESOURCE if the whole resource is
212 * being mapped.
213 */
214 if ((usage & PIPE_TRANSFER_DISCARD_RANGE) &&
215 !(usage & PIPE_TRANSFER_UNSYNCHRONIZED) &&
216 !(prsc->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT) &&
217 prsc->last_level == 0 &&
218 prsc->width0 == box->width &&
219 prsc->height0 == box->height &&
220 prsc->depth0 == box->depth &&
221 prsc->array_size == 1 &&
222 rsc->bo->private) {
223 usage |= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
224 }
225
226 v3d_map_usage_prep(pctx, prsc, usage);
227
228 trans = slab_alloc(&v3d->transfer_pool);
229 if (!trans)
230 return NULL;
231
232 /* XXX: Handle DONTBLOCK, DISCARD_RANGE, PERSISTENT, COHERENT. */
233
234 /* slab_alloc_st() doesn't zero: */
235 memset(trans, 0, sizeof(*trans));
236 ptrans = &trans->base;
237
238 pipe_resource_reference(&ptrans->resource, prsc);
239 ptrans->level = level;
240 ptrans->usage = usage;
241 ptrans->box = *box;
242
243 /* Note that the current kernel implementation is synchronous, so no
244 * need to do syncing stuff here yet.
245 */
246
247 if (usage & PIPE_TRANSFER_UNSYNCHRONIZED)
248 buf = v3d_bo_map_unsynchronized(rsc->bo);
249 else
250 buf = v3d_bo_map(rsc->bo);
251 if (!buf) {
252 fprintf(stderr, "Failed to map bo\n");
253 goto fail;
254 }
255
256 *pptrans = ptrans;
257
258 /* Our load/store routines work on entire compressed blocks. */
259 ptrans->box.x /= util_format_get_blockwidth(format);
260 ptrans->box.y /= util_format_get_blockheight(format);
261 ptrans->box.width = DIV_ROUND_UP(ptrans->box.width,
262 util_format_get_blockwidth(format));
263 ptrans->box.height = DIV_ROUND_UP(ptrans->box.height,
264 util_format_get_blockheight(format));
265
266 struct v3d_resource_slice *slice = &rsc->slices[level];
267 if (rsc->tiled) {
268 /* No direct mappings of tiled, since we need to manually
269 * tile/untile.
270 */
271 if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
272 return NULL;
273
274 ptrans->stride = ptrans->box.width * rsc->cpp;
275 ptrans->layer_stride = ptrans->stride * ptrans->box.height;
276
277 trans->map = malloc(ptrans->layer_stride * ptrans->box.depth);
278
279 if (usage & PIPE_TRANSFER_READ) {
280 for (int z = 0; z < ptrans->box.depth; z++) {
281 void *src = rsc->bo->map +
282 v3d_layer_offset(&rsc->base,
283 ptrans->level,
284 ptrans->box.z + z);
285 v3d_load_tiled_image((trans->map +
286 ptrans->stride *
287 ptrans->box.height * z),
288 ptrans->stride,
289 src,
290 slice->stride,
291 slice->tiling, rsc->cpp,
292 slice->padded_height,
293 &ptrans->box);
294 }
295 }
296 return trans->map;
297 } else {
298 ptrans->stride = slice->stride;
299 ptrans->layer_stride = rsc->cube_map_stride;
300
301 return buf + slice->offset +
302 ptrans->box.y * ptrans->stride +
303 ptrans->box.x * rsc->cpp +
304 ptrans->box.z * rsc->cube_map_stride;
305 }
306
307
308 fail:
309 v3d_resource_transfer_unmap(pctx, ptrans);
310 return NULL;
311 }
312
313 static void
314 v3d_texture_subdata(struct pipe_context *pctx,
315 struct pipe_resource *prsc,
316 unsigned level,
317 unsigned usage,
318 const struct pipe_box *box,
319 const void *data,
320 unsigned stride,
321 unsigned layer_stride)
322 {
323 struct v3d_resource *rsc = v3d_resource(prsc);
324 struct v3d_resource_slice *slice = &rsc->slices[level];
325
326 /* For a direct mapping, we can just take the u_transfer path. */
327 if (!rsc->tiled) {
328 return u_default_texture_subdata(pctx, prsc, level, usage, box,
329 data, stride, layer_stride);
330 }
331
332 /* Otherwise, map and store the texture data directly into the tiled
333 * texture. Note that gallium's texture_subdata may be called with
334 * obvious usage flags missing!
335 */
336 v3d_map_usage_prep(pctx, prsc, usage | (PIPE_TRANSFER_WRITE |
337 PIPE_TRANSFER_DISCARD_RANGE));
338
339 void *buf;
340 if (usage & PIPE_TRANSFER_UNSYNCHRONIZED)
341 buf = v3d_bo_map_unsynchronized(rsc->bo);
342 else
343 buf = v3d_bo_map(rsc->bo);
344
345 for (int i = 0; i < box->depth; i++) {
346 v3d_store_tiled_image(buf +
347 v3d_layer_offset(&rsc->base,
348 level,
349 box->z + i),
350 slice->stride,
351 (void *)data + layer_stride * i,
352 stride,
353 slice->tiling, rsc->cpp, slice->padded_height,
354 box);
355 }
356 }
357
358 static void
359 v3d_resource_destroy(struct pipe_screen *pscreen,
360 struct pipe_resource *prsc)
361 {
362 struct v3d_screen *screen = v3d_screen(pscreen);
363 struct v3d_resource *rsc = v3d_resource(prsc);
364
365 if (rsc->scanout)
366 renderonly_scanout_destroy(rsc->scanout, screen->ro);
367
368 v3d_bo_unreference(&rsc->bo);
369 free(rsc);
370 }
371
372 static bool
373 v3d_resource_get_handle(struct pipe_screen *pscreen,
374 struct pipe_context *pctx,
375 struct pipe_resource *prsc,
376 struct winsys_handle *whandle,
377 unsigned usage)
378 {
379 struct v3d_screen *screen = v3d_screen(pscreen);
380 struct v3d_resource *rsc = v3d_resource(prsc);
381 struct v3d_bo *bo = rsc->bo;
382
383 whandle->stride = rsc->slices[0].stride;
384 whandle->offset = 0;
385
386 /* If we're passing some reference to our BO out to some other part of
387 * the system, then we can't do any optimizations about only us being
388 * the ones seeing it (like BO caching).
389 */
390 bo->private = false;
391
392 if (rsc->tiled) {
393 /* A shared tiled buffer should always be allocated as UIF,
394 * not UBLINEAR or LT.
395 */
396 assert(rsc->slices[0].tiling == VC5_TILING_UIF_XOR ||
397 rsc->slices[0].tiling == VC5_TILING_UIF_NO_XOR);
398 whandle->modifier = DRM_FORMAT_MOD_BROADCOM_UIF;
399 } else {
400 whandle->modifier = DRM_FORMAT_MOD_LINEAR;
401 }
402
403 switch (whandle->type) {
404 case WINSYS_HANDLE_TYPE_SHARED:
405 return v3d_bo_flink(bo, &whandle->handle);
406 case WINSYS_HANDLE_TYPE_KMS:
407 if (screen->ro) {
408 assert(rsc->scanout);
409 bool ok = renderonly_get_handle(rsc->scanout, whandle);
410 whandle->stride = rsc->slices[0].stride;
411 return ok;
412 }
413 whandle->handle = bo->handle;
414 return true;
415 case WINSYS_HANDLE_TYPE_FD:
416 whandle->handle = v3d_bo_get_dmabuf(bo);
417 return whandle->handle != -1;
418 }
419
420 return false;
421 }
422
423 #define PAGE_UB_ROWS (VC5_UIFCFG_PAGE_SIZE / VC5_UIFBLOCK_ROW_SIZE)
424 #define PAGE_UB_ROWS_TIMES_1_5 ((PAGE_UB_ROWS * 3) >> 1)
425 #define PAGE_CACHE_UB_ROWS (VC5_PAGE_CACHE_SIZE / VC5_UIFBLOCK_ROW_SIZE)
426 #define PAGE_CACHE_MINUS_1_5_UB_ROWS (PAGE_CACHE_UB_ROWS - PAGE_UB_ROWS_TIMES_1_5)
427
428 /**
429 * Computes the HW's UIFblock padding for a given height/cpp.
430 *
431 * The goal of the padding is to keep pages of the same color (bank number) at
432 * least half a page away from each other vertically when crossing between
433 * between columns of UIF blocks.
434 */
435 static uint32_t
436 v3d_get_ub_pad(struct v3d_resource *rsc, uint32_t height)
437 {
438 uint32_t utile_h = v3d_utile_height(rsc->cpp);
439 uint32_t uif_block_h = utile_h * 2;
440 uint32_t height_ub = height / uif_block_h;
441
442 uint32_t height_offset_in_pc = height_ub % PAGE_CACHE_UB_ROWS;
443
444 /* For the perfectly-aligned-for-UIF-XOR case, don't add any pad. */
445 if (height_offset_in_pc == 0)
446 return 0;
447
448 /* Try padding up to where we're offset by at least half a page. */
449 if (height_offset_in_pc < PAGE_UB_ROWS_TIMES_1_5) {
450 /* If we fit entirely in the page cache, don't pad. */
451 if (height_ub < PAGE_CACHE_UB_ROWS)
452 return 0;
453 else
454 return PAGE_UB_ROWS_TIMES_1_5 - height_offset_in_pc;
455 }
456
457 /* If we're close to being aligned to page cache size, then round up
458 * and rely on XOR.
459 */
460 if (height_offset_in_pc > PAGE_CACHE_MINUS_1_5_UB_ROWS)
461 return PAGE_CACHE_UB_ROWS - height_offset_in_pc;
462
463 /* Otherwise, we're far enough away (top and bottom) to not need any
464 * padding.
465 */
466 return 0;
467 }
468
469 static void
470 v3d_setup_slices(struct v3d_resource *rsc, uint32_t winsys_stride,
471 bool uif_top)
472 {
473 struct pipe_resource *prsc = &rsc->base;
474 uint32_t width = prsc->width0;
475 uint32_t height = prsc->height0;
476 uint32_t depth = prsc->depth0;
477 /* Note that power-of-two padding is based on level 1. These are not
478 * equivalent to just util_next_power_of_two(dimension), because at a
479 * level 0 dimension of 9, the level 1 power-of-two padded value is 4,
480 * not 8.
481 */
482 uint32_t pot_width = 2 * util_next_power_of_two(u_minify(width, 1));
483 uint32_t pot_height = 2 * util_next_power_of_two(u_minify(height, 1));
484 uint32_t pot_depth = 2 * util_next_power_of_two(u_minify(depth, 1));
485 uint32_t offset = 0;
486 uint32_t utile_w = v3d_utile_width(rsc->cpp);
487 uint32_t utile_h = v3d_utile_height(rsc->cpp);
488 uint32_t uif_block_w = utile_w * 2;
489 uint32_t uif_block_h = utile_h * 2;
490 uint32_t block_width = util_format_get_blockwidth(prsc->format);
491 uint32_t block_height = util_format_get_blockheight(prsc->format);
492 bool msaa = prsc->nr_samples > 1;
493
494 /* MSAA textures/renderbuffers are always laid out as single-level
495 * UIF.
496 */
497 uif_top |= msaa;
498
499 /* Check some easy mistakes to make in a resource_create() call that
500 * will break our setup.
501 */
502 assert(prsc->array_size != 0);
503 assert(prsc->depth0 != 0);
504
505 for (int i = prsc->last_level; i >= 0; i--) {
506 struct v3d_resource_slice *slice = &rsc->slices[i];
507
508 uint32_t level_width, level_height, level_depth;
509 if (i < 2) {
510 level_width = u_minify(width, i);
511 level_height = u_minify(height, i);
512 } else {
513 level_width = u_minify(pot_width, i);
514 level_height = u_minify(pot_height, i);
515 }
516 if (i < 1)
517 level_depth = u_minify(depth, i);
518 else
519 level_depth = u_minify(pot_depth, i);
520
521 if (msaa) {
522 level_width *= 2;
523 level_height *= 2;
524 }
525
526 level_width = DIV_ROUND_UP(level_width, block_width);
527 level_height = DIV_ROUND_UP(level_height, block_height);
528
529 if (!rsc->tiled) {
530 slice->tiling = VC5_TILING_RASTER;
531 if (prsc->target == PIPE_TEXTURE_1D)
532 level_width = align(level_width, 64 / rsc->cpp);
533 } else {
534 if ((i != 0 || !uif_top) &&
535 (level_width <= utile_w ||
536 level_height <= utile_h)) {
537 slice->tiling = VC5_TILING_LINEARTILE;
538 level_width = align(level_width, utile_w);
539 level_height = align(level_height, utile_h);
540 } else if ((i != 0 || !uif_top) &&
541 level_width <= uif_block_w) {
542 slice->tiling = VC5_TILING_UBLINEAR_1_COLUMN;
543 level_width = align(level_width, uif_block_w);
544 level_height = align(level_height, uif_block_h);
545 } else if ((i != 0 || !uif_top) &&
546 level_width <= 2 * uif_block_w) {
547 slice->tiling = VC5_TILING_UBLINEAR_2_COLUMN;
548 level_width = align(level_width, 2 * uif_block_w);
549 level_height = align(level_height, uif_block_h);
550 } else {
551 /* We align the width to a 4-block column of
552 * UIF blocks, but we only align height to UIF
553 * blocks.
554 */
555 level_width = align(level_width,
556 4 * uif_block_w);
557 level_height = align(level_height,
558 uif_block_h);
559
560 slice->ub_pad = v3d_get_ub_pad(rsc,
561 level_height);
562 level_height += slice->ub_pad * uif_block_h;
563
564 /* If the padding set us to to be aligned to
565 * the page cache size, then the HW will use
566 * the XOR bit on odd columns to get us
567 * perfectly misaligned
568 */
569 if ((level_height / uif_block_h) %
570 (VC5_PAGE_CACHE_SIZE /
571 VC5_UIFBLOCK_ROW_SIZE) == 0) {
572 slice->tiling = VC5_TILING_UIF_XOR;
573 } else {
574 slice->tiling = VC5_TILING_UIF_NO_XOR;
575 }
576 }
577 }
578
579 slice->offset = offset;
580 if (winsys_stride)
581 slice->stride = winsys_stride;
582 else
583 slice->stride = level_width * rsc->cpp;
584 slice->padded_height = level_height;
585 slice->size = level_height * slice->stride;
586
587 uint32_t slice_total_size = slice->size * level_depth;
588
589 /* The HW aligns level 1's base to a page if any of level 1 or
590 * below could be UIF XOR. The lower levels then inherit the
591 * alignment for as long as necesary, thanks to being power of
592 * two aligned.
593 */
594 if (i == 1 &&
595 level_width > 4 * uif_block_w &&
596 level_height > PAGE_CACHE_MINUS_1_5_UB_ROWS * uif_block_h) {
597 slice_total_size = align(slice_total_size,
598 VC5_UIFCFG_PAGE_SIZE);
599 }
600
601 offset += slice_total_size;
602
603 }
604 rsc->size = offset;
605
606 /* UIF/UBLINEAR levels need to be aligned to UIF-blocks, and LT only
607 * needs to be aligned to utile boundaries. Since tiles are laid out
608 * from small to big in memory, we need to align the later UIF slices
609 * to UIF blocks, if they were preceded by non-UIF-block-aligned LT
610 * slices.
611 *
612 * We additionally align to 4k, which improves UIF XOR performance.
613 */
614 uint32_t page_align_offset = (align(rsc->slices[0].offset, 4096) -
615 rsc->slices[0].offset);
616 if (page_align_offset) {
617 rsc->size += page_align_offset;
618 for (int i = 0; i <= prsc->last_level; i++)
619 rsc->slices[i].offset += page_align_offset;
620 }
621
622 /* Arrays and cube textures have a stride which is the distance from
623 * one full mipmap tree to the next (64b aligned). For 3D textures,
624 * we need to program the stride between slices of miplevel 0.
625 */
626 if (prsc->target != PIPE_TEXTURE_3D) {
627 rsc->cube_map_stride = align(rsc->slices[0].offset +
628 rsc->slices[0].size, 64);
629 rsc->size += rsc->cube_map_stride * (prsc->array_size - 1);
630 } else {
631 rsc->cube_map_stride = rsc->slices[0].size;
632 }
633 }
634
635 uint32_t
636 v3d_layer_offset(struct pipe_resource *prsc, uint32_t level, uint32_t layer)
637 {
638 struct v3d_resource *rsc = v3d_resource(prsc);
639 struct v3d_resource_slice *slice = &rsc->slices[level];
640
641 if (prsc->target == PIPE_TEXTURE_3D)
642 return slice->offset + layer * slice->size;
643 else
644 return slice->offset + layer * rsc->cube_map_stride;
645 }
646
647 static struct v3d_resource *
648 v3d_resource_setup(struct pipe_screen *pscreen,
649 const struct pipe_resource *tmpl)
650 {
651 struct v3d_screen *screen = v3d_screen(pscreen);
652 struct v3d_resource *rsc = CALLOC_STRUCT(v3d_resource);
653 if (!rsc)
654 return NULL;
655 struct pipe_resource *prsc = &rsc->base;
656
657 *prsc = *tmpl;
658
659 pipe_reference_init(&prsc->reference, 1);
660 prsc->screen = pscreen;
661
662 if (prsc->nr_samples <= 1 ||
663 screen->devinfo.ver >= 40 ||
664 util_format_is_depth_or_stencil(prsc->format)) {
665 rsc->cpp = util_format_get_blocksize(prsc->format);
666 if (screen->devinfo.ver < 40 && prsc->nr_samples > 1)
667 rsc->cpp *= prsc->nr_samples;
668 } else {
669 assert(v3d_rt_format_supported(&screen->devinfo, prsc->format));
670 uint32_t output_image_format =
671 v3d_get_rt_format(&screen->devinfo, prsc->format);
672 uint32_t internal_type;
673 uint32_t internal_bpp;
674 v3d_get_internal_type_bpp_for_output_format(&screen->devinfo,
675 output_image_format,
676 &internal_type,
677 &internal_bpp);
678 switch (internal_bpp) {
679 case V3D_INTERNAL_BPP_32:
680 rsc->cpp = 4;
681 break;
682 case V3D_INTERNAL_BPP_64:
683 rsc->cpp = 8;
684 break;
685 case V3D_INTERNAL_BPP_128:
686 rsc->cpp = 16;
687 break;
688 }
689 }
690
691 assert(rsc->cpp);
692
693 return rsc;
694 }
695
696 static struct pipe_resource *
697 v3d_resource_create_with_modifiers(struct pipe_screen *pscreen,
698 const struct pipe_resource *tmpl,
699 const uint64_t *modifiers,
700 int count)
701 {
702 struct v3d_screen *screen = v3d_screen(pscreen);
703
704 bool linear_ok = drm_find_modifier(DRM_FORMAT_MOD_LINEAR, modifiers, count);
705 struct v3d_resource *rsc = v3d_resource_setup(pscreen, tmpl);
706 struct pipe_resource *prsc = &rsc->base;
707 /* Use a tiled layout if we can, for better 3D performance. */
708 bool should_tile = true;
709
710 /* VBOs/PBOs are untiled (and 1 height). */
711 if (tmpl->target == PIPE_BUFFER)
712 should_tile = false;
713
714 /* Cursors are always linear, and the user can request linear as well.
715 */
716 if (tmpl->bind & (PIPE_BIND_LINEAR | PIPE_BIND_CURSOR))
717 should_tile = false;
718
719 /* 1D and 1D_ARRAY textures are always raster-order. */
720 if (tmpl->target == PIPE_TEXTURE_1D ||
721 tmpl->target == PIPE_TEXTURE_1D_ARRAY)
722 should_tile = false;
723
724 /* Scanout BOs for simulator need to be linear for interaction with
725 * i965.
726 */
727 if (using_v3d_simulator &&
728 tmpl->bind & (PIPE_BIND_SHARED | PIPE_BIND_SCANOUT))
729 should_tile = false;
730
731 /* If using the old-school SCANOUT flag, we don't know what the screen
732 * might support other than linear. Just force linear.
733 */
734 if (tmpl->bind & PIPE_BIND_SCANOUT)
735 should_tile = false;
736
737 /* No user-specified modifier; determine our own. */
738 if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
739 linear_ok = true;
740 rsc->tiled = should_tile;
741 } else if (should_tile &&
742 drm_find_modifier(DRM_FORMAT_MOD_BROADCOM_UIF,
743 modifiers, count)) {
744 rsc->tiled = true;
745 } else if (linear_ok) {
746 rsc->tiled = false;
747 } else {
748 fprintf(stderr, "Unsupported modifier requested\n");
749 goto fail;
750 }
751
752 rsc->internal_format = prsc->format;
753
754 v3d_setup_slices(rsc, 0, tmpl->bind & PIPE_BIND_SHARED);
755
756 /* If we're in a renderonly setup, use the other device to perform our
757 * allocation and just import it to v3d. The other device may be
758 * using CMA, and V3D can import from CMA but doesn't do CMA
759 * allocations on its own.
760 *
761 * We always allocate this way for SHARED, because get_handle will
762 * need a resource on the display fd.
763 */
764 if (screen->ro && (tmpl->bind & (PIPE_BIND_SCANOUT |
765 PIPE_BIND_SHARED))) {
766 struct winsys_handle handle;
767 struct pipe_resource scanout_tmpl = {
768 .target = prsc->target,
769 .format = PIPE_FORMAT_RGBA8888_UNORM,
770 .width0 = 1024, /* one page */
771 .height0 = align(rsc->size, 4096) / 4096,
772 .depth0 = 1,
773 .array_size = 1,
774 };
775
776 rsc->scanout =
777 renderonly_scanout_for_resource(&scanout_tmpl,
778 screen->ro,
779 &handle);
780
781 if (!rsc->scanout) {
782 fprintf(stderr, "Failed to create scanout resource\n");
783 return NULL;
784 }
785 assert(handle.type == WINSYS_HANDLE_TYPE_FD);
786 rsc->bo = v3d_bo_open_dmabuf(screen, handle.handle);
787 close(handle.handle);
788
789 if (!rsc->bo)
790 goto fail;
791
792 v3d_debug_resource_layout(rsc, "renderonly");
793
794 return prsc;
795 } else {
796 if (!v3d_resource_bo_alloc(rsc))
797 goto fail;
798 }
799
800 return prsc;
801 fail:
802 v3d_resource_destroy(pscreen, prsc);
803 return NULL;
804 }
805
806 struct pipe_resource *
807 v3d_resource_create(struct pipe_screen *pscreen,
808 const struct pipe_resource *tmpl)
809 {
810 const uint64_t mod = DRM_FORMAT_MOD_INVALID;
811 return v3d_resource_create_with_modifiers(pscreen, tmpl, &mod, 1);
812 }
813
814 static struct pipe_resource *
815 v3d_resource_from_handle(struct pipe_screen *pscreen,
816 const struct pipe_resource *tmpl,
817 struct winsys_handle *whandle,
818 unsigned usage)
819 {
820 struct v3d_screen *screen = v3d_screen(pscreen);
821 struct v3d_resource *rsc = v3d_resource_setup(pscreen, tmpl);
822 struct pipe_resource *prsc = &rsc->base;
823 struct v3d_resource_slice *slice = &rsc->slices[0];
824
825 if (!rsc)
826 return NULL;
827
828 switch (whandle->modifier) {
829 case DRM_FORMAT_MOD_LINEAR:
830 rsc->tiled = false;
831 break;
832 case DRM_FORMAT_MOD_BROADCOM_UIF:
833 rsc->tiled = true;
834 break;
835 case DRM_FORMAT_MOD_INVALID:
836 rsc->tiled = screen->ro == NULL;
837 break;
838 default:
839 fprintf(stderr,
840 "Attempt to import unsupported modifier 0x%llx\n",
841 (long long)whandle->modifier);
842 goto fail;
843 }
844
845 switch (whandle->type) {
846 case WINSYS_HANDLE_TYPE_SHARED:
847 rsc->bo = v3d_bo_open_name(screen, whandle->handle);
848 break;
849 case WINSYS_HANDLE_TYPE_FD:
850 rsc->bo = v3d_bo_open_dmabuf(screen, whandle->handle);
851 break;
852 default:
853 fprintf(stderr,
854 "Attempt to import unsupported handle type %d\n",
855 whandle->type);
856 goto fail;
857 }
858
859 if (!rsc->bo)
860 goto fail;
861
862 rsc->internal_format = prsc->format;
863
864 v3d_setup_slices(rsc, whandle->stride, true);
865 v3d_debug_resource_layout(rsc, "import");
866
867 if (whandle->offset != 0) {
868 if (rsc->tiled) {
869 fprintf(stderr,
870 "Attempt to import unsupported winsys offset %u\n",
871 whandle->offset);
872 goto fail;
873 }
874 rsc->slices[0].offset += whandle->offset;
875
876 if (rsc->slices[0].offset + rsc->slices[0].size >
877 rsc->bo->size) {
878 fprintf(stderr, "Attempt to import "
879 "with overflowing offset (%d + %d > %d)\n",
880 whandle->offset,
881 rsc->slices[0].size,
882 rsc->bo->size);
883 goto fail;
884 }
885 }
886
887 if (screen->ro) {
888 /* Make sure that renderonly has a handle to our buffer in the
889 * display's fd, so that a later renderonly_get_handle()
890 * returns correct handles or GEM names.
891 */
892 rsc->scanout =
893 renderonly_create_gpu_import_for_resource(prsc,
894 screen->ro,
895 NULL);
896 if (!rsc->scanout) {
897 fprintf(stderr, "Failed to create scanout resource.\n");
898 goto fail;
899 }
900 }
901
902 if (rsc->tiled && whandle->stride != slice->stride) {
903 static bool warned = false;
904 if (!warned) {
905 warned = true;
906 fprintf(stderr,
907 "Attempting to import %dx%d %s with "
908 "unsupported stride %d instead of %d\n",
909 prsc->width0, prsc->height0,
910 util_format_short_name(prsc->format),
911 whandle->stride,
912 slice->stride);
913 }
914 goto fail;
915 } else if (!rsc->tiled) {
916 slice->stride = whandle->stride;
917 }
918
919 return prsc;
920
921 fail:
922 v3d_resource_destroy(pscreen, prsc);
923 return NULL;
924 }
925
926 void
927 v3d_update_shadow_texture(struct pipe_context *pctx,
928 struct pipe_sampler_view *pview)
929 {
930 struct v3d_context *v3d = v3d_context(pctx);
931 struct v3d_sampler_view *view = v3d_sampler_view(pview);
932 struct v3d_resource *shadow = v3d_resource(view->texture);
933 struct v3d_resource *orig = v3d_resource(pview->texture);
934
935 assert(view->texture != pview->texture);
936
937 if (shadow->writes == orig->writes && orig->bo->private)
938 return;
939
940 perf_debug("Updating %dx%d@%d shadow for linear texture\n",
941 orig->base.width0, orig->base.height0,
942 pview->u.tex.first_level);
943
944 for (int i = 0; i <= shadow->base.last_level; i++) {
945 unsigned width = u_minify(shadow->base.width0, i);
946 unsigned height = u_minify(shadow->base.height0, i);
947 struct pipe_blit_info info = {
948 .dst = {
949 .resource = &shadow->base,
950 .level = i,
951 .box = {
952 .x = 0,
953 .y = 0,
954 .z = 0,
955 .width = width,
956 .height = height,
957 .depth = 1,
958 },
959 .format = shadow->base.format,
960 },
961 .src = {
962 .resource = &orig->base,
963 .level = pview->u.tex.first_level + i,
964 .box = {
965 .x = 0,
966 .y = 0,
967 .z = 0,
968 .width = width,
969 .height = height,
970 .depth = 1,
971 },
972 .format = orig->base.format,
973 },
974 .mask = util_format_get_mask(orig->base.format),
975 };
976 pctx->blit(pctx, &info);
977 }
978
979 shadow->writes = orig->writes;
980 }
981
982 static struct pipe_surface *
983 v3d_create_surface(struct pipe_context *pctx,
984 struct pipe_resource *ptex,
985 const struct pipe_surface *surf_tmpl)
986 {
987 struct v3d_context *v3d = v3d_context(pctx);
988 struct v3d_screen *screen = v3d->screen;
989 struct v3d_surface *surface = CALLOC_STRUCT(v3d_surface);
990 struct v3d_resource *rsc = v3d_resource(ptex);
991
992 if (!surface)
993 return NULL;
994
995 struct pipe_surface *psurf = &surface->base;
996 unsigned level = surf_tmpl->u.tex.level;
997 struct v3d_resource_slice *slice = &rsc->slices[level];
998
999 pipe_reference_init(&psurf->reference, 1);
1000 pipe_resource_reference(&psurf->texture, ptex);
1001
1002 psurf->context = pctx;
1003 psurf->format = surf_tmpl->format;
1004 psurf->width = u_minify(ptex->width0, level);
1005 psurf->height = u_minify(ptex->height0, level);
1006 psurf->u.tex.level = level;
1007 psurf->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
1008 psurf->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
1009
1010 surface->offset = v3d_layer_offset(ptex, level,
1011 psurf->u.tex.first_layer);
1012 surface->tiling = slice->tiling;
1013
1014 surface->format = v3d_get_rt_format(&screen->devinfo, psurf->format);
1015
1016 const struct util_format_description *desc =
1017 util_format_description(psurf->format);
1018
1019 surface->swap_rb = (desc->swizzle[0] == PIPE_SWIZZLE_Z &&
1020 psurf->format != PIPE_FORMAT_B5G6R5_UNORM);
1021
1022 if (util_format_is_depth_or_stencil(psurf->format)) {
1023 switch (psurf->format) {
1024 case PIPE_FORMAT_Z16_UNORM:
1025 surface->internal_type = V3D_INTERNAL_TYPE_DEPTH_16;
1026 break;
1027 case PIPE_FORMAT_Z32_FLOAT:
1028 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1029 surface->internal_type = V3D_INTERNAL_TYPE_DEPTH_32F;
1030 break;
1031 default:
1032 surface->internal_type = V3D_INTERNAL_TYPE_DEPTH_24;
1033 }
1034 } else {
1035 uint32_t bpp, type;
1036 v3d_get_internal_type_bpp_for_output_format(&screen->devinfo,
1037 surface->format,
1038 &type, &bpp);
1039 surface->internal_type = type;
1040 surface->internal_bpp = bpp;
1041 }
1042
1043 if (surface->tiling == VC5_TILING_UIF_NO_XOR ||
1044 surface->tiling == VC5_TILING_UIF_XOR) {
1045 surface->padded_height_of_output_image_in_uif_blocks =
1046 (slice->padded_height /
1047 (2 * v3d_utile_height(rsc->cpp)));
1048 }
1049
1050 if (rsc->separate_stencil) {
1051 surface->separate_stencil =
1052 v3d_create_surface(pctx, &rsc->separate_stencil->base,
1053 surf_tmpl);
1054 }
1055
1056 return &surface->base;
1057 }
1058
1059 static void
1060 v3d_surface_destroy(struct pipe_context *pctx, struct pipe_surface *psurf)
1061 {
1062 struct v3d_surface *surf = v3d_surface(psurf);
1063
1064 if (surf->separate_stencil)
1065 pipe_surface_reference(&surf->separate_stencil, NULL);
1066
1067 pipe_resource_reference(&psurf->texture, NULL);
1068 FREE(psurf);
1069 }
1070
1071 static void
1072 v3d_flush_resource(struct pipe_context *pctx, struct pipe_resource *resource)
1073 {
1074 /* All calls to flush_resource are followed by a flush of the context,
1075 * so there's nothing to do.
1076 */
1077 }
1078
1079 static enum pipe_format
1080 v3d_resource_get_internal_format(struct pipe_resource *prsc)
1081 {
1082 return v3d_resource(prsc)->internal_format;
1083 }
1084
1085 static void
1086 v3d_resource_set_stencil(struct pipe_resource *prsc,
1087 struct pipe_resource *stencil)
1088 {
1089 v3d_resource(prsc)->separate_stencil = v3d_resource(stencil);
1090 }
1091
1092 static struct pipe_resource *
1093 v3d_resource_get_stencil(struct pipe_resource *prsc)
1094 {
1095 struct v3d_resource *rsc = v3d_resource(prsc);
1096
1097 return &rsc->separate_stencil->base;
1098 }
1099
1100 static const struct u_transfer_vtbl transfer_vtbl = {
1101 .resource_create = v3d_resource_create,
1102 .resource_destroy = v3d_resource_destroy,
1103 .transfer_map = v3d_resource_transfer_map,
1104 .transfer_unmap = v3d_resource_transfer_unmap,
1105 .transfer_flush_region = u_default_transfer_flush_region,
1106 .get_internal_format = v3d_resource_get_internal_format,
1107 .set_stencil = v3d_resource_set_stencil,
1108 .get_stencil = v3d_resource_get_stencil,
1109 };
1110
1111 void
1112 v3d_resource_screen_init(struct pipe_screen *pscreen)
1113 {
1114 pscreen->resource_create_with_modifiers =
1115 v3d_resource_create_with_modifiers;
1116 pscreen->resource_create = u_transfer_helper_resource_create;
1117 pscreen->resource_from_handle = v3d_resource_from_handle;
1118 pscreen->resource_get_handle = v3d_resource_get_handle;
1119 pscreen->resource_destroy = u_transfer_helper_resource_destroy;
1120 pscreen->transfer_helper = u_transfer_helper_create(&transfer_vtbl,
1121 true, false,
1122 true, true);
1123 }
1124
1125 void
1126 v3d_resource_context_init(struct pipe_context *pctx)
1127 {
1128 pctx->transfer_map = u_transfer_helper_transfer_map;
1129 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
1130 pctx->transfer_unmap = u_transfer_helper_transfer_unmap;
1131 pctx->buffer_subdata = u_default_buffer_subdata;
1132 pctx->texture_subdata = v3d_texture_subdata;
1133 pctx->create_surface = v3d_create_surface;
1134 pctx->surface_destroy = v3d_surface_destroy;
1135 pctx->resource_copy_region = util_resource_copy_region;
1136 pctx->blit = v3d_blit;
1137 pctx->generate_mipmap = v3d_generate_mipmap;
1138 pctx->flush_resource = v3d_flush_resource;
1139 }