etnaviv: rework the stream flush to always go through the context flush
[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_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
69 uint32_t
70 etna_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
80 static void
81 etna_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
94 static void
95 etna_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
109 static void
110 etna_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 /* XXX we can use the RS as a literal copy engine here
118 * the only complexity is tiling; the size of the boxes needs to be aligned
119 * to the tile size
120 * how to handle the case where a resource is copied from/to a non-aligned
121 * position?
122 * from non-aligned: can fall back to rendering-based copy?
123 * to non-aligned: can fall back to rendering-based copy?
124 * XXX this goes wrong when source surface is supertiled.
125 */
126 if (util_blitter_is_copy_supported(ctx->blitter, dst, src)) {
127 etna_blit_save_state(ctx);
128 util_blitter_copy_texture(ctx->blitter, dst, dst_level, dstx, dsty, dstz,
129 src, src_level, src_box);
130 } else {
131 util_resource_copy_region(pctx, dst, dst_level, dstx, dsty, dstz, src,
132 src_level, src_box);
133 }
134 }
135
136 static void
137 etna_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
138 {
139 struct etna_resource *rsc = etna_resource(prsc);
140
141 if (rsc->render) {
142 if (etna_resource_older(rsc, etna_resource(rsc->render))) {
143 etna_copy_resource(pctx, prsc, rsc->render, 0, 0);
144 rsc->seqno = etna_resource(rsc->render)->seqno;
145 }
146 } else if (etna_resource_needs_flush(rsc)) {
147 etna_copy_resource(pctx, prsc, prsc, 0, 0);
148 rsc->flush_seqno = rsc->seqno;
149 }
150 }
151
152 void
153 etna_copy_resource(struct pipe_context *pctx, struct pipe_resource *dst,
154 struct pipe_resource *src, int first_level, int last_level)
155 {
156 struct etna_resource *src_priv = etna_resource(src);
157 struct etna_resource *dst_priv = etna_resource(dst);
158
159 assert(src->format == dst->format);
160 assert(src->array_size == dst->array_size);
161 assert(last_level <= dst->last_level && last_level <= src->last_level);
162
163 struct pipe_blit_info blit = {};
164 blit.mask = util_format_get_mask(dst->format);
165 blit.filter = PIPE_TEX_FILTER_NEAREST;
166 blit.src.resource = src;
167 blit.src.format = src->format;
168 blit.dst.resource = dst;
169 blit.dst.format = dst->format;
170 blit.dst.box.depth = blit.src.box.depth = 1;
171
172 /* Copy each level and each layer */
173 for (int level = first_level; level <= last_level; level++) {
174 blit.src.level = blit.dst.level = level;
175 blit.src.box.width = blit.dst.box.width =
176 MIN2(src_priv->levels[level].padded_width, dst_priv->levels[level].padded_width);
177 blit.src.box.height = blit.dst.box.height =
178 MIN2(src_priv->levels[level].padded_height, dst_priv->levels[level].padded_height);
179 unsigned depth = MIN2(src_priv->levels[level].depth, dst_priv->levels[level].depth);
180 if (dst->array_size > 1) {
181 assert(depth == 1); /* no array of 3d texture */
182 depth = dst->array_size;
183 }
184
185 for (int z = 0; z < depth; z++) {
186 blit.src.box.z = blit.dst.box.z = z;
187 pctx->blit(pctx, &blit);
188 }
189 }
190 }
191
192 void
193 etna_copy_resource_box(struct pipe_context *pctx, struct pipe_resource *dst,
194 struct pipe_resource *src, int level,
195 struct pipe_box *box)
196 {
197 assert(src->format == dst->format);
198 assert(src->array_size == dst->array_size);
199
200 struct pipe_blit_info blit = {};
201 blit.mask = util_format_get_mask(dst->format);
202 blit.filter = PIPE_TEX_FILTER_NEAREST;
203 blit.src.resource = src;
204 blit.src.format = src->format;
205 blit.src.box = *box;
206 blit.dst.resource = dst;
207 blit.dst.format = dst->format;
208 blit.dst.box = *box;
209
210 blit.dst.box.depth = blit.src.box.depth = 1;
211 blit.src.level = blit.dst.level = level;
212
213 for (int z = 0; z < box->depth; z++) {
214 blit.src.box.z = blit.dst.box.z = box->z + z;
215 pctx->blit(pctx, &blit);
216 }
217 }
218
219 void
220 etna_clear_blit_init(struct pipe_context *pctx)
221 {
222 struct etna_context *ctx = etna_context(pctx);
223
224 pctx->clear_render_target = etna_clear_render_target;
225 pctx->clear_depth_stencil = etna_clear_depth_stencil;
226 pctx->resource_copy_region = etna_resource_copy_region;
227 pctx->flush_resource = etna_flush_resource;
228
229 if (ctx->specs.use_blt)
230 etna_clear_blit_blt_init(pctx);
231 else
232 etna_clear_blit_rs_init(pctx);
233 }