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
.vs
);
90 util_blitter_save_tessctrl_shader(ctx
->blitter
, ctx
->prog
.hs
);
91 util_blitter_save_tesseval_shader(ctx
->blitter
, ctx
->prog
.ds
);
92 util_blitter_save_geometry_shader(ctx
->blitter
, ctx
->prog
.gs
);
93 util_blitter_save_so_targets(ctx
->blitter
, ctx
->streamout
.num_targets
,
94 ctx
->streamout
.targets
);
95 util_blitter_save_rasterizer(ctx
->blitter
, ctx
->rasterizer
);
96 util_blitter_save_viewport(ctx
->blitter
, &ctx
->viewport
);
97 util_blitter_save_scissor(ctx
->blitter
, &ctx
->scissor
);
98 util_blitter_save_fragment_shader(ctx
->blitter
, ctx
->prog
.fs
);
99 util_blitter_save_blend(ctx
->blitter
, ctx
->blend
);
100 util_blitter_save_depth_stencil_alpha(ctx
->blitter
, ctx
->zsa
);
101 util_blitter_save_stencil_ref(ctx
->blitter
, &ctx
->stencil_ref
);
102 util_blitter_save_sample_mask(ctx
->blitter
, ctx
->sample_mask
);
103 util_blitter_save_framebuffer(ctx
->blitter
, &ctx
->framebuffer
);
104 util_blitter_save_fragment_sampler_states(ctx
->blitter
,
105 ctx
->tex
[PIPE_SHADER_FRAGMENT
].num_samplers
,
106 (void **)ctx
->tex
[PIPE_SHADER_FRAGMENT
].samplers
);
107 util_blitter_save_fragment_sampler_views(ctx
->blitter
,
108 ctx
->tex
[PIPE_SHADER_FRAGMENT
].num_textures
,
109 ctx
->tex
[PIPE_SHADER_FRAGMENT
].textures
);
111 util_blitter_save_render_condition(ctx
->blitter
,
112 ctx
->cond_query
, ctx
->cond_cond
, ctx
->cond_mode
);
115 fd_batch_set_stage(ctx
->batch
, stage
);
117 ctx
->in_blit
= discard
;
121 fd_blitter_pipe_end(struct fd_context
*ctx
)
124 fd_batch_set_stage(ctx
->batch
, FD_STAGE_NULL
);
125 ctx
->in_blit
= false;
129 fd_blitter_blit(struct fd_context
*ctx
, const struct pipe_blit_info
*info
)
131 struct pipe_resource
*dst
= info
->dst
.resource
;
132 struct pipe_resource
*src
= info
->src
.resource
;
133 struct pipe_context
*pipe
= &ctx
->base
;
134 struct pipe_surface
*dst_view
, dst_templ
;
135 struct pipe_sampler_view src_templ
, *src_view
;
136 bool discard
= false;
138 if (!info
->scissor_enable
&& !info
->alpha_blend
) {
139 discard
= util_texrange_covers_whole_level(info
->dst
.resource
,
140 info
->dst
.level
, info
->dst
.box
.x
, info
->dst
.box
.y
,
141 info
->dst
.box
.z
, info
->dst
.box
.width
,
142 info
->dst
.box
.height
, info
->dst
.box
.depth
);
145 fd_blitter_pipe_begin(ctx
, info
->render_condition_enable
, discard
, FD_STAGE_BLIT
);
147 /* Initialize the surface. */
148 default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
150 dst_templ
.format
= info
->dst
.format
;
151 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
153 /* Initialize the sampler view. */
154 default_src_texture(&src_templ
, src
, info
->src
.level
);
155 src_templ
.format
= info
->src
.format
;
156 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
159 util_blitter_blit_generic(ctx
->blitter
, dst_view
, &info
->dst
.box
,
160 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
161 info
->mask
, info
->filter
,
162 info
->scissor_enable
? &info
->scissor
: NULL
,
165 pipe_surface_reference(&dst_view
, NULL
);
166 pipe_sampler_view_reference(&src_view
, NULL
);
168 fd_blitter_pipe_end(ctx
);
170 /* The fallback blitter must never fail: */
174 /* Generic clear implementation (partially) using u_blitter: */
176 fd_blitter_clear(struct pipe_context
*pctx
, unsigned buffers
,
177 const union pipe_color_union
*color
, double depth
, unsigned stencil
)
179 struct fd_context
*ctx
= fd_context(pctx
);
180 struct pipe_framebuffer_state
*pfb
= &ctx
->batch
->framebuffer
;
181 struct blitter_context
*blitter
= ctx
->blitter
;
183 fd_blitter_pipe_begin(ctx
, false, true, FD_STAGE_CLEAR
);
185 util_blitter_common_clear_setup(blitter
, pfb
->width
, pfb
->height
,
186 buffers
, NULL
, NULL
);
188 struct pipe_stencil_ref sr
= {
189 .ref_value
= { stencil
& 0xff }
191 pctx
->set_stencil_ref(pctx
, &sr
);
193 struct pipe_constant_buffer cb
= {
195 .user_buffer
= &color
->ui
,
197 pctx
->set_constant_buffer(pctx
, PIPE_SHADER_FRAGMENT
, 0, &cb
);
199 if (!ctx
->clear_rs_state
) {
200 const struct pipe_rasterizer_state tmpl
= {
201 .cull_face
= PIPE_FACE_NONE
,
202 .half_pixel_center
= 1,
203 .bottom_edge_rule
= 1,
205 .depth_clip_near
= 1,
208 ctx
->clear_rs_state
= pctx
->create_rasterizer_state(pctx
, &tmpl
);
210 pctx
->bind_rasterizer_state(pctx
, ctx
->clear_rs_state
);
212 struct pipe_viewport_state vp
= {
213 .scale
= { 0.5f
* pfb
->width
, -0.5f
* pfb
->height
, depth
},
214 .translate
= { 0.5f
* pfb
->width
, 0.5f
* pfb
->height
, 0.0f
},
216 pctx
->set_viewport_states(pctx
, 0, 1, &vp
);
218 pctx
->bind_vertex_elements_state(pctx
, ctx
->solid_vbuf_state
.vtx
);
219 pctx
->set_vertex_buffers(pctx
, blitter
->vb_slot
, 1,
220 &ctx
->solid_vbuf_state
.vertexbuf
.vb
[0]);
221 pctx
->set_stream_output_targets(pctx
, 0, NULL
, NULL
);
222 pctx
->bind_vs_state(pctx
, ctx
->solid_prog
.vs
);
223 pctx
->bind_fs_state(pctx
, ctx
->solid_prog
.fs
);
225 struct pipe_draw_info info
= {
226 .mode
= PIPE_PRIM_MAX
, /* maps to DI_PT_RECTLIST */
231 ctx
->draw_vbo(ctx
, &info
, 0);
233 util_blitter_restore_constant_buffer_state(blitter
);
234 util_blitter_restore_vertex_states(blitter
);
235 util_blitter_restore_fragment_states(blitter
);
236 util_blitter_restore_textures(blitter
);
237 util_blitter_restore_fb_state(blitter
);
238 util_blitter_restore_render_cond(blitter
);
239 util_blitter_unset_running_flag(blitter
);
241 fd_blitter_pipe_end(ctx
);
245 * Optimal hardware path for blitting pixels.
246 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
249 fd_blit(struct pipe_context
*pctx
, const struct pipe_blit_info
*blit_info
)
251 struct fd_context
*ctx
= fd_context(pctx
);
252 struct pipe_blit_info info
= *blit_info
;
254 if (info
.render_condition_enable
&& !fd_render_condition_check(pctx
))
257 if (ctx
->blit
&& ctx
->blit(ctx
, &info
))
260 if (info
.mask
& PIPE_MASK_S
) {
261 DBG("cannot blit stencil, skipping");
262 info
.mask
&= ~PIPE_MASK_S
;
265 if (!util_blitter_is_blit_supported(ctx
->blitter
, &info
)) {
266 DBG("blit unsupported %s -> %s",
267 util_format_short_name(info
.src
.resource
->format
),
268 util_format_short_name(info
.dst
.resource
->format
));
272 return fd_blitter_blit(ctx
, &info
);
276 * _copy_region using pipe (3d engine)
279 fd_blitter_pipe_copy_region(struct fd_context
*ctx
,
280 struct pipe_resource
*dst
,
282 unsigned dstx
, unsigned dsty
, unsigned dstz
,
283 struct pipe_resource
*src
,
285 const struct pipe_box
*src_box
)
287 /* not until we allow rendertargets to be buffers */
288 if (dst
->target
== PIPE_BUFFER
|| src
->target
== PIPE_BUFFER
)
291 if (!util_blitter_is_copy_supported(ctx
->blitter
, dst
, src
))
294 /* TODO we could discard if dst box covers dst level fully.. */
295 fd_blitter_pipe_begin(ctx
, false, false, FD_STAGE_BLIT
);
296 util_blitter_copy_texture(ctx
->blitter
,
297 dst
, dst_level
, dstx
, dsty
, dstz
,
298 src
, src_level
, src_box
);
299 fd_blitter_pipe_end(ctx
);
305 * Copy a block of pixels from one resource to another.
306 * The resource must be of the same format.
309 fd_resource_copy_region(struct pipe_context
*pctx
,
310 struct pipe_resource
*dst
,
312 unsigned dstx
, unsigned dsty
, unsigned dstz
,
313 struct pipe_resource
*src
,
315 const struct pipe_box
*src_box
)
317 struct fd_context
*ctx
= fd_context(pctx
);
320 struct pipe_blit_info info
;
322 memset(&info
, 0, sizeof info
);
323 info
.dst
.resource
= dst
;
324 info
.dst
.level
= dst_level
;
325 info
.dst
.box
.x
= dstx
;
326 info
.dst
.box
.y
= dsty
;
327 info
.dst
.box
.z
= dstz
;
328 info
.dst
.box
.width
= src_box
->width
;
329 info
.dst
.box
.height
= src_box
->height
;
330 assert(info
.dst
.box
.width
>= 0);
331 assert(info
.dst
.box
.height
>= 0);
332 info
.dst
.box
.depth
= 1;
333 info
.dst
.format
= dst
->format
;
334 info
.src
.resource
= src
;
335 info
.src
.level
= src_level
;
336 info
.src
.box
= *src_box
;
337 info
.src
.format
= src
->format
;
338 info
.mask
= util_format_get_mask(src
->format
);
339 info
.filter
= PIPE_TEX_FILTER_NEAREST
;
340 info
.scissor_enable
= 0;
342 if (ctx
->blit(ctx
, &info
))
346 /* TODO if we have 2d core, or other DMA engine that could be used
347 * for simple copies and reasonably easily synchronized with the 3d
348 * core, this is where we'd plug it in..
351 /* try blit on 3d pipe: */
352 if (fd_blitter_pipe_copy_region(ctx
,
353 dst
, dst_level
, dstx
, dsty
, dstz
,
354 src
, src_level
, src_box
))
357 /* else fallback to pure sw: */
358 util_resource_copy_region(pctx
,
359 dst
, dst_level
, dstx
, dsty
, dstz
,
360 src
, src_level
, src_box
);