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