1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2017 Rob Clark <robclark@freedesktop.org>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Rob Clark <robclark@freedesktop.org>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "util/u_blitter.h"
28bf215546Sopenharmony_ci#include "util/u_surface.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "freedreno_blitter.h"
31bf215546Sopenharmony_ci#include "freedreno_context.h"
32bf215546Sopenharmony_ci#include "freedreno_fence.h"
33bf215546Sopenharmony_ci#include "freedreno_resource.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci/* generic blit using u_blitter.. slightly modified version of util_blitter_blit
36bf215546Sopenharmony_ci * which also handles PIPE_BUFFER:
37bf215546Sopenharmony_ci */
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_cistatic void
40bf215546Sopenharmony_cidefault_dst_texture(struct pipe_surface *dst_templ, struct pipe_resource *dst,
41bf215546Sopenharmony_ci                    unsigned dstlevel, unsigned dstz)
42bf215546Sopenharmony_ci{
43bf215546Sopenharmony_ci   memset(dst_templ, 0, sizeof(*dst_templ));
44bf215546Sopenharmony_ci   dst_templ->u.tex.level = dstlevel;
45bf215546Sopenharmony_ci   dst_templ->u.tex.first_layer = dstz;
46bf215546Sopenharmony_ci   dst_templ->u.tex.last_layer = dstz;
47bf215546Sopenharmony_ci}
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_cistatic void
50bf215546Sopenharmony_cidefault_src_texture(struct pipe_sampler_view *src_templ,
51bf215546Sopenharmony_ci                    struct pipe_resource *src, unsigned srclevel)
52bf215546Sopenharmony_ci{
53bf215546Sopenharmony_ci   bool cube_as_2darray =
54bf215546Sopenharmony_ci      src->screen->get_param(src->screen, PIPE_CAP_SAMPLER_VIEW_TARGET);
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   memset(src_templ, 0, sizeof(*src_templ));
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   if (cube_as_2darray && (src->target == PIPE_TEXTURE_CUBE ||
59bf215546Sopenharmony_ci                           src->target == PIPE_TEXTURE_CUBE_ARRAY))
60bf215546Sopenharmony_ci      src_templ->target = PIPE_TEXTURE_2D_ARRAY;
61bf215546Sopenharmony_ci   else
62bf215546Sopenharmony_ci      src_templ->target = src->target;
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   if (src->target == PIPE_BUFFER) {
65bf215546Sopenharmony_ci      src_templ->target = PIPE_TEXTURE_1D;
66bf215546Sopenharmony_ci   }
67bf215546Sopenharmony_ci   src_templ->u.tex.first_level = srclevel;
68bf215546Sopenharmony_ci   src_templ->u.tex.last_level = srclevel;
69bf215546Sopenharmony_ci   src_templ->u.tex.first_layer = 0;
70bf215546Sopenharmony_ci   src_templ->u.tex.last_layer = src->target == PIPE_TEXTURE_3D
71bf215546Sopenharmony_ci                                    ? u_minify(src->depth0, srclevel) - 1
72bf215546Sopenharmony_ci                                    : (unsigned)(src->array_size - 1);
73bf215546Sopenharmony_ci   src_templ->swizzle_r = PIPE_SWIZZLE_X;
74bf215546Sopenharmony_ci   src_templ->swizzle_g = PIPE_SWIZZLE_Y;
75bf215546Sopenharmony_ci   src_templ->swizzle_b = PIPE_SWIZZLE_Z;
76bf215546Sopenharmony_ci   src_templ->swizzle_a = PIPE_SWIZZLE_W;
77bf215546Sopenharmony_ci}
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_cistatic void
80bf215546Sopenharmony_cifd_blitter_pipe_begin(struct fd_context *ctx, bool render_cond) assert_dt
81bf215546Sopenharmony_ci{
82bf215546Sopenharmony_ci   util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vtx.vertexbuf.vb);
83bf215546Sopenharmony_ci   util_blitter_save_vertex_elements(ctx->blitter, ctx->vtx.vtx);
84bf215546Sopenharmony_ci   util_blitter_save_vertex_shader(ctx->blitter, ctx->prog.vs);
85bf215546Sopenharmony_ci   util_blitter_save_tessctrl_shader(ctx->blitter, ctx->prog.hs);
86bf215546Sopenharmony_ci   util_blitter_save_tesseval_shader(ctx->blitter, ctx->prog.ds);
87bf215546Sopenharmony_ci   util_blitter_save_geometry_shader(ctx->blitter, ctx->prog.gs);
88bf215546Sopenharmony_ci   util_blitter_save_so_targets(ctx->blitter, ctx->streamout.num_targets,
89bf215546Sopenharmony_ci                                ctx->streamout.targets);
90bf215546Sopenharmony_ci   util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer);
91bf215546Sopenharmony_ci   util_blitter_save_viewport(ctx->blitter, &ctx->viewport);
92bf215546Sopenharmony_ci   util_blitter_save_scissor(ctx->blitter, &ctx->scissor);
93bf215546Sopenharmony_ci   util_blitter_save_fragment_shader(ctx->blitter, ctx->prog.fs);
94bf215546Sopenharmony_ci   util_blitter_save_blend(ctx->blitter, ctx->blend);
95bf215546Sopenharmony_ci   util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa);
96bf215546Sopenharmony_ci   util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref);
97bf215546Sopenharmony_ci   util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask, ctx->min_samples);
98bf215546Sopenharmony_ci   util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer);
99bf215546Sopenharmony_ci   util_blitter_save_fragment_sampler_states(
100bf215546Sopenharmony_ci      ctx->blitter, ctx->tex[PIPE_SHADER_FRAGMENT].num_samplers,
101bf215546Sopenharmony_ci      (void **)ctx->tex[PIPE_SHADER_FRAGMENT].samplers);
102bf215546Sopenharmony_ci   util_blitter_save_fragment_sampler_views(
103bf215546Sopenharmony_ci      ctx->blitter, ctx->tex[PIPE_SHADER_FRAGMENT].num_textures,
104bf215546Sopenharmony_ci      ctx->tex[PIPE_SHADER_FRAGMENT].textures);
105bf215546Sopenharmony_ci   if (!render_cond)
106bf215546Sopenharmony_ci      util_blitter_save_render_condition(ctx->blitter, ctx->cond_query,
107bf215546Sopenharmony_ci                                         ctx->cond_cond, ctx->cond_mode);
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci   if (ctx->batch)
110bf215546Sopenharmony_ci      fd_batch_update_queries(ctx->batch);
111bf215546Sopenharmony_ci}
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_cistatic void
114bf215546Sopenharmony_cifd_blitter_pipe_end(struct fd_context *ctx) assert_dt
115bf215546Sopenharmony_ci{
116bf215546Sopenharmony_ci}
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_cibool
119bf215546Sopenharmony_cifd_blitter_blit(struct fd_context *ctx, const struct pipe_blit_info *info)
120bf215546Sopenharmony_ci{
121bf215546Sopenharmony_ci   struct pipe_context *pctx = &ctx->base;
122bf215546Sopenharmony_ci   struct pipe_resource *dst = info->dst.resource;
123bf215546Sopenharmony_ci   struct pipe_resource *src = info->src.resource;
124bf215546Sopenharmony_ci   struct pipe_context *pipe = &ctx->base;
125bf215546Sopenharmony_ci   struct pipe_surface *dst_view, dst_templ;
126bf215546Sopenharmony_ci   struct pipe_sampler_view src_templ, *src_view;
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci   /* If the blit is updating the whole contents of the resource,
129bf215546Sopenharmony_ci    * invalidate it so we don't trigger any unnecessary tile loads in the 3D
130bf215546Sopenharmony_ci    * path.
131bf215546Sopenharmony_ci    */
132bf215546Sopenharmony_ci   if (util_blit_covers_whole_resource(info))
133bf215546Sopenharmony_ci      pctx->invalidate_resource(pctx, info->dst.resource);
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci   /* The blit format may not match the resource format in this path, so
136bf215546Sopenharmony_ci    * we need to validate that we can use the src/dst resource with the
137bf215546Sopenharmony_ci    * requested format (and uncompress if necessary).  Normally this would
138bf215546Sopenharmony_ci    * happen in ->set_sampler_view(), ->set_framebuffer_state(), etc.  But
139bf215546Sopenharmony_ci    * that would cause recursion back into u_blitter, which ends in tears.
140bf215546Sopenharmony_ci    *
141bf215546Sopenharmony_ci    * To avoid recursion, this needs to be done before util_blitter_save_*()
142bf215546Sopenharmony_ci    */
143bf215546Sopenharmony_ci   if (ctx->validate_format) {
144bf215546Sopenharmony_ci      ctx->validate_format(ctx, fd_resource(dst), info->dst.format);
145bf215546Sopenharmony_ci      ctx->validate_format(ctx, fd_resource(src), info->src.format);
146bf215546Sopenharmony_ci   }
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci   if (src == dst)
149bf215546Sopenharmony_ci      pipe->flush(pipe, NULL, 0);
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ci   DBG_BLIT(info, NULL);
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci   fd_blitter_pipe_begin(ctx, info->render_condition_enable);
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_ci   /* Initialize the surface. */
156bf215546Sopenharmony_ci   default_dst_texture(&dst_templ, dst, info->dst.level, info->dst.box.z);
157bf215546Sopenharmony_ci   dst_templ.format = info->dst.format;
158bf215546Sopenharmony_ci   dst_view = pipe->create_surface(pipe, dst, &dst_templ);
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci   /* Initialize the sampler view. */
161bf215546Sopenharmony_ci   default_src_texture(&src_templ, src, info->src.level);
162bf215546Sopenharmony_ci   src_templ.format = info->src.format;
163bf215546Sopenharmony_ci   src_view = pipe->create_sampler_view(pipe, src, &src_templ);
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci   /* Copy. */
166bf215546Sopenharmony_ci   util_blitter_blit_generic(
167bf215546Sopenharmony_ci      ctx->blitter, dst_view, &info->dst.box, src_view, &info->src.box,
168bf215546Sopenharmony_ci      src->width0, src->height0, info->mask, info->filter,
169bf215546Sopenharmony_ci      info->scissor_enable ? &info->scissor : NULL, info->alpha_blend, false, 0);
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci   pipe_surface_reference(&dst_view, NULL);
172bf215546Sopenharmony_ci   pipe_sampler_view_reference(&src_view, NULL);
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci   fd_blitter_pipe_end(ctx);
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ci   /* While this shouldn't technically be necessary, it is required for
177bf215546Sopenharmony_ci    * dEQP-GLES31.functional.stencil_texturing.format.stencil_index8_cube and
178bf215546Sopenharmony_ci    * 2d_array to pass.
179bf215546Sopenharmony_ci    */
180bf215546Sopenharmony_ci   fd_bc_flush_writer(ctx, fd_resource(info->dst.resource));
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci   /* The fallback blitter must never fail: */
183bf215546Sopenharmony_ci   return true;
184bf215546Sopenharmony_ci}
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_ci/* Generic clear implementation (partially) using u_blitter: */
187bf215546Sopenharmony_civoid
188bf215546Sopenharmony_cifd_blitter_clear(struct pipe_context *pctx, unsigned buffers,
189bf215546Sopenharmony_ci                 const union pipe_color_union *color, double depth,
190bf215546Sopenharmony_ci                 unsigned stencil)
191bf215546Sopenharmony_ci{
192bf215546Sopenharmony_ci   struct fd_context *ctx = fd_context(pctx);
193bf215546Sopenharmony_ci   struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
194bf215546Sopenharmony_ci   struct blitter_context *blitter = ctx->blitter;
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci   /* Note: don't use discard=true, if there was something to
197bf215546Sopenharmony_ci    * discard, that would have been already handled in fd_clear().
198bf215546Sopenharmony_ci    */
199bf215546Sopenharmony_ci   fd_blitter_pipe_begin(ctx, false);
200bf215546Sopenharmony_ci
201bf215546Sopenharmony_ci   util_blitter_save_fragment_constant_buffer_slot(
202bf215546Sopenharmony_ci      ctx->blitter, ctx->constbuf[PIPE_SHADER_FRAGMENT].cb);
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci   util_blitter_common_clear_setup(blitter, pfb->width, pfb->height, buffers,
205bf215546Sopenharmony_ci                                   NULL, NULL);
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_ci   struct pipe_stencil_ref sr = {.ref_value = {stencil & 0xff}};
208bf215546Sopenharmony_ci   pctx->set_stencil_ref(pctx, sr);
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_ci   struct pipe_constant_buffer cb = {
211bf215546Sopenharmony_ci      .buffer_size = 16,
212bf215546Sopenharmony_ci      .user_buffer = &color->ui,
213bf215546Sopenharmony_ci   };
214bf215546Sopenharmony_ci   pctx->set_constant_buffer(pctx, PIPE_SHADER_FRAGMENT, 0, false, &cb);
215bf215546Sopenharmony_ci
216bf215546Sopenharmony_ci   unsigned rs_idx = pfb->samples > 1 ? 1 : 0;
217bf215546Sopenharmony_ci   if (!ctx->clear_rs_state[rs_idx]) {
218bf215546Sopenharmony_ci      const struct pipe_rasterizer_state tmpl = {
219bf215546Sopenharmony_ci         .cull_face = PIPE_FACE_NONE,
220bf215546Sopenharmony_ci         .half_pixel_center = 1,
221bf215546Sopenharmony_ci         .bottom_edge_rule = 1,
222bf215546Sopenharmony_ci         .flatshade = 1,
223bf215546Sopenharmony_ci         .depth_clip_near = 1,
224bf215546Sopenharmony_ci         .depth_clip_far = 1,
225bf215546Sopenharmony_ci         .multisample = pfb->samples > 1,
226bf215546Sopenharmony_ci      };
227bf215546Sopenharmony_ci      ctx->clear_rs_state[rs_idx] = pctx->create_rasterizer_state(pctx, &tmpl);
228bf215546Sopenharmony_ci   }
229bf215546Sopenharmony_ci   pctx->bind_rasterizer_state(pctx, ctx->clear_rs_state[rs_idx]);
230bf215546Sopenharmony_ci
231bf215546Sopenharmony_ci   struct pipe_viewport_state vp = {
232bf215546Sopenharmony_ci      .scale = {0.5f * pfb->width, -0.5f * pfb->height, depth},
233bf215546Sopenharmony_ci      .translate = {0.5f * pfb->width, 0.5f * pfb->height, 0.0f},
234bf215546Sopenharmony_ci   };
235bf215546Sopenharmony_ci   pctx->set_viewport_states(pctx, 0, 1, &vp);
236bf215546Sopenharmony_ci
237bf215546Sopenharmony_ci   pctx->bind_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
238bf215546Sopenharmony_ci   pctx->set_vertex_buffers(pctx, blitter->vb_slot, 1, 0, false,
239bf215546Sopenharmony_ci                            &ctx->solid_vbuf_state.vertexbuf.vb[0]);
240bf215546Sopenharmony_ci   pctx->set_stream_output_targets(pctx, 0, NULL, NULL);
241bf215546Sopenharmony_ci
242bf215546Sopenharmony_ci   if (pfb->layers > 1)
243bf215546Sopenharmony_ci      pctx->bind_vs_state(pctx, ctx->solid_layered_prog.vs);
244bf215546Sopenharmony_ci   else
245bf215546Sopenharmony_ci      pctx->bind_vs_state(pctx, ctx->solid_prog.vs);
246bf215546Sopenharmony_ci
247bf215546Sopenharmony_ci   pctx->bind_fs_state(pctx, ctx->solid_prog.fs);
248bf215546Sopenharmony_ci
249bf215546Sopenharmony_ci   /* Clear geom/tess shaders, lest the draw emit code think we are
250bf215546Sopenharmony_ci    * trying to use use them:
251bf215546Sopenharmony_ci    */
252bf215546Sopenharmony_ci   pctx->bind_gs_state(pctx, NULL);
253bf215546Sopenharmony_ci   pctx->bind_tcs_state(pctx, NULL);
254bf215546Sopenharmony_ci   pctx->bind_tes_state(pctx, NULL);
255bf215546Sopenharmony_ci
256bf215546Sopenharmony_ci   struct pipe_draw_info info = {
257bf215546Sopenharmony_ci      .mode = PIPE_PRIM_MAX, /* maps to DI_PT_RECTLIST */
258bf215546Sopenharmony_ci      .index_bounds_valid = true,
259bf215546Sopenharmony_ci      .max_index = 1,
260bf215546Sopenharmony_ci      .instance_count = MAX2(1, pfb->layers),
261bf215546Sopenharmony_ci   };
262bf215546Sopenharmony_ci   struct pipe_draw_start_count_bias draw = {
263bf215546Sopenharmony_ci      .count = 2,
264bf215546Sopenharmony_ci   };
265bf215546Sopenharmony_ci
266bf215546Sopenharmony_ci   pctx->draw_vbo(pctx, &info, 0, NULL, &draw, 1);
267bf215546Sopenharmony_ci
268bf215546Sopenharmony_ci   /* We expect that this should not have triggered a change in pfb: */
269bf215546Sopenharmony_ci   assert(util_framebuffer_state_equal(pfb, &ctx->framebuffer));
270bf215546Sopenharmony_ci
271bf215546Sopenharmony_ci   util_blitter_restore_constant_buffer_state(blitter);
272bf215546Sopenharmony_ci   util_blitter_restore_vertex_states(blitter);
273bf215546Sopenharmony_ci   util_blitter_restore_fragment_states(blitter);
274bf215546Sopenharmony_ci   util_blitter_restore_textures(blitter);
275bf215546Sopenharmony_ci   util_blitter_restore_fb_state(blitter);
276bf215546Sopenharmony_ci   util_blitter_restore_render_cond(blitter);
277bf215546Sopenharmony_ci   util_blitter_unset_running_flag(blitter);
278bf215546Sopenharmony_ci
279bf215546Sopenharmony_ci   fd_blitter_pipe_end(ctx);
280bf215546Sopenharmony_ci}
281bf215546Sopenharmony_ci
282bf215546Sopenharmony_ci/**
283bf215546Sopenharmony_ci * Optimal hardware path for blitting pixels.
284bf215546Sopenharmony_ci * Scaling, format conversion, up- and downsampling (resolve) are allowed.
285bf215546Sopenharmony_ci */
286bf215546Sopenharmony_cibool
287bf215546Sopenharmony_cifd_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
288bf215546Sopenharmony_ci{
289bf215546Sopenharmony_ci   struct fd_context *ctx = fd_context(pctx);
290bf215546Sopenharmony_ci   struct pipe_blit_info info = *blit_info;
291bf215546Sopenharmony_ci
292bf215546Sopenharmony_ci   if (info.render_condition_enable && !fd_render_condition_check(pctx))
293bf215546Sopenharmony_ci      return true;
294bf215546Sopenharmony_ci
295bf215546Sopenharmony_ci   if (ctx->blit && ctx->blit(ctx, &info))
296bf215546Sopenharmony_ci      return true;
297bf215546Sopenharmony_ci
298bf215546Sopenharmony_ci   if (info.mask & PIPE_MASK_S) {
299bf215546Sopenharmony_ci      DBG("cannot blit stencil, skipping");
300bf215546Sopenharmony_ci      info.mask &= ~PIPE_MASK_S;
301bf215546Sopenharmony_ci   }
302bf215546Sopenharmony_ci
303bf215546Sopenharmony_ci   if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
304bf215546Sopenharmony_ci      DBG("blit unsupported %s -> %s",
305bf215546Sopenharmony_ci          util_format_short_name(info.src.resource->format),
306bf215546Sopenharmony_ci          util_format_short_name(info.dst.resource->format));
307bf215546Sopenharmony_ci      return false;
308bf215546Sopenharmony_ci   }
309bf215546Sopenharmony_ci
310bf215546Sopenharmony_ci   return fd_blitter_blit(ctx, &info);
311bf215546Sopenharmony_ci}
312bf215546Sopenharmony_ci
313bf215546Sopenharmony_ci/**
314bf215546Sopenharmony_ci * _copy_region using pipe (3d engine)
315bf215546Sopenharmony_ci */
316bf215546Sopenharmony_cistatic bool
317bf215546Sopenharmony_cifd_blitter_pipe_copy_region(struct fd_context *ctx, struct pipe_resource *dst,
318bf215546Sopenharmony_ci                            unsigned dst_level, unsigned dstx, unsigned dsty,
319bf215546Sopenharmony_ci                            unsigned dstz, struct pipe_resource *src,
320bf215546Sopenharmony_ci                            unsigned src_level,
321bf215546Sopenharmony_ci                            const struct pipe_box *src_box) assert_dt
322bf215546Sopenharmony_ci{
323bf215546Sopenharmony_ci   /* not until we allow rendertargets to be buffers */
324bf215546Sopenharmony_ci   if (dst->target == PIPE_BUFFER || src->target == PIPE_BUFFER)
325bf215546Sopenharmony_ci      return false;
326bf215546Sopenharmony_ci
327bf215546Sopenharmony_ci   if (!util_blitter_is_copy_supported(ctx->blitter, dst, src))
328bf215546Sopenharmony_ci      return false;
329bf215546Sopenharmony_ci
330bf215546Sopenharmony_ci   if (src == dst) {
331bf215546Sopenharmony_ci      struct pipe_context *pctx = &ctx->base;
332bf215546Sopenharmony_ci      pctx->flush(pctx, NULL, 0);
333bf215546Sopenharmony_ci   }
334bf215546Sopenharmony_ci
335bf215546Sopenharmony_ci   /* TODO we could invalidate if dst box covers dst level fully. */
336bf215546Sopenharmony_ci   fd_blitter_pipe_begin(ctx, false);
337bf215546Sopenharmony_ci   util_blitter_copy_texture(ctx->blitter, dst, dst_level, dstx, dsty, dstz,
338bf215546Sopenharmony_ci                             src, src_level, src_box);
339bf215546Sopenharmony_ci   fd_blitter_pipe_end(ctx);
340bf215546Sopenharmony_ci
341bf215546Sopenharmony_ci   return true;
342bf215546Sopenharmony_ci}
343bf215546Sopenharmony_ci
344bf215546Sopenharmony_ci/**
345bf215546Sopenharmony_ci * Copy a block of pixels from one resource to another.
346bf215546Sopenharmony_ci * The resource must be of the same format.
347bf215546Sopenharmony_ci */
348bf215546Sopenharmony_civoid
349bf215546Sopenharmony_cifd_resource_copy_region(struct pipe_context *pctx, struct pipe_resource *dst,
350bf215546Sopenharmony_ci                        unsigned dst_level, unsigned dstx, unsigned dsty,
351bf215546Sopenharmony_ci                        unsigned dstz, struct pipe_resource *src,
352bf215546Sopenharmony_ci                        unsigned src_level, const struct pipe_box *src_box)
353bf215546Sopenharmony_ci{
354bf215546Sopenharmony_ci   struct fd_context *ctx = fd_context(pctx);
355bf215546Sopenharmony_ci
356bf215546Sopenharmony_ci   /* The blitter path handles compressed formats only if src and dst format
357bf215546Sopenharmony_ci    * match, in other cases just fall back to sw:
358bf215546Sopenharmony_ci    */
359bf215546Sopenharmony_ci   if ((src->format != dst->format) &&
360bf215546Sopenharmony_ci       (util_format_is_compressed(src->format) ||
361bf215546Sopenharmony_ci        util_format_is_compressed(dst->format))) {
362bf215546Sopenharmony_ci      perf_debug_ctx(ctx, "copy_region falls back to sw for {%"PRSC_FMT"} to {%"PRSC_FMT"}",
363bf215546Sopenharmony_ci                     PRSC_ARGS(src), PRSC_ARGS(dst));
364bf215546Sopenharmony_ci      goto fallback;
365bf215546Sopenharmony_ci   }
366bf215546Sopenharmony_ci
367bf215546Sopenharmony_ci   if (ctx->blit) {
368bf215546Sopenharmony_ci      struct pipe_blit_info info;
369bf215546Sopenharmony_ci
370bf215546Sopenharmony_ci      memset(&info, 0, sizeof info);
371bf215546Sopenharmony_ci      info.dst.resource = dst;
372bf215546Sopenharmony_ci      info.dst.level = dst_level;
373bf215546Sopenharmony_ci      info.dst.box.x = dstx;
374bf215546Sopenharmony_ci      info.dst.box.y = dsty;
375bf215546Sopenharmony_ci      info.dst.box.z = dstz;
376bf215546Sopenharmony_ci      info.dst.box.width = src_box->width;
377bf215546Sopenharmony_ci      info.dst.box.height = src_box->height;
378bf215546Sopenharmony_ci      assert(info.dst.box.width >= 0);
379bf215546Sopenharmony_ci      assert(info.dst.box.height >= 0);
380bf215546Sopenharmony_ci      info.dst.box.depth = 1;
381bf215546Sopenharmony_ci      info.dst.format = dst->format;
382bf215546Sopenharmony_ci      info.src.resource = src;
383bf215546Sopenharmony_ci      info.src.level = src_level;
384bf215546Sopenharmony_ci      info.src.box = *src_box;
385bf215546Sopenharmony_ci      info.src.format = src->format;
386bf215546Sopenharmony_ci      info.mask = util_format_get_mask(src->format);
387bf215546Sopenharmony_ci      info.filter = PIPE_TEX_FILTER_NEAREST;
388bf215546Sopenharmony_ci      info.scissor_enable = 0;
389bf215546Sopenharmony_ci
390bf215546Sopenharmony_ci      if (ctx->blit(ctx, &info))
391bf215546Sopenharmony_ci         return;
392bf215546Sopenharmony_ci   }
393bf215546Sopenharmony_ci
394bf215546Sopenharmony_ci   /* try blit on 3d pipe: */
395bf215546Sopenharmony_ci   if (fd_blitter_pipe_copy_region(ctx, dst, dst_level, dstx, dsty, dstz, src,
396bf215546Sopenharmony_ci                                   src_level, src_box))
397bf215546Sopenharmony_ci      return;
398bf215546Sopenharmony_ci
399bf215546Sopenharmony_ci   /* else fallback to pure sw: */
400bf215546Sopenharmony_cifallback:
401bf215546Sopenharmony_ci   util_resource_copy_region(pctx, dst, dst_level, dstx, dsty, dstz, src,
402bf215546Sopenharmony_ci                             src_level, src_box);
403bf215546Sopenharmony_ci}
404