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 <[email protected]>
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/compiler.h"
43 #include "util/u_blitter.h"
44 #include "util/u_inlines.h"
45 #include "util/u_memory.h"
46 #include "util/u_surface.h"
47
48 /* Save current state for blitter operation */
49 void
etna_blit_save_state(struct etna_context * ctx,bool render_cond)50 etna_blit_save_state(struct etna_context *ctx, bool render_cond)
51 {
52 util_blitter_save_fragment_constant_buffer_slot(ctx->blitter,
53 ctx->constant_buffer[PIPE_SHADER_FRAGMENT].cb);
54 util_blitter_save_vertex_buffers(ctx->blitter, ctx->vertex_buffer.vb,
55 ctx->vertex_buffer.count);
56 util_blitter_save_vertex_elements(ctx->blitter, ctx->vertex_elements);
57 util_blitter_save_vertex_shader(ctx->blitter, ctx->shader.bind_vs);
58 util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer);
59 util_blitter_save_viewport(ctx->blitter, &ctx->viewport_s);
60 util_blitter_save_scissor(ctx->blitter, &ctx->scissor);
61 util_blitter_save_fragment_shader(ctx->blitter, ctx->shader.bind_fs);
62 util_blitter_save_blend(ctx->blitter, ctx->blend);
63 util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa);
64 util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref_s);
65 util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask, 0);
66 util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer_s);
67 util_blitter_save_fragment_sampler_states(ctx->blitter,
68 ctx->num_fragment_samplers, (void **)ctx->sampler);
69 util_blitter_save_fragment_sampler_views(ctx->blitter,
70 ctx->num_fragment_sampler_views, ctx->sampler_view);
71
72 if (!render_cond)
73 util_blitter_save_render_condition(ctx->blitter,
74 ctx->cond_query, ctx->cond_cond, ctx->cond_mode);
75
76 if (DBG_ENABLED(ETNA_DBG_DEQP))
77 util_blitter_save_so_targets(ctx->blitter, 0, NULL);
78 }
79
80 uint64_t
etna_clear_blit_pack_rgba(enum pipe_format format,const union pipe_color_union * color)81 etna_clear_blit_pack_rgba(enum pipe_format format, const union pipe_color_union *color)
82 {
83 union util_color uc;
84
85 util_pack_color_union(format, &uc, color);
86
87 switch (util_format_get_blocksize(format)) {
88 case 1:
89 uc.ui[0] = uc.ui[0] << 8 | (uc.ui[0] & 0xff);
90 FALLTHROUGH;
91 case 2:
92 uc.ui[0] = uc.ui[0] << 16 | (uc.ui[0] & 0xffff);
93 FALLTHROUGH;
94 case 4:
95 uc.ui[1] = uc.ui[0];
96 FALLTHROUGH;
97 default:
98 return (uint64_t) uc.ui[1] << 32 | uc.ui[0];
99 }
100 }
101
102 static void
etna_blit(struct pipe_context * pctx,const struct pipe_blit_info * blit_info)103 etna_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
104 {
105 struct etna_context *ctx = etna_context(pctx);
106 struct pipe_blit_info info = *blit_info;
107
108 if (info.render_condition_enable && !etna_render_condition_check(pctx))
109 return;
110
111 if (ctx->blit(pctx, &info))
112 goto success;
113
114 if (util_try_blit_via_copy_region(pctx, &info, false))
115 goto success;
116
117 if (info.mask & PIPE_MASK_S) {
118 DBG("cannot blit stencil, skipping");
119 info.mask &= ~PIPE_MASK_S;
120 }
121
122 if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
123 DBG("blit unsupported %s -> %s",
124 util_format_short_name(info.src.resource->format),
125 util_format_short_name(info.dst.resource->format));
126 return;
127 }
128
129 etna_blit_save_state(ctx, info.render_condition_enable);
130 util_blitter_blit(ctx->blitter, &info, NULL);
131
132 success:
133 if (info.dst.resource->bind & PIPE_BIND_SAMPLER_VIEW)
134 ctx->dirty |= ETNA_DIRTY_TEXTURE_CACHES;
135 }
136
137 static void
etna_clear_render_target(struct pipe_context * pctx,struct pipe_surface * dst,const union pipe_color_union * color,unsigned dstx,unsigned dsty,unsigned width,unsigned height,bool render_condition_enabled)138 etna_clear_render_target(struct pipe_context *pctx, struct pipe_surface *dst,
139 const union pipe_color_union *color, unsigned dstx,
140 unsigned dsty, unsigned width, unsigned height,
141 bool render_condition_enabled)
142 {
143 struct etna_context *ctx = etna_context(pctx);
144
145 /* XXX could fall back to RS when target area is full screen / resolveable
146 * and no TS. */
147 etna_blit_save_state(ctx, false);
148 util_blitter_clear_render_target(ctx->blitter, dst, color, dstx, dsty, width, height);
149 }
150
151 static void
etna_clear_depth_stencil(struct pipe_context * pctx,struct pipe_surface * dst,unsigned clear_flags,double depth,unsigned stencil,unsigned dstx,unsigned dsty,unsigned width,unsigned height,bool render_condition_enabled)152 etna_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *dst,
153 unsigned clear_flags, double depth, unsigned stencil,
154 unsigned dstx, unsigned dsty, unsigned width,
155 unsigned height, bool render_condition_enabled)
156 {
157 struct etna_context *ctx = etna_context(pctx);
158
159 /* XXX could fall back to RS when target area is full screen / resolveable
160 * and no TS. */
161 etna_blit_save_state(ctx, false);
162 util_blitter_clear_depth_stencil(ctx->blitter, dst, clear_flags, depth,
163 stencil, dstx, dsty, width, height);
164 }
165
166 static void
etna_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)167 etna_resource_copy_region(struct pipe_context *pctx, struct pipe_resource *dst,
168 unsigned dst_level, unsigned dstx, unsigned dsty,
169 unsigned dstz, struct pipe_resource *src,
170 unsigned src_level, const struct pipe_box *src_box)
171 {
172 struct etna_context *ctx = etna_context(pctx);
173
174 if (src->target != PIPE_BUFFER && dst->target != PIPE_BUFFER &&
175 util_blitter_is_copy_supported(ctx->blitter, dst, src)) {
176 etna_blit_save_state(ctx, false);
177 util_blitter_copy_texture(ctx->blitter, dst, dst_level, dstx, dsty, dstz,
178 src, src_level, src_box);
179 } else {
180 perf_debug_ctx(ctx, "copy_region falls back to sw");
181 util_resource_copy_region(pctx, dst, dst_level, dstx, dsty, dstz, src,
182 src_level, src_box);
183 }
184 }
185
186 static void
etna_flush_resource(struct pipe_context * pctx,struct pipe_resource * prsc)187 etna_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
188 {
189 struct etna_resource *rsc = etna_resource(prsc);
190
191 if (rsc->render) {
192 if (etna_resource_older(rsc, etna_resource(rsc->render)))
193 etna_copy_resource(pctx, prsc, rsc->render, 0, 0);
194 } else if (!etna_resource_ext_ts(rsc) && etna_resource_needs_flush(rsc)) {
195 etna_copy_resource(pctx, prsc, prsc, 0, 0);
196 }
197 }
198
199 void
etna_copy_resource(struct pipe_context * pctx,struct pipe_resource * dst,struct pipe_resource * src,int first_level,int last_level)200 etna_copy_resource(struct pipe_context *pctx, struct pipe_resource *dst,
201 struct pipe_resource *src, int first_level, int last_level)
202 {
203 struct etna_resource *src_priv = etna_resource(src);
204 struct etna_resource *dst_priv = etna_resource(dst);
205
206 assert(src->format == dst->format);
207 assert(src->array_size == dst->array_size);
208 assert(last_level <= dst->last_level && last_level <= src->last_level);
209
210 struct pipe_blit_info blit = {};
211 blit.mask = util_format_get_mask(dst->format);
212 blit.filter = PIPE_TEX_FILTER_NEAREST;
213 blit.src.resource = src;
214 blit.src.format = src->format;
215 blit.dst.resource = dst;
216 blit.dst.format = dst->format;
217 blit.dst.box.depth = blit.src.box.depth = 1;
218
219 /* Copy each level and each layer */
220 for (int level = first_level; level <= last_level; level++) {
221 /* skip levels that don't need to be flushed or are of the same age */
222 if (src == dst) {
223 if (!etna_resource_level_needs_flush(&src_priv->levels[level]))
224 continue;
225 } else {
226 if (!etna_resource_level_older(&dst_priv->levels[level], &src_priv->levels[level]))
227 continue;
228 }
229
230 blit.src.level = blit.dst.level = level;
231 blit.src.box.width = blit.dst.box.width =
232 MIN2(src_priv->levels[level].width, dst_priv->levels[level].width);
233 blit.src.box.height = blit.dst.box.height =
234 MIN2(src_priv->levels[level].height, dst_priv->levels[level].height);
235 unsigned depth = MIN2(src_priv->levels[level].depth, dst_priv->levels[level].depth);
236 if (dst->array_size > 1) {
237 assert(depth == 1); /* no array of 3d texture */
238 depth = dst->array_size;
239 }
240
241 for (int z = 0; z < depth; z++) {
242 blit.src.box.z = blit.dst.box.z = z;
243 pctx->blit(pctx, &blit);
244 }
245
246 if (src == dst)
247 etna_resource_level_mark_flushed(&dst_priv->levels[level]);
248 else
249 etna_resource_level_copy_seqno(&dst_priv->levels[level], &src_priv->levels[level]);
250 }
251 }
252
253 void
etna_copy_resource_box(struct pipe_context * pctx,struct pipe_resource * dst,struct pipe_resource * src,int dst_level,int src_level,struct pipe_box * box)254 etna_copy_resource_box(struct pipe_context *pctx, struct pipe_resource *dst,
255 struct pipe_resource *src, int dst_level, int src_level,
256 struct pipe_box *box)
257 {
258 struct etna_resource *src_priv = etna_resource(src);
259 struct etna_resource *dst_priv = etna_resource(dst);
260
261 assert(src->format == dst->format);
262 assert(src->array_size == dst->array_size);
263 assert(!etna_resource_level_needs_flush(&dst_priv->levels[dst_level]));
264
265 struct pipe_blit_info blit = {};
266 blit.mask = util_format_get_mask(dst->format);
267 blit.filter = PIPE_TEX_FILTER_NEAREST;
268 blit.src.resource = src;
269 blit.src.format = src->format;
270 blit.src.box = *box;
271 blit.dst.resource = dst;
272 blit.dst.format = dst->format;
273 blit.dst.box = *box;
274
275 blit.dst.box.depth = blit.src.box.depth = 1;
276 blit.src.level = src_level;
277 blit.dst.level = dst_level;
278
279 for (int z = 0; z < box->depth; z++) {
280 blit.src.box.z = blit.dst.box.z = box->z + z;
281 pctx->blit(pctx, &blit);
282 }
283
284 if (src == dst)
285 etna_resource_level_mark_flushed(&dst_priv->levels[dst_level]);
286 else
287 etna_resource_level_copy_seqno(&dst_priv->levels[dst_level],
288 &src_priv->levels[src_level]);
289 }
290
291 void
etna_clear_blit_init(struct pipe_context * pctx)292 etna_clear_blit_init(struct pipe_context *pctx)
293 {
294 struct etna_context *ctx = etna_context(pctx);
295 struct etna_screen *screen = ctx->screen;
296
297 pctx->blit = etna_blit;
298 pctx->clear_render_target = etna_clear_render_target;
299 pctx->clear_depth_stencil = etna_clear_depth_stencil;
300 pctx->resource_copy_region = etna_resource_copy_region;
301 pctx->flush_resource = etna_flush_resource;
302
303 if (screen->specs.use_blt)
304 etna_clear_blit_blt_init(pctx);
305 else
306 etna_clear_blit_rs_init(pctx);
307 }
308