544c032b018665236e790a5fc8a75f9bb339cbd7
[mesa.git] / src / gallium / drivers / vc4 / vc4_resource.c
1 /*
2 * Copyright © 2014 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 "util/u_memory.h"
26 #include "util/u_format.h"
27 #include "util/u_inlines.h"
28 #include "util/u_surface.h"
29 #include "util/u_blitter.h"
30
31 #include "vc4_screen.h"
32 #include "vc4_context.h"
33 #include "vc4_resource.h"
34 #include "vc4_tiling.h"
35
36 static bool miptree_debug = false;
37
38 static void
39 vc4_resource_bo_alloc(struct vc4_resource *rsc)
40 {
41 struct pipe_resource *prsc = &rsc->base.b;
42 struct pipe_screen *pscreen = prsc->screen;
43
44 if (miptree_debug) {
45 fprintf(stderr, "alloc %p: size %d + offset %d -> %d\n",
46 rsc,
47 rsc->slices[0].size,
48 rsc->slices[0].offset,
49 rsc->slices[0].offset +
50 rsc->slices[0].size +
51 rsc->cube_map_stride * (prsc->array_size - 1));
52 }
53
54 vc4_bo_unreference(&rsc->bo);
55 rsc->bo = vc4_bo_alloc(vc4_screen(pscreen),
56 rsc->slices[0].offset +
57 rsc->slices[0].size +
58 rsc->cube_map_stride * (prsc->array_size - 1),
59 "resource");
60 }
61
62 static void
63 vc4_resource_transfer_unmap(struct pipe_context *pctx,
64 struct pipe_transfer *ptrans)
65 {
66 struct vc4_context *vc4 = vc4_context(pctx);
67 struct vc4_transfer *trans = vc4_transfer(ptrans);
68 struct pipe_resource *prsc = ptrans->resource;
69 struct vc4_resource *rsc = vc4_resource(prsc);
70 struct vc4_resource_slice *slice = &rsc->slices[ptrans->level];
71
72 if (trans->map) {
73 if (ptrans->usage & PIPE_TRANSFER_WRITE) {
74 vc4_store_tiled_image(rsc->bo->map + slice->offset +
75 ptrans->box.z * rsc->cube_map_stride,
76 slice->stride,
77 trans->map, ptrans->stride,
78 slice->tiling, rsc->cpp,
79 &ptrans->box);
80 }
81 free(trans->map);
82 }
83
84 pipe_resource_reference(&ptrans->resource, NULL);
85 util_slab_free(&vc4->transfer_pool, ptrans);
86 }
87
88 static void *
89 vc4_resource_transfer_map(struct pipe_context *pctx,
90 struct pipe_resource *prsc,
91 unsigned level, unsigned usage,
92 const struct pipe_box *box,
93 struct pipe_transfer **pptrans)
94 {
95 struct vc4_context *vc4 = vc4_context(pctx);
96 struct vc4_resource *rsc = vc4_resource(prsc);
97 struct vc4_resource_slice *slice = &rsc->slices[level];
98 struct vc4_transfer *trans;
99 struct pipe_transfer *ptrans;
100 enum pipe_format format = prsc->format;
101 char *buf;
102
103 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
104 vc4_resource_bo_alloc(rsc);
105 } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
106 if (vc4_cl_references_bo(pctx, rsc->bo)) {
107 if ((usage & PIPE_TRANSFER_DISCARD_RANGE) &&
108 prsc->last_level == 0 &&
109 prsc->width0 == box->width &&
110 prsc->height0 == box->height &&
111 prsc->depth0 == box->depth) {
112 vc4_resource_bo_alloc(rsc);
113 } else {
114 vc4_flush(pctx);
115 }
116 }
117 }
118
119 if (usage & PIPE_TRANSFER_WRITE)
120 rsc->writes++;
121
122 trans = util_slab_alloc(&vc4->transfer_pool);
123 if (!trans)
124 return NULL;
125
126 /* XXX: Handle DONTBLOCK, DISCARD_RANGE, PERSISTENT, COHERENT. */
127
128 /* util_slab_alloc() doesn't zero: */
129 memset(trans, 0, sizeof(*trans));
130 ptrans = &trans->base;
131
132 pipe_resource_reference(&ptrans->resource, prsc);
133 ptrans->level = level;
134 ptrans->usage = usage;
135 ptrans->box = *box;
136
137 /* Note that the current kernel implementation is synchronous, so no
138 * need to do syncing stuff here yet.
139 */
140
141 if (usage & PIPE_TRANSFER_UNSYNCHRONIZED)
142 buf = vc4_bo_map_unsynchronized(rsc->bo);
143 else
144 buf = vc4_bo_map(rsc->bo);
145 if (!buf) {
146 fprintf(stderr, "Failed to map bo\n");
147 goto fail;
148 }
149
150 *pptrans = ptrans;
151
152 if (rsc->tiled) {
153 uint32_t utile_w = vc4_utile_width(rsc->cpp);
154 uint32_t utile_h = vc4_utile_height(rsc->cpp);
155
156 /* No direct mappings of tiled, since we need to manually
157 * tile/untile.
158 */
159 if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
160 return NULL;
161
162 /* We need to align the box to utile boundaries, since that's
163 * what load/store operate on.
164 */
165 uint32_t box_start_x = ptrans->box.x & (utile_w - 1);
166 uint32_t box_start_y = ptrans->box.y & (utile_h - 1);
167 ptrans->box.width += box_start_x;
168 ptrans->box.x -= box_start_x;
169 ptrans->box.height += box_start_y;
170 ptrans->box.y -= box_start_y;
171 ptrans->box.width = align(ptrans->box.width, utile_w);
172 ptrans->box.height = align(ptrans->box.height, utile_h);
173
174 ptrans->stride = ptrans->box.width * rsc->cpp;
175 ptrans->layer_stride = ptrans->stride;
176
177 trans->map = malloc(ptrans->stride * ptrans->box.height);
178 if (usage & PIPE_TRANSFER_READ) {
179 vc4_load_tiled_image(trans->map, ptrans->stride,
180 buf + slice->offset +
181 box->z * rsc->cube_map_stride,
182 slice->stride,
183 slice->tiling, rsc->cpp,
184 &ptrans->box);
185 }
186 return (trans->map +
187 box_start_x * rsc->cpp +
188 box_start_y * ptrans->stride);
189 } else {
190 ptrans->stride = slice->stride;
191 ptrans->layer_stride = ptrans->stride;
192
193 return buf + slice->offset +
194 box->y / util_format_get_blockheight(format) * ptrans->stride +
195 box->x / util_format_get_blockwidth(format) * rsc->cpp +
196 box->z * rsc->cube_map_stride;
197 }
198
199
200 fail:
201 vc4_resource_transfer_unmap(pctx, ptrans);
202 return NULL;
203 }
204
205 static void
206 vc4_resource_destroy(struct pipe_screen *pscreen,
207 struct pipe_resource *prsc)
208 {
209 struct vc4_resource *rsc = vc4_resource(prsc);
210 pipe_resource_reference(&rsc->shadow_parent, NULL);
211 vc4_bo_unreference(&rsc->bo);
212 free(rsc);
213 }
214
215 static boolean
216 vc4_resource_get_handle(struct pipe_screen *pscreen,
217 struct pipe_resource *prsc,
218 struct winsys_handle *handle)
219 {
220 struct vc4_resource *rsc = vc4_resource(prsc);
221
222 return vc4_screen_bo_get_handle(pscreen, rsc->bo, rsc->slices[0].stride,
223 handle);
224 }
225
226 static const struct u_resource_vtbl vc4_resource_vtbl = {
227 .resource_get_handle = vc4_resource_get_handle,
228 .resource_destroy = vc4_resource_destroy,
229 .transfer_map = vc4_resource_transfer_map,
230 .transfer_flush_region = u_default_transfer_flush_region,
231 .transfer_unmap = vc4_resource_transfer_unmap,
232 .transfer_inline_write = u_default_transfer_inline_write,
233 };
234
235 static void
236 vc4_setup_slices(struct vc4_resource *rsc)
237 {
238 struct pipe_resource *prsc = &rsc->base.b;
239 uint32_t width = prsc->width0;
240 uint32_t height = prsc->height0;
241 uint32_t pot_width = util_next_power_of_two(width);
242 uint32_t pot_height = util_next_power_of_two(height);
243 uint32_t offset = 0;
244 uint32_t utile_w = vc4_utile_width(rsc->cpp);
245 uint32_t utile_h = vc4_utile_height(rsc->cpp);
246
247 for (int i = prsc->last_level; i >= 0; i--) {
248 struct vc4_resource_slice *slice = &rsc->slices[i];
249
250 uint32_t level_width, level_height;
251 if (i == 0) {
252 level_width = width;
253 level_height = height;
254 } else {
255 level_width = u_minify(pot_width, i);
256 level_height = u_minify(pot_height, i);
257 }
258
259 if (!rsc->tiled) {
260 slice->tiling = VC4_TILING_FORMAT_LINEAR;
261 level_width = align(level_width, utile_w);
262 } else {
263 if (vc4_size_is_lt(level_width, level_height,
264 rsc->cpp)) {
265 slice->tiling = VC4_TILING_FORMAT_LT;
266 level_width = align(level_width, utile_w);
267 level_height = align(level_height, utile_h);
268 } else {
269 slice->tiling = VC4_TILING_FORMAT_T;
270 level_width = align(level_width,
271 4 * 2 * utile_w);
272 level_height = align(level_height,
273 4 * 2 * utile_h);
274 }
275 }
276
277 slice->offset = offset;
278 slice->stride = level_width * rsc->cpp;
279 slice->size = level_height * slice->stride;
280
281 offset += slice->size;
282
283 if (miptree_debug) {
284 static const char tiling_chars[] = {
285 [VC4_TILING_FORMAT_LINEAR] = 'R',
286 [VC4_TILING_FORMAT_LT] = 'L',
287 [VC4_TILING_FORMAT_T] = 'T'
288 };
289 fprintf(stderr,
290 "rsc setup %p (format %d), %dx%d: "
291 "level %d (%c) -> %dx%d, stride %d@0x%08x\n",
292 rsc, rsc->vc4_format,
293 prsc->width0, prsc->height0,
294 i, tiling_chars[slice->tiling],
295 level_width, level_height,
296 slice->stride, slice->offset);
297 }
298 }
299
300 /* The texture base pointer that has to point to level 0 doesn't have
301 * intra-page bits, so we have to align it, and thus shift up all the
302 * smaller slices.
303 */
304 uint32_t page_align_offset = (align(rsc->slices[0].offset, 4096) -
305 rsc->slices[0].offset);
306 if (page_align_offset) {
307 for (int i = 0; i <= prsc->last_level; i++)
308 rsc->slices[i].offset += page_align_offset;
309 }
310
311 /* Cube map faces appear as whole miptrees at a page-aligned offset
312 * from the first face's miptree.
313 */
314 if (prsc->target == PIPE_TEXTURE_CUBE) {
315 rsc->cube_map_stride = align(rsc->slices[0].offset +
316 rsc->slices[0].size, 4096);
317 }
318 }
319
320 static struct vc4_resource *
321 vc4_resource_setup(struct pipe_screen *pscreen,
322 const struct pipe_resource *tmpl)
323 {
324 struct vc4_resource *rsc = CALLOC_STRUCT(vc4_resource);
325 if (!rsc)
326 return NULL;
327 struct pipe_resource *prsc = &rsc->base.b;
328
329 *prsc = *tmpl;
330
331 pipe_reference_init(&prsc->reference, 1);
332 prsc->screen = pscreen;
333
334 rsc->base.vtbl = &vc4_resource_vtbl;
335 rsc->cpp = util_format_get_blocksize(tmpl->format);
336
337 assert(rsc->cpp);
338
339 return rsc;
340 }
341
342 static enum vc4_texture_data_type
343 get_resource_texture_format(struct pipe_resource *prsc)
344 {
345 struct vc4_resource *rsc = vc4_resource(prsc);
346 uint8_t format = vc4_get_tex_format(prsc->format);
347
348 if (!rsc->tiled) {
349 assert(format == VC4_TEXTURE_TYPE_RGBA8888);
350 return VC4_TEXTURE_TYPE_RGBA32R;
351 }
352
353 return format;
354 }
355
356 struct pipe_resource *
357 vc4_resource_create(struct pipe_screen *pscreen,
358 const struct pipe_resource *tmpl)
359 {
360 struct vc4_resource *rsc = vc4_resource_setup(pscreen, tmpl);
361 struct pipe_resource *prsc = &rsc->base.b;
362
363 /* We have to make shared be untiled, since we don't have any way to
364 * communicate metadata about tiling currently.
365 */
366 if (tmpl->target == PIPE_BUFFER ||
367 (tmpl->bind & (PIPE_BIND_SCANOUT |
368 PIPE_BIND_LINEAR |
369 PIPE_BIND_SHARED |
370 PIPE_BIND_CURSOR))) {
371 rsc->tiled = false;
372 } else {
373 rsc->tiled = true;
374 }
375
376 if (tmpl->target != PIPE_BUFFER)
377 rsc->vc4_format = get_resource_texture_format(prsc);
378
379 vc4_setup_slices(rsc);
380 vc4_resource_bo_alloc(rsc);
381 if (!rsc->bo)
382 goto fail;
383
384 return prsc;
385 fail:
386 vc4_resource_destroy(pscreen, prsc);
387 return NULL;
388 }
389
390 static struct pipe_resource *
391 vc4_resource_from_handle(struct pipe_screen *pscreen,
392 const struct pipe_resource *tmpl,
393 struct winsys_handle *handle)
394 {
395 struct vc4_resource *rsc = vc4_resource_setup(pscreen, tmpl);
396 struct pipe_resource *prsc = &rsc->base.b;
397 struct vc4_resource_slice *slice = &rsc->slices[0];
398
399 if (!rsc)
400 return NULL;
401
402 rsc->tiled = false;
403 rsc->bo = vc4_screen_bo_from_handle(pscreen, handle);
404 if (!rsc->bo)
405 goto fail;
406
407 if (!using_vc4_simulator)
408 slice->stride = handle->stride;
409 else
410 slice->stride = align(prsc->width0 * rsc->cpp, 16);
411
412 slice->tiling = VC4_TILING_FORMAT_LINEAR;
413
414 rsc->vc4_format = get_resource_texture_format(prsc);
415
416 if (miptree_debug) {
417 fprintf(stderr,
418 "rsc import %p (format %d), %dx%d: "
419 "level 0 (R) -> stride %d@0x%08x\n",
420 rsc, rsc->vc4_format,
421 prsc->width0, prsc->height0,
422 slice->stride, slice->offset);
423 }
424
425 return prsc;
426
427 fail:
428 vc4_resource_destroy(pscreen, prsc);
429 return NULL;
430 }
431
432 static struct pipe_surface *
433 vc4_create_surface(struct pipe_context *pctx,
434 struct pipe_resource *ptex,
435 const struct pipe_surface *surf_tmpl)
436 {
437 struct vc4_surface *surface = CALLOC_STRUCT(vc4_surface);
438 struct vc4_resource *rsc = vc4_resource(ptex);
439
440 if (!surface)
441 return NULL;
442
443 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
444
445 struct pipe_surface *psurf = &surface->base;
446 unsigned level = surf_tmpl->u.tex.level;
447
448 pipe_reference_init(&psurf->reference, 1);
449 pipe_resource_reference(&psurf->texture, ptex);
450
451 psurf->context = pctx;
452 psurf->format = surf_tmpl->format;
453 psurf->width = u_minify(ptex->width0, level);
454 psurf->height = u_minify(ptex->height0, level);
455 psurf->u.tex.level = level;
456 psurf->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
457 psurf->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
458 surface->offset = rsc->slices[level].offset;
459 surface->tiling = rsc->slices[level].tiling;
460
461 return &surface->base;
462 }
463
464 static void
465 vc4_surface_destroy(struct pipe_context *pctx, struct pipe_surface *psurf)
466 {
467 pipe_resource_reference(&psurf->texture, NULL);
468 FREE(psurf);
469 }
470
471 static void
472 vc4_flush_resource(struct pipe_context *pctx, struct pipe_resource *resource)
473 {
474 /* All calls to flush_resource are followed by a flush of the context,
475 * so there's nothing to do.
476 */
477 }
478
479 static bool
480 render_blit(struct pipe_context *ctx, struct pipe_blit_info *info)
481 {
482 struct vc4_context *vc4 = vc4_context(ctx);
483
484 if (!util_blitter_is_blit_supported(vc4->blitter, info)) {
485 fprintf(stderr, "blit unsupported %s -> %s",
486 util_format_short_name(info->src.resource->format),
487 util_format_short_name(info->dst.resource->format));
488 return false;
489 }
490
491 util_blitter_save_vertex_buffer_slot(vc4->blitter, vc4->vertexbuf.vb);
492 util_blitter_save_vertex_elements(vc4->blitter, vc4->vtx);
493 util_blitter_save_vertex_shader(vc4->blitter, vc4->prog.bind_vs);
494 util_blitter_save_rasterizer(vc4->blitter, vc4->rasterizer);
495 util_blitter_save_viewport(vc4->blitter, &vc4->viewport);
496 util_blitter_save_scissor(vc4->blitter, &vc4->scissor);
497 util_blitter_save_fragment_shader(vc4->blitter, vc4->prog.bind_fs);
498 util_blitter_save_blend(vc4->blitter, vc4->blend);
499 util_blitter_save_depth_stencil_alpha(vc4->blitter, vc4->zsa);
500 util_blitter_save_stencil_ref(vc4->blitter, &vc4->stencil_ref);
501 util_blitter_save_sample_mask(vc4->blitter, vc4->sample_mask);
502 util_blitter_save_framebuffer(vc4->blitter, &vc4->framebuffer);
503 util_blitter_save_fragment_sampler_states(vc4->blitter,
504 vc4->fragtex.num_samplers,
505 (void **)vc4->fragtex.samplers);
506 util_blitter_save_fragment_sampler_views(vc4->blitter,
507 vc4->fragtex.num_textures, vc4->fragtex.textures);
508
509 util_blitter_blit(vc4->blitter, info);
510
511 return true;
512 }
513
514 /* Optimal hardware path for blitting pixels.
515 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
516 */
517 static void
518 vc4_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
519 {
520 struct pipe_blit_info info = *blit_info;
521
522 if (info.src.resource->nr_samples > 1 &&
523 info.dst.resource->nr_samples <= 1 &&
524 !util_format_is_depth_or_stencil(info.src.resource->format) &&
525 !util_format_is_pure_integer(info.src.resource->format)) {
526 fprintf(stderr, "color resolve unimplemented");
527 return;
528 }
529
530 if (util_try_blit_via_copy_region(pctx, &info)) {
531 return; /* done */
532 }
533
534 if (info.mask & PIPE_MASK_S) {
535 fprintf(stderr, "cannot blit stencil, skipping");
536 info.mask &= ~PIPE_MASK_S;
537 }
538
539 render_blit(pctx, &info);
540 }
541
542 void
543 vc4_update_shadow_baselevel_texture(struct pipe_context *pctx,
544 struct pipe_sampler_view *view)
545 {
546 struct vc4_resource *shadow = vc4_resource(view->texture);
547 struct vc4_resource *orig = vc4_resource(shadow->shadow_parent);
548 assert(orig);
549
550 if (shadow->writes == orig->writes)
551 return;
552
553 for (int i = 0; i <= shadow->base.b.last_level; i++) {
554 struct pipe_box box = {
555 .x = 0,
556 .y = 0,
557 .z = 0,
558 .width = u_minify(shadow->base.b.width0, i),
559 .height = u_minify(shadow->base.b.height0, i),
560 .depth = 1,
561 };
562
563 util_resource_copy_region(pctx,
564 &shadow->base.b, i, 0, 0, 0,
565 &orig->base.b,
566 view->u.tex.first_level + i,
567 &box);
568 }
569
570 shadow->writes = orig->writes;
571 }
572
573 /**
574 * Converts a 4-byte index buffer to 2 bytes.
575 *
576 * Since GLES2 only has support for 1 and 2-byte indices, the hardware doesn't
577 * include 4-byte index support, and we have to shrink it down.
578 *
579 * There's no fallback support for when indices end up being larger than 2^16,
580 * though it will at least assertion fail. Also, if the original index data
581 * was in user memory, it would be nice to not have uploaded it to a VBO
582 * before translating.
583 */
584 void
585 vc4_update_shadow_index_buffer(struct pipe_context *pctx,
586 const struct pipe_index_buffer *ib)
587 {
588 struct vc4_resource *shadow = vc4_resource(ib->buffer);
589 struct vc4_resource *orig = vc4_resource(shadow->shadow_parent);
590 uint32_t count = shadow->base.b.width0 / 2;
591
592 if (shadow->writes == orig->writes)
593 return;
594
595 struct pipe_transfer *src_transfer;
596 uint32_t *src = pipe_buffer_map_range(pctx, &orig->base.b,
597 ib->offset,
598 count * 4,
599 PIPE_TRANSFER_READ, &src_transfer);
600
601 struct pipe_transfer *dst_transfer;
602 uint16_t *dst = pipe_buffer_map_range(pctx, &shadow->base.b,
603 0,
604 count * 2,
605 PIPE_TRANSFER_WRITE, &dst_transfer);
606
607 for (int i = 0; i < count; i++) {
608 uint32_t src_index = src[i];
609 assert(src_index <= 0xffff);
610 dst[i] = src_index;
611 }
612
613 pctx->transfer_unmap(pctx, dst_transfer);
614 pctx->transfer_unmap(pctx, src_transfer);
615
616 shadow->writes = orig->writes;
617 }
618
619 void
620 vc4_resource_screen_init(struct pipe_screen *pscreen)
621 {
622 pscreen->resource_create = vc4_resource_create;
623 pscreen->resource_from_handle = vc4_resource_from_handle;
624 pscreen->resource_get_handle = u_resource_get_handle_vtbl;
625 pscreen->resource_destroy = u_resource_destroy_vtbl;
626 }
627
628 void
629 vc4_resource_context_init(struct pipe_context *pctx)
630 {
631 pctx->transfer_map = u_transfer_map_vtbl;
632 pctx->transfer_flush_region = u_transfer_flush_region_vtbl;
633 pctx->transfer_unmap = u_transfer_unmap_vtbl;
634 pctx->transfer_inline_write = u_transfer_inline_write_vtbl;
635 pctx->create_surface = vc4_create_surface;
636 pctx->surface_destroy = vc4_surface_destroy;
637 pctx->resource_copy_region = util_resource_copy_region;
638 pctx->blit = vc4_blit;
639 pctx->flush_resource = vc4_flush_resource;
640 }