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