panfrost: Refactor blitting code
[mesa.git] / src / gallium / drivers / panfrost / pan_blit.c
1 /*
2 * Copyright (C) 2014 Broadcom
3 * Copyright (C) 2019 Collabora
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 FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * Authors (Collabora):
25 * Tomeu Vizoso <tomeu.vizoso@collabora.com>
26 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
27 *
28 */
29
30 #include "pan_context.h"
31 #include "util/u_format.h"
32
33 static void
34 panfrost_blitter_save(struct panfrost_context *ctx)
35 {
36
37 util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vertex_buffers);
38 util_blitter_save_vertex_elements(ctx->blitter, ctx->vertex);
39 util_blitter_save_vertex_shader(ctx->blitter, ctx->vs);
40 util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer);
41 util_blitter_save_viewport(ctx->blitter, &ctx->pipe_viewport);
42 util_blitter_save_scissor(ctx->blitter, &ctx->scissor);
43 util_blitter_save_fragment_shader(ctx->blitter, ctx->fs);
44 util_blitter_save_blend(ctx->blitter, ctx->blend);
45 util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->depth_stencil);
46 util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref);
47 util_blitter_save_so_targets(ctx->blitter, 0, NULL);
48
49 /* For later */
50 // util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask);
51
52 util_blitter_save_framebuffer(ctx->blitter, &ctx->pipe_framebuffer);
53 util_blitter_save_fragment_sampler_states(ctx->blitter,
54 ctx->sampler_count[PIPE_SHADER_FRAGMENT],
55 (void **)(&ctx->samplers[PIPE_SHADER_FRAGMENT]));
56 util_blitter_save_fragment_sampler_views(ctx->blitter,
57 ctx->sampler_view_count[PIPE_SHADER_FRAGMENT],
58 (struct pipe_sampler_view **)&ctx->sampler_views[PIPE_SHADER_FRAGMENT]);
59 }
60
61 static bool
62 panfrost_u_blitter_blit(struct pipe_context *pipe,
63 const struct pipe_blit_info *info)
64 {
65 struct panfrost_context *ctx = pan_context(pipe);
66
67 if (!util_blitter_is_blit_supported(ctx->blitter, info)) {
68 fprintf(stderr, "blit unsupported %s -> %s\n",
69 util_format_short_name(info->src.resource->format),
70 util_format_short_name(info->dst.resource->format));
71 return false;
72 }
73
74 /* TODO: Scissor */
75
76 panfrost_blitter_save(ctx);
77 util_blitter_blit(ctx->blitter, info);
78
79 return true;
80 }
81
82 void
83 panfrost_blit(struct pipe_context *pipe,
84 const struct pipe_blit_info *info)
85 {
86 /* We don't have a hardware blit, so we just fake it with
87 * u_blitter. We could do a little better by culling
88 * vertex jobs, though. */
89
90 /* TODO: Implement blitting. Commented out because u_blitter is not
91 * fully integrated and creates bugs in other places. */
92 #if 0
93 if (panfrost_u_blitter_blit(pipe, info))
94 return;
95
96 fprintf(stderr, "Unhandled blit");
97 #endif
98
99 return;
100 }
101
102 /* Blits a framebuffer to "itself". Mali is a tiler, so the
103 * framebuffer is implicitly cleared every frame, so if there is
104 * no actual glClear(), we have to blit it back ourselves.
105 */
106
107 void
108 panfrost_blit_wallpaper(struct panfrost_context *ctx)
109 {
110 struct pipe_blit_info binfo = { };
111
112 panfrost_blitter_save(ctx);
113
114 binfo.src.resource = binfo.dst.resource = ctx->pipe_framebuffer.cbufs[0]->texture;
115 binfo.src.level = binfo.dst.level = 0;
116 binfo.src.box.x = binfo.dst.box.x = 0;
117 binfo.src.box.y = binfo.dst.box.y = 0;
118 binfo.src.box.width = binfo.dst.box.width = ctx->pipe_framebuffer.width;
119 binfo.src.box.height = binfo.dst.box.height = ctx->pipe_framebuffer.height;
120
121 /* This avoids an assert due to missing nir_texop_txb support */
122 //binfo.src.box.depth = binfo.dst.box.depth = 1;
123
124 binfo.src.format = binfo.dst.format = ctx->pipe_framebuffer.cbufs[0]->texture->format;
125
126 assert(ctx->pipe_framebuffer.nr_cbufs == 1);
127 binfo.mask = PIPE_MASK_RGBA;
128 binfo.filter = PIPE_TEX_FILTER_LINEAR;
129 binfo.scissor_enable = FALSE;
130
131 util_blitter_blit(ctx->blitter, &binfo);
132 }
133