1/*
2 * Copyright (c) 2012-2015 Etnaviv Project
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, sub license,
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
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the 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 NON-INFRINGEMENT. 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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 *    Wladimir J. van der Laan <laanwj@gmail.com>
25 */
26
27#include "etnaviv_clear_blit.h"
28
29#include "hw/common.xml.h"
30
31#include "etnaviv_blt.h"
32#include "etnaviv_context.h"
33#include "etnaviv_emit.h"
34#include "etnaviv_format.h"
35#include "etnaviv_resource.h"
36#include "etnaviv_rs.h"
37#include "etnaviv_surface.h"
38#include "etnaviv_translate.h"
39
40#include "pipe/p_defines.h"
41#include "pipe/p_state.h"
42#include "util/u_blitter.h"
43#include "util/u_inlines.h"
44#include "util/u_memory.h"
45#include "util/u_surface.h"
46
47/* Save current state for blitter operation */
48void
49etna_blit_save_state(struct etna_context *ctx)
50{
51   util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vertex_buffer.vb);
52   util_blitter_save_vertex_elements(ctx->blitter, ctx->vertex_elements);
53   util_blitter_save_vertex_shader(ctx->blitter, ctx->shader.bind_vs);
54   util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer);
55   util_blitter_save_viewport(ctx->blitter, &ctx->viewport_s);
56   util_blitter_save_scissor(ctx->blitter, &ctx->scissor_s);
57   util_blitter_save_fragment_shader(ctx->blitter, ctx->shader.bind_fs);
58   util_blitter_save_blend(ctx->blitter, ctx->blend);
59   util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa);
60   util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref_s);
61   util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask);
62   util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer_s);
63   util_blitter_save_fragment_sampler_states(ctx->blitter,
64         ctx->num_fragment_samplers, (void **)ctx->sampler);
65   util_blitter_save_fragment_sampler_views(ctx->blitter,
66         ctx->num_fragment_sampler_views, ctx->sampler_view);
67}
68
69uint32_t
70etna_clear_blit_pack_rgba(enum pipe_format format, const float *rgba)
71{
72   union util_color uc;
73   util_pack_color(rgba, format, &uc);
74   if (util_format_get_blocksize(format) == 2)
75      return uc.ui[0] << 16 | (uc.ui[0] & 0xffff);
76   else
77      return uc.ui[0];
78}
79
80static void
81etna_clear_render_target(struct pipe_context *pctx, struct pipe_surface *dst,
82                         const union pipe_color_union *color, unsigned dstx,
83                         unsigned dsty, unsigned width, unsigned height,
84                         bool render_condition_enabled)
85{
86   struct etna_context *ctx = etna_context(pctx);
87
88   /* XXX could fall back to RS when target area is full screen / resolveable
89    * and no TS. */
90   etna_blit_save_state(ctx);
91   util_blitter_clear_render_target(ctx->blitter, dst, color, dstx, dsty, width, height);
92}
93
94static void
95etna_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *dst,
96                         unsigned clear_flags, double depth, unsigned stencil,
97                         unsigned dstx, unsigned dsty, unsigned width,
98                         unsigned height, bool render_condition_enabled)
99{
100   struct etna_context *ctx = etna_context(pctx);
101
102   /* XXX could fall back to RS when target area is full screen / resolveable
103    * and no TS. */
104   etna_blit_save_state(ctx);
105   util_blitter_clear_depth_stencil(ctx->blitter, dst, clear_flags, depth,
106                                    stencil, dstx, dsty, width, height);
107}
108
109static void
110etna_resource_copy_region(struct pipe_context *pctx, struct pipe_resource *dst,
111                          unsigned dst_level, unsigned dstx, unsigned dsty,
112                          unsigned dstz, struct pipe_resource *src,
113                          unsigned src_level, const struct pipe_box *src_box)
114{
115   struct etna_context *ctx = etna_context(pctx);
116
117   /* The resource must be of the same format. */
118   assert(src->format == dst->format);
119
120   /* XXX we can use the RS as a literal copy engine here
121    * the only complexity is tiling; the size of the boxes needs to be aligned
122    * to the tile size
123    * how to handle the case where a resource is copied from/to a non-aligned
124    * position?
125    * from non-aligned: can fall back to rendering-based copy?
126    * to non-aligned: can fall back to rendering-based copy?
127    * XXX this goes wrong when source surface is supertiled.
128    */
129   if (util_blitter_is_copy_supported(ctx->blitter, dst, src)) {
130      etna_blit_save_state(ctx);
131      util_blitter_copy_texture(ctx->blitter, dst, dst_level, dstx, dsty, dstz,
132                                src, src_level, src_box);
133   } else {
134      util_resource_copy_region(pctx, dst, dst_level, dstx, dsty, dstz, src,
135                                src_level, src_box);
136   }
137}
138
139static void
140etna_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
141{
142   struct etna_resource *rsc = etna_resource(prsc);
143
144   if (rsc->external) {
145      if (etna_resource_older(etna_resource(rsc->external), rsc)) {
146         etna_copy_resource(pctx, rsc->external, prsc, 0, 0);
147         etna_resource(rsc->external)->seqno = rsc->seqno;
148      }
149   } else if (etna_resource_needs_flush(rsc)) {
150      etna_copy_resource(pctx, prsc, prsc, 0, 0);
151      rsc->flush_seqno = rsc->seqno;
152   }
153}
154
155void
156etna_copy_resource(struct pipe_context *pctx, struct pipe_resource *dst,
157                   struct pipe_resource *src, int first_level, int last_level)
158{
159   struct etna_resource *src_priv = etna_resource(src);
160   struct etna_resource *dst_priv = etna_resource(dst);
161
162   assert(src->format == dst->format);
163   assert(src->array_size == dst->array_size);
164   assert(last_level <= dst->last_level && last_level <= src->last_level);
165
166   struct pipe_blit_info blit = {};
167   blit.mask = util_format_get_mask(dst->format);
168   blit.filter = PIPE_TEX_FILTER_NEAREST;
169   blit.src.resource = src;
170   blit.src.format = src->format;
171   blit.dst.resource = dst;
172   blit.dst.format = dst->format;
173   blit.dst.box.depth = blit.src.box.depth = 1;
174
175   /* Copy each level and each layer */
176   for (int level = first_level; level <= last_level; level++) {
177      blit.src.level = blit.dst.level = level;
178      blit.src.box.width = blit.dst.box.width =
179         MIN2(src_priv->levels[level].padded_width, dst_priv->levels[level].padded_width);
180      blit.src.box.height = blit.dst.box.height =
181         MIN2(src_priv->levels[level].padded_height, dst_priv->levels[level].padded_height);
182
183      for (int layer = 0; layer < dst->array_size; layer++) {
184         blit.src.box.z = blit.dst.box.z = layer;
185         pctx->blit(pctx, &blit);
186      }
187   }
188}
189
190void
191etna_copy_resource_box(struct pipe_context *pctx, struct pipe_resource *dst,
192                       struct pipe_resource *src, int level,
193                       struct pipe_box *box)
194{
195   assert(src->format == dst->format);
196   assert(src->array_size == dst->array_size);
197
198   struct pipe_blit_info blit = {};
199   blit.mask = util_format_get_mask(dst->format);
200   blit.filter = PIPE_TEX_FILTER_NEAREST;
201   blit.src.resource = src;
202   blit.src.format = src->format;
203   blit.src.box = *box;
204   blit.dst.resource = dst;
205   blit.dst.format = dst->format;
206   blit.dst.box = *box;
207
208   blit.dst.box.depth = blit.src.box.depth = 1;
209   blit.src.level = blit.dst.level = level;
210
211   for (int layer = 0; layer < dst->array_size; layer++) {
212      blit.src.box.z = blit.dst.box.z = layer;
213      pctx->blit(pctx, &blit);
214   }
215}
216
217void
218etna_clear_blit_init(struct pipe_context *pctx)
219{
220   struct etna_context *ctx = etna_context(pctx);
221
222   pctx->clear_render_target = etna_clear_render_target;
223   pctx->clear_depth_stencil = etna_clear_depth_stencil;
224   pctx->resource_copy_region = etna_resource_copy_region;
225   pctx->flush_resource = etna_flush_resource;
226
227   if (ctx->specs.use_blt)
228      etna_clear_blit_blt_init(pctx);
229   else
230      etna_clear_blit_rs_init(pctx);
231}
232