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