a19248da3a2c4955b8f22b4c4540559964f884c5
[mesa.git] / src / gallium / drivers / r600 / r600_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 #include "r600_pipe.h"
24 #include "util/u_surface.h"
25 #include "util/u_blitter.h"
26 #include "util/u_format.h"
27
28 enum r600_blitter_op /* bitmask */
29 {
30 R600_SAVE_FRAGMENT_STATE = 1,
31 R600_SAVE_TEXTURES = 2,
32 R600_SAVE_FRAMEBUFFER = 4,
33 R600_DISABLE_RENDER_COND = 8,
34
35 R600_CLEAR = R600_SAVE_FRAGMENT_STATE,
36
37 R600_CLEAR_SURFACE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER,
38
39 R600_COPY_BUFFER = R600_DISABLE_RENDER_COND,
40
41 R600_COPY_TEXTURE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |
42 R600_DISABLE_RENDER_COND,
43
44 R600_BLIT = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |
45 R600_DISABLE_RENDER_COND,
46
47 R600_DECOMPRESS = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_DISABLE_RENDER_COND,
48
49 R600_COLOR_RESOLVE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_DISABLE_RENDER_COND
50 };
51
52 static void r600_blitter_begin(struct pipe_context *ctx, enum r600_blitter_op op)
53 {
54 struct r600_context *rctx = (struct r600_context *)ctx;
55
56 r600_suspend_nontimer_queries(rctx);
57
58 util_blitter_save_vertex_buffers(rctx->blitter,
59 util_last_bit(rctx->vertex_buffer_state.enabled_mask),
60 rctx->vertex_buffer_state.vb);
61 util_blitter_save_vertex_elements(rctx->blitter, rctx->vertex_fetch_shader.cso);
62 util_blitter_save_vertex_shader(rctx->blitter, rctx->vs_shader);
63 util_blitter_save_so_targets(rctx->blitter, rctx->num_so_targets,
64 (struct pipe_stream_output_target**)rctx->so_targets);
65 util_blitter_save_rasterizer(rctx->blitter, rctx->rasterizer_state.cso);
66
67 if (op & R600_SAVE_FRAGMENT_STATE) {
68 util_blitter_save_viewport(rctx->blitter, &rctx->viewport.state);
69 util_blitter_save_scissor(rctx->blitter, &rctx->scissor.scissor);
70 util_blitter_save_fragment_shader(rctx->blitter, rctx->ps_shader);
71 util_blitter_save_blend(rctx->blitter, rctx->blend_state.cso);
72 util_blitter_save_depth_stencil_alpha(rctx->blitter, rctx->dsa_state.cso);
73 util_blitter_save_stencil_ref(rctx->blitter, &rctx->stencil_ref.pipe_state);
74 util_blitter_save_sample_mask(rctx->blitter, rctx->sample_mask.sample_mask);
75 }
76
77 if (op & R600_SAVE_FRAMEBUFFER)
78 util_blitter_save_framebuffer(rctx->blitter, &rctx->framebuffer.state);
79
80 if (op & R600_SAVE_TEXTURES) {
81 util_blitter_save_fragment_sampler_states(
82 rctx->blitter, util_last_bit(rctx->samplers[PIPE_SHADER_FRAGMENT].states.enabled_mask),
83 (void**)rctx->samplers[PIPE_SHADER_FRAGMENT].states.states);
84
85 util_blitter_save_fragment_sampler_views(
86 rctx->blitter, util_last_bit(rctx->samplers[PIPE_SHADER_FRAGMENT].views.enabled_mask),
87 (struct pipe_sampler_view**)rctx->samplers[PIPE_SHADER_FRAGMENT].views.views);
88 }
89
90 if ((op & R600_DISABLE_RENDER_COND) && rctx->current_render_cond) {
91 util_blitter_save_render_condition(rctx->blitter,
92 rctx->current_render_cond,
93 rctx->current_render_cond_mode);
94 }
95 }
96
97 static void r600_blitter_end(struct pipe_context *ctx)
98 {
99 struct r600_context *rctx = (struct r600_context *)ctx;
100 r600_resume_nontimer_queries(rctx);
101 }
102
103 static unsigned u_max_layer(struct pipe_resource *r, unsigned level)
104 {
105 switch (r->target) {
106 case PIPE_TEXTURE_CUBE:
107 return 6 - 1;
108 case PIPE_TEXTURE_3D:
109 return u_minify(r->depth0, level) - 1;
110 case PIPE_TEXTURE_1D_ARRAY:
111 case PIPE_TEXTURE_2D_ARRAY:
112 return r->array_size - 1;
113 default:
114 return 0;
115 }
116 }
117
118 static unsigned u_max_sample(struct pipe_resource *r)
119 {
120 return r->nr_samples ? r->nr_samples - 1 : 0;
121 }
122
123 void r600_blit_decompress_depth(struct pipe_context *ctx,
124 struct r600_texture *texture,
125 struct r600_texture *staging,
126 unsigned first_level, unsigned last_level,
127 unsigned first_layer, unsigned last_layer,
128 unsigned first_sample, unsigned last_sample)
129 {
130 struct r600_context *rctx = (struct r600_context *)ctx;
131 unsigned layer, level, sample, checked_last_layer, max_layer, max_sample;
132 struct r600_texture *flushed_depth_texture = staging ?
133 staging : texture->flushed_depth_texture;
134 const struct util_format_description *desc =
135 util_format_description(texture->resource.b.b.format);
136 float depth;
137
138 if (!staging && !texture->dirty_level_mask)
139 return;
140
141 max_sample = u_max_sample(&texture->resource.b.b);
142
143 /* XXX Decompressing MSAA depth textures is broken on R6xx.
144 * There is also a hardlock if CMASK and FMASK are not present.
145 * Just skip this until we find out how to fix it. */
146 if (rctx->chip_class == R600 && max_sample > 0) {
147 texture->dirty_level_mask = 0;
148 return;
149 }
150
151 if (rctx->family == CHIP_RV610 || rctx->family == CHIP_RV630 ||
152 rctx->family == CHIP_RV620 || rctx->family == CHIP_RV635)
153 depth = 0.0f;
154 else
155 depth = 1.0f;
156
157 /* Enable decompression in DB_RENDER_CONTROL */
158 rctx->db_misc_state.flush_depthstencil_through_cb = true;
159 rctx->db_misc_state.copy_depth = util_format_has_depth(desc);
160 rctx->db_misc_state.copy_stencil = util_format_has_stencil(desc);
161 rctx->db_misc_state.copy_sample = first_sample;
162 rctx->db_misc_state.atom.dirty = true;
163
164
165 for (level = first_level; level <= last_level; level++) {
166 if (!staging && !(texture->dirty_level_mask & (1 << level)))
167 continue;
168
169 /* The smaller the mipmap level, the less layers there are
170 * as far as 3D textures are concerned. */
171 max_layer = u_max_layer(&texture->resource.b.b, level);
172 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
173
174 for (layer = first_layer; layer <= checked_last_layer; layer++) {
175 for (sample = first_sample; sample <= last_sample; sample++) {
176 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
177
178 if (sample != rctx->db_misc_state.copy_sample) {
179 rctx->db_misc_state.copy_sample = sample;
180 rctx->db_misc_state.atom.dirty = true;
181 }
182
183 surf_tmpl.format = texture->resource.b.b.format;
184 surf_tmpl.u.tex.level = level;
185 surf_tmpl.u.tex.first_layer = layer;
186 surf_tmpl.u.tex.last_layer = layer;
187 surf_tmpl.usage = PIPE_BIND_DEPTH_STENCIL;
188
189 zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
190
191 surf_tmpl.format = flushed_depth_texture->resource.b.b.format;
192 surf_tmpl.u.tex.level = level;
193 surf_tmpl.u.tex.first_layer = layer;
194 surf_tmpl.u.tex.last_layer = layer;
195 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
196 cbsurf = ctx->create_surface(ctx,
197 &flushed_depth_texture->resource.b.b, &surf_tmpl);
198
199 r600_blitter_begin(ctx, R600_DECOMPRESS);
200 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, 1 << sample,
201 rctx->custom_dsa_flush, depth);
202 r600_blitter_end(ctx);
203
204 pipe_surface_reference(&zsurf, NULL);
205 pipe_surface_reference(&cbsurf, NULL);
206 }
207 }
208
209 /* The texture will always be dirty if some layers or samples aren't flushed.
210 * I don't think this case occurs often though. */
211 if (!staging &&
212 first_layer == 0 && last_layer == max_layer &&
213 first_sample == 0 && last_sample == max_sample) {
214 texture->dirty_level_mask &= ~(1 << level);
215 }
216 }
217
218 /* reenable compression in DB_RENDER_CONTROL */
219 rctx->db_misc_state.flush_depthstencil_through_cb = false;
220 rctx->db_misc_state.atom.dirty = true;
221 }
222
223 void r600_decompress_depth_textures(struct r600_context *rctx,
224 struct r600_samplerview_state *textures)
225 {
226 unsigned i;
227 unsigned depth_texture_mask = textures->compressed_depthtex_mask;
228
229 while (depth_texture_mask) {
230 struct pipe_sampler_view *view;
231 struct r600_texture *tex;
232
233 i = u_bit_scan(&depth_texture_mask);
234
235 view = &textures->views[i]->base;
236 assert(view);
237
238 tex = (struct r600_texture *)view->texture;
239 assert(tex->is_depth && !tex->is_flushing_texture);
240
241 r600_blit_decompress_depth(&rctx->context, tex, NULL,
242 view->u.tex.first_level, view->u.tex.last_level,
243 0, u_max_layer(&tex->resource.b.b, view->u.tex.first_level),
244 0, u_max_sample(&tex->resource.b.b));
245 }
246 }
247
248 static void r600_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 r600_context *rctx = (struct r600_context *)ctx;
254 unsigned layer, level, checked_last_layer, max_layer;
255 void *blend_decompress;
256
257 if (!rtex->dirty_level_mask)
258 return;
259
260 switch (rctx->screen->msaa_texture_support) {
261 case MSAA_TEXTURE_DECOMPRESSED:
262 blend_decompress = rctx->custom_blend_decompress;
263 break;
264 case MSAA_TEXTURE_COMPRESSED:
265 /* XXX the 2x and 4x cases are broken. */
266 if (rtex->resource.b.b.nr_samples == 8)
267 blend_decompress = rctx->custom_blend_fmask_decompress;
268 else
269 blend_decompress = rctx->custom_blend_decompress;
270 break;
271 case MSAA_TEXTURE_SAMPLE_ZERO:
272 default:
273 /* Nothing to do. */
274 rtex->dirty_level_mask = 0;
275 return;
276 }
277
278 for (level = first_level; level <= last_level; level++) {
279 if (!(rtex->dirty_level_mask & (1 << level)))
280 continue;
281
282 /* The smaller the mipmap level, the less layers there are
283 * as far as 3D textures are concerned. */
284 max_layer = u_max_layer(&rtex->resource.b.b, level);
285 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
286
287 for (layer = first_layer; layer <= checked_last_layer; layer++) {
288 struct pipe_surface *cbsurf, surf_tmpl;
289
290 surf_tmpl.format = rtex->resource.b.b.format;
291 surf_tmpl.u.tex.level = level;
292 surf_tmpl.u.tex.first_layer = layer;
293 surf_tmpl.u.tex.last_layer = layer;
294 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
295 cbsurf = ctx->create_surface(ctx, &rtex->resource.b.b, &surf_tmpl);
296
297 r600_blitter_begin(ctx, R600_DECOMPRESS);
298 util_blitter_custom_color(rctx->blitter, cbsurf, blend_decompress);
299 r600_blitter_end(ctx);
300
301 pipe_surface_reference(&cbsurf, NULL);
302 }
303
304 /* The texture will always be dirty if some layers or samples aren't flushed.
305 * I don't think this case occurs often though. */
306 if (first_layer == 0 && last_layer == max_layer) {
307 rtex->dirty_level_mask &= ~(1 << level);
308 }
309 }
310 }
311
312 void r600_decompress_color_textures(struct r600_context *rctx,
313 struct r600_samplerview_state *textures)
314 {
315 unsigned i;
316 unsigned mask = textures->compressed_colortex_mask;
317
318 while (mask) {
319 struct pipe_sampler_view *view;
320 struct r600_texture *tex;
321
322 i = u_bit_scan(&mask);
323
324 view = &textures->views[i]->base;
325 assert(view);
326
327 tex = (struct r600_texture *)view->texture;
328 assert(tex->cmask_size && tex->fmask_size);
329
330 r600_blit_decompress_color(&rctx->context, tex,
331 view->u.tex.first_level, view->u.tex.last_level,
332 0, u_max_layer(&tex->resource.b.b, view->u.tex.first_level));
333 }
334 }
335
336 /* Helper for decompressing a portion of a color or depth resource before
337 * blitting if any decompression is needed.
338 * The driver doesn't decompress resources automatically while u_blitter is
339 * rendering. */
340 static bool r600_decompress_subresource(struct pipe_context *ctx,
341 struct pipe_resource *tex,
342 unsigned level,
343 unsigned first_layer, unsigned last_layer)
344 {
345 struct r600_texture *rtex = (struct r600_texture*)tex;
346
347 if (rtex->is_depth && !rtex->is_flushing_texture) {
348 if (!r600_init_flushed_depth_texture(ctx, tex, NULL))
349 return false; /* error */
350
351 r600_blit_decompress_depth(ctx, rtex, NULL,
352 level, level,
353 first_layer, last_layer,
354 0, u_max_sample(tex));
355 } else if (rtex->fmask_size && rtex->cmask_size) {
356 r600_blit_decompress_color(ctx, rtex, level, level,
357 first_layer, last_layer);
358 }
359 return true;
360 }
361
362 static boolean is_simple_msaa_resolve(const struct pipe_blit_info *info)
363 {
364 unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
365 unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
366
367 return info->dst.resource->format == info->src.resource->format &&
368 info->dst.resource->format == info->dst.format &&
369 info->src.resource->format == info->src.format &&
370 !info->scissor_enable &&
371 info->mask == PIPE_MASK_RGBA &&
372 dst_width == info->src.resource->width0 &&
373 dst_height == info->src.resource->height0 &&
374 info->dst.box.x == 0 &&
375 info->dst.box.y == 0 &&
376 info->dst.box.width == dst_width &&
377 info->dst.box.height == dst_height &&
378 info->src.box.x == 0 &&
379 info->src.box.y == 0 &&
380 info->src.box.width == dst_width &&
381 info->src.box.height == dst_height;
382 }
383
384 static void r600_clear(struct pipe_context *ctx, unsigned buffers,
385 const union pipe_color_union *color,
386 double depth, unsigned stencil)
387 {
388 struct r600_context *rctx = (struct r600_context *)ctx;
389 struct pipe_framebuffer_state *fb = &rctx->framebuffer.state;
390
391 r600_blitter_begin(ctx, R600_CLEAR);
392 util_blitter_clear(rctx->blitter, fb->width, fb->height,
393 fb->nr_cbufs, buffers, fb->nr_cbufs ? fb->cbufs[0]->format : PIPE_FORMAT_NONE,
394 color, depth, stencil);
395 r600_blitter_end(ctx);
396 }
397
398 static void r600_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 r600_context *rctx = (struct r600_context *)ctx;
405
406 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
407 util_blitter_clear_render_target(rctx->blitter, dst, color,
408 dstx, dsty, width, height);
409 r600_blitter_end(ctx);
410 }
411
412 static void r600_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 r600_context *rctx = (struct r600_context *)ctx;
421
422 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
423 util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
424 dstx, dsty, width, height);
425 r600_blitter_end(ctx);
426 }
427
428 void r600_copy_buffer(struct pipe_context *ctx, struct
429 pipe_resource *dst, unsigned dstx,
430 struct pipe_resource *src, const struct pipe_box *src_box)
431 {
432 struct r600_context *rctx = (struct r600_context*)ctx;
433
434 if (rctx->screen->has_streamout &&
435 /* Require dword alignment. */
436 dstx % 4 == 0 && src_box->x % 4 == 0 && src_box->width % 4 == 0) {
437 r600_blitter_begin(ctx, R600_COPY_BUFFER);
438 util_blitter_copy_buffer(rctx->blitter, dst, dstx, src, src_box->x, src_box->width);
439 r600_blitter_end(ctx);
440 } else {
441 util_resource_copy_region(ctx, dst, 0, dstx, 0, 0, src, 0, src_box);
442 }
443 }
444
445 static bool util_format_is_subsampled_2x1_32bpp(enum pipe_format format)
446 {
447 const struct util_format_description *desc = util_format_description(format);
448
449 return desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED &&
450 desc->block.width == 2 &&
451 desc->block.height == 1 &&
452 desc->block.bits == 32;
453 }
454
455 static void r600_resource_copy_region(struct pipe_context *ctx,
456 struct pipe_resource *dst,
457 unsigned dst_level,
458 unsigned dstx, unsigned dsty, unsigned dstz,
459 struct pipe_resource *src,
460 unsigned src_level,
461 const struct pipe_box *src_box)
462 {
463 struct r600_context *rctx = (struct r600_context *)ctx;
464 struct r600_texture *rsrc = (struct r600_texture*)src;
465 struct r600_texture *rdst = (struct r600_texture*)dst;
466 struct pipe_surface *dst_view, dst_templ;
467 struct pipe_sampler_view src_templ, *src_view;
468 unsigned dst_width, dst_height, src_width0, src_height0, src_widthFL, src_heightFL;
469 struct pipe_box sbox;
470 bool copy_all_samples;
471
472 /* Handle buffers first. */
473 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
474 r600_copy_buffer(ctx, dst, dstx, src, src_box);
475 return;
476 }
477
478 assert(u_max_sample(dst) == u_max_sample(src));
479
480 /* The driver doesn't decompress resources automatically while
481 * u_blitter is rendering. */
482 if (!r600_decompress_subresource(ctx, src, src_level,
483 src_box->z, src_box->z + src_box->depth - 1)) {
484 return; /* error */
485 }
486
487 dst_width = rdst->surface.level[dst_level].npix_x;
488 dst_height = rdst->surface.level[dst_level].npix_y;
489 src_width0 = src->width0;
490 src_height0 = src->height0;
491 src_widthFL = rsrc->surface.level[src_level].npix_x;
492 src_heightFL = rsrc->surface.level[src_level].npix_y;
493
494 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz, src_box);
495 util_blitter_default_src_texture(&src_templ, src, src_level);
496
497 if (util_format_is_compressed(src->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, rdst->surface.level[dst_level].npix_x);
507 dst_height = util_format_get_nblocksy(dst->format, rdst->surface.level[dst_level].npix_y);
508 src_width0 = util_format_get_nblocksx(src->format, src->width0);
509 src_height0 = util_format_get_nblocksy(src->format, src->height0);
510 src_widthFL = util_format_get_nblocksx(src->format, rsrc->surface.level[src_level].npix_x);
511 src_heightFL = util_format_get_nblocksy(src->format, rsrc->surface.level[src_level].npix_y);
512
513 dstx = util_format_get_nblocksx(dst->format, dstx);
514 dsty = util_format_get_nblocksy(dst->format, dsty);
515
516 sbox.x = util_format_get_nblocksx(src->format, src_box->x);
517 sbox.y = util_format_get_nblocksy(src->format, src_box->y);
518 sbox.z = src_box->z;
519 sbox.width = util_format_get_nblocksx(src->format, src_box->width);
520 sbox.height = util_format_get_nblocksy(src->format, src_box->height);
521 sbox.depth = src_box->depth;
522 src_box = &sbox;
523 } else if (!util_blitter_is_copy_supported(rctx->blitter, dst, src,
524 PIPE_MASK_RGBAZS)) {
525 if (util_format_is_subsampled_2x1_32bpp(src->format)) {
526
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, rdst->surface.level[dst_level].npix_x);
531 src_width0 = util_format_get_nblocksx(src->format, src->width0);
532 src_widthFL = util_format_get_nblocksx(src->format, rsrc->surface.level[src_level].npix_x);
533
534 dstx = util_format_get_nblocksx(dst->format, dstx);
535
536 sbox = *src_box;
537 sbox.x = util_format_get_nblocksx(src->format, src_box->x);
538 sbox.width = util_format_get_nblocksx(src->format, src_box->width);
539 src_box = &sbox;
540 } else {
541 unsigned blocksize = util_format_get_blocksize(src->format);
542
543 switch (blocksize) {
544 case 1:
545 dst_templ.format = PIPE_FORMAT_R8_UNORM;
546 src_templ.format = PIPE_FORMAT_R8_UNORM;
547 break;
548 case 4:
549 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
550 src_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
551 break;
552 default:
553 fprintf(stderr, "Unhandled format %s with blocksize %u\n",
554 util_format_short_name(src->format), blocksize);
555 assert(0);
556 }
557 }
558 }
559
560 dst_view = r600_create_surface_custom(ctx, dst, &dst_templ, dst_width, dst_height);
561
562 if (rctx->chip_class >= EVERGREEN) {
563 src_view = evergreen_create_sampler_view_custom(ctx, src, &src_templ,
564 src_width0, src_height0);
565 } else {
566 src_view = r600_create_sampler_view_custom(ctx, src, &src_templ,
567 src_widthFL, src_heightFL);
568 }
569
570 copy_all_samples = rctx->screen->msaa_texture_support != MSAA_TEXTURE_SAMPLE_ZERO;
571
572 /* Copy. */
573 r600_blitter_begin(ctx, R600_COPY_TEXTURE);
574 util_blitter_blit_generic(rctx->blitter, dst_view, dstx, dsty,
575 abs(src_box->width), abs(src_box->height),
576 src_view, src_box, src_width0, src_height0,
577 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
578 copy_all_samples);
579 r600_blitter_end(ctx);
580
581 pipe_surface_reference(&dst_view, NULL);
582 pipe_sampler_view_reference(&src_view, NULL);
583 }
584
585 /* For MSAA integer resolving to work, we change the format to NORM using this function. */
586 static enum pipe_format int_to_norm_format(enum pipe_format format)
587 {
588 switch (format) {
589 #define REPLACE_FORMAT_SIGN(format,sign) \
590 case PIPE_FORMAT_##format##_##sign##INT: \
591 return PIPE_FORMAT_##format##_##sign##NORM
592 #define REPLACE_FORMAT(format) \
593 REPLACE_FORMAT_SIGN(format, U); \
594 REPLACE_FORMAT_SIGN(format, S)
595
596 REPLACE_FORMAT_SIGN(B10G10R10A2, U);
597 REPLACE_FORMAT(R8);
598 REPLACE_FORMAT(R8G8);
599 REPLACE_FORMAT(R8G8B8);
600 REPLACE_FORMAT(R8G8B8A8);
601 REPLACE_FORMAT(A8);
602 REPLACE_FORMAT(I8);
603 REPLACE_FORMAT(L8);
604 REPLACE_FORMAT(L8A8);
605 REPLACE_FORMAT(R16);
606 REPLACE_FORMAT(R16G16);
607 REPLACE_FORMAT(R16G16B16);
608 REPLACE_FORMAT(R16G16B16A16);
609 REPLACE_FORMAT(A16);
610 REPLACE_FORMAT(I16);
611 REPLACE_FORMAT(L16);
612 REPLACE_FORMAT(L16A16);
613
614 #undef REPLACE_FORMAT
615 #undef REPLACE_FORMAT_SIGN
616 default:
617 return format;
618 }
619 }
620
621 static void r600_msaa_color_resolve(struct pipe_context *ctx,
622 const struct pipe_blit_info *info)
623 {
624 struct r600_context *rctx = (struct r600_context *)ctx;
625 struct pipe_screen *screen = ctx->screen;
626 struct pipe_resource *tmp, templ;
627 struct pipe_blit_info blit;
628 unsigned sample_mask =
629 rctx->chip_class == CAYMAN ? ~0 :
630 ((1ull << MAX2(1, info->src.resource->nr_samples)) - 1);
631
632 assert(info->src.level == 0);
633 assert(info->src.box.depth == 1);
634 assert(info->dst.box.depth == 1);
635
636 if (is_simple_msaa_resolve(info)) {
637 r600_blitter_begin(ctx, R600_COLOR_RESOLVE);
638 util_blitter_custom_resolve_color(rctx->blitter,
639 info->dst.resource, info->dst.level,
640 info->dst.box.z,
641 info->src.resource, info->src.box.z,
642 sample_mask, rctx->custom_blend_resolve,
643 int_to_norm_format(info->dst.format));
644 r600_blitter_end(ctx);
645 return;
646 }
647
648 /* resolve into a temporary texture, then blit */
649 templ.target = PIPE_TEXTURE_2D;
650 templ.format = info->src.resource->format;
651 templ.width0 = info->src.resource->width0;
652 templ.height0 = info->src.resource->height0;
653 templ.depth0 = 1;
654 templ.array_size = 1;
655 templ.last_level = 0;
656 templ.nr_samples = 0;
657 templ.usage = PIPE_USAGE_STATIC;
658 templ.bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
659 templ.flags = 0;
660
661 tmp = screen->resource_create(screen, &templ);
662
663 /* resolve */
664 r600_blitter_begin(ctx, R600_COLOR_RESOLVE);
665 util_blitter_custom_resolve_color(rctx->blitter,
666 tmp, 0, 0,
667 info->src.resource, info->src.box.z,
668 sample_mask, rctx->custom_blend_resolve,
669 int_to_norm_format(tmp->format));
670 r600_blitter_end(ctx);
671
672 /* blit */
673 blit = *info;
674 blit.src.resource = tmp;
675 blit.src.box.z = 0;
676
677 r600_blitter_begin(ctx, R600_BLIT);
678 util_blitter_blit(rctx->blitter, &blit);
679 r600_blitter_end(ctx);
680
681 pipe_resource_reference(&tmp, NULL);
682 }
683
684 static void r600_blit(struct pipe_context *ctx,
685 const struct pipe_blit_info *info)
686 {
687 struct r600_context *rctx = (struct r600_context*)ctx;
688
689 assert(util_blitter_is_blit_supported(rctx->blitter, info));
690
691 if (info->src.resource->nr_samples > 1 &&
692 info->dst.resource->nr_samples <= 1 &&
693 !util_format_is_depth_or_stencil(info->src.resource->format) &&
694 !util_format_is_pure_integer(int_to_norm_format(info->src.resource->format))) {
695 r600_msaa_color_resolve(ctx, info);
696 return;
697 }
698
699 /* The driver doesn't decompress resources automatically while
700 * u_blitter is rendering. */
701 if (!r600_decompress_subresource(ctx, info->src.resource, info->src.level,
702 info->src.box.z,
703 info->src.box.z + info->src.box.depth - 1)) {
704 return; /* error */
705 }
706
707 r600_blitter_begin(ctx, R600_BLIT);
708 util_blitter_blit(rctx->blitter, info);
709 r600_blitter_end(ctx);
710 }
711
712 void r600_init_blit_functions(struct r600_context *rctx)
713 {
714 rctx->context.clear = r600_clear;
715 rctx->context.clear_render_target = r600_clear_render_target;
716 rctx->context.clear_depth_stencil = r600_clear_depth_stencil;
717 rctx->context.resource_copy_region = r600_resource_copy_region;
718 rctx->context.blit = r600_blit;
719 }