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