2 * Copyright (c) 2012-2015 Etnaviv Project
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:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
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.
24 * Wladimir J. van der Laan <laanwj@gmail.com>
27 #include "etnaviv_clear_blit.h"
29 #include "hw/common.xml.h"
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"
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"
47 /* Save current state for blitter operation */
49 etna_blit_save_state(struct etna_context
*ctx
)
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
);
72 etna_clear_blit_pack_rgba(enum pipe_format format
, const union pipe_color_union
*color
)
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);
81 util_pack_color(color
->f
, format
, &uc
);
84 switch (util_format_get_blocksize(format
)) {
86 uc
.ui
[0] = uc
.ui
[0] << 8 | (uc
.ui
[0] & 0xff);
88 uc
.ui
[0] = uc
.ui
[0] << 16 | (uc
.ui
[0] & 0xffff);
92 return (uint64_t) uc
.ui
[1] << 32 | uc
.ui
[0];
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
)
102 struct etna_context
*ctx
= etna_context(pctx
);
104 /* XXX could fall back to RS when target area is full screen / resolveable
106 etna_blit_save_state(ctx
);
107 util_blitter_clear_render_target(ctx
->blitter
, dst
, color
, dstx
, dsty
, width
, height
);
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
)
116 struct etna_context
*ctx
= etna_context(pctx
);
118 /* XXX could fall back to RS when target area is full screen / resolveable
120 etna_blit_save_state(ctx
);
121 util_blitter_clear_depth_stencil(ctx
->blitter
, dst
, clear_flags
, depth
,
122 stencil
, dstx
, dsty
, width
, height
);
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
)
131 struct etna_context
*ctx
= etna_context(pctx
);
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
136 * how to handle the case where a resource is copied from/to a non-aligned
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.
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
);
147 util_resource_copy_region(pctx
, dst
, dst_level
, dstx
, dsty
, dstz
, src
,
153 etna_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
155 struct etna_resource
*rsc
= etna_resource(prsc
);
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
;
162 } else if (etna_resource_needs_flush(rsc
)) {
163 etna_copy_resource(pctx
, prsc
, prsc
, 0, 0);
164 rsc
->flush_seqno
= rsc
->seqno
;
169 etna_copy_resource(struct pipe_context
*pctx
, struct pipe_resource
*dst
,
170 struct pipe_resource
*src
, int first_level
, int last_level
)
172 struct etna_resource
*src_priv
= etna_resource(src
);
173 struct etna_resource
*dst_priv
= etna_resource(dst
);
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
);
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;
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
;
201 for (int z
= 0; z
< depth
; z
++) {
202 blit
.src
.box
.z
= blit
.dst
.box
.z
= z
;
203 pctx
->blit(pctx
, &blit
);
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
)
213 assert(src
->format
== dst
->format
);
214 assert(src
->array_size
== dst
->array_size
);
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
;
222 blit
.dst
.resource
= dst
;
223 blit
.dst
.format
= dst
->format
;
226 blit
.dst
.box
.depth
= blit
.src
.box
.depth
= 1;
227 blit
.src
.level
= blit
.dst
.level
= level
;
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
);
236 etna_clear_blit_init(struct pipe_context
*pctx
)
238 struct etna_context
*ctx
= etna_context(pctx
);
239 struct etna_screen
*screen
= ctx
->screen
;
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
;
246 if (screen
->specs
.use_blt
)
247 etna_clear_blit_blt_init(pctx
);
249 etna_clear_blit_rs_init(pctx
);