radeonsi: inline util_blitter_copy_texture
[mesa.git] / src / gallium / drivers / radeonsi / si_blit.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "si_pipe.h"
25 #include "util/u_blitter.h"
26 #include "util/u_format.h"
27
28 enum si_blitter_op /* bitmask */
29 {
30 SI_SAVE_TEXTURES = 1,
31 SI_SAVE_FRAMEBUFFER = 2,
32 SI_DISABLE_RENDER_COND = 4,
33
34 SI_CLEAR = 0,
35
36 SI_CLEAR_SURFACE = SI_SAVE_FRAMEBUFFER,
37
38 SI_COPY = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES |
39 SI_DISABLE_RENDER_COND,
40
41 SI_BLIT = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES |
42 SI_DISABLE_RENDER_COND,
43
44 SI_DECOMPRESS = SI_SAVE_FRAMEBUFFER | SI_DISABLE_RENDER_COND,
45
46 SI_COLOR_RESOLVE = SI_SAVE_FRAMEBUFFER | SI_DISABLE_RENDER_COND
47 };
48
49 static void si_blitter_begin(struct pipe_context *ctx, enum si_blitter_op op)
50 {
51 struct si_context *sctx = (struct si_context *)ctx;
52
53 r600_suspend_nontimer_queries(&sctx->b);
54
55 util_blitter_save_blend(sctx->blitter, sctx->queued.named.blend);
56 util_blitter_save_depth_stencil_alpha(sctx->blitter, sctx->queued.named.dsa);
57 util_blitter_save_stencil_ref(sctx->blitter, &sctx->stencil_ref);
58 util_blitter_save_rasterizer(sctx->blitter, sctx->queued.named.rasterizer);
59 util_blitter_save_fragment_shader(sctx->blitter, sctx->ps_shader);
60 util_blitter_save_geometry_shader(sctx->blitter, sctx->gs_shader);
61 util_blitter_save_vertex_shader(sctx->blitter, sctx->vs_shader);
62 util_blitter_save_vertex_elements(sctx->blitter, sctx->vertex_elements);
63 if (sctx->queued.named.viewport) {
64 util_blitter_save_viewport(sctx->blitter, &sctx->queued.named.viewport->viewport);
65 }
66 util_blitter_save_vertex_buffer_slot(sctx->blitter, sctx->vertex_buffer);
67 util_blitter_save_so_targets(sctx->blitter, sctx->b.streamout.num_targets,
68 (struct pipe_stream_output_target**)sctx->b.streamout.targets);
69
70 if (op & SI_SAVE_FRAMEBUFFER)
71 util_blitter_save_framebuffer(sctx->blitter, &sctx->framebuffer);
72
73 if (op & SI_SAVE_TEXTURES) {
74 util_blitter_save_fragment_sampler_states(
75 sctx->blitter, sctx->samplers[PIPE_SHADER_FRAGMENT].n_samplers,
76 (void**)sctx->samplers[PIPE_SHADER_FRAGMENT].samplers);
77
78 util_blitter_save_fragment_sampler_views(sctx->blitter,
79 util_last_bit(sctx->samplers[PIPE_SHADER_FRAGMENT].views.desc.enabled_mask &
80 ((1 << NUM_TEX_UNITS) - 1)),
81 sctx->samplers[PIPE_SHADER_FRAGMENT].views.views);
82 }
83
84 if ((op & SI_DISABLE_RENDER_COND) && sctx->b.current_render_cond) {
85 util_blitter_save_render_condition(sctx->blitter,
86 sctx->b.current_render_cond,
87 sctx->b.current_render_cond_cond,
88 sctx->b.current_render_cond_mode);
89 }
90 }
91
92 static void si_blitter_end(struct pipe_context *ctx)
93 {
94 struct si_context *sctx = (struct si_context *)ctx;
95 r600_resume_nontimer_queries(&sctx->b);
96 }
97
98 static unsigned u_max_sample(struct pipe_resource *r)
99 {
100 return r->nr_samples ? r->nr_samples - 1 : 0;
101 }
102
103 static void si_blit_decompress_depth(struct pipe_context *ctx,
104 struct r600_texture *texture,
105 struct r600_texture *staging,
106 unsigned first_level, unsigned last_level,
107 unsigned first_layer, unsigned last_layer,
108 unsigned first_sample, unsigned last_sample)
109 {
110 struct si_context *sctx = (struct si_context *)ctx;
111 unsigned layer, level, sample, checked_last_layer, max_layer, max_sample;
112 float depth = 1.0f;
113 const struct util_format_description *desc;
114 void **custom_dsa;
115 struct r600_texture *flushed_depth_texture = staging ?
116 staging : texture->flushed_depth_texture;
117
118 if (!staging && !texture->dirty_level_mask)
119 return;
120
121 max_sample = u_max_sample(&texture->resource.b.b);
122
123 desc = util_format_description(flushed_depth_texture->resource.b.b.format);
124 switch (util_format_has_depth(desc) | util_format_has_stencil(desc) << 1) {
125 default:
126 assert(!"No depth or stencil to uncompress");
127 return;
128 case 3:
129 custom_dsa = sctx->custom_dsa_flush_depth_stencil;
130 break;
131 case 2:
132 custom_dsa = sctx->custom_dsa_flush_stencil;
133 break;
134 case 1:
135 custom_dsa = sctx->custom_dsa_flush_depth;
136 break;
137 }
138
139 for (level = first_level; level <= last_level; level++) {
140 if (!staging && !(texture->dirty_level_mask & (1 << level)))
141 continue;
142
143 /* The smaller the mipmap level, the less layers there are
144 * as far as 3D textures are concerned. */
145 max_layer = util_max_layer(&texture->resource.b.b, level);
146 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
147
148 for (layer = first_layer; layer <= checked_last_layer; layer++) {
149 for (sample = first_sample; sample <= last_sample; sample++) {
150 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
151
152 surf_tmpl.format = texture->resource.b.b.format;
153 surf_tmpl.u.tex.level = level;
154 surf_tmpl.u.tex.first_layer = layer;
155 surf_tmpl.u.tex.last_layer = layer;
156
157 zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
158
159 surf_tmpl.format = flushed_depth_texture->resource.b.b.format;
160 cbsurf = ctx->create_surface(ctx,
161 (struct pipe_resource*)flushed_depth_texture, &surf_tmpl);
162
163 si_blitter_begin(ctx, SI_DECOMPRESS);
164 util_blitter_custom_depth_stencil(sctx->blitter, zsurf, cbsurf, 1 << sample,
165 custom_dsa[sample], depth);
166 si_blitter_end(ctx);
167
168 pipe_surface_reference(&zsurf, NULL);
169 pipe_surface_reference(&cbsurf, NULL);
170 }
171 }
172
173 /* The texture will always be dirty if some layers aren't flushed.
174 * I don't think this case can occur though. */
175 if (!staging &&
176 first_layer == 0 && last_layer == max_layer &&
177 first_sample == 0 && last_sample == max_sample) {
178 texture->dirty_level_mask &= ~(1 << level);
179 }
180 }
181 }
182
183 static void si_blit_decompress_depth_in_place(struct si_context *sctx,
184 struct r600_texture *texture,
185 unsigned first_level, unsigned last_level,
186 unsigned first_layer, unsigned last_layer)
187 {
188 struct pipe_surface *zsurf, surf_tmpl = {{0}};
189 unsigned layer, max_layer, checked_last_layer, level;
190
191 surf_tmpl.format = texture->resource.b.b.format;
192
193 for (level = first_level; level <= last_level; level++) {
194 if (!(texture->dirty_level_mask & (1 << level)))
195 continue;
196
197 surf_tmpl.u.tex.level = level;
198
199 /* The smaller the mipmap level, the less layers there are
200 * as far as 3D textures are concerned. */
201 max_layer = util_max_layer(&texture->resource.b.b, level);
202 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
203
204 for (layer = first_layer; layer <= checked_last_layer; layer++) {
205 surf_tmpl.u.tex.first_layer = layer;
206 surf_tmpl.u.tex.last_layer = layer;
207
208 zsurf = sctx->b.b.create_surface(&sctx->b.b, &texture->resource.b.b, &surf_tmpl);
209
210 si_blitter_begin(&sctx->b.b, SI_DECOMPRESS);
211 util_blitter_custom_depth_stencil(sctx->blitter, zsurf, NULL, ~0,
212 sctx->custom_dsa_flush_inplace,
213 1.0f);
214 si_blitter_end(&sctx->b.b);
215
216 pipe_surface_reference(&zsurf, NULL);
217 }
218
219 /* The texture will always be dirty if some layers aren't flushed.
220 * I don't think this case occurs often though. */
221 if (first_layer == 0 && last_layer == max_layer) {
222 texture->dirty_level_mask &= ~(1 << level);
223 }
224 }
225 }
226
227 void si_flush_depth_textures(struct si_context *sctx,
228 struct si_textures_info *textures)
229 {
230 unsigned i;
231
232 for (i = 0; i < textures->n_views; ++i) {
233 struct pipe_sampler_view *view;
234 struct r600_texture *tex;
235
236 view = textures->views.views[i];
237 if (!view) continue;
238
239 tex = (struct r600_texture *)view->texture;
240 if (!tex->is_depth || tex->is_flushing_texture)
241 continue;
242
243 si_blit_decompress_depth_in_place(sctx, tex,
244 view->u.tex.first_level, view->u.tex.last_level,
245 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
246 }
247 }
248
249 static void si_blit_decompress_color(struct pipe_context *ctx,
250 struct r600_texture *rtex,
251 unsigned first_level, unsigned last_level,
252 unsigned first_layer, unsigned last_layer)
253 {
254 struct si_context *sctx = (struct si_context *)ctx;
255 unsigned layer, level, checked_last_layer, max_layer;
256
257 if (!rtex->dirty_level_mask)
258 return;
259
260 for (level = first_level; level <= last_level; level++) {
261 if (!(rtex->dirty_level_mask & (1 << level)))
262 continue;
263
264 /* The smaller the mipmap level, the less layers there are
265 * as far as 3D textures are concerned. */
266 max_layer = util_max_layer(&rtex->resource.b.b, level);
267 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
268
269 for (layer = first_layer; layer <= checked_last_layer; layer++) {
270 struct pipe_surface *cbsurf, surf_tmpl;
271
272 surf_tmpl.format = rtex->resource.b.b.format;
273 surf_tmpl.u.tex.level = level;
274 surf_tmpl.u.tex.first_layer = layer;
275 surf_tmpl.u.tex.last_layer = layer;
276 cbsurf = ctx->create_surface(ctx, &rtex->resource.b.b, &surf_tmpl);
277
278 si_blitter_begin(ctx, SI_DECOMPRESS);
279 util_blitter_custom_color(sctx->blitter, cbsurf,
280 sctx->custom_blend_decompress);
281 si_blitter_end(ctx);
282
283 pipe_surface_reference(&cbsurf, NULL);
284 }
285
286 /* The texture will always be dirty if some layers aren't flushed.
287 * I don't think this case occurs often though. */
288 if (first_layer == 0 && last_layer == max_layer) {
289 rtex->dirty_level_mask &= ~(1 << level);
290 }
291 }
292 }
293
294 void si_decompress_color_textures(struct si_context *sctx,
295 struct si_textures_info *textures)
296 {
297 unsigned i;
298 unsigned mask = textures->compressed_colortex_mask;
299
300 while (mask) {
301 struct pipe_sampler_view *view;
302 struct r600_texture *tex;
303
304 i = u_bit_scan(&mask);
305
306 view = textures->views.views[i];
307 assert(view);
308
309 tex = (struct r600_texture *)view->texture;
310 assert(tex->cmask.size || tex->fmask.size);
311
312 si_blit_decompress_color(&sctx->b.b, tex,
313 view->u.tex.first_level, view->u.tex.last_level,
314 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
315 }
316 }
317
318 static void si_clear(struct pipe_context *ctx, unsigned buffers,
319 const union pipe_color_union *color,
320 double depth, unsigned stencil)
321 {
322 struct si_context *sctx = (struct si_context *)ctx;
323 struct pipe_framebuffer_state *fb = &sctx->framebuffer;
324
325 si_blitter_begin(ctx, SI_CLEAR);
326 util_blitter_clear(sctx->blitter, fb->width, fb->height,
327 util_framebuffer_get_num_layers(fb),
328 buffers, color, depth, stencil);
329 si_blitter_end(ctx);
330 }
331
332 static void si_clear_render_target(struct pipe_context *ctx,
333 struct pipe_surface *dst,
334 const union pipe_color_union *color,
335 unsigned dstx, unsigned dsty,
336 unsigned width, unsigned height)
337 {
338 struct si_context *sctx = (struct si_context *)ctx;
339
340 si_blitter_begin(ctx, SI_CLEAR_SURFACE);
341 util_blitter_clear_render_target(sctx->blitter, dst, color,
342 dstx, dsty, width, height);
343 si_blitter_end(ctx);
344 }
345
346 static void si_clear_depth_stencil(struct pipe_context *ctx,
347 struct pipe_surface *dst,
348 unsigned clear_flags,
349 double depth,
350 unsigned stencil,
351 unsigned dstx, unsigned dsty,
352 unsigned width, unsigned height)
353 {
354 struct si_context *sctx = (struct si_context *)ctx;
355
356 si_blitter_begin(ctx, SI_CLEAR_SURFACE);
357 util_blitter_clear_depth_stencil(sctx->blitter, dst, clear_flags, depth, stencil,
358 dstx, dsty, width, height);
359 si_blitter_end(ctx);
360 }
361
362 /* Helper for decompressing a portion of a color or depth resource before
363 * blitting if any decompression is needed.
364 * The driver doesn't decompress resources automatically while u_blitter is
365 * rendering. */
366 static void si_decompress_subresource(struct pipe_context *ctx,
367 struct pipe_resource *tex,
368 unsigned level,
369 unsigned first_layer, unsigned last_layer)
370 {
371 struct si_context *sctx = (struct si_context *)ctx;
372 struct r600_texture *rtex = (struct r600_texture*)tex;
373
374 if (rtex->is_depth && !rtex->is_flushing_texture) {
375 si_blit_decompress_depth_in_place(sctx, rtex,
376 level, level,
377 first_layer, last_layer);
378 } else if (rtex->fmask.size || rtex->cmask.size) {
379 si_blit_decompress_color(ctx, rtex, level, level,
380 first_layer, last_layer);
381 }
382 }
383
384 struct texture_orig_info {
385 unsigned format;
386 unsigned width0;
387 unsigned height0;
388 unsigned npix_x;
389 unsigned npix_y;
390 unsigned npix0_x;
391 unsigned npix0_y;
392 };
393
394 static void si_compressed_to_blittable(struct pipe_resource *tex,
395 unsigned level,
396 struct texture_orig_info *orig)
397 {
398 struct r600_texture *rtex = (struct r600_texture*)tex;
399 unsigned pixsize = util_format_get_blocksize(rtex->resource.b.b.format);
400 int new_format;
401 int new_height, new_width;
402
403 orig->format = tex->format;
404 orig->width0 = tex->width0;
405 orig->height0 = tex->height0;
406 orig->npix0_x = rtex->surface.level[0].npix_x;
407 orig->npix0_y = rtex->surface.level[0].npix_y;
408 orig->npix_x = rtex->surface.level[level].npix_x;
409 orig->npix_y = rtex->surface.level[level].npix_y;
410
411 if (pixsize == 8)
412 new_format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
413 else
414 new_format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
415
416 new_width = util_format_get_nblocksx(tex->format, orig->width0);
417 new_height = util_format_get_nblocksy(tex->format, orig->height0);
418
419 tex->width0 = new_width;
420 tex->height0 = new_height;
421 tex->format = new_format;
422 rtex->surface.level[0].npix_x = util_format_get_nblocksx(orig->format, orig->npix0_x);
423 rtex->surface.level[0].npix_y = util_format_get_nblocksy(orig->format, orig->npix0_y);
424 rtex->surface.level[level].npix_x = util_format_get_nblocksx(orig->format, orig->npix_x);
425 rtex->surface.level[level].npix_y = util_format_get_nblocksy(orig->format, orig->npix_y);
426
427 /* By dividing the dimensions by 4, we effectively decrement
428 * last_level by 2, therefore the last 2 mipmap levels disappear and
429 * aren't blittable. Note that the last 3 mipmap levels (4x4, 2x2,
430 * 1x1) have equal slice sizes, which is an important assumption
431 * for this to work.
432 *
433 * In order to make the last 2 mipmap levels blittable, we have to
434 * add the slice size of the last mipmap level to the texture
435 * address, so that even though the hw thinks it reads last_level-2,
436 * it will actually read last_level-1, and if we add the slice size*2,
437 * it will read last_level. That's how this workaround works.
438 */
439 if (level > rtex->resource.b.b.last_level-2)
440 rtex->mipmap_shift = level - (rtex->resource.b.b.last_level-2);
441 }
442
443 static void si_change_format(struct pipe_resource *tex,
444 unsigned level,
445 struct texture_orig_info *orig,
446 enum pipe_format format)
447 {
448 struct r600_texture *rtex = (struct r600_texture*)tex;
449
450 orig->format = tex->format;
451 orig->width0 = tex->width0;
452 orig->height0 = tex->height0;
453 orig->npix0_x = rtex->surface.level[0].npix_x;
454 orig->npix0_y = rtex->surface.level[0].npix_y;
455 orig->npix_x = rtex->surface.level[level].npix_x;
456 orig->npix_y = rtex->surface.level[level].npix_y;
457
458 tex->format = format;
459 }
460
461 static void si_reset_blittable_to_orig(struct pipe_resource *tex,
462 unsigned level,
463 struct texture_orig_info *orig)
464 {
465 struct r600_texture *rtex = (struct r600_texture*)tex;
466
467 tex->format = orig->format;
468 tex->width0 = orig->width0;
469 tex->height0 = orig->height0;
470 rtex->surface.level[0].npix_x = orig->npix0_x;
471 rtex->surface.level[0].npix_y = orig->npix0_y;
472 rtex->surface.level[level].npix_x = orig->npix_x;
473 rtex->surface.level[level].npix_y = orig->npix_y;
474 rtex->mipmap_shift = 0;
475 }
476
477 static void si_resource_copy_region(struct pipe_context *ctx,
478 struct pipe_resource *dst,
479 unsigned dst_level,
480 unsigned dstx, unsigned dsty, unsigned dstz,
481 struct pipe_resource *src,
482 unsigned src_level,
483 const struct pipe_box *src_box)
484 {
485 struct si_context *sctx = (struct si_context *)ctx;
486 struct pipe_surface *dst_view, dst_templ;
487 struct pipe_sampler_view src_templ, *src_view;
488 struct texture_orig_info orig_info[2];
489 struct pipe_box sbox, dstbox;
490 boolean restore_orig[2];
491
492 /* Fallback for buffers. */
493 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
494 si_copy_buffer(sctx, dst, src, dstx, src_box->x, src_box->width);
495 return;
496 }
497
498 memset(orig_info, 0, sizeof(orig_info));
499
500 /* The driver doesn't decompress resources automatically while
501 * u_blitter is rendering. */
502 si_decompress_subresource(ctx, src, src_level,
503 src_box->z, src_box->z + src_box->depth - 1);
504
505 restore_orig[0] = restore_orig[1] = FALSE;
506
507 if (util_format_is_compressed(src->format) &&
508 util_format_is_compressed(dst->format)) {
509 si_compressed_to_blittable(src, src_level, &orig_info[0]);
510 restore_orig[0] = TRUE;
511 sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
512 sbox.y = util_format_get_nblocksy(orig_info[0].format, src_box->y);
513 sbox.z = src_box->z;
514 sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
515 sbox.height = util_format_get_nblocksy(orig_info[0].format, src_box->height);
516 sbox.depth = src_box->depth;
517 src_box = &sbox;
518
519 si_compressed_to_blittable(dst, dst_level, &orig_info[1]);
520 restore_orig[1] = TRUE;
521 /* translate the dst box as well */
522 dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
523 dsty = util_format_get_nblocksy(orig_info[1].format, dsty);
524 } else if (!util_blitter_is_copy_supported(sctx->blitter, dst, src)) {
525 unsigned blocksize = util_format_get_blocksize(src->format);
526
527 switch (blocksize) {
528 case 1:
529 si_change_format(src, src_level, &orig_info[0],
530 PIPE_FORMAT_R8_UNORM);
531 si_change_format(dst, dst_level, &orig_info[1],
532 PIPE_FORMAT_R8_UNORM);
533 break;
534 case 2:
535 si_change_format(src, src_level, &orig_info[0],
536 PIPE_FORMAT_R8G8_UNORM);
537 si_change_format(dst, dst_level, &orig_info[1],
538 PIPE_FORMAT_R8G8_UNORM);
539 break;
540 case 4:
541 si_change_format(src, src_level, &orig_info[0],
542 PIPE_FORMAT_R8G8B8A8_UNORM);
543 si_change_format(dst, dst_level, &orig_info[1],
544 PIPE_FORMAT_R8G8B8A8_UNORM);
545 break;
546 case 8:
547 si_change_format(src, src_level, &orig_info[0],
548 PIPE_FORMAT_R16G16B16A16_UINT);
549 si_change_format(dst, dst_level, &orig_info[1],
550 PIPE_FORMAT_R16G16B16A16_UINT);
551 break;
552 case 16:
553 si_change_format(src, src_level, &orig_info[0],
554 PIPE_FORMAT_R32G32B32A32_UINT);
555 si_change_format(dst, dst_level, &orig_info[1],
556 PIPE_FORMAT_R32G32B32A32_UINT);
557 break;
558 default:
559 fprintf(stderr, "Unhandled format %s with blocksize %u\n",
560 util_format_short_name(src->format), blocksize);
561 assert(0);
562 }
563 restore_orig[0] = TRUE;
564 restore_orig[1] = TRUE;
565 }
566
567 /* Initialize the surface. */
568 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
569 dst_view = ctx->create_surface(ctx, dst, &dst_templ);
570
571 /* Initialize the sampler view. */
572 util_blitter_default_src_texture(&src_templ, src, src_level);
573 src_view = ctx->create_sampler_view(ctx, src, &src_templ);
574
575 u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
576 abs(src_box->depth), &dstbox);
577
578 /* Copy. */
579 si_blitter_begin(ctx, SI_COPY);
580 util_blitter_blit_generic(sctx->blitter, dst_view, &dstbox,
581 src_view, src_box, src->width0, src->height0,
582 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL);
583 si_blitter_end(ctx);
584
585 pipe_surface_reference(&dst_view, NULL);
586 pipe_sampler_view_reference(&src_view, NULL);
587
588 if (restore_orig[0])
589 si_reset_blittable_to_orig(src, src_level, &orig_info[0]);
590
591 if (restore_orig[1])
592 si_reset_blittable_to_orig(dst, dst_level, &orig_info[1]);
593 }
594
595 /* For MSAA integer resolving to work, we change the format to NORM using this function. */
596 static enum pipe_format int_to_norm_format(enum pipe_format format)
597 {
598 switch (format) {
599 #define REPLACE_FORMAT_SIGN(format,sign) \
600 case PIPE_FORMAT_##format##_##sign##INT: \
601 return PIPE_FORMAT_##format##_##sign##NORM
602 #define REPLACE_FORMAT(format) \
603 REPLACE_FORMAT_SIGN(format, U); \
604 REPLACE_FORMAT_SIGN(format, S)
605
606 REPLACE_FORMAT_SIGN(B10G10R10A2, U);
607 REPLACE_FORMAT(R8);
608 REPLACE_FORMAT(R8G8);
609 REPLACE_FORMAT(R8G8B8X8);
610 REPLACE_FORMAT(R8G8B8A8);
611 REPLACE_FORMAT(A8);
612 REPLACE_FORMAT(I8);
613 REPLACE_FORMAT(L8);
614 REPLACE_FORMAT(L8A8);
615 REPLACE_FORMAT(R16);
616 REPLACE_FORMAT(R16G16);
617 REPLACE_FORMAT(R16G16B16X16);
618 REPLACE_FORMAT(R16G16B16A16);
619 REPLACE_FORMAT(A16);
620 REPLACE_FORMAT(I16);
621 REPLACE_FORMAT(L16);
622 REPLACE_FORMAT(L16A16);
623
624 #undef REPLACE_FORMAT
625 #undef REPLACE_FORMAT_SIGN
626 default:
627 return format;
628 }
629 }
630
631 static bool do_hardware_msaa_resolve(struct pipe_context *ctx,
632 const struct pipe_blit_info *info)
633 {
634 struct si_context *sctx = (struct si_context*)ctx;
635 struct r600_texture *dst = (struct r600_texture*)info->dst.resource;
636 unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
637 unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
638 enum pipe_format format = int_to_norm_format(info->dst.format);
639 unsigned sample_mask = ~0;
640
641 if (info->src.resource->nr_samples > 1 &&
642 info->dst.resource->nr_samples <= 1 &&
643 util_max_layer(info->src.resource, 0) == 0 &&
644 util_max_layer(info->dst.resource, info->dst.level) == 0 &&
645 info->dst.format == info->src.format &&
646 !util_format_is_pure_integer(format) &&
647 !util_format_is_depth_or_stencil(format) &&
648 !info->scissor_enable &&
649 (info->mask & PIPE_MASK_RGBA) == PIPE_MASK_RGBA &&
650 dst_width == info->src.resource->width0 &&
651 dst_height == info->src.resource->height0 &&
652 info->dst.box.x == 0 &&
653 info->dst.box.y == 0 &&
654 info->dst.box.width == dst_width &&
655 info->dst.box.height == dst_height &&
656 info->dst.box.depth == 1 &&
657 info->src.box.x == 0 &&
658 info->src.box.y == 0 &&
659 info->src.box.width == dst_width &&
660 info->src.box.height == dst_height &&
661 info->src.box.depth == 1 &&
662 dst->surface.level[info->dst.level].mode >= RADEON_SURF_MODE_1D &&
663 !(dst->surface.flags & RADEON_SURF_SCANOUT)) {
664 si_blitter_begin(ctx, SI_COLOR_RESOLVE);
665 util_blitter_custom_resolve_color(sctx->blitter,
666 info->dst.resource, info->dst.level,
667 info->dst.box.z,
668 info->src.resource, info->src.box.z,
669 sample_mask, sctx->custom_blend_resolve,
670 format);
671 si_blitter_end(ctx);
672 return true;
673 }
674 return false;
675 }
676
677 static void si_blit(struct pipe_context *ctx,
678 const struct pipe_blit_info *info)
679 {
680 struct si_context *sctx = (struct si_context*)ctx;
681
682 if (do_hardware_msaa_resolve(ctx, info)) {
683 return;
684 }
685
686 assert(util_blitter_is_blit_supported(sctx->blitter, info));
687
688 /* The driver doesn't decompress resources automatically while
689 * u_blitter is rendering. */
690 si_decompress_subresource(ctx, info->src.resource, info->src.level,
691 info->src.box.z,
692 info->src.box.z + info->src.box.depth - 1);
693
694 si_blitter_begin(ctx, SI_BLIT);
695 util_blitter_blit(sctx->blitter, info);
696 si_blitter_end(ctx);
697 }
698
699 static void si_flush_resource(struct pipe_context *ctx,
700 struct pipe_resource *resource)
701 {
702 }
703
704 void si_init_blit_functions(struct si_context *sctx)
705 {
706 sctx->b.b.clear = si_clear;
707 sctx->b.b.clear_render_target = si_clear_render_target;
708 sctx->b.b.clear_depth_stencil = si_clear_depth_stencil;
709 sctx->b.b.resource_copy_region = si_resource_copy_region;
710 sctx->b.b.blit = si_blit;
711 sctx->b.b.flush_resource = si_flush_resource;
712 sctx->b.blit_decompress_depth = si_blit_decompress_depth;
713 }