freedreno_blitter.c revision 01e04c3f
1/*
2 * Copyright (C) 2017 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 *    Rob Clark <robclark@freedesktop.org>
25 */
26
27#include "util/u_blitter.h"
28
29#include "freedreno_blitter.h"
30#include "freedreno_context.h"
31
32/* generic blit using u_blitter.. slightly modified version of util_blitter_blit
33 * which also handles PIPE_BUFFER:
34 */
35
36static void
37default_dst_texture(struct pipe_surface *dst_templ, struct pipe_resource *dst,
38		unsigned dstlevel, unsigned dstz)
39{
40	memset(dst_templ, 0, sizeof(*dst_templ));
41	if (dst->target == PIPE_BUFFER)
42		dst_templ->format = PIPE_FORMAT_R8_UINT;
43	else
44		dst_templ->format = util_format_linear(dst->format);
45	dst_templ->u.tex.level = dstlevel;
46	dst_templ->u.tex.first_layer = dstz;
47	dst_templ->u.tex.last_layer = dstz;
48}
49
50static void
51default_src_texture(struct pipe_sampler_view *src_templ,
52		struct pipe_resource *src, unsigned srclevel)
53{
54	bool cube_as_2darray = src->screen->get_param(src->screen,
55			PIPE_CAP_SAMPLER_VIEW_TARGET);
56
57	memset(src_templ, 0, sizeof(*src_templ));
58
59	if (cube_as_2darray && (src->target == PIPE_TEXTURE_CUBE ||
60			src->target == PIPE_TEXTURE_CUBE_ARRAY))
61		src_templ->target = PIPE_TEXTURE_2D_ARRAY;
62	else
63		src_templ->target = src->target;
64
65	if (src->target  == PIPE_BUFFER) {
66		src_templ->target = PIPE_TEXTURE_1D;
67		src_templ->format = PIPE_FORMAT_R8_UINT;
68	} else {
69		src_templ->format = util_format_linear(src->format);
70	}
71	src_templ->u.tex.first_level = srclevel;
72	src_templ->u.tex.last_level = srclevel;
73	src_templ->u.tex.first_layer = 0;
74	src_templ->u.tex.last_layer =
75			src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1
76					: (unsigned)(src->array_size - 1);
77	src_templ->swizzle_r = PIPE_SWIZZLE_X;
78	src_templ->swizzle_g = PIPE_SWIZZLE_Y;
79	src_templ->swizzle_b = PIPE_SWIZZLE_Z;
80	src_templ->swizzle_a = PIPE_SWIZZLE_W;
81}
82
83void
84fd_blitter_blit(struct fd_context *ctx, const struct pipe_blit_info *info)
85{
86	struct pipe_resource *dst = info->dst.resource;
87	struct pipe_resource *src = info->src.resource;
88	struct pipe_context *pipe = &ctx->base;
89	struct pipe_surface *dst_view, dst_templ;
90	struct pipe_sampler_view src_templ, *src_view;
91
92	/* Initialize the surface. */
93	default_dst_texture(&dst_templ, dst, info->dst.level,
94			info->dst.box.z);
95	dst_templ.format = info->dst.format;
96	dst_view = pipe->create_surface(pipe, dst, &dst_templ);
97
98	/* Initialize the sampler view. */
99	default_src_texture(&src_templ, src, info->src.level);
100	src_templ.format = info->src.format;
101	src_view = pipe->create_sampler_view(pipe, src, &src_templ);
102
103	/* Copy. */
104	util_blitter_blit_generic(ctx->blitter, dst_view, &info->dst.box,
105			src_view, &info->src.box, src->width0, src->height0,
106			info->mask, info->filter,
107			info->scissor_enable ? &info->scissor : NULL,
108			info->alpha_blend);
109
110	pipe_surface_reference(&dst_view, NULL);
111	pipe_sampler_view_reference(&src_view, NULL);
112}
113