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