gallium: add support for LODQ opcodes.
[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.state);
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 rtex->fmask.size ? sctx->custom_blend_decompress :
281 sctx->custom_blend_fastclear);
282 si_blitter_end(ctx);
283
284 pipe_surface_reference(&cbsurf, NULL);
285 }
286
287 /* The texture will always be dirty if some layers aren't flushed.
288 * I don't think this case occurs often though. */
289 if (first_layer == 0 && last_layer == max_layer) {
290 rtex->dirty_level_mask &= ~(1 << level);
291 }
292 }
293 }
294
295 void si_decompress_color_textures(struct si_context *sctx,
296 struct si_textures_info *textures)
297 {
298 unsigned i;
299 unsigned mask = textures->compressed_colortex_mask;
300
301 while (mask) {
302 struct pipe_sampler_view *view;
303 struct r600_texture *tex;
304
305 i = u_bit_scan(&mask);
306
307 view = textures->views.views[i];
308 assert(view);
309
310 tex = (struct r600_texture *)view->texture;
311 assert(tex->cmask.size || tex->fmask.size);
312
313 si_blit_decompress_color(&sctx->b.b, tex,
314 view->u.tex.first_level, view->u.tex.last_level,
315 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
316 }
317 }
318
319 static void si_clear(struct pipe_context *ctx, unsigned buffers,
320 const union pipe_color_union *color,
321 double depth, unsigned stencil)
322 {
323 struct si_context *sctx = (struct si_context *)ctx;
324 struct pipe_framebuffer_state *fb = &sctx->framebuffer.state;
325
326 if (buffers & PIPE_CLEAR_COLOR) {
327 evergreen_do_fast_color_clear(&sctx->b, fb, &sctx->framebuffer.atom,
328 &buffers, color);
329 }
330
331 if (buffers & PIPE_CLEAR_COLOR) {
332 int i;
333
334 /* These buffers cannot use fast clear, make sure to disable expansion. */
335 for (i = 0; i < fb->nr_cbufs; i++) {
336 struct r600_texture *tex;
337
338 /* If not clearing this buffer, skip. */
339 if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
340 continue;
341
342 if (!fb->cbufs[i])
343 continue;
344
345 tex = (struct r600_texture *)fb->cbufs[i]->texture;
346 if (tex->fmask.size == 0)
347 tex->dirty_level_mask &= ~(1 << fb->cbufs[i]->u.tex.level);
348 }
349 }
350
351 si_blitter_begin(ctx, SI_CLEAR);
352 util_blitter_clear(sctx->blitter, fb->width, fb->height,
353 util_framebuffer_get_num_layers(fb),
354 buffers, color, depth, stencil);
355 si_blitter_end(ctx);
356 }
357
358 static void si_clear_render_target(struct pipe_context *ctx,
359 struct pipe_surface *dst,
360 const union pipe_color_union *color,
361 unsigned dstx, unsigned dsty,
362 unsigned width, unsigned height)
363 {
364 struct si_context *sctx = (struct si_context *)ctx;
365
366 si_blitter_begin(ctx, SI_CLEAR_SURFACE);
367 util_blitter_clear_render_target(sctx->blitter, dst, color,
368 dstx, dsty, width, height);
369 si_blitter_end(ctx);
370 }
371
372 static void si_clear_depth_stencil(struct pipe_context *ctx,
373 struct pipe_surface *dst,
374 unsigned clear_flags,
375 double depth,
376 unsigned stencil,
377 unsigned dstx, unsigned dsty,
378 unsigned width, unsigned height)
379 {
380 struct si_context *sctx = (struct si_context *)ctx;
381
382 si_blitter_begin(ctx, SI_CLEAR_SURFACE);
383 util_blitter_clear_depth_stencil(sctx->blitter, dst, clear_flags, depth, stencil,
384 dstx, dsty, width, height);
385 si_blitter_end(ctx);
386 }
387
388 /* Helper for decompressing a portion of a color or depth resource before
389 * blitting if any decompression is needed.
390 * The driver doesn't decompress resources automatically while u_blitter is
391 * rendering. */
392 static void si_decompress_subresource(struct pipe_context *ctx,
393 struct pipe_resource *tex,
394 unsigned level,
395 unsigned first_layer, unsigned last_layer)
396 {
397 struct si_context *sctx = (struct si_context *)ctx;
398 struct r600_texture *rtex = (struct r600_texture*)tex;
399
400 if (rtex->is_depth && !rtex->is_flushing_texture) {
401 si_blit_decompress_depth_in_place(sctx, rtex,
402 level, level,
403 first_layer, last_layer);
404 } else if (rtex->fmask.size || rtex->cmask.size) {
405 si_blit_decompress_color(ctx, rtex, level, level,
406 first_layer, last_layer);
407 }
408 }
409
410 struct texture_orig_info {
411 unsigned format;
412 unsigned width0;
413 unsigned height0;
414 unsigned npix_x;
415 unsigned npix_y;
416 unsigned npix0_x;
417 unsigned npix0_y;
418 };
419
420 static void si_compressed_to_blittable(struct pipe_resource *tex,
421 unsigned level,
422 struct texture_orig_info *orig)
423 {
424 struct r600_texture *rtex = (struct r600_texture*)tex;
425 unsigned pixsize = util_format_get_blocksize(rtex->resource.b.b.format);
426 int new_format;
427 int new_height, new_width;
428
429 orig->format = tex->format;
430 orig->width0 = tex->width0;
431 orig->height0 = tex->height0;
432 orig->npix0_x = rtex->surface.level[0].npix_x;
433 orig->npix0_y = rtex->surface.level[0].npix_y;
434 orig->npix_x = rtex->surface.level[level].npix_x;
435 orig->npix_y = rtex->surface.level[level].npix_y;
436
437 if (pixsize == 8)
438 new_format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
439 else
440 new_format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
441
442 new_width = util_format_get_nblocksx(tex->format, orig->width0);
443 new_height = util_format_get_nblocksy(tex->format, orig->height0);
444
445 tex->width0 = new_width;
446 tex->height0 = new_height;
447 tex->format = new_format;
448 rtex->surface.level[0].npix_x = util_format_get_nblocksx(orig->format, orig->npix0_x);
449 rtex->surface.level[0].npix_y = util_format_get_nblocksy(orig->format, orig->npix0_y);
450 rtex->surface.level[level].npix_x = util_format_get_nblocksx(orig->format, orig->npix_x);
451 rtex->surface.level[level].npix_y = util_format_get_nblocksy(orig->format, orig->npix_y);
452
453 /* By dividing the dimensions by 4, we effectively decrement
454 * last_level by 2, therefore the last 2 mipmap levels disappear and
455 * aren't blittable. Note that the last 3 mipmap levels (4x4, 2x2,
456 * 1x1) have equal slice sizes, which is an important assumption
457 * for this to work.
458 *
459 * In order to make the last 2 mipmap levels blittable, we have to
460 * add the slice size of the last mipmap level to the texture
461 * address, so that even though the hw thinks it reads last_level-2,
462 * it will actually read last_level-1, and if we add the slice size*2,
463 * it will read last_level. That's how this workaround works.
464 */
465 if (level > rtex->resource.b.b.last_level-2)
466 rtex->mipmap_shift = level - (rtex->resource.b.b.last_level-2);
467 }
468
469 static void si_change_format(struct pipe_resource *tex,
470 unsigned level,
471 struct texture_orig_info *orig,
472 enum pipe_format format)
473 {
474 struct r600_texture *rtex = (struct r600_texture*)tex;
475
476 orig->format = tex->format;
477 orig->width0 = tex->width0;
478 orig->height0 = tex->height0;
479 orig->npix0_x = rtex->surface.level[0].npix_x;
480 orig->npix0_y = rtex->surface.level[0].npix_y;
481 orig->npix_x = rtex->surface.level[level].npix_x;
482 orig->npix_y = rtex->surface.level[level].npix_y;
483
484 tex->format = format;
485 }
486
487 static void si_reset_blittable_to_orig(struct pipe_resource *tex,
488 unsigned level,
489 struct texture_orig_info *orig)
490 {
491 struct r600_texture *rtex = (struct r600_texture*)tex;
492
493 tex->format = orig->format;
494 tex->width0 = orig->width0;
495 tex->height0 = orig->height0;
496 rtex->surface.level[0].npix_x = orig->npix0_x;
497 rtex->surface.level[0].npix_y = orig->npix0_y;
498 rtex->surface.level[level].npix_x = orig->npix_x;
499 rtex->surface.level[level].npix_y = orig->npix_y;
500 rtex->mipmap_shift = 0;
501 }
502
503 static void si_resource_copy_region(struct pipe_context *ctx,
504 struct pipe_resource *dst,
505 unsigned dst_level,
506 unsigned dstx, unsigned dsty, unsigned dstz,
507 struct pipe_resource *src,
508 unsigned src_level,
509 const struct pipe_box *src_box)
510 {
511 struct si_context *sctx = (struct si_context *)ctx;
512 struct r600_texture *rdst = (struct r600_texture*)dst;
513 struct pipe_surface *dst_view, dst_templ;
514 struct pipe_sampler_view src_templ, *src_view;
515 struct texture_orig_info orig_info[2];
516 struct pipe_box sbox, dstbox;
517 boolean restore_orig[2];
518
519 /* Fallback for buffers. */
520 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
521 si_copy_buffer(sctx, dst, src, dstx, src_box->x, src_box->width);
522 return;
523 }
524
525 memset(orig_info, 0, sizeof(orig_info));
526
527 /* The driver doesn't decompress resources automatically while
528 * u_blitter is rendering. */
529 si_decompress_subresource(ctx, src, src_level,
530 src_box->z, src_box->z + src_box->depth - 1);
531
532 restore_orig[0] = restore_orig[1] = FALSE;
533
534 if (util_format_is_compressed(src->format) &&
535 util_format_is_compressed(dst->format)) {
536 si_compressed_to_blittable(src, src_level, &orig_info[0]);
537 restore_orig[0] = TRUE;
538 sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
539 sbox.y = util_format_get_nblocksy(orig_info[0].format, src_box->y);
540 sbox.z = src_box->z;
541 sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
542 sbox.height = util_format_get_nblocksy(orig_info[0].format, src_box->height);
543 sbox.depth = src_box->depth;
544 src_box = &sbox;
545
546 si_compressed_to_blittable(dst, dst_level, &orig_info[1]);
547 restore_orig[1] = TRUE;
548 /* translate the dst box as well */
549 dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
550 dsty = util_format_get_nblocksy(orig_info[1].format, dsty);
551 } else if (!util_blitter_is_copy_supported(sctx->blitter, dst, src)) {
552 unsigned blocksize = util_format_get_blocksize(src->format);
553
554 switch (blocksize) {
555 case 1:
556 si_change_format(src, src_level, &orig_info[0],
557 PIPE_FORMAT_R8_UNORM);
558 si_change_format(dst, dst_level, &orig_info[1],
559 PIPE_FORMAT_R8_UNORM);
560 break;
561 case 2:
562 si_change_format(src, src_level, &orig_info[0],
563 PIPE_FORMAT_R8G8_UNORM);
564 si_change_format(dst, dst_level, &orig_info[1],
565 PIPE_FORMAT_R8G8_UNORM);
566 break;
567 case 4:
568 si_change_format(src, src_level, &orig_info[0],
569 PIPE_FORMAT_R8G8B8A8_UNORM);
570 si_change_format(dst, dst_level, &orig_info[1],
571 PIPE_FORMAT_R8G8B8A8_UNORM);
572 break;
573 case 8:
574 si_change_format(src, src_level, &orig_info[0],
575 PIPE_FORMAT_R16G16B16A16_UINT);
576 si_change_format(dst, dst_level, &orig_info[1],
577 PIPE_FORMAT_R16G16B16A16_UINT);
578 break;
579 case 16:
580 si_change_format(src, src_level, &orig_info[0],
581 PIPE_FORMAT_R32G32B32A32_UINT);
582 si_change_format(dst, dst_level, &orig_info[1],
583 PIPE_FORMAT_R32G32B32A32_UINT);
584 break;
585 default:
586 fprintf(stderr, "Unhandled format %s with blocksize %u\n",
587 util_format_short_name(src->format), blocksize);
588 assert(0);
589 }
590 restore_orig[0] = TRUE;
591 restore_orig[1] = TRUE;
592 }
593
594 /* Initialize the surface. */
595 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
596 dst_view = r600_create_surface_custom(ctx, dst, &dst_templ,
597 rdst->surface.level[dst_level].npix_x,
598 rdst->surface.level[dst_level].npix_y);
599
600 /* Initialize the sampler view. */
601 util_blitter_default_src_texture(&src_templ, src, src_level);
602 src_view = ctx->create_sampler_view(ctx, src, &src_templ);
603
604 u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
605 abs(src_box->depth), &dstbox);
606
607 /* Copy. */
608 si_blitter_begin(ctx, SI_COPY);
609 util_blitter_blit_generic(sctx->blitter, dst_view, &dstbox,
610 src_view, src_box, src->width0, src->height0,
611 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL);
612 si_blitter_end(ctx);
613
614 pipe_surface_reference(&dst_view, NULL);
615 pipe_sampler_view_reference(&src_view, NULL);
616
617 if (restore_orig[0])
618 si_reset_blittable_to_orig(src, src_level, &orig_info[0]);
619
620 if (restore_orig[1])
621 si_reset_blittable_to_orig(dst, dst_level, &orig_info[1]);
622 }
623
624 /* For MSAA integer resolving to work, we change the format to NORM using this function. */
625 static enum pipe_format int_to_norm_format(enum pipe_format format)
626 {
627 switch (format) {
628 #define REPLACE_FORMAT_SIGN(format,sign) \
629 case PIPE_FORMAT_##format##_##sign##INT: \
630 return PIPE_FORMAT_##format##_##sign##NORM
631 #define REPLACE_FORMAT(format) \
632 REPLACE_FORMAT_SIGN(format, U); \
633 REPLACE_FORMAT_SIGN(format, S)
634
635 REPLACE_FORMAT_SIGN(B10G10R10A2, U);
636 REPLACE_FORMAT(R8);
637 REPLACE_FORMAT(R8G8);
638 REPLACE_FORMAT(R8G8B8X8);
639 REPLACE_FORMAT(R8G8B8A8);
640 REPLACE_FORMAT(A8);
641 REPLACE_FORMAT(I8);
642 REPLACE_FORMAT(L8);
643 REPLACE_FORMAT(L8A8);
644 REPLACE_FORMAT(R16);
645 REPLACE_FORMAT(R16G16);
646 REPLACE_FORMAT(R16G16B16X16);
647 REPLACE_FORMAT(R16G16B16A16);
648 REPLACE_FORMAT(A16);
649 REPLACE_FORMAT(I16);
650 REPLACE_FORMAT(L16);
651 REPLACE_FORMAT(L16A16);
652
653 #undef REPLACE_FORMAT
654 #undef REPLACE_FORMAT_SIGN
655 default:
656 return format;
657 }
658 }
659
660 static bool do_hardware_msaa_resolve(struct pipe_context *ctx,
661 const struct pipe_blit_info *info)
662 {
663 struct si_context *sctx = (struct si_context*)ctx;
664 struct r600_texture *dst = (struct r600_texture*)info->dst.resource;
665 unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
666 unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
667 enum pipe_format format = int_to_norm_format(info->dst.format);
668 unsigned sample_mask = ~0;
669
670 if (info->src.resource->nr_samples > 1 &&
671 info->dst.resource->nr_samples <= 1 &&
672 util_max_layer(info->src.resource, 0) == 0 &&
673 util_max_layer(info->dst.resource, info->dst.level) == 0 &&
674 info->dst.format == info->src.format &&
675 !util_format_is_pure_integer(format) &&
676 !util_format_is_depth_or_stencil(format) &&
677 !info->scissor_enable &&
678 (info->mask & PIPE_MASK_RGBA) == PIPE_MASK_RGBA &&
679 dst_width == info->src.resource->width0 &&
680 dst_height == info->src.resource->height0 &&
681 info->dst.box.x == 0 &&
682 info->dst.box.y == 0 &&
683 info->dst.box.width == dst_width &&
684 info->dst.box.height == dst_height &&
685 info->dst.box.depth == 1 &&
686 info->src.box.x == 0 &&
687 info->src.box.y == 0 &&
688 info->src.box.width == dst_width &&
689 info->src.box.height == dst_height &&
690 info->src.box.depth == 1 &&
691 dst->surface.level[info->dst.level].mode >= RADEON_SURF_MODE_1D &&
692 !(dst->surface.flags & RADEON_SURF_SCANOUT)) {
693 si_blitter_begin(ctx, SI_COLOR_RESOLVE);
694 util_blitter_custom_resolve_color(sctx->blitter,
695 info->dst.resource, info->dst.level,
696 info->dst.box.z,
697 info->src.resource, info->src.box.z,
698 sample_mask, sctx->custom_blend_resolve,
699 format);
700 si_blitter_end(ctx);
701 return true;
702 }
703 return false;
704 }
705
706 static void si_blit(struct pipe_context *ctx,
707 const struct pipe_blit_info *info)
708 {
709 struct si_context *sctx = (struct si_context*)ctx;
710
711 if (do_hardware_msaa_resolve(ctx, info)) {
712 return;
713 }
714
715 assert(util_blitter_is_blit_supported(sctx->blitter, info));
716
717 /* The driver doesn't decompress resources automatically while
718 * u_blitter is rendering. */
719 si_decompress_subresource(ctx, info->src.resource, info->src.level,
720 info->src.box.z,
721 info->src.box.z + info->src.box.depth - 1);
722
723 si_blitter_begin(ctx, SI_BLIT);
724 util_blitter_blit(sctx->blitter, info);
725 si_blitter_end(ctx);
726 }
727
728 static void si_flush_resource(struct pipe_context *ctx,
729 struct pipe_resource *res)
730 {
731 struct r600_texture *rtex = (struct r600_texture*)res;
732
733 assert(res->target != PIPE_BUFFER);
734
735 if (!rtex->is_depth && rtex->cmask.size) {
736 si_blit_decompress_color(ctx, rtex, 0, res->last_level,
737 0, res->array_size - 1);
738 }
739 }
740
741 void si_init_blit_functions(struct si_context *sctx)
742 {
743 sctx->b.b.clear = si_clear;
744 sctx->b.b.clear_render_target = si_clear_render_target;
745 sctx->b.b.clear_depth_stencil = si_clear_depth_stencil;
746 sctx->b.b.resource_copy_region = si_resource_copy_region;
747 sctx->b.b.blit = si_blit;
748 sctx->b.b.flush_resource = si_flush_resource;
749 sctx->b.blit_decompress_depth = si_blit_decompress_depth;
750 }