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/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)50 etna_blit_save_state(struct etna_context *ctx)
51 {
52 util_blitter_save_fragment_constant_buffer_slot(ctx->blitter,
53 ctx->constant_buffer[PIPE_SHADER_FRAGMENT].cb);
54 util_blitter_save_vertex_buffer_slot(ctx->blitter, ctx->vertex_buffer.vb);
55 util_blitter_save_vertex_elements(ctx->blitter, ctx->vertex_elements);
56 util_blitter_save_vertex_shader(ctx->blitter, ctx->shader.bind_vs);
57 util_blitter_save_rasterizer(ctx->blitter, ctx->rasterizer);
58 util_blitter_save_viewport(ctx->blitter, &ctx->viewport_s);
59 util_blitter_save_scissor(ctx->blitter, &ctx->scissor);
60 util_blitter_save_fragment_shader(ctx->blitter, ctx->shader.bind_fs);
61 util_blitter_save_blend(ctx->blitter, ctx->blend);
62 util_blitter_save_depth_stencil_alpha(ctx->blitter, ctx->zsa);
63 util_blitter_save_stencil_ref(ctx->blitter, &ctx->stencil_ref_s);
64 util_blitter_save_sample_mask(ctx->blitter, ctx->sample_mask);
65 util_blitter_save_framebuffer(ctx->blitter, &ctx->framebuffer_s);
66 util_blitter_save_fragment_sampler_states(ctx->blitter,
67 ctx->num_fragment_samplers, (void **)ctx->sampler);
68 util_blitter_save_fragment_sampler_views(ctx->blitter,
69 ctx->num_fragment_sampler_views, ctx->sampler_view);
70 }
71
72 uint64_t
etna_clear_blit_pack_rgba(enum pipe_format format,const union pipe_color_union * color)73 etna_clear_blit_pack_rgba(enum pipe_format format, const union pipe_color_union *color)
74 {
75 union util_color uc;
76
77 util_pack_color_union(format, &uc, color);
78
79 switch (util_format_get_blocksize(format)) {
80 case 1:
81 uc.ui[0] = uc.ui[0] << 8 | (uc.ui[0] & 0xff);
82 FALLTHROUGH;
83 case 2:
84 uc.ui[0] = uc.ui[0] << 16 | (uc.ui[0] & 0xffff);
85 FALLTHROUGH;
86 case 4:
87 uc.ui[1] = uc.ui[0];
88 FALLTHROUGH;
89 default:
90 return (uint64_t) uc.ui[1] << 32 | uc.ui[0];
91 }
92 }
93
94 static void
etna_blit(struct pipe_context * pctx,const struct pipe_blit_info * blit_info)95 etna_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
96 {
97 struct etna_context *ctx = etna_context(pctx);
98 struct pipe_blit_info info = *blit_info;
99
100 if (ctx->blit(pctx, &info))
101 return;
102
103 if (util_try_blit_via_copy_region(pctx, &info))
104 return;
105
106 if (info.mask & PIPE_MASK_S) {
107 DBG("cannot blit stencil, skipping");
108 info.mask &= ~PIPE_MASK_S;
109 }
110
111 if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
112 DBG("blit unsupported %s -> %s",
113 util_format_short_name(info.src.resource->format),
114 util_format_short_name(info.dst.resource->format));
115 return;
116 }
117
118 etna_blit_save_state(ctx);
119 util_blitter_blit(ctx->blitter, &info);
120 }
121
122 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)123 etna_clear_render_target(struct pipe_context *pctx, struct pipe_surface *dst,
124 const union pipe_color_union *color, unsigned dstx,
125 unsigned dsty, unsigned width, unsigned height,
126 bool render_condition_enabled)
127 {
128 struct etna_context *ctx = etna_context(pctx);
129
130 /* XXX could fall back to RS when target area is full screen / resolveable
131 * and no TS. */
132 etna_blit_save_state(ctx);
133 util_blitter_clear_render_target(ctx->blitter, dst, color, dstx, dsty, width, height);
134 }
135
136 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)137 etna_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *dst,
138 unsigned clear_flags, double depth, unsigned stencil,
139 unsigned dstx, unsigned dsty, unsigned width,
140 unsigned height, bool render_condition_enabled)
141 {
142 struct etna_context *ctx = etna_context(pctx);
143
144 /* XXX could fall back to RS when target area is full screen / resolveable
145 * and no TS. */
146 etna_blit_save_state(ctx);
147 util_blitter_clear_depth_stencil(ctx->blitter, dst, clear_flags, depth,
148 stencil, dstx, dsty, width, height);
149 }
150
151 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)152 etna_resource_copy_region(struct pipe_context *pctx, struct pipe_resource *dst,
153 unsigned dst_level, unsigned dstx, unsigned dsty,
154 unsigned dstz, struct pipe_resource *src,
155 unsigned src_level, const struct pipe_box *src_box)
156 {
157 struct etna_context *ctx = etna_context(pctx);
158
159 if (src->target != PIPE_BUFFER && dst->target != PIPE_BUFFER &&
160 util_blitter_is_copy_supported(ctx->blitter, dst, src)) {
161 etna_blit_save_state(ctx);
162 util_blitter_copy_texture(ctx->blitter, dst, dst_level, dstx, dsty, dstz,
163 src, src_level, src_box);
164 } else {
165 util_resource_copy_region(pctx, dst, dst_level, dstx, dsty, dstz, src,
166 src_level, src_box);
167 }
168 }
169
170 static void
etna_flush_resource(struct pipe_context * pctx,struct pipe_resource * prsc)171 etna_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
172 {
173 struct etna_resource *rsc = etna_resource(prsc);
174
175 if (rsc->render) {
176 if (etna_resource_older(rsc, etna_resource(rsc->render))) {
177 etna_copy_resource(pctx, prsc, rsc->render, 0, 0);
178 rsc->seqno = etna_resource(rsc->render)->seqno;
179 }
180 } else if (etna_resource_needs_flush(rsc)) {
181 etna_copy_resource(pctx, prsc, prsc, 0, 0);
182 rsc->flush_seqno = rsc->seqno;
183 }
184 }
185
186 void
etna_copy_resource(struct pipe_context * pctx,struct pipe_resource * dst,struct pipe_resource * src,int first_level,int last_level)187 etna_copy_resource(struct pipe_context *pctx, struct pipe_resource *dst,
188 struct pipe_resource *src, int first_level, int last_level)
189 {
190 struct etna_resource *src_priv = etna_resource(src);
191 struct etna_resource *dst_priv = etna_resource(dst);
192
193 assert(src->format == dst->format);
194 assert(src->array_size == dst->array_size);
195 assert(last_level <= dst->last_level && last_level <= src->last_level);
196
197 struct pipe_blit_info blit = {};
198 blit.mask = util_format_get_mask(dst->format);
199 blit.filter = PIPE_TEX_FILTER_NEAREST;
200 blit.src.resource = src;
201 blit.src.format = src->format;
202 blit.dst.resource = dst;
203 blit.dst.format = dst->format;
204 blit.dst.box.depth = blit.src.box.depth = 1;
205
206 /* Copy each level and each layer */
207 for (int level = first_level; level <= last_level; level++) {
208 blit.src.level = blit.dst.level = level;
209 blit.src.box.width = blit.dst.box.width =
210 MIN2(src_priv->levels[level].padded_width, dst_priv->levels[level].padded_width);
211 blit.src.box.height = blit.dst.box.height =
212 MIN2(src_priv->levels[level].padded_height, dst_priv->levels[level].padded_height);
213 unsigned depth = MIN2(src_priv->levels[level].depth, dst_priv->levels[level].depth);
214 if (dst->array_size > 1) {
215 assert(depth == 1); /* no array of 3d texture */
216 depth = dst->array_size;
217 }
218
219 for (int z = 0; z < depth; z++) {
220 blit.src.box.z = blit.dst.box.z = z;
221 pctx->blit(pctx, &blit);
222 }
223 }
224 }
225
226 void
etna_copy_resource_box(struct pipe_context * pctx,struct pipe_resource * dst,struct pipe_resource * src,int level,struct pipe_box * box)227 etna_copy_resource_box(struct pipe_context *pctx, struct pipe_resource *dst,
228 struct pipe_resource *src, int level,
229 struct pipe_box *box)
230 {
231 assert(src->format == dst->format);
232 assert(src->array_size == dst->array_size);
233
234 struct pipe_blit_info blit = {};
235 blit.mask = util_format_get_mask(dst->format);
236 blit.filter = PIPE_TEX_FILTER_NEAREST;
237 blit.src.resource = src;
238 blit.src.format = src->format;
239 blit.src.box = *box;
240 blit.dst.resource = dst;
241 blit.dst.format = dst->format;
242 blit.dst.box = *box;
243
244 blit.dst.box.depth = blit.src.box.depth = 1;
245 blit.src.level = blit.dst.level = level;
246
247 for (int z = 0; z < box->depth; z++) {
248 blit.src.box.z = blit.dst.box.z = box->z + z;
249 pctx->blit(pctx, &blit);
250 }
251 }
252
253 void
etna_clear_blit_init(struct pipe_context * pctx)254 etna_clear_blit_init(struct pipe_context *pctx)
255 {
256 struct etna_context *ctx = etna_context(pctx);
257 struct etna_screen *screen = ctx->screen;
258
259 pctx->blit = etna_blit;
260 pctx->clear_render_target = etna_clear_render_target;
261 pctx->clear_depth_stencil = etna_clear_depth_stencil;
262 pctx->resource_copy_region = etna_resource_copy_region;
263 pctx->flush_resource = etna_flush_resource;
264
265 if (screen->specs.use_blt)
266 etna_clear_blit_blt_init(pctx);
267 else
268 etna_clear_blit_rs_init(pctx);
269 }
270