si_blit.c revision af69d88d
1/*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#include "si_pipe.h"
25#include "util/u_format.h"
26
27enum si_blitter_op /* bitmask */
28{
29	SI_SAVE_TEXTURES      = 1,
30	SI_SAVE_FRAMEBUFFER   = 2,
31	SI_DISABLE_RENDER_COND = 4,
32
33	SI_CLEAR         = 0,
34
35	SI_CLEAR_SURFACE = SI_SAVE_FRAMEBUFFER,
36
37	SI_COPY          = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES |
38			   SI_DISABLE_RENDER_COND,
39
40	SI_BLIT          = SI_SAVE_FRAMEBUFFER | SI_SAVE_TEXTURES,
41
42	SI_DECOMPRESS    = SI_SAVE_FRAMEBUFFER | SI_DISABLE_RENDER_COND,
43
44	SI_COLOR_RESOLVE = SI_SAVE_FRAMEBUFFER
45};
46
47static void si_blitter_begin(struct pipe_context *ctx, enum si_blitter_op op)
48{
49	struct si_context *sctx = (struct si_context *)ctx;
50
51	r600_suspend_nontimer_queries(&sctx->b);
52
53	util_blitter_save_blend(sctx->blitter, sctx->queued.named.blend);
54	util_blitter_save_depth_stencil_alpha(sctx->blitter, sctx->queued.named.dsa);
55	util_blitter_save_stencil_ref(sctx->blitter, &sctx->stencil_ref);
56	util_blitter_save_rasterizer(sctx->blitter, sctx->queued.named.rasterizer);
57	util_blitter_save_fragment_shader(sctx->blitter, sctx->ps_shader);
58	util_blitter_save_geometry_shader(sctx->blitter, sctx->gs_shader);
59	util_blitter_save_vertex_shader(sctx->blitter, sctx->vs_shader);
60	util_blitter_save_vertex_elements(sctx->blitter, sctx->vertex_elements);
61	if (sctx->queued.named.sample_mask) {
62		util_blitter_save_sample_mask(sctx->blitter,
63					      sctx->queued.named.sample_mask->sample_mask);
64	}
65	if (sctx->queued.named.viewport) {
66		util_blitter_save_viewport(sctx->blitter, &sctx->queued.named.viewport->viewport);
67	}
68	if (sctx->queued.named.scissor) {
69		util_blitter_save_scissor(sctx->blitter, &sctx->queued.named.scissor->scissor);
70	}
71	util_blitter_save_vertex_buffer_slot(sctx->blitter, sctx->vertex_buffer);
72	util_blitter_save_so_targets(sctx->blitter, sctx->b.streamout.num_targets,
73				     (struct pipe_stream_output_target**)sctx->b.streamout.targets);
74
75	if (op & SI_SAVE_FRAMEBUFFER)
76		util_blitter_save_framebuffer(sctx->blitter, &sctx->framebuffer.state);
77
78	if (op & SI_SAVE_TEXTURES) {
79		util_blitter_save_fragment_sampler_states(
80			sctx->blitter, 2,
81			sctx->samplers[PIPE_SHADER_FRAGMENT].states.saved_states);
82
83		util_blitter_save_fragment_sampler_views(sctx->blitter, 2,
84			sctx->samplers[PIPE_SHADER_FRAGMENT].views.views);
85	}
86
87	if ((op & SI_DISABLE_RENDER_COND) && sctx->b.current_render_cond) {
88		util_blitter_save_render_condition(sctx->blitter,
89                                                   sctx->b.current_render_cond,
90                                                   sctx->b.current_render_cond_cond,
91                                                   sctx->b.current_render_cond_mode);
92	}
93}
94
95static void si_blitter_end(struct pipe_context *ctx)
96{
97	struct si_context *sctx = (struct si_context *)ctx;
98	r600_resume_nontimer_queries(&sctx->b);
99}
100
101static unsigned u_max_sample(struct pipe_resource *r)
102{
103	return r->nr_samples ? r->nr_samples - 1 : 0;
104}
105
106static void si_blit_decompress_depth(struct pipe_context *ctx,
107				     struct r600_texture *texture,
108				     struct r600_texture *staging,
109				     unsigned first_level, unsigned last_level,
110				     unsigned first_layer, unsigned last_layer,
111				     unsigned first_sample, unsigned last_sample)
112{
113	struct si_context *sctx = (struct si_context *)ctx;
114	unsigned layer, level, sample, checked_last_layer, max_layer, max_sample;
115	float depth = 1.0f;
116	const struct util_format_description *desc;
117	void **custom_dsa;
118	struct r600_texture *flushed_depth_texture = staging ?
119			staging : texture->flushed_depth_texture;
120
121	if (!staging && !texture->dirty_level_mask)
122		return;
123
124	max_sample = u_max_sample(&texture->resource.b.b);
125
126	desc = util_format_description(flushed_depth_texture->resource.b.b.format);
127	switch (util_format_has_depth(desc) | util_format_has_stencil(desc) << 1) {
128	default:
129		assert(!"No depth or stencil to uncompress");
130		return;
131	case 3:
132		custom_dsa = sctx->custom_dsa_flush_depth_stencil;
133		break;
134	case 2:
135		custom_dsa = sctx->custom_dsa_flush_stencil;
136		break;
137	case 1:
138		custom_dsa = sctx->custom_dsa_flush_depth;
139		break;
140	}
141
142	for (level = first_level; level <= last_level; level++) {
143		if (!staging && !(texture->dirty_level_mask & (1 << level)))
144			continue;
145
146		/* The smaller the mipmap level, the less layers there are
147		 * as far as 3D textures are concerned. */
148		max_layer = util_max_layer(&texture->resource.b.b, level);
149		checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
150
151		for (layer = first_layer; layer <= checked_last_layer; layer++) {
152			for (sample = first_sample; sample <= last_sample; sample++) {
153				struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
154
155				surf_tmpl.format = texture->resource.b.b.format;
156				surf_tmpl.u.tex.level = level;
157				surf_tmpl.u.tex.first_layer = layer;
158				surf_tmpl.u.tex.last_layer = layer;
159
160				zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
161
162				surf_tmpl.format = flushed_depth_texture->resource.b.b.format;
163				cbsurf = ctx->create_surface(ctx,
164						(struct pipe_resource*)flushed_depth_texture, &surf_tmpl);
165
166				si_blitter_begin(ctx, SI_DECOMPRESS);
167				util_blitter_custom_depth_stencil(sctx->blitter, zsurf, cbsurf, 1 << sample,
168								  custom_dsa[sample], depth);
169				si_blitter_end(ctx);
170
171				pipe_surface_reference(&zsurf, NULL);
172				pipe_surface_reference(&cbsurf, NULL);
173			}
174		}
175
176		/* The texture will always be dirty if some layers aren't flushed.
177		 * I don't think this case can occur though. */
178		if (!staging &&
179		    first_layer == 0 && last_layer == max_layer &&
180		    first_sample == 0 && last_sample == max_sample) {
181			texture->dirty_level_mask &= ~(1 << level);
182		}
183	}
184}
185
186static void si_blit_decompress_depth_in_place(struct si_context *sctx,
187                                              struct r600_texture *texture,
188                                              unsigned first_level, unsigned last_level,
189                                              unsigned first_layer, unsigned last_layer)
190{
191	struct pipe_surface *zsurf, surf_tmpl = {{0}};
192	unsigned layer, max_layer, checked_last_layer, level;
193
194	surf_tmpl.format = texture->resource.b.b.format;
195
196	for (level = first_level; level <= last_level; level++) {
197		if (!(texture->dirty_level_mask & (1 << level)))
198			continue;
199
200		surf_tmpl.u.tex.level = level;
201
202		/* The smaller the mipmap level, the less layers there are
203		 * as far as 3D textures are concerned. */
204		max_layer = util_max_layer(&texture->resource.b.b, level);
205		checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
206
207		for (layer = first_layer; layer <= checked_last_layer; layer++) {
208			surf_tmpl.u.tex.first_layer = layer;
209			surf_tmpl.u.tex.last_layer = layer;
210
211			zsurf = sctx->b.b.create_surface(&sctx->b.b, &texture->resource.b.b, &surf_tmpl);
212
213			si_blitter_begin(&sctx->b.b, SI_DECOMPRESS);
214			util_blitter_custom_depth_stencil(sctx->blitter, zsurf, NULL, ~0,
215							  sctx->custom_dsa_flush_inplace,
216							  1.0f);
217			si_blitter_end(&sctx->b.b);
218
219			pipe_surface_reference(&zsurf, NULL);
220		}
221
222		/* The texture will always be dirty if some layers aren't flushed.
223		 * I don't think this case occurs often though. */
224		if (first_layer == 0 && last_layer == max_layer) {
225			texture->dirty_level_mask &= ~(1 << level);
226		}
227	}
228}
229
230void si_flush_depth_textures(struct si_context *sctx,
231			     struct si_textures_info *textures)
232{
233	unsigned i;
234	unsigned mask = textures->depth_texture_mask;
235
236	while (mask) {
237		struct pipe_sampler_view *view;
238		struct r600_texture *tex;
239
240		i = u_bit_scan(&mask);
241
242		view = textures->views.views[i];
243		assert(view);
244
245		tex = (struct r600_texture *)view->texture;
246		assert(tex->is_depth && !tex->is_flushing_texture);
247
248		si_blit_decompress_depth_in_place(sctx, tex,
249						  view->u.tex.first_level, view->u.tex.last_level,
250						  0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
251	}
252}
253
254static void si_blit_decompress_color(struct pipe_context *ctx,
255		struct r600_texture *rtex,
256		unsigned first_level, unsigned last_level,
257		unsigned first_layer, unsigned last_layer)
258{
259	struct si_context *sctx = (struct si_context *)ctx;
260	unsigned layer, level, checked_last_layer, max_layer;
261
262	if (!rtex->dirty_level_mask)
263		return;
264
265	for (level = first_level; level <= last_level; level++) {
266		if (!(rtex->dirty_level_mask & (1 << level)))
267			continue;
268
269		/* The smaller the mipmap level, the less layers there are
270		 * as far as 3D textures are concerned. */
271		max_layer = util_max_layer(&rtex->resource.b.b, level);
272		checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
273
274		for (layer = first_layer; layer <= checked_last_layer; layer++) {
275			struct pipe_surface *cbsurf, surf_tmpl;
276
277			surf_tmpl.format = rtex->resource.b.b.format;
278			surf_tmpl.u.tex.level = level;
279			surf_tmpl.u.tex.first_layer = layer;
280			surf_tmpl.u.tex.last_layer = layer;
281			cbsurf = ctx->create_surface(ctx, &rtex->resource.b.b, &surf_tmpl);
282
283			si_blitter_begin(ctx, SI_DECOMPRESS);
284			util_blitter_custom_color(sctx->blitter, cbsurf,
285				rtex->fmask.size ? sctx->custom_blend_decompress :
286						   sctx->custom_blend_fastclear);
287			si_blitter_end(ctx);
288
289			pipe_surface_reference(&cbsurf, NULL);
290		}
291
292		/* The texture will always be dirty if some layers aren't flushed.
293		 * I don't think this case occurs often though. */
294		if (first_layer == 0 && last_layer == max_layer) {
295			rtex->dirty_level_mask &= ~(1 << level);
296		}
297	}
298}
299
300void si_decompress_color_textures(struct si_context *sctx,
301				  struct si_textures_info *textures)
302{
303	unsigned i;
304	unsigned mask = textures->compressed_colortex_mask;
305
306	while (mask) {
307		struct pipe_sampler_view *view;
308		struct r600_texture *tex;
309
310		i = u_bit_scan(&mask);
311
312		view = textures->views.views[i];
313		assert(view);
314
315		tex = (struct r600_texture *)view->texture;
316		assert(tex->cmask.size || tex->fmask.size);
317
318		si_blit_decompress_color(&sctx->b.b, tex,
319					 view->u.tex.first_level, view->u.tex.last_level,
320					 0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
321	}
322}
323
324static void si_clear(struct pipe_context *ctx, unsigned buffers,
325		     const union pipe_color_union *color,
326		     double depth, unsigned stencil)
327{
328	struct si_context *sctx = (struct si_context *)ctx;
329	struct pipe_framebuffer_state *fb = &sctx->framebuffer.state;
330
331	if (buffers & PIPE_CLEAR_COLOR) {
332		evergreen_do_fast_color_clear(&sctx->b, fb, &sctx->framebuffer.atom,
333					      &buffers, color);
334	}
335
336	if (buffers & PIPE_CLEAR_COLOR) {
337		int i;
338
339		/* These buffers cannot use fast clear, make sure to disable expansion. */
340		for (i = 0; i < fb->nr_cbufs; i++) {
341			struct r600_texture *tex;
342
343			/* If not clearing this buffer, skip. */
344			if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
345				continue;
346
347			if (!fb->cbufs[i])
348				continue;
349
350			tex = (struct r600_texture *)fb->cbufs[i]->texture;
351			if (tex->fmask.size == 0)
352				tex->dirty_level_mask &= ~(1 << fb->cbufs[i]->u.tex.level);
353		}
354	}
355
356	si_blitter_begin(ctx, SI_CLEAR);
357	util_blitter_clear(sctx->blitter, fb->width, fb->height,
358			   util_framebuffer_get_num_layers(fb),
359			   buffers, color, depth, stencil);
360	si_blitter_end(ctx);
361}
362
363static void si_clear_render_target(struct pipe_context *ctx,
364				   struct pipe_surface *dst,
365				   const union pipe_color_union *color,
366				   unsigned dstx, unsigned dsty,
367				   unsigned width, unsigned height)
368{
369	struct si_context *sctx = (struct si_context *)ctx;
370
371	si_blitter_begin(ctx, SI_CLEAR_SURFACE);
372	util_blitter_clear_render_target(sctx->blitter, dst, color,
373					 dstx, dsty, width, height);
374	si_blitter_end(ctx);
375}
376
377static void si_clear_depth_stencil(struct pipe_context *ctx,
378				   struct pipe_surface *dst,
379				   unsigned clear_flags,
380				   double depth,
381				   unsigned stencil,
382				   unsigned dstx, unsigned dsty,
383				   unsigned width, unsigned height)
384{
385	struct si_context *sctx = (struct si_context *)ctx;
386
387	si_blitter_begin(ctx, SI_CLEAR_SURFACE);
388	util_blitter_clear_depth_stencil(sctx->blitter, dst, clear_flags, depth, stencil,
389					 dstx, dsty, width, height);
390	si_blitter_end(ctx);
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. */
397static void si_decompress_subresource(struct pipe_context *ctx,
398				      struct pipe_resource *tex,
399				      unsigned level,
400				      unsigned first_layer, unsigned last_layer)
401{
402	struct si_context *sctx = (struct si_context *)ctx;
403	struct r600_texture *rtex = (struct r600_texture*)tex;
404
405	if (rtex->is_depth && !rtex->is_flushing_texture) {
406		si_blit_decompress_depth_in_place(sctx, rtex,
407						  level, level,
408						  first_layer, last_layer);
409	} else if (rtex->fmask.size || rtex->cmask.size) {
410		si_blit_decompress_color(ctx, rtex, level, level,
411					 first_layer, last_layer);
412	}
413}
414
415struct texture_orig_info {
416	unsigned format;
417	unsigned width0;
418	unsigned height0;
419	unsigned npix_x;
420	unsigned npix_y;
421	unsigned npix0_x;
422	unsigned npix0_y;
423};
424
425static void si_compressed_to_blittable(struct pipe_resource *tex,
426				       unsigned level,
427				       struct texture_orig_info *orig)
428{
429	struct r600_texture *rtex = (struct r600_texture*)tex;
430	unsigned pixsize = util_format_get_blocksize(rtex->resource.b.b.format);
431	int new_format;
432	int new_height, new_width;
433
434	orig->format = tex->format;
435	orig->width0 = tex->width0;
436	orig->height0 = tex->height0;
437	orig->npix0_x = rtex->surface.level[0].npix_x;
438	orig->npix0_y = rtex->surface.level[0].npix_y;
439	orig->npix_x = rtex->surface.level[level].npix_x;
440	orig->npix_y = rtex->surface.level[level].npix_y;
441
442	if (pixsize == 8)
443		new_format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
444	else
445		new_format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
446
447	new_width = util_format_get_nblocksx(tex->format, orig->width0);
448	new_height = util_format_get_nblocksy(tex->format, orig->height0);
449
450	tex->width0 = new_width;
451	tex->height0 = new_height;
452	tex->format = new_format;
453	rtex->surface.level[0].npix_x = util_format_get_nblocksx(orig->format, orig->npix0_x);
454	rtex->surface.level[0].npix_y = util_format_get_nblocksy(orig->format, orig->npix0_y);
455	rtex->surface.level[level].npix_x = util_format_get_nblocksx(orig->format, orig->npix_x);
456	rtex->surface.level[level].npix_y = util_format_get_nblocksy(orig->format, orig->npix_y);
457
458	/* By dividing the dimensions by 4, we effectively decrement
459	 * last_level by 2, therefore the last 2 mipmap levels disappear and
460	 * aren't blittable. Note that the last 3 mipmap levels (4x4, 2x2,
461	 * 1x1) have equal slice sizes, which is an important assumption
462	 * for this to work.
463	 *
464	 * In order to make the last 2 mipmap levels blittable, we have to
465	 * add the slice size of the last mipmap level to the texture
466	 * address, so that even though the hw thinks it reads last_level-2,
467	 * it will actually read last_level-1, and if we add the slice size*2,
468	 * it will read last_level. That's how this workaround works.
469	 */
470	if (level > rtex->resource.b.b.last_level-2)
471		rtex->mipmap_shift = level - (rtex->resource.b.b.last_level-2);
472}
473
474static void si_change_format(struct pipe_resource *tex,
475			     unsigned level,
476			     struct texture_orig_info *orig,
477			     enum pipe_format format)
478{
479	struct r600_texture *rtex = (struct r600_texture*)tex;
480
481	orig->format = tex->format;
482	orig->width0 = tex->width0;
483	orig->height0 = tex->height0;
484	orig->npix0_x = rtex->surface.level[0].npix_x;
485	orig->npix0_y = rtex->surface.level[0].npix_y;
486	orig->npix_x = rtex->surface.level[level].npix_x;
487	orig->npix_y = rtex->surface.level[level].npix_y;
488
489	tex->format = format;
490}
491
492static void si_reset_blittable_to_orig(struct pipe_resource *tex,
493				       unsigned level,
494				       struct texture_orig_info *orig)
495{
496	struct r600_texture *rtex = (struct r600_texture*)tex;
497
498	tex->format = orig->format;
499	tex->width0 = orig->width0;
500	tex->height0 = orig->height0;
501	rtex->surface.level[0].npix_x = orig->npix0_x;
502	rtex->surface.level[0].npix_y = orig->npix0_y;
503	rtex->surface.level[level].npix_x = orig->npix_x;
504	rtex->surface.level[level].npix_y = orig->npix_y;
505	rtex->mipmap_shift = 0;
506}
507
508static void si_resource_copy_region(struct pipe_context *ctx,
509				    struct pipe_resource *dst,
510				    unsigned dst_level,
511				    unsigned dstx, unsigned dsty, unsigned dstz,
512				    struct pipe_resource *src,
513				    unsigned src_level,
514				    const struct pipe_box *src_box)
515{
516	struct si_context *sctx = (struct si_context *)ctx;
517	struct r600_texture *rdst = (struct r600_texture*)dst;
518	struct pipe_surface *dst_view, dst_templ;
519	struct pipe_sampler_view src_templ, *src_view;
520	struct texture_orig_info orig_info[2];
521	struct pipe_box sbox, dstbox;
522	boolean restore_orig[2];
523
524	/* Fallback for buffers. */
525	if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
526		si_copy_buffer(sctx, dst, src, dstx, src_box->x, src_box->width);
527		return;
528	}
529
530	memset(orig_info, 0, sizeof(orig_info));
531
532	/* The driver doesn't decompress resources automatically while
533	 * u_blitter is rendering. */
534	si_decompress_subresource(ctx, src, src_level,
535				  src_box->z, src_box->z + src_box->depth - 1);
536
537	restore_orig[0] = restore_orig[1] = FALSE;
538
539	if (util_format_is_compressed(src->format) &&
540	    util_format_is_compressed(dst->format)) {
541		si_compressed_to_blittable(src, src_level, &orig_info[0]);
542		restore_orig[0] = TRUE;
543		sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
544		sbox.y = util_format_get_nblocksy(orig_info[0].format, src_box->y);
545		sbox.z = src_box->z;
546		sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
547		sbox.height = util_format_get_nblocksy(orig_info[0].format, src_box->height);
548		sbox.depth = src_box->depth;
549		src_box = &sbox;
550
551		si_compressed_to_blittable(dst, dst_level, &orig_info[1]);
552		restore_orig[1] = TRUE;
553		/* translate the dst box as well */
554		dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
555		dsty = util_format_get_nblocksy(orig_info[1].format, dsty);
556	} else if (!util_blitter_is_copy_supported(sctx->blitter, dst, src)) {
557		if (util_format_is_subsampled_422(src->format)) {
558			/* XXX untested */
559			si_change_format(src, src_level, &orig_info[0],
560					 PIPE_FORMAT_R8G8B8A8_UINT);
561			si_change_format(dst, dst_level, &orig_info[1],
562					 PIPE_FORMAT_R8G8B8A8_UINT);
563
564			sbox = *src_box;
565			sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
566			sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
567			src_box = &sbox;
568			dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
569
570			restore_orig[0] = TRUE;
571			restore_orig[1] = TRUE;
572		} else {
573			unsigned blocksize = util_format_get_blocksize(src->format);
574
575			switch (blocksize) {
576			case 1:
577				si_change_format(src, src_level, &orig_info[0],
578						PIPE_FORMAT_R8_UNORM);
579				si_change_format(dst, dst_level, &orig_info[1],
580						PIPE_FORMAT_R8_UNORM);
581				break;
582			case 2:
583				si_change_format(src, src_level, &orig_info[0],
584						PIPE_FORMAT_R8G8_UNORM);
585				si_change_format(dst, dst_level, &orig_info[1],
586						PIPE_FORMAT_R8G8_UNORM);
587				break;
588			case 4:
589				si_change_format(src, src_level, &orig_info[0],
590						PIPE_FORMAT_R8G8B8A8_UNORM);
591				si_change_format(dst, dst_level, &orig_info[1],
592						PIPE_FORMAT_R8G8B8A8_UNORM);
593				break;
594			case 8:
595				si_change_format(src, src_level, &orig_info[0],
596						PIPE_FORMAT_R16G16B16A16_UINT);
597				si_change_format(dst, dst_level, &orig_info[1],
598						PIPE_FORMAT_R16G16B16A16_UINT);
599				break;
600			case 16:
601				si_change_format(src, src_level, &orig_info[0],
602						PIPE_FORMAT_R32G32B32A32_UINT);
603				si_change_format(dst, dst_level, &orig_info[1],
604						PIPE_FORMAT_R32G32B32A32_UINT);
605				break;
606			default:
607				fprintf(stderr, "Unhandled format %s with blocksize %u\n",
608					util_format_short_name(src->format), blocksize);
609				assert(0);
610			}
611			restore_orig[0] = TRUE;
612			restore_orig[1] = TRUE;
613		}
614	}
615
616	/* Initialize the surface. */
617	util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
618	dst_view = r600_create_surface_custom(ctx, dst, &dst_templ,
619					      rdst->surface.level[dst_level].npix_x,
620					      rdst->surface.level[dst_level].npix_y);
621
622	/* Initialize the sampler view. */
623	util_blitter_default_src_texture(&src_templ, src, src_level);
624	src_view = ctx->create_sampler_view(ctx, src, &src_templ);
625
626	u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
627		 abs(src_box->depth), &dstbox);
628
629	/* Copy. */
630	si_blitter_begin(ctx, SI_COPY);
631	util_blitter_blit_generic(sctx->blitter, dst_view, &dstbox,
632				  src_view, src_box, src->width0, src->height0,
633				  PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL);
634	si_blitter_end(ctx);
635
636	pipe_surface_reference(&dst_view, NULL);
637	pipe_sampler_view_reference(&src_view, NULL);
638
639	if (restore_orig[0])
640		si_reset_blittable_to_orig(src, src_level, &orig_info[0]);
641
642	if (restore_orig[1])
643		si_reset_blittable_to_orig(dst, dst_level, &orig_info[1]);
644}
645
646/* For MSAA integer resolving to work, we change the format to NORM using this function. */
647static enum pipe_format int_to_norm_format(enum pipe_format format)
648{
649	switch (format) {
650#define REPLACE_FORMAT_SIGN(format,sign) \
651	case PIPE_FORMAT_##format##_##sign##INT: \
652		return PIPE_FORMAT_##format##_##sign##NORM
653#define REPLACE_FORMAT(format) \
654		REPLACE_FORMAT_SIGN(format, U); \
655		REPLACE_FORMAT_SIGN(format, S)
656
657	REPLACE_FORMAT_SIGN(B10G10R10A2, U);
658	REPLACE_FORMAT(R8);
659	REPLACE_FORMAT(R8G8);
660	REPLACE_FORMAT(R8G8B8X8);
661	REPLACE_FORMAT(R8G8B8A8);
662	REPLACE_FORMAT(A8);
663	REPLACE_FORMAT(I8);
664	REPLACE_FORMAT(L8);
665	REPLACE_FORMAT(L8A8);
666	REPLACE_FORMAT(R16);
667	REPLACE_FORMAT(R16G16);
668	REPLACE_FORMAT(R16G16B16X16);
669	REPLACE_FORMAT(R16G16B16A16);
670	REPLACE_FORMAT(A16);
671	REPLACE_FORMAT(I16);
672	REPLACE_FORMAT(L16);
673	REPLACE_FORMAT(L16A16);
674
675#undef REPLACE_FORMAT
676#undef REPLACE_FORMAT_SIGN
677	default:
678		return format;
679	}
680}
681
682static bool do_hardware_msaa_resolve(struct pipe_context *ctx,
683				     const struct pipe_blit_info *info)
684{
685	struct si_context *sctx = (struct si_context*)ctx;
686	struct r600_texture *dst = (struct r600_texture*)info->dst.resource;
687	unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
688	unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
689	enum pipe_format format = int_to_norm_format(info->dst.format);
690	unsigned sample_mask = ~0;
691
692	if (info->src.resource->nr_samples > 1 &&
693	    info->dst.resource->nr_samples <= 1 &&
694	    util_max_layer(info->src.resource, 0) == 0 &&
695	    util_max_layer(info->dst.resource, info->dst.level) == 0 &&
696	    info->dst.format == info->src.format &&
697	    !util_format_is_pure_integer(format) &&
698	    !util_format_is_depth_or_stencil(format) &&
699	    !info->scissor_enable &&
700	    (info->mask & PIPE_MASK_RGBA) == PIPE_MASK_RGBA &&
701	    dst_width == info->src.resource->width0 &&
702	    dst_height == info->src.resource->height0 &&
703	    info->dst.box.x == 0 &&
704	    info->dst.box.y == 0 &&
705	    info->dst.box.width == dst_width &&
706	    info->dst.box.height == dst_height &&
707	    info->dst.box.depth == 1 &&
708	    info->src.box.x == 0 &&
709	    info->src.box.y == 0 &&
710	    info->src.box.width == dst_width &&
711	    info->src.box.height == dst_height &&
712	    info->src.box.depth == 1 &&
713	    dst->surface.level[info->dst.level].mode >= RADEON_SURF_MODE_1D &&
714	    !(dst->surface.flags & RADEON_SURF_SCANOUT) &&
715	    (!dst->cmask.size || !dst->dirty_level_mask) /* dst cannot be fast-cleared */) {
716		si_blitter_begin(ctx, SI_COLOR_RESOLVE |
717				 (info->render_condition_enable ? 0 : SI_DISABLE_RENDER_COND));
718		util_blitter_custom_resolve_color(sctx->blitter,
719						  info->dst.resource, info->dst.level,
720						  info->dst.box.z,
721						  info->src.resource, info->src.box.z,
722						  sample_mask, sctx->custom_blend_resolve,
723						  format);
724		si_blitter_end(ctx);
725		return true;
726	}
727	return false;
728}
729
730static void si_blit(struct pipe_context *ctx,
731		    const struct pipe_blit_info *info)
732{
733	struct si_context *sctx = (struct si_context*)ctx;
734
735	if (do_hardware_msaa_resolve(ctx, info)) {
736		return;
737	}
738
739	assert(util_blitter_is_blit_supported(sctx->blitter, info));
740
741	/* The driver doesn't decompress resources automatically while
742	 * u_blitter is rendering. */
743	si_decompress_subresource(ctx, info->src.resource, info->src.level,
744				  info->src.box.z,
745				  info->src.box.z + info->src.box.depth - 1);
746
747	si_blitter_begin(ctx, SI_BLIT |
748			 (info->render_condition_enable ? 0 : SI_DISABLE_RENDER_COND));
749	util_blitter_blit(sctx->blitter, info);
750	si_blitter_end(ctx);
751}
752
753static void si_flush_resource(struct pipe_context *ctx,
754			      struct pipe_resource *res)
755{
756	struct r600_texture *rtex = (struct r600_texture*)res;
757
758	assert(res->target != PIPE_BUFFER);
759
760	if (!rtex->is_depth && rtex->cmask.size) {
761		si_blit_decompress_color(ctx, rtex, 0, res->last_level,
762					 0, util_max_layer(res, 0));
763	}
764}
765
766void si_init_blit_functions(struct si_context *sctx)
767{
768	sctx->b.b.clear = si_clear;
769	sctx->b.b.clear_render_target = si_clear_render_target;
770	sctx->b.b.clear_depth_stencil = si_clear_depth_stencil;
771	sctx->b.b.resource_copy_region = si_resource_copy_region;
772	sctx->b.b.blit = si_blit;
773	sctx->b.b.flush_resource = si_flush_resource;
774	sctx->b.blit_decompress_depth = si_blit_decompress_depth;
775}
776