radeonsi: optimize scissor states
[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_format.h"
26 #include "util/u_surface.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
43 SI_DECOMPRESS = SI_SAVE_FRAMEBUFFER | SI_DISABLE_RENDER_COND,
44
45 SI_COLOR_RESOLVE = SI_SAVE_FRAMEBUFFER
46 };
47
48 static void si_blitter_begin(struct pipe_context *ctx, enum si_blitter_op op)
49 {
50 struct si_context *sctx = (struct si_context *)ctx;
51
52 r600_suspend_nontimer_queries(&sctx->b);
53
54 util_blitter_save_blend(sctx->blitter, sctx->queued.named.blend);
55 util_blitter_save_depth_stencil_alpha(sctx->blitter, sctx->queued.named.dsa);
56 util_blitter_save_stencil_ref(sctx->blitter, &sctx->stencil_ref);
57 util_blitter_save_rasterizer(sctx->blitter, sctx->queued.named.rasterizer);
58 util_blitter_save_fragment_shader(sctx->blitter, sctx->ps_shader);
59 util_blitter_save_geometry_shader(sctx->blitter, sctx->gs_shader);
60 util_blitter_save_tessctrl_shader(sctx->blitter, sctx->tcs_shader);
61 util_blitter_save_tesseval_shader(sctx->blitter, sctx->tes_shader);
62 util_blitter_save_vertex_shader(sctx->blitter, sctx->vs_shader);
63 util_blitter_save_vertex_elements(sctx->blitter, sctx->vertex_elements);
64 if (sctx->queued.named.sample_mask) {
65 util_blitter_save_sample_mask(sctx->blitter,
66 sctx->queued.named.sample_mask->sample_mask);
67 }
68 if (sctx->queued.named.viewport[0]) {
69 util_blitter_save_viewport(sctx->blitter, &sctx->queued.named.viewport[0]->viewport);
70 }
71 util_blitter_save_scissor(sctx->blitter, &sctx->scissors.states[0]);
72 util_blitter_save_vertex_buffer_slot(sctx->blitter, sctx->vertex_buffer);
73 util_blitter_save_so_targets(sctx->blitter, sctx->b.streamout.num_targets,
74 (struct pipe_stream_output_target**)sctx->b.streamout.targets);
75
76 if (op & SI_SAVE_FRAMEBUFFER)
77 util_blitter_save_framebuffer(sctx->blitter, &sctx->framebuffer.state);
78
79 if (op & SI_SAVE_TEXTURES) {
80 util_blitter_save_fragment_sampler_states(
81 sctx->blitter, 2,
82 sctx->samplers[PIPE_SHADER_FRAGMENT].states.saved_states);
83
84 util_blitter_save_fragment_sampler_views(sctx->blitter, 2,
85 sctx->samplers[PIPE_SHADER_FRAGMENT].views.views);
86 }
87
88 if ((op & SI_DISABLE_RENDER_COND) && sctx->b.current_render_cond) {
89 util_blitter_save_render_condition(sctx->blitter,
90 sctx->b.current_render_cond,
91 sctx->b.current_render_cond_cond,
92 sctx->b.current_render_cond_mode);
93 }
94 }
95
96 static void si_blitter_end(struct pipe_context *ctx)
97 {
98 struct si_context *sctx = (struct si_context *)ctx;
99 r600_resume_nontimer_queries(&sctx->b);
100 }
101
102 static unsigned u_max_sample(struct pipe_resource *r)
103 {
104 return r->nr_samples ? r->nr_samples - 1 : 0;
105 }
106
107 static void si_blit_decompress_depth(struct pipe_context *ctx,
108 struct r600_texture *texture,
109 struct r600_texture *staging,
110 unsigned first_level, unsigned last_level,
111 unsigned first_layer, unsigned last_layer,
112 unsigned first_sample, unsigned last_sample)
113 {
114 struct si_context *sctx = (struct si_context *)ctx;
115 unsigned layer, level, sample, checked_last_layer, max_layer, max_sample;
116 float depth = 1.0f;
117 const struct util_format_description *desc;
118 struct r600_texture *flushed_depth_texture = staging ?
119 staging : texture->flushed_depth_texture;
120
121 if (!staging && !texture->dirty_level_mask)
122 return;
123
124 max_sample = u_max_sample(&texture->resource.b.b);
125
126 desc = util_format_description(flushed_depth_texture->resource.b.b.format);
127
128 if (util_format_has_depth(desc))
129 sctx->dbcb_depth_copy_enabled = true;
130 if (util_format_has_stencil(desc))
131 sctx->dbcb_stencil_copy_enabled = true;
132
133 assert(sctx->dbcb_depth_copy_enabled || sctx->dbcb_stencil_copy_enabled);
134
135 for (level = first_level; level <= last_level; level++) {
136 if (!staging && !(texture->dirty_level_mask & (1 << level)))
137 continue;
138
139 /* The smaller the mipmap level, the less layers there are
140 * as far as 3D textures are concerned. */
141 max_layer = util_max_layer(&texture->resource.b.b, level);
142 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
143
144 for (layer = first_layer; layer <= checked_last_layer; layer++) {
145 for (sample = first_sample; sample <= last_sample; sample++) {
146 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
147
148 sctx->dbcb_copy_sample = sample;
149 si_mark_atom_dirty(sctx, &sctx->db_render_state);
150
151 surf_tmpl.format = texture->resource.b.b.format;
152 surf_tmpl.u.tex.level = level;
153 surf_tmpl.u.tex.first_layer = layer;
154 surf_tmpl.u.tex.last_layer = layer;
155
156 zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
157
158 surf_tmpl.format = flushed_depth_texture->resource.b.b.format;
159 cbsurf = ctx->create_surface(ctx,
160 (struct pipe_resource*)flushed_depth_texture, &surf_tmpl);
161
162 si_blitter_begin(ctx, SI_DECOMPRESS);
163 util_blitter_custom_depth_stencil(sctx->blitter, zsurf, cbsurf, 1 << sample,
164 sctx->custom_dsa_flush, depth);
165 si_blitter_end(ctx);
166
167 pipe_surface_reference(&zsurf, NULL);
168 pipe_surface_reference(&cbsurf, NULL);
169 }
170 }
171
172 /* The texture will always be dirty if some layers aren't flushed.
173 * I don't think this case can occur though. */
174 if (!staging &&
175 first_layer == 0 && last_layer == max_layer &&
176 first_sample == 0 && last_sample == max_sample) {
177 texture->dirty_level_mask &= ~(1 << level);
178 }
179 }
180
181 sctx->dbcb_depth_copy_enabled = false;
182 sctx->dbcb_stencil_copy_enabled = false;
183 si_mark_atom_dirty(sctx, &sctx->db_render_state);
184 }
185
186 static void si_blit_decompress_depth_in_place(struct si_context *sctx,
187 struct r600_texture *texture,
188 unsigned first_level, unsigned last_level,
189 unsigned first_layer, unsigned last_layer)
190 {
191 struct pipe_surface *zsurf, surf_tmpl = {{0}};
192 unsigned layer, max_layer, checked_last_layer, level;
193
194 sctx->db_inplace_flush_enabled = true;
195 si_mark_atom_dirty(sctx, &sctx->db_render_state);
196
197 surf_tmpl.format = texture->resource.b.b.format;
198
199 for (level = first_level; level <= last_level; level++) {
200 if (!(texture->dirty_level_mask & (1 << level)))
201 continue;
202
203 surf_tmpl.u.tex.level = level;
204
205 /* The smaller the mipmap level, the less layers there are
206 * as far as 3D textures are concerned. */
207 max_layer = util_max_layer(&texture->resource.b.b, level);
208 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
209
210 for (layer = first_layer; layer <= checked_last_layer; layer++) {
211 surf_tmpl.u.tex.first_layer = layer;
212 surf_tmpl.u.tex.last_layer = layer;
213
214 zsurf = sctx->b.b.create_surface(&sctx->b.b, &texture->resource.b.b, &surf_tmpl);
215
216 si_blitter_begin(&sctx->b.b, SI_DECOMPRESS);
217 util_blitter_custom_depth_stencil(sctx->blitter, zsurf, NULL, ~0,
218 sctx->custom_dsa_flush,
219 1.0f);
220 si_blitter_end(&sctx->b.b);
221
222 pipe_surface_reference(&zsurf, NULL);
223 }
224
225 /* The texture will always be dirty if some layers aren't flushed.
226 * I don't think this case occurs often though. */
227 if (first_layer == 0 && last_layer == max_layer) {
228 texture->dirty_level_mask &= ~(1 << level);
229 }
230 }
231
232 sctx->db_inplace_flush_enabled = false;
233 si_mark_atom_dirty(sctx, &sctx->db_render_state);
234 }
235
236 void si_flush_depth_textures(struct si_context *sctx,
237 struct si_textures_info *textures)
238 {
239 unsigned i;
240 unsigned mask = textures->depth_texture_mask;
241
242 while (mask) {
243 struct pipe_sampler_view *view;
244 struct r600_texture *tex;
245
246 i = u_bit_scan(&mask);
247
248 view = textures->views.views[i];
249 assert(view);
250
251 tex = (struct r600_texture *)view->texture;
252 assert(tex->is_depth && !tex->is_flushing_texture);
253
254 si_blit_decompress_depth_in_place(sctx, tex,
255 view->u.tex.first_level, view->u.tex.last_level,
256 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
257 }
258 }
259
260 static void si_blit_decompress_color(struct pipe_context *ctx,
261 struct r600_texture *rtex,
262 unsigned first_level, unsigned last_level,
263 unsigned first_layer, unsigned last_layer)
264 {
265 struct si_context *sctx = (struct si_context *)ctx;
266 unsigned layer, level, checked_last_layer, max_layer;
267
268 if (!rtex->dirty_level_mask)
269 return;
270
271 for (level = first_level; level <= last_level; level++) {
272 if (!(rtex->dirty_level_mask & (1 << level)))
273 continue;
274
275 /* The smaller the mipmap level, the less layers there are
276 * as far as 3D textures are concerned. */
277 max_layer = util_max_layer(&rtex->resource.b.b, level);
278 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
279
280 for (layer = first_layer; layer <= checked_last_layer; layer++) {
281 struct pipe_surface *cbsurf, surf_tmpl;
282
283 surf_tmpl.format = rtex->resource.b.b.format;
284 surf_tmpl.u.tex.level = level;
285 surf_tmpl.u.tex.first_layer = layer;
286 surf_tmpl.u.tex.last_layer = layer;
287 cbsurf = ctx->create_surface(ctx, &rtex->resource.b.b, &surf_tmpl);
288
289 si_blitter_begin(ctx, SI_DECOMPRESS);
290 util_blitter_custom_color(sctx->blitter, cbsurf,
291 rtex->fmask.size ? sctx->custom_blend_decompress :
292 sctx->custom_blend_fastclear);
293 si_blitter_end(ctx);
294
295 pipe_surface_reference(&cbsurf, NULL);
296 }
297
298 /* The texture will always be dirty if some layers aren't flushed.
299 * I don't think this case occurs often though. */
300 if (first_layer == 0 && last_layer == max_layer) {
301 rtex->dirty_level_mask &= ~(1 << level);
302 }
303 }
304 }
305
306 void si_decompress_color_textures(struct si_context *sctx,
307 struct si_textures_info *textures)
308 {
309 unsigned i;
310 unsigned mask = textures->compressed_colortex_mask;
311
312 while (mask) {
313 struct pipe_sampler_view *view;
314 struct r600_texture *tex;
315
316 i = u_bit_scan(&mask);
317
318 view = textures->views.views[i];
319 assert(view);
320
321 tex = (struct r600_texture *)view->texture;
322 assert(tex->cmask.size || tex->fmask.size);
323
324 si_blit_decompress_color(&sctx->b.b, tex,
325 view->u.tex.first_level, view->u.tex.last_level,
326 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
327 }
328 }
329
330 static void si_clear(struct pipe_context *ctx, unsigned buffers,
331 const union pipe_color_union *color,
332 double depth, unsigned stencil)
333 {
334 struct si_context *sctx = (struct si_context *)ctx;
335 struct pipe_framebuffer_state *fb = &sctx->framebuffer.state;
336 struct pipe_surface *zsbuf = fb->zsbuf;
337 struct r600_texture *zstex =
338 zsbuf ? (struct r600_texture*)zsbuf->texture : NULL;
339
340 if (buffers & PIPE_CLEAR_COLOR) {
341 evergreen_do_fast_color_clear(&sctx->b, fb, &sctx->framebuffer.atom,
342 &buffers, color);
343 if (!buffers)
344 return; /* all buffers have been fast cleared */
345 }
346
347 if (buffers & PIPE_CLEAR_COLOR) {
348 int i;
349
350 /* These buffers cannot use fast clear, make sure to disable expansion. */
351 for (i = 0; i < fb->nr_cbufs; i++) {
352 struct r600_texture *tex;
353
354 /* If not clearing this buffer, skip. */
355 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
356 continue;
357
358 if (!fb->cbufs[i])
359 continue;
360
361 tex = (struct r600_texture *)fb->cbufs[i]->texture;
362 if (tex->fmask.size == 0)
363 tex->dirty_level_mask &= ~(1 << fb->cbufs[i]->u.tex.level);
364 }
365 }
366
367 if (buffers & PIPE_CLEAR_DEPTH &&
368 zstex && zstex->htile_buffer &&
369 zsbuf->u.tex.level == 0 &&
370 zsbuf->u.tex.first_layer == 0 &&
371 zsbuf->u.tex.last_layer == util_max_layer(&zstex->resource.b.b, 0)) {
372 /* Need to disable EXPCLEAR temporarily if clearing
373 * to a new value. */
374 if (zstex->depth_cleared && zstex->depth_clear_value != depth) {
375 sctx->db_depth_disable_expclear = true;
376 }
377
378 zstex->depth_clear_value = depth;
379 si_mark_atom_dirty(sctx, &sctx->framebuffer.atom); /* updates DB_DEPTH_CLEAR */
380 sctx->db_depth_clear = true;
381 si_mark_atom_dirty(sctx, &sctx->db_render_state);
382 }
383
384 si_blitter_begin(ctx, SI_CLEAR);
385 util_blitter_clear(sctx->blitter, fb->width, fb->height,
386 util_framebuffer_get_num_layers(fb),
387 buffers, color, depth, stencil);
388 si_blitter_end(ctx);
389
390 if (sctx->db_depth_clear) {
391 sctx->db_depth_clear = false;
392 sctx->db_depth_disable_expclear = false;
393 zstex->depth_cleared = true;
394 si_mark_atom_dirty(sctx, &sctx->db_render_state);
395 }
396 }
397
398 static void si_clear_render_target(struct pipe_context *ctx,
399 struct pipe_surface *dst,
400 const union pipe_color_union *color,
401 unsigned dstx, unsigned dsty,
402 unsigned width, unsigned height)
403 {
404 struct si_context *sctx = (struct si_context *)ctx;
405
406 si_blitter_begin(ctx, SI_CLEAR_SURFACE);
407 util_blitter_clear_render_target(sctx->blitter, dst, color,
408 dstx, dsty, width, height);
409 si_blitter_end(ctx);
410 }
411
412 static void si_clear_depth_stencil(struct pipe_context *ctx,
413 struct pipe_surface *dst,
414 unsigned clear_flags,
415 double depth,
416 unsigned stencil,
417 unsigned dstx, unsigned dsty,
418 unsigned width, unsigned height)
419 {
420 struct si_context *sctx = (struct si_context *)ctx;
421
422 si_blitter_begin(ctx, SI_CLEAR_SURFACE);
423 util_blitter_clear_depth_stencil(sctx->blitter, dst, clear_flags, depth, stencil,
424 dstx, dsty, width, height);
425 si_blitter_end(ctx);
426 }
427
428 /* Helper for decompressing a portion of a color or depth resource before
429 * blitting if any decompression is needed.
430 * The driver doesn't decompress resources automatically while u_blitter is
431 * rendering. */
432 static void si_decompress_subresource(struct pipe_context *ctx,
433 struct pipe_resource *tex,
434 unsigned level,
435 unsigned first_layer, unsigned last_layer)
436 {
437 struct si_context *sctx = (struct si_context *)ctx;
438 struct r600_texture *rtex = (struct r600_texture*)tex;
439
440 if (rtex->is_depth && !rtex->is_flushing_texture) {
441 si_blit_decompress_depth_in_place(sctx, rtex,
442 level, level,
443 first_layer, last_layer);
444 } else if (rtex->fmask.size || rtex->cmask.size) {
445 si_blit_decompress_color(ctx, rtex, level, level,
446 first_layer, last_layer);
447 }
448 }
449
450 struct texture_orig_info {
451 unsigned format;
452 unsigned width0;
453 unsigned height0;
454 unsigned npix_x;
455 unsigned npix_y;
456 unsigned npix0_x;
457 unsigned npix0_y;
458 };
459
460 void si_resource_copy_region(struct pipe_context *ctx,
461 struct pipe_resource *dst,
462 unsigned dst_level,
463 unsigned dstx, unsigned dsty, unsigned dstz,
464 struct pipe_resource *src,
465 unsigned src_level,
466 const struct pipe_box *src_box)
467 {
468 struct si_context *sctx = (struct si_context *)ctx;
469 struct pipe_surface *dst_view, dst_templ;
470 struct pipe_sampler_view src_templ, *src_view;
471 unsigned dst_width, dst_height, src_width0, src_height0;
472 unsigned src_force_level = 0;
473 struct pipe_box sbox, dstbox;
474
475 /* Handle buffers first. */
476 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
477 si_copy_buffer(sctx, dst, src, dstx, src_box->x, src_box->width, false);
478 return;
479 }
480
481 assert(u_max_sample(dst) == u_max_sample(src));
482
483 /* The driver doesn't decompress resources automatically while
484 * u_blitter is rendering. */
485 si_decompress_subresource(ctx, src, src_level,
486 src_box->z, src_box->z + src_box->depth - 1);
487
488 dst_width = u_minify(dst->width0, dst_level);
489 dst_height = u_minify(dst->height0, dst_level);
490 src_width0 = src->width0;
491 src_height0 = src->height0;
492
493 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
494 util_blitter_default_src_texture(&src_templ, src, src_level);
495
496 if (util_format_is_compressed(src->format) &&
497 util_format_is_compressed(dst->format)) {
498 unsigned blocksize = util_format_get_blocksize(src->format);
499
500 if (blocksize == 8)
501 src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
502 else
503 src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
504 dst_templ.format = src_templ.format;
505
506 dst_width = util_format_get_nblocksx(dst->format, dst_width);
507 dst_height = util_format_get_nblocksy(dst->format, dst_height);
508 src_width0 = util_format_get_nblocksx(src->format, src_width0);
509 src_height0 = util_format_get_nblocksy(src->format, src_height0);
510
511 dstx = util_format_get_nblocksx(dst->format, dstx);
512 dsty = util_format_get_nblocksy(dst->format, dsty);
513
514 sbox.x = util_format_get_nblocksx(src->format, src_box->x);
515 sbox.y = util_format_get_nblocksy(src->format, src_box->y);
516 sbox.z = src_box->z;
517 sbox.width = util_format_get_nblocksx(src->format, src_box->width);
518 sbox.height = util_format_get_nblocksy(src->format, src_box->height);
519 sbox.depth = src_box->depth;
520 src_box = &sbox;
521
522 src_force_level = src_level;
523 } else if (!util_blitter_is_copy_supported(sctx->blitter, dst, src) ||
524 /* also *8_SNORM has precision issues, use UNORM instead */
525 util_format_is_snorm(src->format)) {
526 if (util_format_is_subsampled_422(src->format)) {
527 src_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;
528 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;
529
530 dst_width = util_format_get_nblocksx(dst->format, dst_width);
531 src_width0 = util_format_get_nblocksx(src->format, src_width0);
532
533 dstx = util_format_get_nblocksx(dst->format, dstx);
534
535 sbox = *src_box;
536 sbox.x = util_format_get_nblocksx(src->format, src_box->x);
537 sbox.width = util_format_get_nblocksx(src->format, src_box->width);
538 src_box = &sbox;
539 } else {
540 unsigned blocksize = util_format_get_blocksize(src->format);
541
542 switch (blocksize) {
543 case 1:
544 dst_templ.format = PIPE_FORMAT_R8_UNORM;
545 src_templ.format = PIPE_FORMAT_R8_UNORM;
546 break;
547 case 2:
548 dst_templ.format = PIPE_FORMAT_R8G8_UNORM;
549 src_templ.format = PIPE_FORMAT_R8G8_UNORM;
550 break;
551 case 4:
552 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
553 src_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
554 break;
555 case 8:
556 dst_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
557 src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
558 break;
559 case 16:
560 dst_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;
561 src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;
562 break;
563 default:
564 fprintf(stderr, "Unhandled format %s with blocksize %u\n",
565 util_format_short_name(src->format), blocksize);
566 assert(0);
567 }
568 }
569 }
570
571 /* Initialize the surface. */
572 dst_view = r600_create_surface_custom(ctx, dst, &dst_templ,
573 dst_width, dst_height);
574
575 /* Initialize the sampler view. */
576 src_view = si_create_sampler_view_custom(ctx, src, &src_templ,
577 src_width0, src_height0,
578 src_force_level);
579
580 u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
581 abs(src_box->depth), &dstbox);
582
583 /* Copy. */
584 si_blitter_begin(ctx, SI_COPY);
585 util_blitter_blit_generic(sctx->blitter, dst_view, &dstbox,
586 src_view, src_box, src_width0, src_height0,
587 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
588 FALSE);
589 si_blitter_end(ctx);
590
591 pipe_surface_reference(&dst_view, NULL);
592 pipe_sampler_view_reference(&src_view, NULL);
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 (!dst->cmask.size || !dst->dirty_level_mask) /* dst cannot be fast-cleared */) {
665 si_blitter_begin(ctx, SI_COLOR_RESOLVE |
666 (info->render_condition_enable ? 0 : SI_DISABLE_RENDER_COND));
667 util_blitter_custom_resolve_color(sctx->blitter,
668 info->dst.resource, info->dst.level,
669 info->dst.box.z,
670 info->src.resource, info->src.box.z,
671 sample_mask, sctx->custom_blend_resolve,
672 format);
673 si_blitter_end(ctx);
674 return true;
675 }
676 return false;
677 }
678
679 static void si_blit(struct pipe_context *ctx,
680 const struct pipe_blit_info *info)
681 {
682 struct si_context *sctx = (struct si_context*)ctx;
683
684 if (do_hardware_msaa_resolve(ctx, info)) {
685 return;
686 }
687
688 assert(util_blitter_is_blit_supported(sctx->blitter, info));
689
690 /* The driver doesn't decompress resources automatically while
691 * u_blitter is rendering. */
692 si_decompress_subresource(ctx, info->src.resource, info->src.level,
693 info->src.box.z,
694 info->src.box.z + info->src.box.depth - 1);
695
696 if (sctx->screen->b.debug_flags & DBG_FORCE_DMA &&
697 util_try_blit_via_copy_region(ctx, info))
698 return;
699
700 si_blitter_begin(ctx, SI_BLIT |
701 (info->render_condition_enable ? 0 : SI_DISABLE_RENDER_COND));
702 util_blitter_blit(sctx->blitter, info);
703 si_blitter_end(ctx);
704 }
705
706 static void si_flush_resource(struct pipe_context *ctx,
707 struct pipe_resource *res)
708 {
709 struct r600_texture *rtex = (struct r600_texture*)res;
710
711 assert(res->target != PIPE_BUFFER);
712
713 if (!rtex->is_depth && rtex->cmask.size) {
714 si_blit_decompress_color(ctx, rtex, 0, res->last_level,
715 0, util_max_layer(res, 0));
716 }
717 }
718
719 void si_init_blit_functions(struct si_context *sctx)
720 {
721 sctx->b.b.clear = si_clear;
722 sctx->b.b.clear_render_target = si_clear_render_target;
723 sctx->b.b.clear_depth_stencil = si_clear_depth_stencil;
724 sctx->b.b.resource_copy_region = si_resource_copy_region;
725 sctx->b.b.blit = si_blit;
726 sctx->b.b.flush_resource = si_flush_resource;
727 sctx->b.blit_decompress_depth = si_blit_decompress_depth;
728 }