freedreno: catch failing fd_blit and fallback to software blit
[mesa.git] / src / gallium / drivers / freedreno / freedreno_blitter.c
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 #include "util/u_surface.h"
29
30 #include "freedreno_blitter.h"
31 #include "freedreno_context.h"
32 #include "freedreno_resource.h"
33 #include "freedreno_fence.h"
34
35 /* generic blit using u_blitter.. slightly modified version of util_blitter_blit
36 * which also handles PIPE_BUFFER:
37 */
38
39 static void
40 default_dst_texture(struct pipe_surface *dst_templ, struct pipe_resource *dst,
41 unsigned dstlevel, unsigned dstz)
42 {
43 memset(dst_templ, 0, sizeof(*dst_templ));
44 if (dst->target == PIPE_BUFFER)
45 dst_templ->format = PIPE_FORMAT_R8_UINT;
46 else
47 dst_templ->format = util_format_linear(dst->format);
48 dst_templ->u.tex.level = dstlevel;
49 dst_templ->u.tex.first_layer = dstz;
50 dst_templ->u.tex.last_layer = dstz;
51 }
52
53 static void
54 default_src_texture(struct pipe_sampler_view *src_templ,
55 struct pipe_resource *src, unsigned srclevel)
56 {
57 bool cube_as_2darray = src->screen->get_param(src->screen,
58 PIPE_CAP_SAMPLER_VIEW_TARGET);
59
60 memset(src_templ, 0, sizeof(*src_templ));
61
62 if (cube_as_2darray && (src->target == PIPE_TEXTURE_CUBE ||
63 src->target == PIPE_TEXTURE_CUBE_ARRAY))
64 src_templ->target = PIPE_TEXTURE_2D_ARRAY;
65 else
66 src_templ->target = src->target;
67
68 if (src->target == PIPE_BUFFER) {
69 src_templ->target = PIPE_TEXTURE_1D;
70 src_templ->format = PIPE_FORMAT_R8_UINT;
71 } else {
72 src_templ->format = util_format_linear(src->format);
73 }
74 src_templ->u.tex.first_level = srclevel;
75 src_templ->u.tex.last_level = srclevel;
76 src_templ->u.tex.first_layer = 0;
77 src_templ->u.tex.last_layer =
78 src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1
79 : (unsigned)(src->array_size - 1);
80 src_templ->swizzle_r = PIPE_SWIZZLE_X;
81 src_templ->swizzle_g = PIPE_SWIZZLE_Y;
82 src_templ->swizzle_b = PIPE_SWIZZLE_Z;
83 src_templ->swizzle_a = PIPE_SWIZZLE_W;
84 }
85
86 static void
87 fd_blitter_pipe_begin(struct fd_context *ctx, bool render_cond, bool discard,
88 enum fd_render_stage stage)
89 {
90 fd_fence_ref(ctx->base.screen, &ctx->last_fence, NULL);
91
92 util_blitter_save_fragment_constant_buffer_slot(ctx->blitter,
93 ctx->constbuf[PIPE_SHADER_FRAGMENT].cb);
94 util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vtx.vertexbuf.vb);
95 util_blitter_save_vertex_elements(ctx->blitter, ctx->vtx.vtx);
96 util_blitter_save_vertex_shader(ctx->blitter, ctx->prog.vp);
97 util_blitter_save_so_targets(ctx->blitter, ctx->streamout.num_targets,
98 ctx->streamout.targets);
99 util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer);
100 util_blitter_save_viewport(ctx->blitter, &ctx->viewport);
101 util_blitter_save_scissor(ctx->blitter, &ctx->scissor);
102 util_blitter_save_fragment_shader(ctx->blitter, ctx->prog.fp);
103 util_blitter_save_blend(ctx->blitter, ctx->blend);
104 util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa);
105 util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref);
106 util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask);
107 util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer);
108 util_blitter_save_fragment_sampler_states(ctx->blitter,
109 ctx->tex[PIPE_SHADER_FRAGMENT].num_samplers,
110 (void **)ctx->tex[PIPE_SHADER_FRAGMENT].samplers);
111 util_blitter_save_fragment_sampler_views(ctx->blitter,
112 ctx->tex[PIPE_SHADER_FRAGMENT].num_textures,
113 ctx->tex[PIPE_SHADER_FRAGMENT].textures);
114 if (!render_cond)
115 util_blitter_save_render_condition(ctx->blitter,
116 ctx->cond_query, ctx->cond_cond, ctx->cond_mode);
117
118 if (ctx->batch)
119 fd_batch_set_stage(ctx->batch, stage);
120
121 ctx->in_blit = discard;
122 }
123
124 static void
125 fd_blitter_pipe_end(struct fd_context *ctx)
126 {
127 if (ctx->batch)
128 fd_batch_set_stage(ctx->batch, FD_STAGE_NULL);
129 ctx->in_blit = false;
130 }
131
132 bool
133 fd_blitter_blit(struct fd_context *ctx, const struct pipe_blit_info *info)
134 {
135 struct pipe_resource *dst = info->dst.resource;
136 struct pipe_resource *src = info->src.resource;
137 struct pipe_context *pipe = &ctx->base;
138 struct pipe_surface *dst_view, dst_templ;
139 struct pipe_sampler_view src_templ, *src_view;
140 bool discard = false;
141
142 if (!info->scissor_enable && !info->alpha_blend) {
143 discard = util_texrange_covers_whole_level(info->dst.resource,
144 info->dst.level, info->dst.box.x, info->dst.box.y,
145 info->dst.box.z, info->dst.box.width,
146 info->dst.box.height, info->dst.box.depth);
147 }
148
149 fd_blitter_pipe_begin(ctx, info->render_condition_enable, discard, FD_STAGE_BLIT);
150
151 /* Initialize the surface. */
152 default_dst_texture(&dst_templ, dst, info->dst.level,
153 info->dst.box.z);
154 dst_templ.format = info->dst.format;
155 dst_view = pipe->create_surface(pipe, dst, &dst_templ);
156
157 /* Initialize the sampler view. */
158 default_src_texture(&src_templ, src, info->src.level);
159 src_templ.format = info->src.format;
160 src_view = pipe->create_sampler_view(pipe, src, &src_templ);
161
162 /* Copy. */
163 util_blitter_blit_generic(ctx->blitter, dst_view, &info->dst.box,
164 src_view, &info->src.box, src->width0, src->height0,
165 info->mask, info->filter,
166 info->scissor_enable ? &info->scissor : NULL,
167 info->alpha_blend);
168
169 pipe_surface_reference(&dst_view, NULL);
170 pipe_sampler_view_reference(&src_view, NULL);
171
172 fd_blitter_pipe_end(ctx);
173
174 /* The fallback blitter must never fail: */
175 return true;
176 }
177
178 /* Generic clear implementation (partially) using u_blitter: */
179 void
180 fd_blitter_clear(struct pipe_context *pctx, unsigned buffers,
181 const union pipe_color_union *color, double depth, unsigned stencil)
182 {
183 struct fd_context *ctx = fd_context(pctx);
184 struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
185 struct blitter_context *blitter = ctx->blitter;
186
187 fd_blitter_pipe_begin(ctx, false, true, FD_STAGE_CLEAR);
188
189 util_blitter_common_clear_setup(blitter, pfb->width, pfb->height,
190 buffers, NULL, NULL);
191
192 struct pipe_stencil_ref sr = {
193 .ref_value = { stencil & 0xff }
194 };
195 pctx->set_stencil_ref(pctx, &sr);
196
197 struct pipe_constant_buffer cb = {
198 .buffer_size = 16,
199 .user_buffer = &color->ui,
200 };
201 pctx->set_constant_buffer(pctx, PIPE_SHADER_FRAGMENT, 0, &cb);
202
203 if (!ctx->clear_rs_state) {
204 const struct pipe_rasterizer_state tmpl = {
205 .cull_face = PIPE_FACE_NONE,
206 .half_pixel_center = 1,
207 .bottom_edge_rule = 1,
208 .flatshade = 1,
209 .depth_clip_near = 1,
210 .depth_clip_far = 1,
211 };
212 ctx->clear_rs_state = pctx->create_rasterizer_state(pctx, &tmpl);
213 }
214 pctx->bind_rasterizer_state(pctx, ctx->clear_rs_state);
215
216 struct pipe_viewport_state vp = {
217 .scale = { 0.5f * pfb->width, -0.5f * pfb->height, depth },
218 .translate = { 0.5f * pfb->width, 0.5f * pfb->height, 0.0f },
219 };
220 pctx->set_viewport_states(pctx, 0, 1, &vp);
221
222 pctx->bind_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
223 pctx->set_vertex_buffers(pctx, blitter->vb_slot, 1,
224 &ctx->solid_vbuf_state.vertexbuf.vb[0]);
225 pctx->set_stream_output_targets(pctx, 0, NULL, NULL);
226 pctx->bind_vs_state(pctx, ctx->solid_prog.vp);
227 pctx->bind_fs_state(pctx, ctx->solid_prog.fp);
228
229 struct pipe_draw_info info = {
230 .mode = PIPE_PRIM_MAX, /* maps to DI_PT_RECTLIST */
231 .count = 2,
232 .max_index = 1,
233 .instance_count = 1,
234 };
235 ctx->draw_vbo(ctx, &info, 0);
236
237 util_blitter_restore_constant_buffer_state(blitter);
238 util_blitter_restore_vertex_states(blitter);
239 util_blitter_restore_fragment_states(blitter);
240 util_blitter_restore_textures(blitter);
241 util_blitter_restore_fb_state(blitter);
242 util_blitter_restore_render_cond(blitter);
243 util_blitter_unset_running_flag(blitter);
244
245 fd_blitter_pipe_end(ctx);
246 }
247
248 /**
249 * Optimal hardware path for blitting pixels.
250 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
251 */
252 bool
253 fd_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
254 {
255 struct fd_context *ctx = fd_context(pctx);
256 struct pipe_blit_info info = *blit_info;
257
258 if (info.render_condition_enable && !fd_render_condition_check(pctx))
259 return true;
260
261 if (ctx->blit && ctx->blit(ctx, &info))
262 return true;
263
264 if (info.mask & PIPE_MASK_S) {
265 DBG("cannot blit stencil, skipping");
266 info.mask &= ~PIPE_MASK_S;
267 }
268
269 if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
270 DBG("blit unsupported %s -> %s",
271 util_format_short_name(info.src.resource->format),
272 util_format_short_name(info.dst.resource->format));
273 return false;
274 }
275
276 return fd_blitter_blit(ctx, &info);
277 }
278
279 /**
280 * _copy_region using pipe (3d engine)
281 */
282 static bool
283 fd_blitter_pipe_copy_region(struct fd_context *ctx,
284 struct pipe_resource *dst,
285 unsigned dst_level,
286 unsigned dstx, unsigned dsty, unsigned dstz,
287 struct pipe_resource *src,
288 unsigned src_level,
289 const struct pipe_box *src_box)
290 {
291 /* not until we allow rendertargets to be buffers */
292 if (dst->target == PIPE_BUFFER || src->target == PIPE_BUFFER)
293 return false;
294
295 if (!util_blitter_is_copy_supported(ctx->blitter, dst, src))
296 return false;
297
298 /* TODO we could discard if dst box covers dst level fully.. */
299 fd_blitter_pipe_begin(ctx, false, false, FD_STAGE_BLIT);
300 util_blitter_copy_texture(ctx->blitter,
301 dst, dst_level, dstx, dsty, dstz,
302 src, src_level, src_box);
303 fd_blitter_pipe_end(ctx);
304
305 return true;
306 }
307
308 /**
309 * Copy a block of pixels from one resource to another.
310 * The resource must be of the same format.
311 */
312 void
313 fd_resource_copy_region(struct pipe_context *pctx,
314 struct pipe_resource *dst,
315 unsigned dst_level,
316 unsigned dstx, unsigned dsty, unsigned dstz,
317 struct pipe_resource *src,
318 unsigned src_level,
319 const struct pipe_box *src_box)
320 {
321 struct fd_context *ctx = fd_context(pctx);
322
323 if (ctx->blit) {
324 struct pipe_blit_info info;
325
326 memset(&info, 0, sizeof info);
327 info.dst.resource = dst;
328 info.dst.level = dst_level;
329 info.dst.box.x = dstx;
330 info.dst.box.y = dsty;
331 info.dst.box.z = dstz;
332 info.dst.box.width = src_box->width;
333 info.dst.box.height = src_box->height;
334 assert(info.dst.box.width >= 0);
335 assert(info.dst.box.height >= 0);
336 info.dst.box.depth = 1;
337 info.dst.format = dst->format;
338 info.src.resource = src;
339 info.src.level = src_level;
340 info.src.box = *src_box;
341 info.src.format = src->format;
342 info.mask = util_format_get_mask(src->format);
343 info.filter = PIPE_TEX_FILTER_NEAREST;
344 info.scissor_enable = 0;
345
346 if (ctx->blit(ctx, &info))
347 return;
348 }
349
350 /* TODO if we have 2d core, or other DMA engine that could be used
351 * for simple copies and reasonably easily synchronized with the 3d
352 * core, this is where we'd plug it in..
353 */
354
355 /* try blit on 3d pipe: */
356 if (fd_blitter_pipe_copy_region(ctx,
357 dst, dst_level, dstx, dsty, dstz,
358 src, src_level, src_box))
359 return;
360
361 /* else fallback to pure sw: */
362 util_resource_copy_region(pctx,
363 dst, dst_level, dstx, dsty, dstz,
364 src, src_level, src_box);
365 }