2 * Copyright © 2014 Broadcom
3 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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:
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
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
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"
31 #include "vc4_screen.h"
32 #include "vc4_context.h"
33 #include "vc4_resource.h"
34 #include "vc4_tiling.h"
37 vc4_resource_transfer_unmap(struct pipe_context
*pctx
,
38 struct pipe_transfer
*ptrans
)
40 struct vc4_context
*vc4
= vc4_context(pctx
);
41 struct vc4_transfer
*trans
= vc4_transfer(ptrans
);
42 struct pipe_resource
*prsc
= ptrans
->resource
;
43 struct vc4_resource
*rsc
= vc4_resource(prsc
);
44 struct vc4_resource_slice
*slice
= &rsc
->slices
[ptrans
->level
];
47 if (ptrans
->usage
& PIPE_TRANSFER_WRITE
) {
48 vc4_store_tiled_image(rsc
->bo
->map
+ slice
->offset
,
50 trans
->map
, ptrans
->stride
,
51 slice
->tiling
, rsc
->cpp
,
57 pipe_resource_reference(&ptrans
->resource
, NULL
);
58 util_slab_free(&vc4
->transfer_pool
, ptrans
);
62 vc4_resource_transfer_map(struct pipe_context
*pctx
,
63 struct pipe_resource
*prsc
,
64 unsigned level
, unsigned usage
,
65 const struct pipe_box
*box
,
66 struct pipe_transfer
**pptrans
)
68 struct vc4_context
*vc4
= vc4_context(pctx
);
69 struct vc4_resource
*rsc
= vc4_resource(prsc
);
70 struct vc4_resource_slice
*slice
= &rsc
->slices
[level
];
71 struct vc4_transfer
*trans
;
72 struct pipe_transfer
*ptrans
;
73 enum pipe_format format
= prsc
->format
;
76 vc4_flush_for_bo(pctx
, rsc
->bo
);
78 trans
= util_slab_alloc(&vc4
->transfer_pool
);
82 /* XXX: Handle DISCARD_WHOLE_RESOURCE, DONTBLOCK, UNSYNCHRONIZED,
83 * DISCARD_WHOLE_RESOURCE, PERSISTENT, COHERENT.
86 /* util_slab_alloc() doesn't zero: */
87 memset(trans
, 0, sizeof(*trans
));
88 ptrans
= &trans
->base
;
90 pipe_resource_reference(&ptrans
->resource
, prsc
);
91 ptrans
->level
= level
;
92 ptrans
->usage
= usage
;
95 /* Note that the current kernel implementation is synchronous, so no
96 * need to do syncing stuff here yet.
99 buf
= vc4_bo_map(rsc
->bo
);
101 fprintf(stderr
, "Failed to map bo\n");
108 uint32_t utile_w
= vc4_utile_width(rsc
->cpp
);
109 uint32_t utile_h
= vc4_utile_height(rsc
->cpp
);
111 /* No direct mappings of tiled, since we need to manually
114 if (usage
& PIPE_TRANSFER_MAP_DIRECTLY
)
117 /* We need to align the box to utile boundaries, since that's
118 * what load/store operate on.
120 uint32_t box_start_x
= ptrans
->box
.x
& (utile_w
- 1);
121 uint32_t box_start_y
= ptrans
->box
.y
& (utile_h
- 1);
122 ptrans
->box
.width
+= box_start_x
;
123 ptrans
->box
.x
-= box_start_x
;
124 ptrans
->box
.height
+= box_start_y
;
125 ptrans
->box
.y
-= box_start_y
;
126 ptrans
->box
.width
= align(ptrans
->box
.width
, utile_w
);
127 ptrans
->box
.height
= align(ptrans
->box
.height
, utile_h
);
129 ptrans
->stride
= ptrans
->box
.width
* rsc
->cpp
;
130 ptrans
->layer_stride
= ptrans
->stride
;
132 trans
->map
= malloc(ptrans
->stride
* ptrans
->box
.height
);
133 if (usage
& PIPE_TRANSFER_READ
) {
134 vc4_load_tiled_image(trans
->map
, ptrans
->stride
,
137 slice
->tiling
, rsc
->cpp
,
141 box_start_x
* rsc
->cpp
+
142 box_start_y
* ptrans
->stride
);
144 ptrans
->stride
= slice
->stride
;
145 ptrans
->layer_stride
= ptrans
->stride
;
147 return buf
+ slice
->offset
+
148 box
->y
/ util_format_get_blockheight(format
) * ptrans
->stride
+
149 box
->x
/ util_format_get_blockwidth(format
) * rsc
->cpp
+
150 box
->z
* slice
->size0
;
155 vc4_resource_transfer_unmap(pctx
, ptrans
);
160 vc4_resource_destroy(struct pipe_screen
*pscreen
,
161 struct pipe_resource
*prsc
)
163 struct vc4_resource
*rsc
= vc4_resource(prsc
);
164 vc4_bo_unreference(&rsc
->bo
);
169 vc4_resource_get_handle(struct pipe_screen
*pscreen
,
170 struct pipe_resource
*prsc
,
171 struct winsys_handle
*handle
)
173 struct vc4_resource
*rsc
= vc4_resource(prsc
);
175 return vc4_screen_bo_get_handle(pscreen
, rsc
->bo
, rsc
->slices
[0].stride
,
179 static const struct u_resource_vtbl vc4_resource_vtbl
= {
180 .resource_get_handle
= vc4_resource_get_handle
,
181 .resource_destroy
= vc4_resource_destroy
,
182 .transfer_map
= vc4_resource_transfer_map
,
183 .transfer_flush_region
= u_default_transfer_flush_region
,
184 .transfer_unmap
= vc4_resource_transfer_unmap
,
185 .transfer_inline_write
= u_default_transfer_inline_write
,
189 vc4_setup_slices(struct vc4_resource
*rsc
)
191 struct pipe_resource
*prsc
= &rsc
->base
.b
;
192 uint32_t width
= prsc
->width0
;
193 uint32_t height
= prsc
->height0
;
194 uint32_t depth
= prsc
->depth0
;
196 uint32_t utile_w
= vc4_utile_width(rsc
->cpp
);
197 uint32_t utile_h
= vc4_utile_height(rsc
->cpp
);
199 for (int i
= prsc
->last_level
; i
>= 0; i
--) {
200 struct vc4_resource_slice
*slice
= &rsc
->slices
[i
];
201 uint32_t level_width
= u_minify(width
, i
);
202 uint32_t level_height
= u_minify(height
, i
);
204 if (rsc
->tiled
== VC4_TILING_FORMAT_LINEAR
) {
205 slice
->tiling
= VC4_TILING_FORMAT_LINEAR
;
206 level_width
= align(level_width
, 16);
208 if (vc4_size_is_lt(level_width
, level_height
,
210 slice
->tiling
= VC4_TILING_FORMAT_LT
;
211 level_width
= align(level_width
, utile_w
);
212 level_height
= align(level_height
, utile_h
);
214 slice
->tiling
= VC4_TILING_FORMAT_T
;
215 level_width
= align(level_width
,
217 level_height
= align(level_height
,
222 slice
->offset
= offset
;
223 slice
->stride
= level_width
* rsc
->cpp
;
224 slice
->size0
= level_height
* slice
->stride
;
226 /* Note, since we have cubes but no 3D, depth is invariant
229 offset
+= slice
->size0
* depth
;
232 /* The texture base pointer that has to point to level 0 doesn't have
233 * intra-page bits, so we have to align it, and thus shift up all the
236 uint32_t page_align_offset
= (align(rsc
->slices
[0].offset
, 4096) -
237 rsc
->slices
[0].offset
);
238 if (page_align_offset
) {
239 for (int i
= 0; i
<= prsc
->last_level
; i
++)
240 rsc
->slices
[i
].offset
+= page_align_offset
;
244 static struct vc4_resource
*
245 vc4_resource_setup(struct pipe_screen
*pscreen
,
246 const struct pipe_resource
*tmpl
)
248 struct vc4_resource
*rsc
= CALLOC_STRUCT(vc4_resource
);
251 struct pipe_resource
*prsc
= &rsc
->base
.b
;
255 pipe_reference_init(&prsc
->reference
, 1);
256 prsc
->screen
= pscreen
;
258 rsc
->base
.vtbl
= &vc4_resource_vtbl
;
259 rsc
->cpp
= util_format_get_blocksize(tmpl
->format
);
266 static enum vc4_texture_data_type
267 get_resource_texture_format(struct pipe_resource
*prsc
)
269 struct vc4_resource
*rsc
= vc4_resource(prsc
);
270 uint8_t format
= vc4_get_tex_format(prsc
->format
);
273 assert(format
== VC4_TEXTURE_TYPE_RGBA8888
);
274 return VC4_TEXTURE_TYPE_RGBA32R
;
280 static struct pipe_resource
*
281 vc4_resource_create(struct pipe_screen
*pscreen
,
282 const struct pipe_resource
*tmpl
)
284 struct vc4_resource
*rsc
= vc4_resource_setup(pscreen
, tmpl
);
285 struct pipe_resource
*prsc
= &rsc
->base
.b
;
287 /* We have to make shared be untiled, since we don't have any way to
288 * communicate metadata about tiling currently.
290 if (tmpl
->target
== PIPE_BUFFER
||
291 (tmpl
->bind
& (PIPE_BIND_SCANOUT
|
294 PIPE_BIND_CURSOR
))) {
300 vc4_setup_slices(rsc
);
302 rsc
->bo
= vc4_bo_alloc(vc4_screen(pscreen
),
303 rsc
->slices
[0].offset
+
304 rsc
->slices
[0].size0
* prsc
->depth0
,
309 if (tmpl
->target
!= PIPE_BUFFER
)
310 rsc
->vc4_format
= get_resource_texture_format(prsc
);
314 vc4_resource_destroy(pscreen
, prsc
);
318 static struct pipe_resource
*
319 vc4_resource_from_handle(struct pipe_screen
*pscreen
,
320 const struct pipe_resource
*tmpl
,
321 struct winsys_handle
*handle
)
323 struct vc4_resource
*rsc
= vc4_resource_setup(pscreen
, tmpl
);
324 struct pipe_resource
*prsc
= &rsc
->base
.b
;
325 struct vc4_resource_slice
*slice
= &rsc
->slices
[0];
331 rsc
->bo
= vc4_screen_bo_from_handle(pscreen
, handle
, &slice
->stride
);
335 #ifdef USE_VC4_SIMULATOR
336 slice
->stride
= align(prsc
->width0
* rsc
->cpp
, 16);
338 slice
->tiling
= VC4_TILING_FORMAT_LINEAR
;
340 rsc
->vc4_format
= get_resource_texture_format(prsc
);
345 vc4_resource_destroy(pscreen
, prsc
);
349 static struct pipe_surface
*
350 vc4_create_surface(struct pipe_context
*pctx
,
351 struct pipe_resource
*ptex
,
352 const struct pipe_surface
*surf_tmpl
)
354 struct vc4_surface
*surface
= CALLOC_STRUCT(vc4_surface
);
355 struct vc4_resource
*rsc
= vc4_resource(ptex
);
360 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
362 struct pipe_surface
*psurf
= &surface
->base
;
363 unsigned level
= surf_tmpl
->u
.tex
.level
;
365 pipe_reference_init(&psurf
->reference
, 1);
366 pipe_resource_reference(&psurf
->texture
, ptex
);
368 psurf
->context
= pctx
;
369 psurf
->format
= surf_tmpl
->format
;
370 psurf
->width
= u_minify(ptex
->width0
, level
);
371 psurf
->height
= u_minify(ptex
->height0
, level
);
372 psurf
->u
.tex
.level
= level
;
373 psurf
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
374 psurf
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
375 surface
->offset
= rsc
->slices
[level
].offset
;
376 surface
->tiling
= rsc
->slices
[level
].tiling
;
378 return &surface
->base
;
382 vc4_surface_destroy(struct pipe_context
*pctx
, struct pipe_surface
*psurf
)
384 pipe_resource_reference(&psurf
->texture
, NULL
);
389 vc4_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*resource
)
391 struct vc4_context
*vc4
= vc4_context(pctx
);
393 /* XXX: Skip this if we don't have any queued drawing to it. */
394 vc4
->base
.flush(pctx
, NULL
, 0);
397 render_blit(struct pipe_context
*ctx
, struct pipe_blit_info
*info
)
399 struct vc4_context
*vc4
= vc4_context(ctx
);
401 if (!util_blitter_is_blit_supported(vc4
->blitter
, info
)) {
402 fprintf(stderr
, "blit unsupported %s -> %s",
403 util_format_short_name(info
->src
.resource
->format
),
404 util_format_short_name(info
->dst
.resource
->format
));
408 util_blitter_save_vertex_buffer_slot(vc4
->blitter
, vc4
->vertexbuf
.vb
);
409 util_blitter_save_vertex_elements(vc4
->blitter
, vc4
->vtx
);
410 util_blitter_save_vertex_shader(vc4
->blitter
, vc4
->prog
.bind_vs
);
411 util_blitter_save_rasterizer(vc4
->blitter
, vc4
->rasterizer
);
412 util_blitter_save_viewport(vc4
->blitter
, &vc4
->viewport
);
413 util_blitter_save_scissor(vc4
->blitter
, &vc4
->scissor
);
414 util_blitter_save_fragment_shader(vc4
->blitter
, vc4
->prog
.bind_fs
);
415 util_blitter_save_blend(vc4
->blitter
, vc4
->blend
);
416 util_blitter_save_depth_stencil_alpha(vc4
->blitter
, vc4
->zsa
);
417 util_blitter_save_stencil_ref(vc4
->blitter
, &vc4
->stencil_ref
);
418 util_blitter_save_sample_mask(vc4
->blitter
, vc4
->sample_mask
);
419 util_blitter_save_framebuffer(vc4
->blitter
, &vc4
->framebuffer
);
420 util_blitter_save_fragment_sampler_states(vc4
->blitter
,
421 vc4
->fragtex
.num_samplers
,
422 (void **)vc4
->fragtex
.samplers
);
423 util_blitter_save_fragment_sampler_views(vc4
->blitter
,
424 vc4
->fragtex
.num_textures
, vc4
->fragtex
.textures
);
426 util_blitter_blit(vc4
->blitter
, info
);
431 /* Optimal hardware path for blitting pixels.
432 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
435 vc4_blit(struct pipe_context
*pctx
, const struct pipe_blit_info
*blit_info
)
437 struct pipe_blit_info info
= *blit_info
;
439 if (info
.src
.resource
->nr_samples
> 1 &&
440 info
.dst
.resource
->nr_samples
<= 1 &&
441 !util_format_is_depth_or_stencil(info
.src
.resource
->format
) &&
442 !util_format_is_pure_integer(info
.src
.resource
->format
)) {
443 fprintf(stderr
, "color resolve unimplemented");
447 if (util_try_blit_via_copy_region(pctx
, &info
)) {
451 if (info
.mask
& PIPE_MASK_S
) {
452 fprintf(stderr
, "cannot blit stencil, skipping");
453 info
.mask
&= ~PIPE_MASK_S
;
456 render_blit(pctx
, &info
);
460 vc4_resource_screen_init(struct pipe_screen
*pscreen
)
462 pscreen
->resource_create
= vc4_resource_create
;
463 pscreen
->resource_from_handle
= vc4_resource_from_handle
;
464 pscreen
->resource_get_handle
= u_resource_get_handle_vtbl
;
465 pscreen
->resource_destroy
= u_resource_destroy_vtbl
;
469 vc4_resource_context_init(struct pipe_context
*pctx
)
471 pctx
->transfer_map
= u_transfer_map_vtbl
;
472 pctx
->transfer_flush_region
= u_transfer_flush_region_vtbl
;
473 pctx
->transfer_unmap
= u_transfer_unmap_vtbl
;
474 pctx
->transfer_inline_write
= u_transfer_inline_write_vtbl
;
475 pctx
->create_surface
= vc4_create_surface
;
476 pctx
->surface_destroy
= vc4_surface_destroy
;
477 pctx
->resource_copy_region
= util_resource_copy_region
;
478 pctx
->blit
= vc4_blit
;
479 pctx
->flush_resource
= vc4_flush_resource
;