radeonsi: use shader_info::cs::local_size_variable to clean up some code
[mesa.git] / src / gallium / drivers / freedreno / freedreno_blitter.c
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
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
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
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
121 fd_blitter_pipe_end(struct fd_context *ctx)
122 {
123 ctx->in_discard_blit = false;
124 }
125
126 bool
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
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 fd_blitter_pipe_begin(ctx, false, true, FD_STAGE_CLEAR);
182
183 util_blitter_common_clear_setup(blitter, pfb->width, pfb->height,
184 buffers, NULL, NULL);
185
186 struct pipe_stencil_ref sr = {
187 .ref_value = { stencil & 0xff }
188 };
189 pctx->set_stencil_ref(pctx, &sr);
190
191 struct pipe_constant_buffer cb = {
192 .buffer_size = 16,
193 .user_buffer = &color->ui,
194 };
195 pctx->set_constant_buffer(pctx, PIPE_SHADER_FRAGMENT, 0, &cb);
196
197 if (!ctx->clear_rs_state) {
198 const struct pipe_rasterizer_state tmpl = {
199 .cull_face = PIPE_FACE_NONE,
200 .half_pixel_center = 1,
201 .bottom_edge_rule = 1,
202 .flatshade = 1,
203 .depth_clip_near = 1,
204 .depth_clip_far = 1,
205 };
206 ctx->clear_rs_state = pctx->create_rasterizer_state(pctx, &tmpl);
207 }
208 pctx->bind_rasterizer_state(pctx, ctx->clear_rs_state);
209
210 struct pipe_viewport_state vp = {
211 .scale = { 0.5f * pfb->width, -0.5f * pfb->height, depth },
212 .translate = { 0.5f * pfb->width, 0.5f * pfb->height, 0.0f },
213 };
214 pctx->set_viewport_states(pctx, 0, 1, &vp);
215
216 pctx->bind_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
217 pctx->set_vertex_buffers(pctx, blitter->vb_slot, 1,
218 &ctx->solid_vbuf_state.vertexbuf.vb[0]);
219 pctx->set_stream_output_targets(pctx, 0, NULL, NULL);
220 pctx->bind_vs_state(pctx, ctx->solid_prog.vs);
221 pctx->bind_fs_state(pctx, ctx->solid_prog.fs);
222
223 struct pipe_draw_info info = {
224 .mode = PIPE_PRIM_MAX, /* maps to DI_PT_RECTLIST */
225 .count = 2,
226 .max_index = 1,
227 .instance_count = 1,
228 };
229 ctx->draw_vbo(ctx, &info, 0);
230
231 util_blitter_restore_constant_buffer_state(blitter);
232 util_blitter_restore_vertex_states(blitter);
233 util_blitter_restore_fragment_states(blitter);
234 util_blitter_restore_textures(blitter);
235 util_blitter_restore_fb_state(blitter);
236 util_blitter_restore_render_cond(blitter);
237 util_blitter_unset_running_flag(blitter);
238
239 fd_blitter_pipe_end(ctx);
240 }
241
242 /**
243 * Optimal hardware path for blitting pixels.
244 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
245 */
246 bool
247 fd_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
248 {
249 struct fd_context *ctx = fd_context(pctx);
250 struct pipe_blit_info info = *blit_info;
251
252 if (info.render_condition_enable && !fd_render_condition_check(pctx))
253 return true;
254
255 if (ctx->blit && ctx->blit(ctx, &info))
256 return true;
257
258 if (info.mask & PIPE_MASK_S) {
259 DBG("cannot blit stencil, skipping");
260 info.mask &= ~PIPE_MASK_S;
261 }
262
263 if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
264 DBG("blit unsupported %s -> %s",
265 util_format_short_name(info.src.resource->format),
266 util_format_short_name(info.dst.resource->format));
267 return false;
268 }
269
270 return fd_blitter_blit(ctx, &info);
271 }
272
273 /**
274 * _copy_region using pipe (3d engine)
275 */
276 static bool
277 fd_blitter_pipe_copy_region(struct fd_context *ctx,
278 struct pipe_resource *dst,
279 unsigned dst_level,
280 unsigned dstx, unsigned dsty, unsigned dstz,
281 struct pipe_resource *src,
282 unsigned src_level,
283 const struct pipe_box *src_box)
284 {
285 /* not until we allow rendertargets to be buffers */
286 if (dst->target == PIPE_BUFFER || src->target == PIPE_BUFFER)
287 return false;
288
289 if (!util_blitter_is_copy_supported(ctx->blitter, dst, src))
290 return false;
291
292 /* TODO we could discard if dst box covers dst level fully.. */
293 fd_blitter_pipe_begin(ctx, false, false, FD_STAGE_BLIT);
294 util_blitter_copy_texture(ctx->blitter,
295 dst, dst_level, dstx, dsty, dstz,
296 src, src_level, src_box);
297 fd_blitter_pipe_end(ctx);
298
299 return true;
300 }
301
302 /**
303 * Copy a block of pixels from one resource to another.
304 * The resource must be of the same format.
305 */
306 void
307 fd_resource_copy_region(struct pipe_context *pctx,
308 struct pipe_resource *dst,
309 unsigned dst_level,
310 unsigned dstx, unsigned dsty, unsigned dstz,
311 struct pipe_resource *src,
312 unsigned src_level,
313 const struct pipe_box *src_box)
314 {
315 struct fd_context *ctx = fd_context(pctx);
316
317 if (ctx->blit) {
318 struct pipe_blit_info info;
319
320 memset(&info, 0, sizeof info);
321 info.dst.resource = dst;
322 info.dst.level = dst_level;
323 info.dst.box.x = dstx;
324 info.dst.box.y = dsty;
325 info.dst.box.z = dstz;
326 info.dst.box.width = src_box->width;
327 info.dst.box.height = src_box->height;
328 assert(info.dst.box.width >= 0);
329 assert(info.dst.box.height >= 0);
330 info.dst.box.depth = 1;
331 info.dst.format = dst->format;
332 info.src.resource = src;
333 info.src.level = src_level;
334 info.src.box = *src_box;
335 info.src.format = src->format;
336 info.mask = util_format_get_mask(src->format);
337 info.filter = PIPE_TEX_FILTER_NEAREST;
338 info.scissor_enable = 0;
339
340 if (ctx->blit(ctx, &info))
341 return;
342 }
343
344 /* TODO if we have 2d core, or other DMA engine that could be used
345 * for simple copies and reasonably easily synchronized with the 3d
346 * core, this is where we'd plug it in..
347 */
348
349 /* try blit on 3d pipe: */
350 if (fd_blitter_pipe_copy_region(ctx,
351 dst, dst_level, dstx, dsty, dstz,
352 src, src_level, src_box))
353 return;
354
355 /* else fallback to pure sw: */
356 util_resource_copy_region(pctx,
357 dst, dst_level, dstx, dsty, dstz,
358 src, src_level, src_box);
359 }