xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/freedreno/freedreno_blitter.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2017 Rob Clark <[email protected]>
3  * SPDX-License-Identifier: MIT
4  *
5  * Authors:
6  *    Rob Clark <[email protected]>
7  */
8 
9 #include "util/u_blitter.h"
10 #include "util/u_surface.h"
11 
12 #include "freedreno_blitter.h"
13 #include "freedreno_context.h"
14 #include "freedreno_fence.h"
15 #include "freedreno_resource.h"
16 
17 /* generic blit using u_blitter.. slightly modified version of util_blitter_blit
18  * which also handles PIPE_BUFFER:
19  */
20 
21 static void
default_dst_texture(struct pipe_surface * dst_templ,struct pipe_resource * dst,unsigned dstlevel,unsigned dstz)22 default_dst_texture(struct pipe_surface *dst_templ, struct pipe_resource *dst,
23                     unsigned dstlevel, unsigned dstz)
24 {
25    memset(dst_templ, 0, sizeof(*dst_templ));
26    dst_templ->u.tex.level = dstlevel;
27    dst_templ->u.tex.first_layer = dstz;
28    dst_templ->u.tex.last_layer = dstz;
29 }
30 
31 static void
default_src_texture(struct pipe_sampler_view * src_templ,struct pipe_resource * src,unsigned srclevel)32 default_src_texture(struct pipe_sampler_view *src_templ,
33                     struct pipe_resource *src, unsigned srclevel)
34 {
35    bool cube_as_2darray =
36       src->screen->get_param(src->screen, PIPE_CAP_SAMPLER_VIEW_TARGET);
37 
38    memset(src_templ, 0, sizeof(*src_templ));
39 
40    if (cube_as_2darray && (src->target == PIPE_TEXTURE_CUBE ||
41                            src->target == PIPE_TEXTURE_CUBE_ARRAY))
42       src_templ->target = PIPE_TEXTURE_2D_ARRAY;
43    else
44       src_templ->target = src->target;
45 
46    if (src->target == PIPE_BUFFER) {
47       src_templ->target = PIPE_TEXTURE_1D;
48    }
49    src_templ->u.tex.first_level = srclevel;
50    src_templ->u.tex.last_level = srclevel;
51    src_templ->u.tex.first_layer = 0;
52    src_templ->u.tex.last_layer = src->target == PIPE_TEXTURE_3D
53                                     ? u_minify(src->depth0, srclevel) - 1
54                                     : (unsigned)(src->array_size - 1);
55    src_templ->swizzle_r = PIPE_SWIZZLE_X;
56    src_templ->swizzle_g = PIPE_SWIZZLE_Y;
57    src_templ->swizzle_b = PIPE_SWIZZLE_Z;
58    src_templ->swizzle_a = PIPE_SWIZZLE_W;
59 }
60 
61 static void
fd_blitter_pipe_begin(struct fd_context * ctx,bool render_cond)62 fd_blitter_pipe_begin(struct fd_context *ctx, bool render_cond) assert_dt
63 {
64    util_blitter_save_vertex_buffers(
65       ctx->blitter, ctx->vtx.vertexbuf.vb,
66       util_last_bit(ctx->vtx.vertexbuf.enabled_mask));
67    util_blitter_save_vertex_elements(ctx->blitter, ctx->vtx.vtx);
68    util_blitter_save_vertex_shader(ctx->blitter, ctx->prog.vs);
69    util_blitter_save_tessctrl_shader(ctx->blitter, ctx->prog.hs);
70    util_blitter_save_tesseval_shader(ctx->blitter, ctx->prog.ds);
71    util_blitter_save_geometry_shader(ctx->blitter, ctx->prog.gs);
72    util_blitter_save_so_targets(ctx->blitter, ctx->streamout.num_targets,
73                                 ctx->streamout.targets);
74    util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer);
75    util_blitter_save_viewport(ctx->blitter, &ctx->viewport[0]);
76    util_blitter_save_scissor(ctx->blitter, &ctx->scissor[0]);
77    util_blitter_save_fragment_shader(ctx->blitter, ctx->prog.fs);
78    util_blitter_save_blend(ctx->blitter, ctx->blend);
79    util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa);
80    util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref);
81    util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask, ctx->min_samples);
82    util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer);
83    util_blitter_save_fragment_sampler_states(
84       ctx->blitter, ctx->tex[PIPE_SHADER_FRAGMENT].num_samplers,
85       (void **)ctx->tex[PIPE_SHADER_FRAGMENT].samplers);
86    util_blitter_save_fragment_sampler_views(
87       ctx->blitter, ctx->tex[PIPE_SHADER_FRAGMENT].num_textures,
88       ctx->tex[PIPE_SHADER_FRAGMENT].textures);
89    util_blitter_save_fragment_constant_buffer_slot(ctx->blitter,
90                                                    ctx->constbuf[PIPE_SHADER_FRAGMENT].cb);
91    if (!render_cond)
92       util_blitter_save_render_condition(ctx->blitter, ctx->cond_query,
93                                          ctx->cond_cond, ctx->cond_mode);
94 
95    if (ctx->batch)
96       fd_batch_update_queries(ctx->batch);
97 }
98 
99 static void
fd_blitter_pipe_end(struct fd_context * ctx)100 fd_blitter_pipe_end(struct fd_context *ctx) assert_dt
101 {
102 }
103 
104 static void
fd_blitter_prep(struct fd_context * ctx,const struct pipe_blit_info * info)105 fd_blitter_prep(struct fd_context *ctx, const struct pipe_blit_info *info)
106    assert_dt
107 {
108    struct pipe_resource *dst = info->dst.resource;
109    struct pipe_resource *src = info->src.resource;
110    struct pipe_context *pipe = &ctx->base;
111 
112    /* If the blit is updating the whole contents of the resource,
113     * invalidate it so we don't trigger any unnecessary tile loads in the 3D
114     * path.
115     */
116    if (util_blit_covers_whole_resource(info))
117       pipe->invalidate_resource(pipe, info->dst.resource);
118 
119    /* The blit format may not match the resource format in this path, so
120     * we need to validate that we can use the src/dst resource with the
121     * requested format (and uncompress if necessary).  Normally this would
122     * happen in ->set_sampler_view(), ->set_framebuffer_state(), etc.  But
123     * that would cause recursion back into u_blitter, which ends in tears.
124     *
125     * To avoid recursion, this needs to be done before util_blitter_save_*()
126     */
127    if (ctx->validate_format) {
128       ctx->validate_format(ctx, fd_resource(dst), info->dst.format);
129       ctx->validate_format(ctx, fd_resource(src), info->src.format);
130    }
131 
132    if (src == dst)
133       pipe->flush(pipe, NULL, 0);
134 
135    DBG_BLIT(info, NULL);
136 
137    fd_blitter_pipe_begin(ctx, info->render_condition_enable);
138 }
139 
140 bool
fd_blitter_blit(struct fd_context * ctx,const struct pipe_blit_info * info)141 fd_blitter_blit(struct fd_context *ctx, const struct pipe_blit_info *info)
142 {
143    struct pipe_resource *dst = info->dst.resource;
144    struct pipe_resource *src = info->src.resource;
145    struct pipe_context *pipe = &ctx->base;
146    struct pipe_surface *dst_view, dst_templ;
147    struct pipe_sampler_view src_templ, *src_view;
148 
149    fd_blitter_prep(ctx, info);
150 
151    /* Initialize the surface. */
152    default_dst_texture(&dst_templ, dst, info->dst.level, info->dst.box.z);
153    dst_templ.format = info->dst.format;
154    dst_view = pipe->create_surface(pipe, dst, &dst_templ);
155 
156    /* Initialize the sampler view. */
157    default_src_texture(&src_templ, src, info->src.level);
158    src_templ.format = info->src.format;
159    src_view = pipe->create_sampler_view(pipe, src, &src_templ);
160 
161    /* Copy. */
162    util_blitter_blit_generic(
163       ctx->blitter, dst_view, &info->dst.box, src_view, &info->src.box,
164       src->width0, src->height0, info->mask, info->filter,
165       info->scissor_enable ? &info->scissor : NULL, info->alpha_blend, false, 0,
166       NULL);
167 
168    pipe_surface_reference(&dst_view, NULL);
169    pipe_sampler_view_reference(&src_view, NULL);
170 
171    fd_blitter_pipe_end(ctx);
172 
173    /* While this shouldn't technically be necessary, it is required for
174     * dEQP-GLES31.functional.stencil_texturing.format.stencil_index8_cube and
175     * 2d_array to pass.
176     */
177    fd_bc_flush_writer(ctx, fd_resource(info->dst.resource));
178 
179    /* The fallback blitter must never fail: */
180    return true;
181 }
182 
183 /* Generic clear implementation (partially) using u_blitter: */
184 void
fd_blitter_clear(struct pipe_context * pctx,unsigned buffers,const union pipe_color_union * color,double depth,unsigned stencil)185 fd_blitter_clear(struct pipe_context *pctx, unsigned buffers,
186                  const union pipe_color_union *color, double depth,
187                  unsigned stencil)
188 {
189    struct fd_context *ctx = fd_context(pctx);
190    struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
191    struct blitter_context *blitter = ctx->blitter;
192 
193    /* Note: don't use discard=true, if there was something to
194     * discard, that would have been already handled in fd_clear().
195     */
196    fd_blitter_pipe_begin(ctx, false);
197 
198    util_blitter_save_fragment_constant_buffer_slot(
199       ctx->blitter, ctx->constbuf[PIPE_SHADER_FRAGMENT].cb);
200 
201    util_blitter_common_clear_setup(blitter, pfb->width, pfb->height, buffers,
202                                    NULL, NULL);
203 
204    struct pipe_stencil_ref sr = {.ref_value = {stencil & 0xff}};
205    pctx->set_stencil_ref(pctx, sr);
206 
207    struct pipe_constant_buffer cb = {
208       .buffer_size = 16,
209       .user_buffer = &color->ui,
210    };
211    pctx->set_constant_buffer(pctx, PIPE_SHADER_FRAGMENT, 0, false, &cb);
212 
213    unsigned rs_idx = pfb->samples > 1 ? 1 : 0;
214    if (!ctx->clear_rs_state[rs_idx]) {
215       const struct pipe_rasterizer_state tmpl = {
216          .cull_face = PIPE_FACE_NONE,
217          .half_pixel_center = 1,
218          .bottom_edge_rule = 1,
219          .flatshade = 1,
220          .depth_clip_near = 1,
221          .depth_clip_far = 1,
222          .multisample = pfb->samples > 1,
223       };
224       ctx->clear_rs_state[rs_idx] = pctx->create_rasterizer_state(pctx, &tmpl);
225    }
226    pctx->bind_rasterizer_state(pctx, ctx->clear_rs_state[rs_idx]);
227 
228    struct pipe_viewport_state vp = {
229       .scale = {0.5f * pfb->width, -0.5f * pfb->height, depth},
230       .translate = {0.5f * pfb->width, 0.5f * pfb->height, 0.0f},
231    };
232    pctx->set_viewport_states(pctx, 0, 1, &vp);
233 
234    pctx->bind_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
235    util_set_vertex_buffers(pctx, 1, false,
236                            &ctx->solid_vbuf_state.vertexbuf.vb[0]);
237    pctx->set_stream_output_targets(pctx, 0, NULL, NULL);
238 
239    if (pfb->layers > 1)
240       pctx->bind_vs_state(pctx, ctx->solid_layered_prog.vs);
241    else
242       pctx->bind_vs_state(pctx, ctx->solid_prog.vs);
243 
244    pctx->bind_fs_state(pctx, ctx->solid_prog.fs);
245 
246    /* Clear geom/tess shaders, lest the draw emit code think we are
247     * trying to use use them:
248     */
249    pctx->bind_gs_state(pctx, NULL);
250    pctx->bind_tcs_state(pctx, NULL);
251    pctx->bind_tes_state(pctx, NULL);
252 
253    struct pipe_draw_info info = {
254       .mode = MESA_PRIM_COUNT, /* maps to DI_PT_RECTLIST */
255       .index_bounds_valid = true,
256       .max_index = 1,
257       .instance_count = MAX2(1, pfb->layers),
258    };
259    struct pipe_draw_start_count_bias draw = {
260       .count = 2,
261    };
262 
263    pctx->draw_vbo(pctx, &info, 0, NULL, &draw, 1);
264 
265    /* We expect that this should not have triggered a change in pfb: */
266    assert(util_framebuffer_state_equal(pfb, &ctx->framebuffer));
267 
268    util_blitter_restore_constant_buffer_state(blitter);
269    util_blitter_restore_vertex_states(blitter);
270    util_blitter_restore_fragment_states(blitter);
271    util_blitter_restore_textures(blitter);
272    util_blitter_restore_fb_state(blitter);
273    util_blitter_restore_render_cond(blitter);
274    util_blitter_unset_running_flag(blitter);
275 
276    fd_blitter_pipe_end(ctx);
277 }
278 
279 /* Partially generic clear_render_target implementation using u_blitter */
280 void
fd_blitter_clear_render_target(struct pipe_context * pctx,struct pipe_surface * ps,const union pipe_color_union * color,unsigned x,unsigned y,unsigned w,unsigned h,bool render_condition_enabled)281 fd_blitter_clear_render_target(struct pipe_context *pctx, struct pipe_surface *ps,
282                                const union pipe_color_union *color, unsigned x,
283                                unsigned y, unsigned w, unsigned h,
284                                bool render_condition_enabled)
285 {
286    struct fd_context *ctx = fd_context(pctx);
287 
288    fd_blitter_pipe_begin(ctx, render_condition_enabled);
289    util_blitter_clear_render_target(ctx->blitter, ps, color, x, y, w, h);
290    fd_blitter_pipe_end(ctx);
291 }
292 
293 /* Partially generic clear_depth_stencil implementation using u_blitter */
294 void
fd_blitter_clear_depth_stencil(struct pipe_context * pctx,struct pipe_surface * ps,unsigned buffers,double depth,unsigned stencil,unsigned x,unsigned y,unsigned w,unsigned h,bool render_condition_enabled)295 fd_blitter_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *ps,
296                                unsigned buffers, double depth, unsigned stencil,
297                                unsigned x, unsigned y, unsigned w, unsigned h,
298                                bool render_condition_enabled)
299 {
300    struct fd_context *ctx = fd_context(pctx);
301 
302    fd_blitter_pipe_begin(ctx, render_condition_enabled);
303    util_blitter_clear_depth_stencil(ctx->blitter, ps, buffers, depth,
304                                     stencil, x, y, w, h);
305    fd_blitter_pipe_end(ctx);
306 }
307 
308 static void
fd_blit_stencil_fallback(struct fd_context * ctx,const struct pipe_blit_info * info)309 fd_blit_stencil_fallback(struct fd_context *ctx, const struct pipe_blit_info *info)
310    assert_dt
311 {
312    struct pipe_context *pctx = &ctx->base;
313    struct pipe_surface *dst_view, dst_templ;
314 
315    util_blitter_default_dst_texture(&dst_templ, info->dst.resource,
316                                     info->dst.level, info->dst.box.z);
317 
318    dst_view = pctx->create_surface(pctx, info->dst.resource, &dst_templ);
319 
320    fd_blitter_prep(ctx, info);
321 
322    util_blitter_clear_depth_stencil(ctx->blitter, dst_view, PIPE_CLEAR_STENCIL,
323                                     0, 0, info->dst.box.x, info->dst.box.y,
324                                     info->dst.box.width, info->dst.box.height);
325 
326    fd_blitter_prep(ctx, info);
327 
328    util_blitter_stencil_fallback(
329       ctx->blitter, info->dst.resource, info->dst.level, &info->dst.box,
330       info->src.resource, info->src.level, &info->src.box,
331       info->scissor_enable ? &info->scissor : NULL);
332 
333    pipe_surface_release(pctx, &dst_view);
334 }
335 
336 /**
337  * Optimal hardware path for blitting pixels.
338  * Scaling, format conversion, up- and downsampling (resolve) are allowed.
339  */
340 bool
fd_blit(struct pipe_context * pctx,const struct pipe_blit_info * blit_info)341 fd_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
342 {
343    struct fd_context *ctx = fd_context(pctx);
344    struct pipe_blit_info info = *blit_info;
345 
346    if (info.render_condition_enable && !fd_render_condition_check(pctx))
347       return true;
348 
349    if (ctx->blit && ctx->blit(ctx, &info))
350       return true;
351 
352    if (info.mask & PIPE_MASK_S) {
353       fd_blit_stencil_fallback(ctx, &info);
354       info.mask &= ~PIPE_MASK_S;
355       if (!info.mask)
356          return true;
357    }
358 
359    if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
360       DBG("blit unsupported %s -> %s",
361           util_format_short_name(info.src.resource->format),
362           util_format_short_name(info.dst.resource->format));
363       return false;
364    }
365 
366    return fd_blitter_blit(ctx, &info);
367 }
368 
369 /**
370  * _copy_region using pipe (3d engine)
371  */
372 static bool
fd_blitter_pipe_copy_region(struct fd_context * ctx,struct pipe_resource * dst,unsigned dst_level,unsigned dstx,unsigned dsty,unsigned dstz,struct pipe_resource * src,unsigned src_level,const struct pipe_box * src_box)373 fd_blitter_pipe_copy_region(struct fd_context *ctx, struct pipe_resource *dst,
374                             unsigned dst_level, unsigned dstx, unsigned dsty,
375                             unsigned dstz, struct pipe_resource *src,
376                             unsigned src_level,
377                             const struct pipe_box *src_box) assert_dt
378 {
379    /* not until we allow rendertargets to be buffers */
380    if (dst->target == PIPE_BUFFER || src->target == PIPE_BUFFER)
381       return false;
382 
383    if (!util_blitter_is_copy_supported(ctx->blitter, dst, src))
384       return false;
385 
386    if (src == dst) {
387       struct pipe_context *pctx = &ctx->base;
388       pctx->flush(pctx, NULL, 0);
389    }
390 
391    /* TODO we could invalidate if dst box covers dst level fully. */
392    fd_blitter_pipe_begin(ctx, false);
393    util_blitter_copy_texture(ctx->blitter, dst, dst_level, dstx, dsty, dstz,
394                              src, src_level, src_box);
395    fd_blitter_pipe_end(ctx);
396 
397    return true;
398 }
399 
400 /**
401  * Copy a block of pixels from one resource to another.
402  * The resource must be of the same format.
403  */
404 void
fd_resource_copy_region(struct pipe_context * pctx,struct pipe_resource * dst,unsigned dst_level,unsigned dstx,unsigned dsty,unsigned dstz,struct pipe_resource * src,unsigned src_level,const struct pipe_box * src_box)405 fd_resource_copy_region(struct pipe_context *pctx, struct pipe_resource *dst,
406                         unsigned dst_level, unsigned dstx, unsigned dsty,
407                         unsigned dstz, struct pipe_resource *src,
408                         unsigned src_level, const struct pipe_box *src_box)
409 {
410    struct fd_context *ctx = fd_context(pctx);
411 
412    /* The blitter path handles compressed formats only if src and dst format
413     * match, in other cases just fall back to sw:
414     */
415    if ((src->format != dst->format) &&
416        (util_format_is_compressed(src->format) ||
417         util_format_is_compressed(dst->format))) {
418       perf_debug_ctx(ctx, "copy_region falls back to sw for {%"PRSC_FMT"} to {%"PRSC_FMT"}",
419                      PRSC_ARGS(src), PRSC_ARGS(dst));
420       goto fallback;
421    }
422 
423    if (ctx->blit) {
424       struct pipe_blit_info info;
425 
426       memset(&info, 0, sizeof info);
427       info.dst.resource = dst;
428       info.dst.level = dst_level;
429       info.dst.box.x = dstx;
430       info.dst.box.y = dsty;
431       info.dst.box.z = dstz;
432       info.dst.box.width = src_box->width;
433       info.dst.box.height = src_box->height;
434       assert(info.dst.box.width >= 0);
435       assert(info.dst.box.height >= 0);
436       info.dst.box.depth = 1;
437       info.dst.format = dst->format;
438       info.src.resource = src;
439       info.src.level = src_level;
440       info.src.box = *src_box;
441       info.src.format = src->format;
442       info.mask = util_format_get_mask(src->format);
443       info.filter = PIPE_TEX_FILTER_NEAREST;
444       info.scissor_enable = 0;
445 
446       if (ctx->blit(ctx, &info))
447          return;
448    }
449 
450    /* try blit on 3d pipe: */
451    if (fd_blitter_pipe_copy_region(ctx, dst, dst_level, dstx, dsty, dstz, src,
452                                    src_level, src_box))
453       return;
454 
455    /* else fallback to pure sw: */
456 fallback:
457    util_resource_copy_region(pctx, dst, dst_level, dstx, dsty, dstz, src,
458                              src_level, src_box);
459 }
460