vc4: Add a blitter path using just the render thread.
[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 void
30 vc4_tile_blit_color_rcl(struct vc4_context *vc4,
31 struct vc4_surface *dst_surf,
32 struct vc4_surface *src_surf)
33 {
34 struct vc4_resource *src = vc4_resource(src_surf->base.texture);
35 struct vc4_resource *dst = vc4_resource(dst_surf->base.texture);
36
37 uint32_t min_x_tile = 0;
38 uint32_t min_y_tile = 0;
39 uint32_t max_x_tile = (dst_surf->base.width - 1) / 64;
40 uint32_t max_y_tile = (dst_surf->base.height - 1) / 64;
41 uint32_t xtiles = max_x_tile - min_x_tile + 1;
42 uint32_t ytiles = max_y_tile - min_y_tile + 1;
43 uint32_t reloc_size = 9;
44 uint32_t config_size = 11 + reloc_size;
45 uint32_t loadstore_size = 7 + reloc_size;
46 uint32_t tilecoords_size = 3;
47 cl_ensure_space(&vc4->rcl,
48 config_size +
49 xtiles * ytiles * (loadstore_size * 2 +
50 tilecoords_size * 1));
51 cl_ensure_space(&vc4->bo_handles, 2 * sizeof(uint32_t));
52 cl_ensure_space(&vc4->bo_pointers, 2 * sizeof(struct vc4_bo *));
53
54 cl_start_reloc(&vc4->rcl, 1);
55 cl_u8(&vc4->rcl, VC4_PACKET_TILE_RENDERING_MODE_CONFIG);
56 cl_reloc(vc4, &vc4->rcl, dst->bo, dst_surf->offset);
57 cl_u16(&vc4->rcl, dst_surf->base.width);
58 cl_u16(&vc4->rcl, dst_surf->base.height);
59 cl_u16(&vc4->rcl, ((dst_surf->tiling <<
60 VC4_RENDER_CONFIG_MEMORY_FORMAT_SHIFT) |
61 (vc4_rt_format_is_565(dst_surf->base.format) ?
62 VC4_RENDER_CONFIG_FORMAT_BGR565 :
63 VC4_RENDER_CONFIG_FORMAT_RGBA8888)));
64
65 uint32_t src_hindex = vc4_gem_hindex(vc4, src->bo);
66
67 for (int y = min_y_tile; y <= max_y_tile; y++) {
68 for (int x = min_x_tile; x <= max_x_tile; x++) {
69 bool end_of_frame = (x == max_x_tile &&
70 y == max_y_tile);
71
72 cl_start_reloc(&vc4->rcl, 1);
73 cl_u8(&vc4->rcl, VC4_PACKET_LOAD_TILE_BUFFER_GENERAL);
74 cl_u8(&vc4->rcl,
75 VC4_LOADSTORE_TILE_BUFFER_COLOR |
76 (src_surf->tiling <<
77 VC4_LOADSTORE_TILE_BUFFER_FORMAT_SHIFT));
78 cl_u8(&vc4->rcl,
79 vc4_rt_format_is_565(src_surf->base.format) ?
80 VC4_LOADSTORE_TILE_BUFFER_BGR565 :
81 VC4_LOADSTORE_TILE_BUFFER_RGBA8888);
82 cl_reloc_hindex(&vc4->rcl, src_hindex,
83 src_surf->offset);
84
85 cl_u8(&vc4->rcl, VC4_PACKET_TILE_COORDINATES);
86 cl_u8(&vc4->rcl, x);
87 cl_u8(&vc4->rcl, y);
88
89 if (end_of_frame) {
90 cl_u8(&vc4->rcl,
91 VC4_PACKET_STORE_MS_TILE_BUFFER_AND_EOF);
92 } else {
93 cl_u8(&vc4->rcl,
94 VC4_PACKET_STORE_MS_TILE_BUFFER);
95 }
96 }
97 }
98
99 vc4->draw_min_x = 0;
100 vc4->draw_min_y = 0;
101 vc4->draw_max_x = dst_surf->base.width;
102 vc4->draw_max_y = dst_surf->base.height;
103
104 dst->writes++;
105 vc4->needs_flush = true;
106 }
107
108 static struct vc4_surface *
109 vc4_get_blit_surface(struct pipe_context *pctx,
110 struct pipe_resource *prsc, unsigned level)
111 {
112 struct pipe_surface tmpl;
113
114 memset(&tmpl, 0, sizeof(tmpl));
115 tmpl.format = prsc->format;
116 tmpl.u.tex.level = level;
117 tmpl.u.tex.first_layer = 0;
118 tmpl.u.tex.last_layer = 0;
119
120 return vc4_surface(pctx->create_surface(pctx, prsc, &tmpl));
121 }
122
123 static bool
124 vc4_tile_blit(struct pipe_context *pctx, const struct pipe_blit_info *info)
125 {
126 struct vc4_context *vc4 = vc4_context(pctx);
127
128 if ((info->mask & PIPE_MASK_RGBA) == 0)
129 return false;
130
131 if (info->dst.box.x != 0 || info->dst.box.y != 0 ||
132 info->src.box.x != 0 || info->src.box.y != 0 ||
133 info->dst.box.width != info->src.box.width ||
134 info->dst.box.height != info->src.box.height) {
135 return false;
136 }
137
138 struct vc4_surface *dst_surf =
139 vc4_get_blit_surface(pctx, info->dst.resource, info->dst.level);
140 struct vc4_surface *src_surf =
141 vc4_get_blit_surface(pctx, info->src.resource, info->src.level);
142
143 vc4_flush(pctx);
144 vc4_tile_blit_color_rcl(vc4, dst_surf, src_surf);
145 vc4_job_submit(vc4);
146
147 pctx->surface_destroy(pctx, &dst_surf->base);
148 pctx->surface_destroy(pctx, &src_surf->base);
149
150 return true;
151 }
152
153 static bool
154 vc4_render_blit(struct pipe_context *ctx, struct pipe_blit_info *info)
155 {
156 struct vc4_context *vc4 = vc4_context(ctx);
157
158 if (!util_blitter_is_blit_supported(vc4->blitter, info)) {
159 fprintf(stderr, "blit unsupported %s -> %s",
160 util_format_short_name(info->src.resource->format),
161 util_format_short_name(info->dst.resource->format));
162 return false;
163 }
164
165 util_blitter_save_vertex_buffer_slot(vc4->blitter, vc4->vertexbuf.vb);
166 util_blitter_save_vertex_elements(vc4->blitter, vc4->vtx);
167 util_blitter_save_vertex_shader(vc4->blitter, vc4->prog.bind_vs);
168 util_blitter_save_rasterizer(vc4->blitter, vc4->rasterizer);
169 util_blitter_save_viewport(vc4->blitter, &vc4->viewport);
170 util_blitter_save_scissor(vc4->blitter, &vc4->scissor);
171 util_blitter_save_fragment_shader(vc4->blitter, vc4->prog.bind_fs);
172 util_blitter_save_blend(vc4->blitter, vc4->blend);
173 util_blitter_save_depth_stencil_alpha(vc4->blitter, vc4->zsa);
174 util_blitter_save_stencil_ref(vc4->blitter, &vc4->stencil_ref);
175 util_blitter_save_sample_mask(vc4->blitter, vc4->sample_mask);
176 util_blitter_save_framebuffer(vc4->blitter, &vc4->framebuffer);
177 util_blitter_save_fragment_sampler_states(vc4->blitter,
178 vc4->fragtex.num_samplers,
179 (void **)vc4->fragtex.samplers);
180 util_blitter_save_fragment_sampler_views(vc4->blitter,
181 vc4->fragtex.num_textures, vc4->fragtex.textures);
182
183 util_blitter_blit(vc4->blitter, info);
184
185 return true;
186 }
187
188 /* Optimal hardware path for blitting pixels.
189 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
190 */
191 void
192 vc4_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
193 {
194 struct pipe_blit_info info = *blit_info;
195
196 if (info.src.resource->nr_samples > 1 &&
197 info.dst.resource->nr_samples <= 1 &&
198 !util_format_is_depth_or_stencil(info.src.resource->format) &&
199 !util_format_is_pure_integer(info.src.resource->format)) {
200 fprintf(stderr, "color resolve unimplemented");
201 return;
202 }
203
204 if (vc4_tile_blit(pctx, blit_info))
205 return;
206
207 if (util_try_blit_via_copy_region(pctx, &info)) {
208 return; /* done */
209 }
210
211 if (info.mask & PIPE_MASK_S) {
212 fprintf(stderr, "cannot blit stencil, skipping");
213 info.mask &= ~PIPE_MASK_S;
214 }
215
216 vc4_render_blit(pctx, &info);
217 }