1 /*
2 * Copyright (C) 2017 Rob Clark <robclark@freedesktop.org>
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, 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:
10 *
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
13 * 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 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
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "util/u_blitter.h"
28 #include "util/u_surface.h"
29
30 #include "freedreno_blitter.h"
31 #include "freedreno_context.h"
32 #include "freedreno_resource.h"
33 #include "freedreno_fence.h"
34
35 /* generic blit using u_blitter.. slightly modified version of util_blitter_blit
36 * which also handles PIPE_BUFFER:
37 */
38
39 static void
default_dst_texture(struct pipe_surface * dst_templ,struct pipe_resource * dst,unsigned dstlevel,unsigned dstz)40 default_dst_texture(struct pipe_surface *dst_templ, struct pipe_resource *dst,
41 unsigned dstlevel, unsigned dstz)
42 {
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;
47 }
48
49 static void
default_src_texture(struct pipe_sampler_view * src_templ,struct pipe_resource * src,unsigned srclevel)50 default_src_texture(struct pipe_sampler_view *src_templ,
51 struct pipe_resource *src, unsigned srclevel)
52 {
53 bool cube_as_2darray = src->screen->get_param(src->screen,
54 PIPE_CAP_SAMPLER_VIEW_TARGET);
55
56 memset(src_templ, 0, sizeof(*src_templ));
57
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;
61 else
62 src_templ->target = src->target;
63
64 if (src->target == PIPE_BUFFER) {
65 src_templ->target = PIPE_TEXTURE_1D;
66 }
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;
77 }
78
79 static void
fd_blitter_pipe_begin(struct fd_context * ctx,bool render_cond,bool discard,enum fd_render_stage stage)80 fd_blitter_pipe_begin(struct fd_context *ctx, bool render_cond, bool discard,
81 enum fd_render_stage stage)
82 {
83 fd_fence_ref(&ctx->last_fence, NULL);
84
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);
110 if (!render_cond)
111 util_blitter_save_render_condition(ctx->blitter,
112 ctx->cond_query, ctx->cond_cond, ctx->cond_mode);
113
114 if (ctx->batch)
115 fd_batch_set_stage(ctx->batch, stage);
116
117 ctx->in_discard_blit = discard;
118 }
119
120 static void
fd_blitter_pipe_end(struct fd_context * ctx)121 fd_blitter_pipe_end(struct fd_context *ctx)
122 {
123 ctx->in_discard_blit = false;
124 }
125
126 bool
fd_blitter_blit(struct fd_context * ctx,const struct pipe_blit_info * info)127 fd_blitter_blit(struct fd_context *ctx, const struct pipe_blit_info *info)
128 {
129 struct pipe_resource *dst = info->dst.resource;
130 struct pipe_resource *src = info->src.resource;
131 struct pipe_context *pipe = &ctx->base;
132 struct pipe_surface *dst_view, dst_templ;
133 struct pipe_sampler_view src_templ, *src_view;
134 bool discard = false;
135
136 if (!info->scissor_enable && !info->alpha_blend) {
137 discard = util_texrange_covers_whole_level(info->dst.resource,
138 info->dst.level, info->dst.box.x, info->dst.box.y,
139 info->dst.box.z, info->dst.box.width,
140 info->dst.box.height, info->dst.box.depth);
141 }
142
143 fd_blitter_pipe_begin(ctx, info->render_condition_enable, discard, FD_STAGE_BLIT);
144
145 /* Initialize the surface. */
146 default_dst_texture(&dst_templ, dst, info->dst.level,
147 info->dst.box.z);
148 dst_templ.format = info->dst.format;
149 dst_view = pipe->create_surface(pipe, dst, &dst_templ);
150
151 /* Initialize the sampler view. */
152 default_src_texture(&src_templ, src, info->src.level);
153 src_templ.format = info->src.format;
154 src_view = pipe->create_sampler_view(pipe, src, &src_templ);
155
156 /* Copy. */
157 util_blitter_blit_generic(ctx->blitter, dst_view, &info->dst.box,
158 src_view, &info->src.box, src->width0, src->height0,
159 info->mask, info->filter,
160 info->scissor_enable ? &info->scissor : NULL,
161 info->alpha_blend);
162
163 pipe_surface_reference(&dst_view, NULL);
164 pipe_sampler_view_reference(&src_view, NULL);
165
166 fd_blitter_pipe_end(ctx);
167
168 /* The fallback blitter must never fail: */
169 return true;
170 }
171
172 /* Generic clear implementation (partially) using u_blitter: */
173 void
fd_blitter_clear(struct pipe_context * pctx,unsigned buffers,const union pipe_color_union * color,double depth,unsigned stencil)174 fd_blitter_clear(struct pipe_context *pctx, unsigned buffers,
175 const union pipe_color_union *color, double depth, unsigned stencil)
176 {
177 struct fd_context *ctx = fd_context(pctx);
178 struct pipe_framebuffer_state *pfb = &ctx->batch->framebuffer;
179 struct blitter_context *blitter = ctx->blitter;
180
181 /* Note: don't use discard=true, if there was something to
182 * discard, that would have been already handled in fd_clear().
183 */
184 fd_blitter_pipe_begin(ctx, false, false, FD_STAGE_CLEAR);
185
186 util_blitter_common_clear_setup(blitter, pfb->width, pfb->height,
187 buffers, NULL, NULL);
188
189 struct pipe_stencil_ref sr = {
190 .ref_value = { stencil & 0xff }
191 };
192 pctx->set_stencil_ref(pctx, &sr);
193
194 struct pipe_constant_buffer cb = {
195 .buffer_size = 16,
196 .user_buffer = &color->ui,
197 };
198 pctx->set_constant_buffer(pctx, PIPE_SHADER_FRAGMENT, 0, &cb);
199
200 unsigned rs_idx = pfb->samples > 1 ? 1 : 0;
201 if (!ctx->clear_rs_state[rs_idx]) {
202 const struct pipe_rasterizer_state tmpl = {
203 .cull_face = PIPE_FACE_NONE,
204 .half_pixel_center = 1,
205 .bottom_edge_rule = 1,
206 .flatshade = 1,
207 .depth_clip_near = 1,
208 .depth_clip_far = 1,
209 .multisample = pfb->samples > 1,
210 };
211 ctx->clear_rs_state[rs_idx] = pctx->create_rasterizer_state(pctx, &tmpl);
212 }
213 pctx->bind_rasterizer_state(pctx, ctx->clear_rs_state[rs_idx]);
214
215 struct pipe_viewport_state vp = {
216 .scale = { 0.5f * pfb->width, -0.5f * pfb->height, depth },
217 .translate = { 0.5f * pfb->width, 0.5f * pfb->height, 0.0f },
218 };
219 pctx->set_viewport_states(pctx, 0, 1, &vp);
220
221 pctx->bind_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
222 pctx->set_vertex_buffers(pctx, blitter->vb_slot, 1,
223 &ctx->solid_vbuf_state.vertexbuf.vb[0]);
224 pctx->set_stream_output_targets(pctx, 0, NULL, NULL);
225 pctx->bind_vs_state(pctx, ctx->solid_prog.vs);
226 pctx->bind_fs_state(pctx, ctx->solid_prog.fs);
227
228 /* Clear geom/tess shaders, lest the draw emit code think we are
229 * trying to use use them:
230 */
231 pctx->bind_gs_state(pctx, NULL);
232 pctx->bind_tcs_state(pctx, NULL);
233 pctx->bind_tes_state(pctx, NULL);
234
235 struct pipe_draw_info info = {
236 .mode = PIPE_PRIM_MAX, /* maps to DI_PT_RECTLIST */
237 .count = 2,
238 .max_index = 1,
239 .instance_count = 1,
240 };
241 pctx->draw_vbo(pctx, &info);
242
243 /* We expect that this should not have triggered a change in pfb: */
244 assert(util_framebuffer_state_equal(pfb, &ctx->framebuffer));
245
246 util_blitter_restore_constant_buffer_state(blitter);
247 util_blitter_restore_vertex_states(blitter);
248 util_blitter_restore_fragment_states(blitter);
249 util_blitter_restore_textures(blitter);
250 util_blitter_restore_fb_state(blitter);
251 util_blitter_restore_render_cond(blitter);
252 util_blitter_unset_running_flag(blitter);
253
254 fd_blitter_pipe_end(ctx);
255 }
256
257 /**
258 * Optimal hardware path for blitting pixels.
259 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
260 */
261 bool
fd_blit(struct pipe_context * pctx,const struct pipe_blit_info * blit_info)262 fd_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
263 {
264 struct fd_context *ctx = fd_context(pctx);
265 struct pipe_blit_info info = *blit_info;
266
267 if (info.render_condition_enable && !fd_render_condition_check(pctx))
268 return true;
269
270 if (ctx->blit && ctx->blit(ctx, &info))
271 return true;
272
273 if (info.mask & PIPE_MASK_S) {
274 DBG("cannot blit stencil, skipping");
275 info.mask &= ~PIPE_MASK_S;
276 }
277
278 if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
279 DBG("blit unsupported %s -> %s",
280 util_format_short_name(info.src.resource->format),
281 util_format_short_name(info.dst.resource->format));
282 return false;
283 }
284
285 return fd_blitter_blit(ctx, &info);
286 }
287
288 /**
289 * _copy_region using pipe (3d engine)
290 */
291 static bool
fd_blitter_pipe_copy_region(struct fd_context * ctx,struct pipe_resource * dst,unsigned dst_level,unsigned dstx,unsigned dsty,unsigned dstz,struct pipe_resource * src,unsigned src_level,const struct pipe_box * src_box)292 fd_blitter_pipe_copy_region(struct fd_context *ctx,
293 struct pipe_resource *dst,
294 unsigned dst_level,
295 unsigned dstx, unsigned dsty, unsigned dstz,
296 struct pipe_resource *src,
297 unsigned src_level,
298 const struct pipe_box *src_box)
299 {
300 /* not until we allow rendertargets to be buffers */
301 if (dst->target == PIPE_BUFFER || src->target == PIPE_BUFFER)
302 return false;
303
304 if (!util_blitter_is_copy_supported(ctx->blitter, dst, src))
305 return false;
306
307 /* TODO we could discard if dst box covers dst level fully.. */
308 fd_blitter_pipe_begin(ctx, false, false, FD_STAGE_BLIT);
309 util_blitter_copy_texture(ctx->blitter,
310 dst, dst_level, dstx, dsty, dstz,
311 src, src_level, src_box);
312 fd_blitter_pipe_end(ctx);
313
314 return true;
315 }
316
317 /**
318 * Copy a block of pixels from one resource to another.
319 * The resource must be of the same format.
320 */
321 void
fd_resource_copy_region(struct pipe_context * pctx,struct pipe_resource * dst,unsigned dst_level,unsigned dstx,unsigned dsty,unsigned dstz,struct pipe_resource * src,unsigned src_level,const struct pipe_box * src_box)322 fd_resource_copy_region(struct pipe_context *pctx,
323 struct pipe_resource *dst,
324 unsigned dst_level,
325 unsigned dstx, unsigned dsty, unsigned dstz,
326 struct pipe_resource *src,
327 unsigned src_level,
328 const struct pipe_box *src_box)
329 {
330 struct fd_context *ctx = fd_context(pctx);
331
332 if (ctx->blit) {
333 struct pipe_blit_info info;
334
335 memset(&info, 0, sizeof info);
336 info.dst.resource = dst;
337 info.dst.level = dst_level;
338 info.dst.box.x = dstx;
339 info.dst.box.y = dsty;
340 info.dst.box.z = dstz;
341 info.dst.box.width = src_box->width;
342 info.dst.box.height = src_box->height;
343 assert(info.dst.box.width >= 0);
344 assert(info.dst.box.height >= 0);
345 info.dst.box.depth = 1;
346 info.dst.format = dst->format;
347 info.src.resource = src;
348 info.src.level = src_level;
349 info.src.box = *src_box;
350 info.src.format = src->format;
351 info.mask = util_format_get_mask(src->format);
352 info.filter = PIPE_TEX_FILTER_NEAREST;
353 info.scissor_enable = 0;
354
355 if (ctx->blit(ctx, &info))
356 return;
357 }
358
359 /* TODO if we have 2d core, or other DMA engine that could be used
360 * for simple copies and reasonably easily synchronized with the 3d
361 * core, this is where we'd plug it in..
362 */
363
364 /* try blit on 3d pipe: */
365 if (fd_blitter_pipe_copy_region(ctx,
366 dst, dst_level, dstx, dsty, dstz,
367 src, src_level, src_box))
368 return;
369
370 /* else fallback to pure sw: */
371 util_resource_copy_region(pctx,
372 dst, dst_level, dstx, dsty, dstz,
373 src, src_level, src_box);
374 }
375