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