1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (c) 2012-2015 Etnaviv Project
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, sub license,
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
12bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
13bf215546Sopenharmony_ci * of the 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 NON-INFRINGEMENT. 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
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Wladimir J. van der Laan <laanwj@gmail.com>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "etnaviv_clear_blit.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "hw/common.xml.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "etnaviv_blt.h"
32bf215546Sopenharmony_ci#include "etnaviv_context.h"
33bf215546Sopenharmony_ci#include "etnaviv_emit.h"
34bf215546Sopenharmony_ci#include "etnaviv_format.h"
35bf215546Sopenharmony_ci#include "etnaviv_resource.h"
36bf215546Sopenharmony_ci#include "etnaviv_rs.h"
37bf215546Sopenharmony_ci#include "etnaviv_surface.h"
38bf215546Sopenharmony_ci#include "etnaviv_translate.h"
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci#include "pipe/p_defines.h"
41bf215546Sopenharmony_ci#include "pipe/p_state.h"
42bf215546Sopenharmony_ci#include "util/compiler.h"
43bf215546Sopenharmony_ci#include "util/u_blitter.h"
44bf215546Sopenharmony_ci#include "util/u_inlines.h"
45bf215546Sopenharmony_ci#include "util/u_memory.h"
46bf215546Sopenharmony_ci#include "util/u_surface.h"
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci/* Save current state for blitter operation */
49bf215546Sopenharmony_civoid
50bf215546Sopenharmony_cietna_blit_save_state(struct etna_context *ctx)
51bf215546Sopenharmony_ci{
52bf215546Sopenharmony_ci   util_blitter_save_fragment_constant_buffer_slot(ctx->blitter,
53bf215546Sopenharmony_ci                                                   ctx->constant_buffer[PIPE_SHADER_FRAGMENT].cb);
54bf215546Sopenharmony_ci   util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vertex_buffer.vb);
55bf215546Sopenharmony_ci   util_blitter_save_vertex_elements(ctx->blitter, ctx->vertex_elements);
56bf215546Sopenharmony_ci   util_blitter_save_vertex_shader(ctx->blitter, ctx->shader.bind_vs);
57bf215546Sopenharmony_ci   util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer);
58bf215546Sopenharmony_ci   util_blitter_save_viewport(ctx->blitter, &ctx->viewport_s);
59bf215546Sopenharmony_ci   util_blitter_save_scissor(ctx->blitter, &ctx->scissor);
60bf215546Sopenharmony_ci   util_blitter_save_fragment_shader(ctx->blitter, ctx->shader.bind_fs);
61bf215546Sopenharmony_ci   util_blitter_save_blend(ctx->blitter, ctx->blend);
62bf215546Sopenharmony_ci   util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa);
63bf215546Sopenharmony_ci   util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref_s);
64bf215546Sopenharmony_ci   util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask, 0);
65bf215546Sopenharmony_ci   util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer_s);
66bf215546Sopenharmony_ci   util_blitter_save_fragment_sampler_states(ctx->blitter,
67bf215546Sopenharmony_ci         ctx->num_fragment_samplers, (void **)ctx->sampler);
68bf215546Sopenharmony_ci   util_blitter_save_fragment_sampler_views(ctx->blitter,
69bf215546Sopenharmony_ci         ctx->num_fragment_sampler_views, ctx->sampler_view);
70bf215546Sopenharmony_ci}
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ciuint64_t
73bf215546Sopenharmony_cietna_clear_blit_pack_rgba(enum pipe_format format, const union pipe_color_union *color)
74bf215546Sopenharmony_ci{
75bf215546Sopenharmony_ci   union util_color uc;
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   util_pack_color_union(format, &uc, color);
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci   switch (util_format_get_blocksize(format)) {
80bf215546Sopenharmony_ci   case 1:
81bf215546Sopenharmony_ci      uc.ui[0] = uc.ui[0] << 8 | (uc.ui[0] & 0xff);
82bf215546Sopenharmony_ci      FALLTHROUGH;
83bf215546Sopenharmony_ci   case 2:
84bf215546Sopenharmony_ci      uc.ui[0] =  uc.ui[0] << 16 | (uc.ui[0] & 0xffff);
85bf215546Sopenharmony_ci      FALLTHROUGH;
86bf215546Sopenharmony_ci   case 4:
87bf215546Sopenharmony_ci      uc.ui[1] = uc.ui[0];
88bf215546Sopenharmony_ci      FALLTHROUGH;
89bf215546Sopenharmony_ci   default:
90bf215546Sopenharmony_ci      return (uint64_t) uc.ui[1] << 32 | uc.ui[0];
91bf215546Sopenharmony_ci   }
92bf215546Sopenharmony_ci}
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_cistatic void
95bf215546Sopenharmony_cietna_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
96bf215546Sopenharmony_ci{
97bf215546Sopenharmony_ci   struct etna_context *ctx = etna_context(pctx);
98bf215546Sopenharmony_ci   struct pipe_blit_info info = *blit_info;
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci   if (ctx->blit(pctx, &info))
101bf215546Sopenharmony_ci      return;
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci   if (util_try_blit_via_copy_region(pctx, &info, false))
104bf215546Sopenharmony_ci      return;
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   if (info.mask & PIPE_MASK_S) {
107bf215546Sopenharmony_ci      DBG("cannot blit stencil, skipping");
108bf215546Sopenharmony_ci      info.mask &= ~PIPE_MASK_S;
109bf215546Sopenharmony_ci   }
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
112bf215546Sopenharmony_ci      DBG("blit unsupported %s -> %s",
113bf215546Sopenharmony_ci          util_format_short_name(info.src.resource->format),
114bf215546Sopenharmony_ci          util_format_short_name(info.dst.resource->format));
115bf215546Sopenharmony_ci      return;
116bf215546Sopenharmony_ci   }
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci   etna_blit_save_state(ctx);
119bf215546Sopenharmony_ci   util_blitter_blit(ctx->blitter, &info);
120bf215546Sopenharmony_ci}
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_cistatic void
123bf215546Sopenharmony_cietna_clear_render_target(struct pipe_context *pctx, struct pipe_surface *dst,
124bf215546Sopenharmony_ci                         const union pipe_color_union *color, unsigned dstx,
125bf215546Sopenharmony_ci                         unsigned dsty, unsigned width, unsigned height,
126bf215546Sopenharmony_ci                         bool render_condition_enabled)
127bf215546Sopenharmony_ci{
128bf215546Sopenharmony_ci   struct etna_context *ctx = etna_context(pctx);
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci   /* XXX could fall back to RS when target area is full screen / resolveable
131bf215546Sopenharmony_ci    * and no TS. */
132bf215546Sopenharmony_ci   etna_blit_save_state(ctx);
133bf215546Sopenharmony_ci   util_blitter_clear_render_target(ctx->blitter, dst, color, dstx, dsty, width, height);
134bf215546Sopenharmony_ci}
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_cistatic void
137bf215546Sopenharmony_cietna_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *dst,
138bf215546Sopenharmony_ci                         unsigned clear_flags, double depth, unsigned stencil,
139bf215546Sopenharmony_ci                         unsigned dstx, unsigned dsty, unsigned width,
140bf215546Sopenharmony_ci                         unsigned height, bool render_condition_enabled)
141bf215546Sopenharmony_ci{
142bf215546Sopenharmony_ci   struct etna_context *ctx = etna_context(pctx);
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci   /* XXX could fall back to RS when target area is full screen / resolveable
145bf215546Sopenharmony_ci    * and no TS. */
146bf215546Sopenharmony_ci   etna_blit_save_state(ctx);
147bf215546Sopenharmony_ci   util_blitter_clear_depth_stencil(ctx->blitter, dst, clear_flags, depth,
148bf215546Sopenharmony_ci                                    stencil, dstx, dsty, width, height);
149bf215546Sopenharmony_ci}
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_cistatic void
152bf215546Sopenharmony_cietna_resource_copy_region(struct pipe_context *pctx, struct pipe_resource *dst,
153bf215546Sopenharmony_ci                          unsigned dst_level, unsigned dstx, unsigned dsty,
154bf215546Sopenharmony_ci                          unsigned dstz, struct pipe_resource *src,
155bf215546Sopenharmony_ci                          unsigned src_level, const struct pipe_box *src_box)
156bf215546Sopenharmony_ci{
157bf215546Sopenharmony_ci   struct etna_context *ctx = etna_context(pctx);
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ci   if (src->target != PIPE_BUFFER && dst->target != PIPE_BUFFER &&
160bf215546Sopenharmony_ci       util_blitter_is_copy_supported(ctx->blitter, dst, src)) {
161bf215546Sopenharmony_ci      etna_blit_save_state(ctx);
162bf215546Sopenharmony_ci      util_blitter_copy_texture(ctx->blitter, dst, dst_level, dstx, dsty, dstz,
163bf215546Sopenharmony_ci                                src, src_level, src_box);
164bf215546Sopenharmony_ci   } else {
165bf215546Sopenharmony_ci      util_resource_copy_region(pctx, dst, dst_level, dstx, dsty, dstz, src,
166bf215546Sopenharmony_ci                                src_level, src_box);
167bf215546Sopenharmony_ci   }
168bf215546Sopenharmony_ci}
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_cistatic void
171bf215546Sopenharmony_cietna_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
172bf215546Sopenharmony_ci{
173bf215546Sopenharmony_ci   struct etna_resource *rsc = etna_resource(prsc);
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci   if (rsc->render) {
176bf215546Sopenharmony_ci      if (etna_resource_older(rsc, etna_resource(rsc->render))) {
177bf215546Sopenharmony_ci         etna_copy_resource(pctx, prsc, rsc->render, 0, 0);
178bf215546Sopenharmony_ci         rsc->seqno = etna_resource(rsc->render)->seqno;
179bf215546Sopenharmony_ci      }
180bf215546Sopenharmony_ci   } else if (etna_resource_needs_flush(rsc)) {
181bf215546Sopenharmony_ci      etna_copy_resource(pctx, prsc, prsc, 0, 0);
182bf215546Sopenharmony_ci      rsc->flush_seqno = rsc->seqno;
183bf215546Sopenharmony_ci   }
184bf215546Sopenharmony_ci}
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_civoid
187bf215546Sopenharmony_cietna_copy_resource(struct pipe_context *pctx, struct pipe_resource *dst,
188bf215546Sopenharmony_ci                   struct pipe_resource *src, int first_level, int last_level)
189bf215546Sopenharmony_ci{
190bf215546Sopenharmony_ci   struct etna_resource *src_priv = etna_resource(src);
191bf215546Sopenharmony_ci   struct etna_resource *dst_priv = etna_resource(dst);
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci   assert(src->format == dst->format);
194bf215546Sopenharmony_ci   assert(src->array_size == dst->array_size);
195bf215546Sopenharmony_ci   assert(last_level <= dst->last_level && last_level <= src->last_level);
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ci   struct pipe_blit_info blit = {};
198bf215546Sopenharmony_ci   blit.mask = util_format_get_mask(dst->format);
199bf215546Sopenharmony_ci   blit.filter = PIPE_TEX_FILTER_NEAREST;
200bf215546Sopenharmony_ci   blit.src.resource = src;
201bf215546Sopenharmony_ci   blit.src.format = src->format;
202bf215546Sopenharmony_ci   blit.dst.resource = dst;
203bf215546Sopenharmony_ci   blit.dst.format = dst->format;
204bf215546Sopenharmony_ci   blit.dst.box.depth = blit.src.box.depth = 1;
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_ci   /* Copy each level and each layer */
207bf215546Sopenharmony_ci   for (int level = first_level; level <= last_level; level++) {
208bf215546Sopenharmony_ci      blit.src.level = blit.dst.level = level;
209bf215546Sopenharmony_ci      blit.src.box.width = blit.dst.box.width =
210bf215546Sopenharmony_ci         MIN2(src_priv->levels[level].padded_width, dst_priv->levels[level].padded_width);
211bf215546Sopenharmony_ci      blit.src.box.height = blit.dst.box.height =
212bf215546Sopenharmony_ci         MIN2(src_priv->levels[level].padded_height, dst_priv->levels[level].padded_height);
213bf215546Sopenharmony_ci      unsigned depth = MIN2(src_priv->levels[level].depth, dst_priv->levels[level].depth);
214bf215546Sopenharmony_ci      if (dst->array_size > 1) {
215bf215546Sopenharmony_ci         assert(depth == 1); /* no array of 3d texture */
216bf215546Sopenharmony_ci         depth = dst->array_size;
217bf215546Sopenharmony_ci      }
218bf215546Sopenharmony_ci
219bf215546Sopenharmony_ci      for (int z = 0; z < depth; z++) {
220bf215546Sopenharmony_ci         blit.src.box.z = blit.dst.box.z = z;
221bf215546Sopenharmony_ci         pctx->blit(pctx, &blit);
222bf215546Sopenharmony_ci      }
223bf215546Sopenharmony_ci   }
224bf215546Sopenharmony_ci}
225bf215546Sopenharmony_ci
226bf215546Sopenharmony_civoid
227bf215546Sopenharmony_cietna_copy_resource_box(struct pipe_context *pctx, struct pipe_resource *dst,
228bf215546Sopenharmony_ci                       struct pipe_resource *src, int level,
229bf215546Sopenharmony_ci                       struct pipe_box *box)
230bf215546Sopenharmony_ci{
231bf215546Sopenharmony_ci   assert(src->format == dst->format);
232bf215546Sopenharmony_ci   assert(src->array_size == dst->array_size);
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_ci   struct pipe_blit_info blit = {};
235bf215546Sopenharmony_ci   blit.mask = util_format_get_mask(dst->format);
236bf215546Sopenharmony_ci   blit.filter = PIPE_TEX_FILTER_NEAREST;
237bf215546Sopenharmony_ci   blit.src.resource = src;
238bf215546Sopenharmony_ci   blit.src.format = src->format;
239bf215546Sopenharmony_ci   blit.src.box = *box;
240bf215546Sopenharmony_ci   blit.dst.resource = dst;
241bf215546Sopenharmony_ci   blit.dst.format = dst->format;
242bf215546Sopenharmony_ci   blit.dst.box = *box;
243bf215546Sopenharmony_ci
244bf215546Sopenharmony_ci   blit.dst.box.depth = blit.src.box.depth = 1;
245bf215546Sopenharmony_ci   blit.src.level = blit.dst.level = level;
246bf215546Sopenharmony_ci
247bf215546Sopenharmony_ci   for (int z = 0; z < box->depth; z++) {
248bf215546Sopenharmony_ci      blit.src.box.z = blit.dst.box.z = box->z + z;
249bf215546Sopenharmony_ci      pctx->blit(pctx, &blit);
250bf215546Sopenharmony_ci   }
251bf215546Sopenharmony_ci}
252bf215546Sopenharmony_ci
253bf215546Sopenharmony_civoid
254bf215546Sopenharmony_cietna_clear_blit_init(struct pipe_context *pctx)
255bf215546Sopenharmony_ci{
256bf215546Sopenharmony_ci   struct etna_context *ctx = etna_context(pctx);
257bf215546Sopenharmony_ci   struct etna_screen *screen = ctx->screen;
258bf215546Sopenharmony_ci
259bf215546Sopenharmony_ci   pctx->blit = etna_blit;
260bf215546Sopenharmony_ci   pctx->clear_render_target = etna_clear_render_target;
261bf215546Sopenharmony_ci   pctx->clear_depth_stencil = etna_clear_depth_stencil;
262bf215546Sopenharmony_ci   pctx->resource_copy_region = etna_resource_copy_region;
263bf215546Sopenharmony_ci   pctx->flush_resource = etna_flush_resource;
264bf215546Sopenharmony_ci
265bf215546Sopenharmony_ci   if (screen->specs.use_blt)
266bf215546Sopenharmony_ci      etna_clear_blit_blt_init(pctx);
267bf215546Sopenharmony_ci   else
268bf215546Sopenharmony_ci      etna_clear_blit_rs_init(pctx);
269bf215546Sopenharmony_ci}
270