vc4: Fix check for tile RCL blits with mismatched y.
[mesa.git] / src / gallium / drivers / vc4 / vc4_blit.c
1 /*
2 * Copyright © 2015 Broadcom
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "util/u_format.h"
25 #include "util/u_surface.h"
26 #include "util/u_blitter.h"
27 #include "vc4_context.h"
28
29 static struct pipe_surface *
30 vc4_get_blit_surface(struct pipe_context *pctx,
31 struct pipe_resource *prsc, unsigned level)
32 {
33 struct pipe_surface tmpl;
34
35 memset(&tmpl, 0, sizeof(tmpl));
36 tmpl.format = prsc->format;
37 tmpl.u.tex.level = level;
38 tmpl.u.tex.first_layer = 0;
39 tmpl.u.tex.last_layer = 0;
40
41 return pctx->create_surface(pctx, prsc, &tmpl);
42 }
43
44 static bool
45 is_tile_unaligned(unsigned size, unsigned tile_size)
46 {
47 return size & (tile_size - 1);
48 }
49
50 static bool
51 vc4_tile_blit(struct pipe_context *pctx, const struct pipe_blit_info *info)
52 {
53 struct vc4_context *vc4 = vc4_context(pctx);
54 int tile_width = 64;
55 int tile_height = 64;
56
57 if (util_format_is_depth_or_stencil(info->dst.resource->format))
58 return false;
59
60 if (info->scissor_enable)
61 return false;
62
63 if ((info->mask & PIPE_MASK_RGBA) == 0)
64 return false;
65
66 if (info->dst.box.x != info->src.box.x ||
67 info->dst.box.y != info->src.box.y ||
68 info->dst.box.width != info->src.box.width ||
69 info->dst.box.height != info->src.box.height) {
70 return false;
71 }
72
73 if (is_tile_unaligned(info->dst.box.x, tile_width) ||
74 is_tile_unaligned(info->dst.box.y, tile_height) ||
75 is_tile_unaligned(info->dst.box.width, tile_width) ||
76 is_tile_unaligned(info->dst.box.height, tile_height)) {
77 return false;
78 }
79
80 if (info->dst.resource->format != info->src.resource->format)
81 return false;
82
83 vc4_flush(pctx);
84
85 struct pipe_surface *dst_surf =
86 vc4_get_blit_surface(pctx, info->dst.resource, info->dst.level);
87 struct pipe_surface *src_surf =
88 vc4_get_blit_surface(pctx, info->src.resource, info->src.level);
89
90 pipe_surface_reference(&vc4->color_read, src_surf);
91 pipe_surface_reference(&vc4->color_write, dst_surf);
92 pipe_surface_reference(&vc4->zs_read, NULL);
93 pipe_surface_reference(&vc4->zs_write, NULL);
94 vc4->draw_min_x = info->dst.box.x;
95 vc4->draw_min_y = info->dst.box.y;
96 vc4->draw_max_x = info->dst.box.x + info->dst.box.width;
97 vc4->draw_max_y = info->dst.box.y + info->dst.box.height;
98 vc4->draw_width = dst_surf->width;
99 vc4->draw_height = dst_surf->height;
100
101 vc4->needs_flush = true;
102 vc4_job_submit(vc4);
103
104 pipe_surface_reference(&dst_surf, NULL);
105 pipe_surface_reference(&src_surf, NULL);
106
107 return true;
108 }
109
110 static bool
111 vc4_render_blit(struct pipe_context *ctx, struct pipe_blit_info *info)
112 {
113 struct vc4_context *vc4 = vc4_context(ctx);
114
115 if (!util_blitter_is_blit_supported(vc4->blitter, info)) {
116 fprintf(stderr, "blit unsupported %s -> %s\n",
117 util_format_short_name(info->src.resource->format),
118 util_format_short_name(info->dst.resource->format));
119 return false;
120 }
121
122 util_blitter_save_vertex_buffer_slot(vc4->blitter, vc4->vertexbuf.vb);
123 util_blitter_save_vertex_elements(vc4->blitter, vc4->vtx);
124 util_blitter_save_vertex_shader(vc4->blitter, vc4->prog.bind_vs);
125 util_blitter_save_rasterizer(vc4->blitter, vc4->rasterizer);
126 util_blitter_save_viewport(vc4->blitter, &vc4->viewport);
127 util_blitter_save_scissor(vc4->blitter, &vc4->scissor);
128 util_blitter_save_fragment_shader(vc4->blitter, vc4->prog.bind_fs);
129 util_blitter_save_blend(vc4->blitter, vc4->blend);
130 util_blitter_save_depth_stencil_alpha(vc4->blitter, vc4->zsa);
131 util_blitter_save_stencil_ref(vc4->blitter, &vc4->stencil_ref);
132 util_blitter_save_sample_mask(vc4->blitter, vc4->sample_mask);
133 util_blitter_save_framebuffer(vc4->blitter, &vc4->framebuffer);
134 util_blitter_save_fragment_sampler_states(vc4->blitter,
135 vc4->fragtex.num_samplers,
136 (void **)vc4->fragtex.samplers);
137 util_blitter_save_fragment_sampler_views(vc4->blitter,
138 vc4->fragtex.num_textures, vc4->fragtex.textures);
139
140 util_blitter_blit(vc4->blitter, info);
141
142 return true;
143 }
144
145 /* Optimal hardware path for blitting pixels.
146 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
147 */
148 void
149 vc4_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
150 {
151 struct pipe_blit_info info = *blit_info;
152
153 if (info.src.resource->nr_samples > 1 &&
154 info.dst.resource->nr_samples <= 1 &&
155 !util_format_is_depth_or_stencil(info.src.resource->format) &&
156 !util_format_is_pure_integer(info.src.resource->format)) {
157 fprintf(stderr, "color resolve unimplemented\n");
158 return;
159 }
160
161 if (vc4_tile_blit(pctx, blit_info))
162 return;
163
164 if (util_try_blit_via_copy_region(pctx, &info)) {
165 return; /* done */
166 }
167
168 if (info.mask & PIPE_MASK_S) {
169 fprintf(stderr, "cannot blit stencil, skipping\n");
170 info.mask &= ~PIPE_MASK_S;
171 }
172
173 vc4_render_blit(pctx, &info);
174 }