2 * Copyright (C) 2017 Rob Clark <robclark@freedesktop.org>
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, sublicense,
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 next
12 * paragraph) shall be included in all copies or substantial portions of the
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 NONINFRINGEMENT. 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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Rob Clark <robclark@freedesktop.org>
27 #include "util/u_blitter.h"
28 #include "util/u_surface.h"
30 #include "freedreno_blitter.h"
31 #include "freedreno_context.h"
32 #include "freedreno_resource.h"
33 #include "freedreno_fence.h"
35 /* generic blit using u_blitter.. slightly modified version of util_blitter_blit
36 * which also handles PIPE_BUFFER:
40 default_dst_texture(struct pipe_surface
*dst_templ
, struct pipe_resource
*dst
,
41 unsigned dstlevel
, unsigned dstz
)
43 memset(dst_templ
, 0, sizeof(*dst_templ
));
44 dst_templ
->u
.tex
.level
= dstlevel
;
45 dst_templ
->u
.tex
.first_layer
= dstz
;
46 dst_templ
->u
.tex
.last_layer
= dstz
;
50 default_src_texture(struct pipe_sampler_view
*src_templ
,
51 struct pipe_resource
*src
, unsigned srclevel
)
53 bool cube_as_2darray
= src
->screen
->get_param(src
->screen
,
54 PIPE_CAP_SAMPLER_VIEW_TARGET
);
56 memset(src_templ
, 0, sizeof(*src_templ
));
58 if (cube_as_2darray
&& (src
->target
== PIPE_TEXTURE_CUBE
||
59 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
))
60 src_templ
->target
= PIPE_TEXTURE_2D_ARRAY
;
62 src_templ
->target
= src
->target
;
64 if (src
->target
== PIPE_BUFFER
) {
65 src_templ
->target
= PIPE_TEXTURE_1D
;
67 src_templ
->u
.tex
.first_level
= srclevel
;
68 src_templ
->u
.tex
.last_level
= srclevel
;
69 src_templ
->u
.tex
.first_layer
= 0;
70 src_templ
->u
.tex
.last_layer
=
71 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
72 : (unsigned)(src
->array_size
- 1);
73 src_templ
->swizzle_r
= PIPE_SWIZZLE_X
;
74 src_templ
->swizzle_g
= PIPE_SWIZZLE_Y
;
75 src_templ
->swizzle_b
= PIPE_SWIZZLE_Z
;
76 src_templ
->swizzle_a
= PIPE_SWIZZLE_W
;
80 fd_blitter_pipe_begin(struct fd_context
*ctx
, bool render_cond
, bool discard
,
81 enum fd_render_stage stage
)
83 fd_fence_ref(&ctx
->last_fence
, NULL
);
85 util_blitter_save_fragment_constant_buffer_slot(ctx
->blitter
,
86 ctx
->constbuf
[PIPE_SHADER_FRAGMENT
].cb
);
87 util_blitter_save_vertex_buffer_slot(ctx
->blitter
, ctx
->vtx
.vertexbuf
.vb
);
88 util_blitter_save_vertex_elements(ctx
->blitter
, ctx
->vtx
.vtx
);
89 util_blitter_save_vertex_shader(ctx
->blitter
, ctx
->prog
.vp
);
90 util_blitter_save_so_targets(ctx
->blitter
, ctx
->streamout
.num_targets
,
91 ctx
->streamout
.targets
);
92 util_blitter_save_rasterizer(ctx
->blitter
, ctx
->rasterizer
);
93 util_blitter_save_viewport(ctx
->blitter
, &ctx
->viewport
);
94 util_blitter_save_scissor(ctx
->blitter
, &ctx
->scissor
);
95 util_blitter_save_fragment_shader(ctx
->blitter
, ctx
->prog
.fp
);
96 util_blitter_save_blend(ctx
->blitter
, ctx
->blend
);
97 util_blitter_save_depth_stencil_alpha(ctx
->blitter
, ctx
->zsa
);
98 util_blitter_save_stencil_ref(ctx
->blitter
, &ctx
->stencil_ref
);
99 util_blitter_save_sample_mask(ctx
->blitter
, ctx
->sample_mask
);
100 util_blitter_save_framebuffer(ctx
->blitter
, &ctx
->framebuffer
);
101 util_blitter_save_fragment_sampler_states(ctx
->blitter
,
102 ctx
->tex
[PIPE_SHADER_FRAGMENT
].num_samplers
,
103 (void **)ctx
->tex
[PIPE_SHADER_FRAGMENT
].samplers
);
104 util_blitter_save_fragment_sampler_views(ctx
->blitter
,
105 ctx
->tex
[PIPE_SHADER_FRAGMENT
].num_textures
,
106 ctx
->tex
[PIPE_SHADER_FRAGMENT
].textures
);
108 util_blitter_save_render_condition(ctx
->blitter
,
109 ctx
->cond_query
, ctx
->cond_cond
, ctx
->cond_mode
);
112 fd_batch_set_stage(ctx
->batch
, stage
);
114 ctx
->in_blit
= discard
;
118 fd_blitter_pipe_end(struct fd_context
*ctx
)
121 fd_batch_set_stage(ctx
->batch
, FD_STAGE_NULL
);
122 ctx
->in_blit
= false;
126 fd_blitter_blit(struct fd_context
*ctx
, const struct pipe_blit_info
*info
)
128 struct pipe_resource
*dst
= info
->dst
.resource
;
129 struct pipe_resource
*src
= info
->src
.resource
;
130 struct pipe_context
*pipe
= &ctx
->base
;
131 struct pipe_surface
*dst_view
, dst_templ
;
132 struct pipe_sampler_view src_templ
, *src_view
;
133 bool discard
= false;
135 if (!info
->scissor_enable
&& !info
->alpha_blend
) {
136 discard
= util_texrange_covers_whole_level(info
->dst
.resource
,
137 info
->dst
.level
, info
->dst
.box
.x
, info
->dst
.box
.y
,
138 info
->dst
.box
.z
, info
->dst
.box
.width
,
139 info
->dst
.box
.height
, info
->dst
.box
.depth
);
142 fd_blitter_pipe_begin(ctx
, info
->render_condition_enable
, discard
, FD_STAGE_BLIT
);
144 /* Initialize the surface. */
145 default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
147 dst_templ
.format
= info
->dst
.format
;
148 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
150 /* Initialize the sampler view. */
151 default_src_texture(&src_templ
, src
, info
->src
.level
);
152 src_templ
.format
= info
->src
.format
;
153 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
156 util_blitter_blit_generic(ctx
->blitter
, dst_view
, &info
->dst
.box
,
157 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
158 info
->mask
, info
->filter
,
159 info
->scissor_enable
? &info
->scissor
: NULL
,
162 pipe_surface_reference(&dst_view
, NULL
);
163 pipe_sampler_view_reference(&src_view
, NULL
);
165 fd_blitter_pipe_end(ctx
);
167 /* The fallback blitter must never fail: */
171 /* Generic clear implementation (partially) using u_blitter: */
173 fd_blitter_clear(struct pipe_context
*pctx
, unsigned buffers
,
174 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
176 struct fd_context
*ctx
= fd_context(pctx
);
177 struct pipe_framebuffer_state
*pfb
= &ctx
->batch
->framebuffer
;
178 struct blitter_context
*blitter
= ctx
->blitter
;
180 fd_blitter_pipe_begin(ctx
, false, true, FD_STAGE_CLEAR
);
182 util_blitter_common_clear_setup(blitter
, pfb
->width
, pfb
->height
,
183 buffers
, NULL
, NULL
);
185 struct pipe_stencil_ref sr
= {
186 .ref_value
= { stencil
& 0xff }
188 pctx
->set_stencil_ref(pctx
, &sr
);
190 struct pipe_constant_buffer cb
= {
192 .user_buffer
= &color
->ui
,
194 pctx
->set_constant_buffer(pctx
, PIPE_SHADER_FRAGMENT
, 0, &cb
);
196 if (!ctx
->clear_rs_state
) {
197 const struct pipe_rasterizer_state tmpl
= {
198 .cull_face
= PIPE_FACE_NONE
,
199 .half_pixel_center
= 1,
200 .bottom_edge_rule
= 1,
202 .depth_clip_near
= 1,
205 ctx
->clear_rs_state
= pctx
->create_rasterizer_state(pctx
, &tmpl
);
207 pctx
->bind_rasterizer_state(pctx
, ctx
->clear_rs_state
);
209 struct pipe_viewport_state vp
= {
210 .scale
= { 0.5f
* pfb
->width
, -0.5f
* pfb
->height
, depth
},
211 .translate
= { 0.5f
* pfb
->width
, 0.5f
* pfb
->height
, 0.0f
},
213 pctx
->set_viewport_states(pctx
, 0, 1, &vp
);
215 pctx
->bind_vertex_elements_state(pctx
, ctx
->solid_vbuf_state
.vtx
);
216 pctx
->set_vertex_buffers(pctx
, blitter
->vb_slot
, 1,
217 &ctx
->solid_vbuf_state
.vertexbuf
.vb
[0]);
218 pctx
->set_stream_output_targets(pctx
, 0, NULL
, NULL
);
219 pctx
->bind_vs_state(pctx
, ctx
->solid_prog
.vp
);
220 pctx
->bind_fs_state(pctx
, ctx
->solid_prog
.fp
);
222 struct pipe_draw_info info
= {
223 .mode
= PIPE_PRIM_MAX
, /* maps to DI_PT_RECTLIST */
228 ctx
->draw_vbo(ctx
, &info
, 0);
230 util_blitter_restore_constant_buffer_state(blitter
);
231 util_blitter_restore_vertex_states(blitter
);
232 util_blitter_restore_fragment_states(blitter
);
233 util_blitter_restore_textures(blitter
);
234 util_blitter_restore_fb_state(blitter
);
235 util_blitter_restore_render_cond(blitter
);
236 util_blitter_unset_running_flag(blitter
);
238 fd_blitter_pipe_end(ctx
);
242 * Optimal hardware path for blitting pixels.
243 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
246 fd_blit(struct pipe_context
*pctx
, const struct pipe_blit_info
*blit_info
)
248 struct fd_context
*ctx
= fd_context(pctx
);
249 struct pipe_blit_info info
= *blit_info
;
251 if (info
.render_condition_enable
&& !fd_render_condition_check(pctx
))
254 if (ctx
->blit
&& ctx
->blit(ctx
, &info
))
257 if (info
.mask
& PIPE_MASK_S
) {
258 DBG("cannot blit stencil, skipping");
259 info
.mask
&= ~PIPE_MASK_S
;
262 if (!util_blitter_is_blit_supported(ctx
->blitter
, &info
)) {
263 DBG("blit unsupported %s -> %s",
264 util_format_short_name(info
.src
.resource
->format
),
265 util_format_short_name(info
.dst
.resource
->format
));
269 return fd_blitter_blit(ctx
, &info
);
273 * _copy_region using pipe (3d engine)
276 fd_blitter_pipe_copy_region(struct fd_context
*ctx
,
277 struct pipe_resource
*dst
,
279 unsigned dstx
, unsigned dsty
, unsigned dstz
,
280 struct pipe_resource
*src
,
282 const struct pipe_box
*src_box
)
284 /* not until we allow rendertargets to be buffers */
285 if (dst
->target
== PIPE_BUFFER
|| src
->target
== PIPE_BUFFER
)
288 if (!util_blitter_is_copy_supported(ctx
->blitter
, dst
, src
))
291 /* TODO we could discard if dst box covers dst level fully.. */
292 fd_blitter_pipe_begin(ctx
, false, false, FD_STAGE_BLIT
);
293 util_blitter_copy_texture(ctx
->blitter
,
294 dst
, dst_level
, dstx
, dsty
, dstz
,
295 src
, src_level
, src_box
);
296 fd_blitter_pipe_end(ctx
);
302 * Copy a block of pixels from one resource to another.
303 * The resource must be of the same format.
306 fd_resource_copy_region(struct pipe_context
*pctx
,
307 struct pipe_resource
*dst
,
309 unsigned dstx
, unsigned dsty
, unsigned dstz
,
310 struct pipe_resource
*src
,
312 const struct pipe_box
*src_box
)
314 struct fd_context
*ctx
= fd_context(pctx
);
317 struct pipe_blit_info info
;
319 memset(&info
, 0, sizeof info
);
320 info
.dst
.resource
= dst
;
321 info
.dst
.level
= dst_level
;
322 info
.dst
.box
.x
= dstx
;
323 info
.dst
.box
.y
= dsty
;
324 info
.dst
.box
.z
= dstz
;
325 info
.dst
.box
.width
= src_box
->width
;
326 info
.dst
.box
.height
= src_box
->height
;
327 assert(info
.dst
.box
.width
>= 0);
328 assert(info
.dst
.box
.height
>= 0);
329 info
.dst
.box
.depth
= 1;
330 info
.dst
.format
= dst
->format
;
331 info
.src
.resource
= src
;
332 info
.src
.level
= src_level
;
333 info
.src
.box
= *src_box
;
334 info
.src
.format
= src
->format
;
335 info
.mask
= util_format_get_mask(src
->format
);
336 info
.filter
= PIPE_TEX_FILTER_NEAREST
;
337 info
.scissor_enable
= 0;
339 if (ctx
->blit(ctx
, &info
))
343 /* TODO if we have 2d core, or other DMA engine that could be used
344 * for simple copies and reasonably easily synchronized with the 3d
345 * core, this is where we'd plug it in..
348 /* try blit on 3d pipe: */
349 if (fd_blitter_pipe_copy_region(ctx
,
350 dst
, dst_level
, dstx
, dsty
, dstz
,
351 src
, src_level
, src_box
))
354 /* else fallback to pure sw: */
355 util_resource_copy_region(pctx
,
356 dst
, dst_level
, dstx
, dsty
, dstz
,
357 src
, src_level
, src_box
);