1 /*
2 * Copyright © 2017 Rob Clark <robclark@freedesktop.org>
3 * SPDX-License-Identifier: MIT
4 *
5 * Authors:
6 * Rob Clark <robclark@freedesktop.org>
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->caps.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, MESA_PRIM_UNKNOWN);
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 util_blitter_restore_constant_buffer_state(ctx->blitter);
103 }
104
105 static void
fd_blitter_prep(struct fd_context * ctx,const struct pipe_blit_info * info)106 fd_blitter_prep(struct fd_context *ctx, const struct pipe_blit_info *info)
107 assert_dt
108 {
109 struct pipe_resource *dst = info->dst.resource;
110 struct pipe_resource *src = info->src.resource;
111 struct pipe_context *pipe = &ctx->base;
112
113 /* If the blit is updating the whole contents of the resource,
114 * invalidate it so we don't trigger any unnecessary tile loads in the 3D
115 * path.
116 */
117 if (util_blit_covers_whole_resource(info))
118 pipe->invalidate_resource(pipe, info->dst.resource);
119
120 /* The blit format may not match the resource format in this path, so
121 * we need to validate that we can use the src/dst resource with the
122 * requested format (and uncompress if necessary). Normally this would
123 * happen in ->set_sampler_view(), ->set_framebuffer_state(), etc. But
124 * that would cause recursion back into u_blitter, which ends in tears.
125 *
126 * To avoid recursion, this needs to be done before util_blitter_save_*()
127 */
128 if (ctx->validate_format) {
129 ctx->validate_format(ctx, fd_resource(dst), info->dst.format);
130 ctx->validate_format(ctx, fd_resource(src), info->src.format);
131 }
132
133 if (src == dst)
134 pipe->flush(pipe, NULL, 0);
135
136 DBG_BLIT(info, NULL);
137
138 fd_blitter_pipe_begin(ctx, info->render_condition_enable);
139 }
140
141 bool
fd_blitter_blit(struct fd_context * ctx,const struct pipe_blit_info * info)142 fd_blitter_blit(struct fd_context *ctx, const struct pipe_blit_info *info)
143 {
144 struct pipe_resource *dst = info->dst.resource;
145 struct pipe_resource *src = info->src.resource;
146 struct pipe_context *pipe = &ctx->base;
147 struct pipe_surface *dst_view, dst_templ;
148 struct pipe_sampler_view src_templ, *src_view;
149
150 fd_blitter_prep(ctx, info);
151
152 /* Initialize the surface. */
153 default_dst_texture(&dst_templ, dst, info->dst.level, 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(
164 ctx->blitter, dst_view, &info->dst.box, src_view, &info->src.box,
165 src->width0, src->height0, info->mask, info->filter,
166 info->scissor_enable ? &info->scissor : NULL, info->alpha_blend, false, 0,
167 NULL);
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 /* While this shouldn't technically be necessary, it is required for
175 * dEQP-GLES31.functional.stencil_texturing.format.stencil_index8_cube and
176 * 2d_array to pass.
177 */
178 fd_bc_flush_writer(ctx, fd_resource(info->dst.resource));
179
180 /* The fallback blitter must never fail: */
181 return true;
182 }
183
184 /* Generic clear implementation (partially) using u_blitter: */
185 void
fd_blitter_clear(struct pipe_context * pctx,unsigned buffers,const union pipe_color_union * color,double depth,unsigned stencil)186 fd_blitter_clear(struct pipe_context *pctx, unsigned buffers,
187 const union pipe_color_union *color, double depth,
188 unsigned stencil)
189 {
190 struct fd_context *ctx = fd_context(pctx);
191 struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
192 struct blitter_context *blitter = ctx->blitter;
193
194 /* Note: don't use discard=true, if there was something to
195 * discard, that would have been already handled in fd_clear().
196 */
197 fd_blitter_pipe_begin(ctx, false);
198
199 util_blitter_common_clear_setup(blitter, pfb->width, pfb->height, buffers,
200 NULL, NULL);
201
202 struct pipe_stencil_ref sr = {.ref_value = {stencil & 0xff}};
203 pctx->set_stencil_ref(pctx, sr);
204
205 struct pipe_constant_buffer cb = {
206 .buffer_size = 16,
207 .user_buffer = &color->ui,
208 };
209 pctx->set_constant_buffer(pctx, PIPE_SHADER_FRAGMENT, 0, false, &cb);
210
211 unsigned rs_idx = pfb->samples > 1 ? 1 : 0;
212 if (!ctx->clear_rs_state[rs_idx]) {
213 const struct pipe_rasterizer_state tmpl = {
214 .cull_face = PIPE_FACE_NONE,
215 .half_pixel_center = 1,
216 .bottom_edge_rule = 1,
217 .flatshade = 1,
218 .depth_clip_near = 1,
219 .depth_clip_far = 1,
220 .multisample = pfb->samples > 1,
221 };
222 ctx->clear_rs_state[rs_idx] = pctx->create_rasterizer_state(pctx, &tmpl);
223 }
224 pctx->bind_rasterizer_state(pctx, ctx->clear_rs_state[rs_idx]);
225
226 struct pipe_viewport_state vp = {
227 .scale = {0.5f * pfb->width, -0.5f * pfb->height, depth},
228 .translate = {0.5f * pfb->width, 0.5f * pfb->height, 0.0f},
229 };
230 pctx->set_viewport_states(pctx, 0, 1, &vp);
231
232 pctx->bind_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
233 util_set_vertex_buffers(pctx, 1, false,
234 &ctx->solid_vbuf_state.vertexbuf.vb[0]);
235 pctx->set_stream_output_targets(pctx, 0, NULL, NULL, 0);
236
237 if (pfb->layers > 1)
238 pctx->bind_vs_state(pctx, ctx->solid_layered_prog.vs);
239 else
240 pctx->bind_vs_state(pctx, ctx->solid_prog.vs);
241
242 pctx->bind_fs_state(pctx, ctx->solid_prog.fs);
243
244 /* Clear geom/tess shaders, lest the draw emit code think we are
245 * trying to use use them:
246 */
247 pctx->bind_gs_state(pctx, NULL);
248 pctx->bind_tcs_state(pctx, NULL);
249 pctx->bind_tes_state(pctx, NULL);
250
251 struct pipe_draw_info info = {
252 .mode = MESA_PRIM_COUNT, /* maps to DI_PT_RECTLIST */
253 .index_bounds_valid = true,
254 .max_index = 1,
255 .instance_count = MAX2(1, pfb->layers),
256 };
257 struct pipe_draw_start_count_bias draw = {
258 .count = 2,
259 };
260
261 pctx->draw_vbo(pctx, &info, 0, NULL, &draw, 1);
262
263 /* We expect that this should not have triggered a change in pfb: */
264 assert(util_framebuffer_state_equal(pfb, &ctx->framebuffer));
265
266 util_blitter_restore_vertex_states(blitter);
267 util_blitter_restore_fragment_states(blitter);
268 util_blitter_restore_textures(blitter);
269 util_blitter_restore_fb_state(blitter);
270 util_blitter_restore_render_cond(blitter);
271 util_blitter_unset_running_flag(blitter);
272
273 fd_blitter_pipe_end(ctx);
274 }
275
276 /* Partially generic clear_render_target implementation using u_blitter */
277 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)278 fd_blitter_clear_render_target(struct pipe_context *pctx, struct pipe_surface *ps,
279 const union pipe_color_union *color, unsigned x,
280 unsigned y, unsigned w, unsigned h,
281 bool render_condition_enabled)
282 {
283 struct fd_context *ctx = fd_context(pctx);
284
285 fd_blitter_pipe_begin(ctx, render_condition_enabled);
286 util_blitter_clear_render_target(ctx->blitter, ps, color, x, y, w, h);
287 fd_blitter_pipe_end(ctx);
288 }
289
290 /* Partially generic clear_depth_stencil implementation using u_blitter */
291 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)292 fd_blitter_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *ps,
293 unsigned buffers, double depth, unsigned stencil,
294 unsigned x, unsigned y, unsigned w, unsigned h,
295 bool render_condition_enabled)
296 {
297 struct fd_context *ctx = fd_context(pctx);
298
299 fd_blitter_pipe_begin(ctx, render_condition_enabled);
300 util_blitter_clear_depth_stencil(ctx->blitter, ps, buffers, depth,
301 stencil, x, y, w, h);
302 fd_blitter_pipe_end(ctx);
303 }
304
305 static void
fd_blit_stencil_fallback(struct fd_context * ctx,const struct pipe_blit_info * info)306 fd_blit_stencil_fallback(struct fd_context *ctx, const struct pipe_blit_info *info)
307 assert_dt
308 {
309 struct pipe_context *pctx = &ctx->base;
310 struct pipe_surface *dst_view, dst_templ;
311
312 util_blitter_default_dst_texture(&dst_templ, info->dst.resource,
313 info->dst.level, info->dst.box.z);
314
315 dst_view = pctx->create_surface(pctx, info->dst.resource, &dst_templ);
316
317 fd_blitter_prep(ctx, info);
318
319 util_blitter_clear_depth_stencil(ctx->blitter, dst_view, PIPE_CLEAR_STENCIL,
320 0, 0, info->dst.box.x, info->dst.box.y,
321 info->dst.box.width, info->dst.box.height);
322
323 fd_blitter_prep(ctx, info);
324
325 util_blitter_stencil_fallback(
326 ctx->blitter, info->dst.resource, info->dst.level, &info->dst.box,
327 info->src.resource, info->src.level, &info->src.box,
328 info->scissor_enable ? &info->scissor : NULL);
329
330 pipe_surface_release(pctx, &dst_view);
331 }
332
333 /**
334 * Optimal hardware path for blitting pixels.
335 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
336 */
337 bool
fd_blit(struct pipe_context * pctx,const struct pipe_blit_info * blit_info)338 fd_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
339 {
340 struct fd_context *ctx = fd_context(pctx);
341 struct pipe_blit_info info = *blit_info;
342
343 if (info.render_condition_enable && !fd_render_condition_check(pctx))
344 return true;
345
346 if (ctx->blit && ctx->blit(ctx, &info))
347 return true;
348
349 if (info.mask & PIPE_MASK_S) {
350 fd_blit_stencil_fallback(ctx, &info);
351 info.mask &= ~PIPE_MASK_S;
352 if (!info.mask)
353 return true;
354 }
355
356 if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
357 DBG("blit unsupported %s -> %s",
358 util_format_short_name(info.src.resource->format),
359 util_format_short_name(info.dst.resource->format));
360 return false;
361 }
362
363 return fd_blitter_blit(ctx, &info);
364 }
365
366 /**
367 * _copy_region using pipe (3d engine)
368 */
369 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)370 fd_blitter_pipe_copy_region(struct fd_context *ctx, struct pipe_resource *dst,
371 unsigned dst_level, unsigned dstx, unsigned dsty,
372 unsigned dstz, struct pipe_resource *src,
373 unsigned src_level,
374 const struct pipe_box *src_box) assert_dt
375 {
376 /* not until we allow rendertargets to be buffers */
377 if (dst->target == PIPE_BUFFER || src->target == PIPE_BUFFER)
378 return false;
379
380 if (!util_blitter_is_copy_supported(ctx->blitter, dst, src))
381 return false;
382
383 if (src == dst) {
384 struct pipe_context *pctx = &ctx->base;
385 pctx->flush(pctx, NULL, 0);
386 }
387
388 /* TODO we could invalidate if dst box covers dst level fully. */
389 fd_blitter_pipe_begin(ctx, false);
390 util_blitter_copy_texture(ctx->blitter, dst, dst_level, dstx, dsty, dstz,
391 src, src_level, src_box);
392 fd_blitter_pipe_end(ctx);
393
394 return true;
395 }
396
397 /**
398 * Copy a block of pixels from one resource to another.
399 * The resource must be of the same format.
400 */
401 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)402 fd_resource_copy_region(struct pipe_context *pctx, struct pipe_resource *dst,
403 unsigned dst_level, unsigned dstx, unsigned dsty,
404 unsigned dstz, struct pipe_resource *src,
405 unsigned src_level, const struct pipe_box *src_box)
406 {
407 struct fd_context *ctx = fd_context(pctx);
408
409 /* The blitter path handles compressed formats only if src and dst format
410 * match, in other cases just fall back to sw:
411 */
412 if ((src->format != dst->format) &&
413 (util_format_is_compressed(src->format) ||
414 util_format_is_compressed(dst->format))) {
415 perf_debug_ctx(ctx, "copy_region falls back to sw for {%"PRSC_FMT"} to {%"PRSC_FMT"}",
416 PRSC_ARGS(src), PRSC_ARGS(dst));
417 goto fallback;
418 }
419
420 if (ctx->blit) {
421 struct pipe_blit_info info;
422
423 memset(&info, 0, sizeof info);
424 info.dst.resource = dst;
425 info.dst.level = dst_level;
426 info.dst.box.x = dstx;
427 info.dst.box.y = dsty;
428 info.dst.box.z = dstz;
429 info.dst.box.width = src_box->width;
430 info.dst.box.height = src_box->height;
431 assert(info.dst.box.width >= 0);
432 assert(info.dst.box.height >= 0);
433 info.dst.box.depth = 1;
434 info.dst.format = dst->format;
435 info.src.resource = src;
436 info.src.level = src_level;
437 info.src.box = *src_box;
438 info.src.format = src->format;
439 info.mask = util_format_get_mask(src->format);
440 info.filter = PIPE_TEX_FILTER_NEAREST;
441 info.scissor_enable = 0;
442 info.swizzle_enable = 0;
443 if (ctx->blit(ctx, &info))
444 return;
445 }
446
447 /* try blit on 3d pipe: */
448 if (fd_blitter_pipe_copy_region(ctx, dst, dst_level, dstx, dsty, dstz, src,
449 src_level, src_box))
450 return;
451
452 /* else fallback to pure sw: */
453 fallback:
454 util_resource_copy_region(pctx, dst, dst_level, dstx, dsty, dstz, src,
455 src_level, src_box);
456 }
457