etnaviv: call util_blitter_save_fragment_constant_buffer_slot(..)
[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 if (util_format_is_pure_uint(format)) {
77 util_format_write_4ui(format, color->ui, 0, &uc, 0, 0, 0, 1, 1);
78 } else if (util_format_is_pure_sint(format)) {
79 util_format_write_4i(format, color->i, 0, &uc, 0, 0, 0, 1, 1);
80 } else {
81 util_pack_color(color->f, format, &uc);
82 }
83
84 switch (util_format_get_blocksize(format)) {
85 case 1:
86 uc.ui[0] = uc.ui[0] << 8 | (uc.ui[0] & 0xff);
87 case 2:
88 uc.ui[0] = uc.ui[0] << 16 | (uc.ui[0] & 0xffff);
89 case 4:
90 uc.ui[1] = uc.ui[0];
91 default:
92 return (uint64_t) uc.ui[1] << 32 | uc.ui[0];
93 }
94 }
95
96 static void
97 etna_clear_render_target(struct pipe_context *pctx, struct pipe_surface *dst,
98 const union pipe_color_union *color, unsigned dstx,
99 unsigned dsty, unsigned width, unsigned height,
100 bool render_condition_enabled)
101 {
102 struct etna_context *ctx = etna_context(pctx);
103
104 /* XXX could fall back to RS when target area is full screen / resolveable
105 * and no TS. */
106 etna_blit_save_state(ctx);
107 util_blitter_clear_render_target(ctx->blitter, dst, color, dstx, dsty, width, height);
108 }
109
110 static void
111 etna_clear_depth_stencil(struct pipe_context *pctx, struct pipe_surface *dst,
112 unsigned clear_flags, double depth, unsigned stencil,
113 unsigned dstx, unsigned dsty, unsigned width,
114 unsigned height, bool render_condition_enabled)
115 {
116 struct etna_context *ctx = etna_context(pctx);
117
118 /* XXX could fall back to RS when target area is full screen / resolveable
119 * and no TS. */
120 etna_blit_save_state(ctx);
121 util_blitter_clear_depth_stencil(ctx->blitter, dst, clear_flags, depth,
122 stencil, dstx, dsty, width, height);
123 }
124
125 static void
126 etna_resource_copy_region(struct pipe_context *pctx, struct pipe_resource *dst,
127 unsigned dst_level, unsigned dstx, unsigned dsty,
128 unsigned dstz, struct pipe_resource *src,
129 unsigned src_level, const struct pipe_box *src_box)
130 {
131 struct etna_context *ctx = etna_context(pctx);
132
133 /* XXX we can use the RS as a literal copy engine here
134 * the only complexity is tiling; the size of the boxes needs to be aligned
135 * to the tile size
136 * how to handle the case where a resource is copied from/to a non-aligned
137 * position?
138 * from non-aligned: can fall back to rendering-based copy?
139 * to non-aligned: can fall back to rendering-based copy?
140 * XXX this goes wrong when source surface is supertiled.
141 */
142 if (util_blitter_is_copy_supported(ctx->blitter, dst, src)) {
143 etna_blit_save_state(ctx);
144 util_blitter_copy_texture(ctx->blitter, dst, dst_level, dstx, dsty, dstz,
145 src, src_level, src_box);
146 } else {
147 util_resource_copy_region(pctx, dst, dst_level, dstx, dsty, dstz, src,
148 src_level, src_box);
149 }
150 }
151
152 static void
153 etna_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
154 {
155 struct etna_resource *rsc = etna_resource(prsc);
156
157 if (rsc->render) {
158 if (etna_resource_older(rsc, etna_resource(rsc->render))) {
159 etna_copy_resource(pctx, prsc, rsc->render, 0, 0);
160 rsc->seqno = etna_resource(rsc->render)->seqno;
161 }
162 } else if (etna_resource_needs_flush(rsc)) {
163 etna_copy_resource(pctx, prsc, prsc, 0, 0);
164 rsc->flush_seqno = rsc->seqno;
165 }
166 }
167
168 void
169 etna_copy_resource(struct pipe_context *pctx, struct pipe_resource *dst,
170 struct pipe_resource *src, int first_level, int last_level)
171 {
172 struct etna_resource *src_priv = etna_resource(src);
173 struct etna_resource *dst_priv = etna_resource(dst);
174
175 assert(src->format == dst->format);
176 assert(src->array_size == dst->array_size);
177 assert(last_level <= dst->last_level && last_level <= src->last_level);
178
179 struct pipe_blit_info blit = {};
180 blit.mask = util_format_get_mask(dst->format);
181 blit.filter = PIPE_TEX_FILTER_NEAREST;
182 blit.src.resource = src;
183 blit.src.format = src->format;
184 blit.dst.resource = dst;
185 blit.dst.format = dst->format;
186 blit.dst.box.depth = blit.src.box.depth = 1;
187
188 /* Copy each level and each layer */
189 for (int level = first_level; level <= last_level; level++) {
190 blit.src.level = blit.dst.level = level;
191 blit.src.box.width = blit.dst.box.width =
192 MIN2(src_priv->levels[level].padded_width, dst_priv->levels[level].padded_width);
193 blit.src.box.height = blit.dst.box.height =
194 MIN2(src_priv->levels[level].padded_height, dst_priv->levels[level].padded_height);
195 unsigned depth = MIN2(src_priv->levels[level].depth, dst_priv->levels[level].depth);
196 if (dst->array_size > 1) {
197 assert(depth == 1); /* no array of 3d texture */
198 depth = dst->array_size;
199 }
200
201 for (int z = 0; z < depth; z++) {
202 blit.src.box.z = blit.dst.box.z = z;
203 pctx->blit(pctx, &blit);
204 }
205 }
206 }
207
208 void
209 etna_copy_resource_box(struct pipe_context *pctx, struct pipe_resource *dst,
210 struct pipe_resource *src, int level,
211 struct pipe_box *box)
212 {
213 assert(src->format == dst->format);
214 assert(src->array_size == dst->array_size);
215
216 struct pipe_blit_info blit = {};
217 blit.mask = util_format_get_mask(dst->format);
218 blit.filter = PIPE_TEX_FILTER_NEAREST;
219 blit.src.resource = src;
220 blit.src.format = src->format;
221 blit.src.box = *box;
222 blit.dst.resource = dst;
223 blit.dst.format = dst->format;
224 blit.dst.box = *box;
225
226 blit.dst.box.depth = blit.src.box.depth = 1;
227 blit.src.level = blit.dst.level = level;
228
229 for (int z = 0; z < box->depth; z++) {
230 blit.src.box.z = blit.dst.box.z = box->z + z;
231 pctx->blit(pctx, &blit);
232 }
233 }
234
235 void
236 etna_clear_blit_init(struct pipe_context *pctx)
237 {
238 struct etna_context *ctx = etna_context(pctx);
239 struct etna_screen *screen = ctx->screen;
240
241 pctx->clear_render_target = etna_clear_render_target;
242 pctx->clear_depth_stencil = etna_clear_depth_stencil;
243 pctx->resource_copy_region = etna_resource_copy_region;
244 pctx->flush_resource = etna_flush_resource;
245
246 if (screen->specs.use_blt)
247 etna_clear_blit_blt_init(pctx);
248 else
249 etna_clear_blit_rs_init(pctx);
250 }