13464ebd5Sriastradh/*
23464ebd5Sriastradh * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
33464ebd5Sriastradh *
43464ebd5Sriastradh * Permission is hereby granted, free of charge, to any person obtaining a
53464ebd5Sriastradh * copy of this software and associated documentation files (the "Software"),
63464ebd5Sriastradh * to deal in the Software without restriction, including without limitation
73464ebd5Sriastradh * on the rights to use, copy, modify, merge, publish, distribute, sub
83464ebd5Sriastradh * license, and/or sell copies of the Software, and to permit persons to whom
93464ebd5Sriastradh * the Software is furnished to do so, subject to the following conditions:
103464ebd5Sriastradh *
113464ebd5Sriastradh * The above copyright notice and this permission notice (including the next
123464ebd5Sriastradh * paragraph) shall be included in all copies or substantial portions of the
133464ebd5Sriastradh * Software.
143464ebd5Sriastradh *
153464ebd5Sriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
163464ebd5Sriastradh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
173464ebd5Sriastradh * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
183464ebd5Sriastradh * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
193464ebd5Sriastradh * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
203464ebd5Sriastradh * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
213464ebd5Sriastradh * USE OR OTHER DEALINGS IN THE SOFTWARE.
223464ebd5Sriastradh */
233464ebd5Sriastradh#include "r600_pipe.h"
2401e04c3fSmrg#include "compute_memory_pool.h"
2501e04c3fSmrg#include "evergreen_compute.h"
26af69d88dSmrg#include "util/u_surface.h"
277ec681f3Smrg#include "util/format/u_format.h"
28af69d88dSmrg#include "evergreend.h"
293464ebd5Sriastradh
303464ebd5Sriastradhenum r600_blitter_op /* bitmask */
313464ebd5Sriastradh{
32af69d88dSmrg	R600_SAVE_FRAGMENT_STATE = 1,
33af69d88dSmrg	R600_SAVE_TEXTURES       = 2,
34af69d88dSmrg	R600_SAVE_FRAMEBUFFER    = 4,
35af69d88dSmrg	R600_DISABLE_RENDER_COND = 8,
363464ebd5Sriastradh
37af69d88dSmrg	R600_CLEAR         = R600_SAVE_FRAGMENT_STATE,
383464ebd5Sriastradh
39af69d88dSmrg	R600_CLEAR_SURFACE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER,
403464ebd5Sriastradh
41af69d88dSmrg	R600_COPY_BUFFER   = R600_DISABLE_RENDER_COND,
42af69d88dSmrg
43af69d88dSmrg	R600_COPY_TEXTURE  = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |
443464ebd5Sriastradh			     R600_DISABLE_RENDER_COND,
453464ebd5Sriastradh
46af69d88dSmrg	R600_BLIT          = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES,
47af69d88dSmrg
48af69d88dSmrg	R600_DECOMPRESS    = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_DISABLE_RENDER_COND,
49af69d88dSmrg
50af69d88dSmrg	R600_COLOR_RESOLVE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER
513464ebd5Sriastradh};
523464ebd5Sriastradh
533464ebd5Sriastradhstatic void r600_blitter_begin(struct pipe_context *ctx, enum r600_blitter_op op)
543464ebd5Sriastradh{
55af69d88dSmrg	struct r600_context *rctx = (struct r600_context *)ctx;
563464ebd5Sriastradh
5701e04c3fSmrg	if (rctx->cmd_buf_is_compute) {
5801e04c3fSmrg		rctx->b.gfx.flush(rctx, PIPE_FLUSH_ASYNC, NULL);
5901e04c3fSmrg		rctx->cmd_buf_is_compute = false;
6001e04c3fSmrg	}
613464ebd5Sriastradh
62af69d88dSmrg	util_blitter_save_vertex_buffer_slot(rctx->blitter, rctx->vertex_buffer_state.vb);
63af69d88dSmrg	util_blitter_save_vertex_elements(rctx->blitter, rctx->vertex_fetch_shader.cso);
643464ebd5Sriastradh	util_blitter_save_vertex_shader(rctx->blitter, rctx->vs_shader);
65af69d88dSmrg	util_blitter_save_geometry_shader(rctx->blitter, rctx->gs_shader);
6601e04c3fSmrg	util_blitter_save_tessctrl_shader(rctx->blitter, rctx->tcs_shader);
6701e04c3fSmrg	util_blitter_save_tesseval_shader(rctx->blitter, rctx->tes_shader);
68af69d88dSmrg	util_blitter_save_so_targets(rctx->blitter, rctx->b.streamout.num_targets,
69af69d88dSmrg				     (struct pipe_stream_output_target**)rctx->b.streamout.targets);
70af69d88dSmrg	util_blitter_save_rasterizer(rctx->blitter, rctx->rasterizer_state.cso);
71af69d88dSmrg
72af69d88dSmrg	if (op & R600_SAVE_FRAGMENT_STATE) {
7301e04c3fSmrg		util_blitter_save_viewport(rctx->blitter, &rctx->b.viewports.states[0]);
7401e04c3fSmrg		util_blitter_save_scissor(rctx->blitter, &rctx->b.scissors.states[0]);
75af69d88dSmrg		util_blitter_save_fragment_shader(rctx->blitter, rctx->ps_shader);
76af69d88dSmrg		util_blitter_save_blend(rctx->blitter, rctx->blend_state.cso);
77af69d88dSmrg		util_blitter_save_depth_stencil_alpha(rctx->blitter, rctx->dsa_state.cso);
78af69d88dSmrg		util_blitter_save_stencil_ref(rctx->blitter, &rctx->stencil_ref.pipe_state);
79af69d88dSmrg                util_blitter_save_sample_mask(rctx->blitter, rctx->sample_mask.sample_mask);
803464ebd5Sriastradh	}
813464ebd5Sriastradh
823464ebd5Sriastradh	if (op & R600_SAVE_FRAMEBUFFER)
83af69d88dSmrg		util_blitter_save_framebuffer(rctx->blitter, &rctx->framebuffer.state);
843464ebd5Sriastradh
853464ebd5Sriastradh	if (op & R600_SAVE_TEXTURES) {
863464ebd5Sriastradh		util_blitter_save_fragment_sampler_states(
87af69d88dSmrg			rctx->blitter, util_last_bit(rctx->samplers[PIPE_SHADER_FRAGMENT].states.enabled_mask),
88af69d88dSmrg			(void**)rctx->samplers[PIPE_SHADER_FRAGMENT].states.states);
893464ebd5Sriastradh
903464ebd5Sriastradh		util_blitter_save_fragment_sampler_views(
91af69d88dSmrg			rctx->blitter, util_last_bit(rctx->samplers[PIPE_SHADER_FRAGMENT].views.enabled_mask),
92af69d88dSmrg			(struct pipe_sampler_view**)rctx->samplers[PIPE_SHADER_FRAGMENT].views.views);
933464ebd5Sriastradh	}
943464ebd5Sriastradh
9501e04c3fSmrg	if (op & R600_DISABLE_RENDER_COND)
9601e04c3fSmrg		rctx->b.render_cond_force_off = true;
973464ebd5Sriastradh}
983464ebd5Sriastradh
993464ebd5Sriastradhstatic void r600_blitter_end(struct pipe_context *ctx)
1003464ebd5Sriastradh{
101af69d88dSmrg	struct r600_context *rctx = (struct r600_context *)ctx;
10201e04c3fSmrg
10301e04c3fSmrg	rctx->b.render_cond_force_off = false;
1043464ebd5Sriastradh}
1053464ebd5Sriastradh
106af69d88dSmrgstatic unsigned u_max_sample(struct pipe_resource *r)
1073464ebd5Sriastradh{
108af69d88dSmrg	return r->nr_samples ? r->nr_samples - 1 : 0;
109af69d88dSmrg}
1103464ebd5Sriastradh
111af69d88dSmrgstatic void r600_blit_decompress_depth(struct pipe_context *ctx,
112af69d88dSmrg				       struct r600_texture *texture,
113af69d88dSmrg				       struct r600_texture *staging,
114af69d88dSmrg				       unsigned first_level, unsigned last_level,
115af69d88dSmrg				       unsigned first_layer, unsigned last_layer,
116af69d88dSmrg				       unsigned first_sample, unsigned last_sample)
117af69d88dSmrg{
118af69d88dSmrg	struct r600_context *rctx = (struct r600_context *)ctx;
119af69d88dSmrg	unsigned layer, level, sample, checked_last_layer, max_layer, max_sample;
120af69d88dSmrg	struct r600_texture *flushed_depth_texture = staging ?
121af69d88dSmrg			staging : texture->flushed_depth_texture;
122af69d88dSmrg	const struct util_format_description *desc =
123af69d88dSmrg		util_format_description(texture->resource.b.b.format);
124af69d88dSmrg	float depth;
125af69d88dSmrg
126af69d88dSmrg	if (!staging && !texture->dirty_level_mask)
1273464ebd5Sriastradh		return;
1283464ebd5Sriastradh
129af69d88dSmrg	max_sample = u_max_sample(&texture->resource.b.b);
1303464ebd5Sriastradh
131af69d88dSmrg	/* XXX Decompressing MSAA depth textures is broken on R6xx.
132af69d88dSmrg	 * There is also a hardlock if CMASK and FMASK are not present.
133af69d88dSmrg	 * Just skip this until we find out how to fix it. */
134af69d88dSmrg	if (rctx->b.chip_class == R600 && max_sample > 0) {
135af69d88dSmrg		texture->dirty_level_mask = 0;
136af69d88dSmrg		return;
137af69d88dSmrg	}
1383464ebd5Sriastradh
139af69d88dSmrg	if (rctx->b.family == CHIP_RV610 || rctx->b.family == CHIP_RV630 ||
140af69d88dSmrg	    rctx->b.family == CHIP_RV620 || rctx->b.family == CHIP_RV635)
1413464ebd5Sriastradh		depth = 0.0f;
142af69d88dSmrg	else
143af69d88dSmrg		depth = 1.0f;
1443464ebd5Sriastradh
145af69d88dSmrg	/* Enable decompression in DB_RENDER_CONTROL */
146af69d88dSmrg	rctx->db_misc_state.flush_depthstencil_through_cb = true;
147af69d88dSmrg	rctx->db_misc_state.copy_depth = util_format_has_depth(desc);
148af69d88dSmrg	rctx->db_misc_state.copy_stencil = util_format_has_stencil(desc);
149af69d88dSmrg	rctx->db_misc_state.copy_sample = first_sample;
15001e04c3fSmrg	r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
151af69d88dSmrg
152af69d88dSmrg	for (level = first_level; level <= last_level; level++) {
153af69d88dSmrg		if (!staging && !(texture->dirty_level_mask & (1 << level)))
154af69d88dSmrg			continue;
1553464ebd5Sriastradh
156af69d88dSmrg		/* The smaller the mipmap level, the less layers there are
157af69d88dSmrg		 * as far as 3D textures are concerned. */
158af69d88dSmrg		max_layer = util_max_layer(&texture->resource.b.b, level);
159af69d88dSmrg		checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
160af69d88dSmrg
161af69d88dSmrg		for (layer = first_layer; layer <= checked_last_layer; layer++) {
162af69d88dSmrg			for (sample = first_sample; sample <= last_sample; sample++) {
163af69d88dSmrg				struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
164af69d88dSmrg
165af69d88dSmrg				if (sample != rctx->db_misc_state.copy_sample) {
166af69d88dSmrg					rctx->db_misc_state.copy_sample = sample;
16701e04c3fSmrg					r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
168af69d88dSmrg				}
169af69d88dSmrg
170af69d88dSmrg				surf_tmpl.format = texture->resource.b.b.format;
171af69d88dSmrg				surf_tmpl.u.tex.level = level;
172af69d88dSmrg				surf_tmpl.u.tex.first_layer = layer;
173af69d88dSmrg				surf_tmpl.u.tex.last_layer = layer;
174af69d88dSmrg
175af69d88dSmrg				zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
176af69d88dSmrg
177af69d88dSmrg				surf_tmpl.format = flushed_depth_texture->resource.b.b.format;
178af69d88dSmrg				cbsurf = ctx->create_surface(ctx,
179af69d88dSmrg						&flushed_depth_texture->resource.b.b, &surf_tmpl);
180af69d88dSmrg
181af69d88dSmrg				r600_blitter_begin(ctx, R600_DECOMPRESS);
182af69d88dSmrg				util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, 1 << sample,
183af69d88dSmrg								  rctx->custom_dsa_flush, depth);
184af69d88dSmrg				r600_blitter_end(ctx);
185af69d88dSmrg
186af69d88dSmrg				pipe_surface_reference(&zsurf, NULL);
187af69d88dSmrg				pipe_surface_reference(&cbsurf, NULL);
188af69d88dSmrg			}
189af69d88dSmrg		}
190af69d88dSmrg
191af69d88dSmrg		/* The texture will always be dirty if some layers or samples aren't flushed.
192af69d88dSmrg		 * I don't think this case occurs often though. */
193af69d88dSmrg		if (!staging &&
194af69d88dSmrg		    first_layer == 0 && last_layer == max_layer &&
195af69d88dSmrg		    first_sample == 0 && last_sample == max_sample) {
196af69d88dSmrg			texture->dirty_level_mask &= ~(1 << level);
197af69d88dSmrg		}
198af69d88dSmrg	}
1993464ebd5Sriastradh
200af69d88dSmrg	/* reenable compression in DB_RENDER_CONTROL */
201af69d88dSmrg	rctx->db_misc_state.flush_depthstencil_through_cb = false;
20201e04c3fSmrg	r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
2033464ebd5Sriastradh}
2043464ebd5Sriastradh
205af69d88dSmrgstatic void r600_blit_decompress_depth_in_place(struct r600_context *rctx,
206af69d88dSmrg                                                struct r600_texture *texture,
20701e04c3fSmrg						bool is_stencil_sampler,
208af69d88dSmrg                                                unsigned first_level, unsigned last_level,
209af69d88dSmrg                                                unsigned first_layer, unsigned last_layer)
2103464ebd5Sriastradh{
211af69d88dSmrg	struct pipe_surface *zsurf, surf_tmpl = {{0}};
212af69d88dSmrg	unsigned layer, max_layer, checked_last_layer, level;
21301e04c3fSmrg	unsigned *dirty_level_mask;
2143464ebd5Sriastradh
215af69d88dSmrg	/* Enable decompression in DB_RENDER_CONTROL */
21601e04c3fSmrg	if (is_stencil_sampler) {
21701e04c3fSmrg		rctx->db_misc_state.flush_stencil_inplace = true;
21801e04c3fSmrg		dirty_level_mask = &texture->stencil_dirty_level_mask;
21901e04c3fSmrg	} else {
22001e04c3fSmrg		rctx->db_misc_state.flush_depth_inplace = true;
22101e04c3fSmrg		dirty_level_mask = &texture->dirty_level_mask;
22201e04c3fSmrg	}
22301e04c3fSmrg	r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
2243464ebd5Sriastradh
225af69d88dSmrg	surf_tmpl.format = texture->resource.b.b.format;
2263464ebd5Sriastradh
227af69d88dSmrg	for (level = first_level; level <= last_level; level++) {
22801e04c3fSmrg		if (!(*dirty_level_mask & (1 << level)))
2293464ebd5Sriastradh			continue;
2303464ebd5Sriastradh
231af69d88dSmrg		surf_tmpl.u.tex.level = level;
232af69d88dSmrg
233af69d88dSmrg		/* The smaller the mipmap level, the less layers there are
234af69d88dSmrg		 * as far as 3D textures are concerned. */
235af69d88dSmrg		max_layer = util_max_layer(&texture->resource.b.b, level);
236af69d88dSmrg		checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
2373464ebd5Sriastradh
238af69d88dSmrg		for (layer = first_layer; layer <= checked_last_layer; layer++) {
239af69d88dSmrg			surf_tmpl.u.tex.first_layer = layer;
240af69d88dSmrg			surf_tmpl.u.tex.last_layer = layer;
241af69d88dSmrg
242af69d88dSmrg			zsurf = rctx->b.b.create_surface(&rctx->b.b, &texture->resource.b.b, &surf_tmpl);
243af69d88dSmrg
244af69d88dSmrg			r600_blitter_begin(&rctx->b.b, R600_DECOMPRESS);
245af69d88dSmrg			util_blitter_custom_depth_stencil(rctx->blitter, zsurf, NULL, ~0,
246af69d88dSmrg							  rctx->custom_dsa_flush, 1.0f);
247af69d88dSmrg			r600_blitter_end(&rctx->b.b);
248af69d88dSmrg
249af69d88dSmrg			pipe_surface_reference(&zsurf, NULL);
250af69d88dSmrg		}
251af69d88dSmrg
252af69d88dSmrg		/* The texture will always be dirty if some layers or samples aren't flushed.
253af69d88dSmrg		 * I don't think this case occurs often though. */
254af69d88dSmrg		if (first_layer == 0 && last_layer == max_layer) {
25501e04c3fSmrg			*dirty_level_mask &= ~(1 << level);
256af69d88dSmrg		}
2573464ebd5Sriastradh	}
2583464ebd5Sriastradh
259af69d88dSmrg	/* Disable decompression in DB_RENDER_CONTROL */
26001e04c3fSmrg	rctx->db_misc_state.flush_depth_inplace = false;
26101e04c3fSmrg	rctx->db_misc_state.flush_stencil_inplace = false;
26201e04c3fSmrg	r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
263af69d88dSmrg}
2643464ebd5Sriastradh
265af69d88dSmrgvoid r600_decompress_depth_textures(struct r600_context *rctx,
266af69d88dSmrg			       struct r600_samplerview_state *textures)
267af69d88dSmrg{
268af69d88dSmrg	unsigned i;
269af69d88dSmrg	unsigned depth_texture_mask = textures->compressed_depthtex_mask;
270af69d88dSmrg
271af69d88dSmrg	while (depth_texture_mask) {
272af69d88dSmrg		struct pipe_sampler_view *view;
27301e04c3fSmrg		struct r600_pipe_sampler_view *rview;
274af69d88dSmrg		struct r600_texture *tex;
275af69d88dSmrg
276af69d88dSmrg		i = u_bit_scan(&depth_texture_mask);
277af69d88dSmrg
278af69d88dSmrg		view = &textures->views[i]->base;
279af69d88dSmrg		assert(view);
28001e04c3fSmrg		rview = (struct r600_pipe_sampler_view*)view;
281af69d88dSmrg
282af69d88dSmrg		tex = (struct r600_texture *)view->texture;
28301e04c3fSmrg		assert(tex->db_compatible);
284af69d88dSmrg
28501e04c3fSmrg		if (r600_can_sample_zs(tex, rview->is_stencil_sampler)) {
286af69d88dSmrg			r600_blit_decompress_depth_in_place(rctx, tex,
28701e04c3fSmrg						   rview->is_stencil_sampler,
288af69d88dSmrg						   view->u.tex.first_level, view->u.tex.last_level,
289af69d88dSmrg						   0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
290af69d88dSmrg		} else {
291af69d88dSmrg			r600_blit_decompress_depth(&rctx->b.b, tex, NULL,
292af69d88dSmrg						   view->u.tex.first_level, view->u.tex.last_level,
293af69d88dSmrg						   0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level),
294af69d88dSmrg						   0, u_max_sample(&tex->resource.b.b));
295af69d88dSmrg		}
296af69d88dSmrg	}
297af69d88dSmrg}
298af69d88dSmrg
29901e04c3fSmrgvoid r600_decompress_depth_images(struct r600_context *rctx,
30001e04c3fSmrg				  struct r600_image_state *images)
30101e04c3fSmrg{
30201e04c3fSmrg	unsigned i;
30301e04c3fSmrg	unsigned depth_texture_mask = images->compressed_depthtex_mask;
30401e04c3fSmrg
30501e04c3fSmrg	while (depth_texture_mask) {
30601e04c3fSmrg		struct r600_image_view *view;
30701e04c3fSmrg		struct r600_texture *tex;
30801e04c3fSmrg
30901e04c3fSmrg		i = u_bit_scan(&depth_texture_mask);
31001e04c3fSmrg
31101e04c3fSmrg		view = &images->views[i];
31201e04c3fSmrg		assert(view);
31301e04c3fSmrg
31401e04c3fSmrg		tex = (struct r600_texture *)view->base.resource;
31501e04c3fSmrg		assert(tex->db_compatible);
31601e04c3fSmrg
31701e04c3fSmrg		if (r600_can_sample_zs(tex, false)) {
31801e04c3fSmrg			r600_blit_decompress_depth_in_place(rctx, tex,
31901e04c3fSmrg							    false,
32001e04c3fSmrg							    view->base.u.tex.level,
32101e04c3fSmrg							    view->base.u.tex.level,
32201e04c3fSmrg							    0, util_max_layer(&tex->resource.b.b, view->base.u.tex.level));
32301e04c3fSmrg		} else {
32401e04c3fSmrg			r600_blit_decompress_depth(&rctx->b.b, tex, NULL,
32501e04c3fSmrg						   view->base.u.tex.level,
32601e04c3fSmrg						   view->base.u.tex.level,
32701e04c3fSmrg						   0, util_max_layer(&tex->resource.b.b, view->base.u.tex.level),
32801e04c3fSmrg						   0, u_max_sample(&tex->resource.b.b));
32901e04c3fSmrg		}
33001e04c3fSmrg	}
33101e04c3fSmrg}
33201e04c3fSmrg
333af69d88dSmrgstatic void r600_blit_decompress_color(struct pipe_context *ctx,
334af69d88dSmrg		struct r600_texture *rtex,
335af69d88dSmrg		unsigned first_level, unsigned last_level,
336af69d88dSmrg		unsigned first_layer, unsigned last_layer)
337af69d88dSmrg{
338af69d88dSmrg	struct r600_context *rctx = (struct r600_context *)ctx;
339af69d88dSmrg	unsigned layer, level, checked_last_layer, max_layer;
340af69d88dSmrg
341af69d88dSmrg	if (!rtex->dirty_level_mask)
342af69d88dSmrg		return;
3433464ebd5Sriastradh
344af69d88dSmrg	for (level = first_level; level <= last_level; level++) {
345af69d88dSmrg		if (!(rtex->dirty_level_mask & (1 << level)))
3463464ebd5Sriastradh			continue;
3473464ebd5Sriastradh
348af69d88dSmrg		/* The smaller the mipmap level, the less layers there are
349af69d88dSmrg		 * as far as 3D textures are concerned. */
350af69d88dSmrg		max_layer = util_max_layer(&rtex->resource.b.b, level);
351af69d88dSmrg		checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
352af69d88dSmrg
353af69d88dSmrg		for (layer = first_layer; layer <= checked_last_layer; layer++) {
354af69d88dSmrg			struct pipe_surface *cbsurf, surf_tmpl;
355af69d88dSmrg
356af69d88dSmrg			surf_tmpl.format = rtex->resource.b.b.format;
357af69d88dSmrg			surf_tmpl.u.tex.level = level;
358af69d88dSmrg			surf_tmpl.u.tex.first_layer = layer;
359af69d88dSmrg			surf_tmpl.u.tex.last_layer = layer;
360af69d88dSmrg			cbsurf = ctx->create_surface(ctx, &rtex->resource.b.b, &surf_tmpl);
361af69d88dSmrg
362af69d88dSmrg			r600_blitter_begin(ctx, R600_DECOMPRESS);
363af69d88dSmrg			util_blitter_custom_color(rctx->blitter, cbsurf,
364af69d88dSmrg				rtex->fmask.size ? rctx->custom_blend_decompress : rctx->custom_blend_fastclear);
365af69d88dSmrg			r600_blitter_end(ctx);
366af69d88dSmrg
367af69d88dSmrg			pipe_surface_reference(&cbsurf, NULL);
368af69d88dSmrg		}
369af69d88dSmrg
370af69d88dSmrg		/* The texture will always be dirty if some layers aren't flushed.
371af69d88dSmrg		 * I don't think this case occurs often though. */
372af69d88dSmrg		if (first_layer == 0 && last_layer == max_layer) {
373af69d88dSmrg			rtex->dirty_level_mask &= ~(1 << level);
374af69d88dSmrg		}
3753464ebd5Sriastradh	}
3763464ebd5Sriastradh}
3773464ebd5Sriastradh
378af69d88dSmrgvoid r600_decompress_color_textures(struct r600_context *rctx,
379af69d88dSmrg				    struct r600_samplerview_state *textures)
380af69d88dSmrg{
381af69d88dSmrg	unsigned i;
382af69d88dSmrg	unsigned mask = textures->compressed_colortex_mask;
383af69d88dSmrg
384af69d88dSmrg	while (mask) {
385af69d88dSmrg		struct pipe_sampler_view *view;
386af69d88dSmrg		struct r600_texture *tex;
387af69d88dSmrg
388af69d88dSmrg		i = u_bit_scan(&mask);
389af69d88dSmrg
390af69d88dSmrg		view = &textures->views[i]->base;
391af69d88dSmrg		assert(view);
392af69d88dSmrg
393af69d88dSmrg		tex = (struct r600_texture *)view->texture;
394af69d88dSmrg		assert(tex->cmask.size);
395af69d88dSmrg
396af69d88dSmrg		r600_blit_decompress_color(&rctx->b.b, tex,
397af69d88dSmrg					   view->u.tex.first_level, view->u.tex.last_level,
398af69d88dSmrg					   0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
399af69d88dSmrg	}
400af69d88dSmrg}
401af69d88dSmrg
40201e04c3fSmrgvoid r600_decompress_color_images(struct r600_context *rctx,
40301e04c3fSmrg				  struct r600_image_state *images)
40401e04c3fSmrg{
40501e04c3fSmrg	unsigned i;
40601e04c3fSmrg	unsigned mask = images->compressed_colortex_mask;
40701e04c3fSmrg
40801e04c3fSmrg	while (mask) {
40901e04c3fSmrg		struct r600_image_view *view;
41001e04c3fSmrg		struct r600_texture *tex;
41101e04c3fSmrg
41201e04c3fSmrg		i = u_bit_scan(&mask);
41301e04c3fSmrg
41401e04c3fSmrg		view = &images->views[i];
41501e04c3fSmrg		assert(view);
41601e04c3fSmrg
41701e04c3fSmrg		tex = (struct r600_texture *)view->base.resource;
41801e04c3fSmrg		assert(tex->cmask.size);
41901e04c3fSmrg
42001e04c3fSmrg		r600_blit_decompress_color(&rctx->b.b, tex,
42101e04c3fSmrg					   view->base.u.tex.level, view->base.u.tex.level,
42201e04c3fSmrg					   view->base.u.tex.first_layer,
42301e04c3fSmrg					   view->base.u.tex.last_layer);
42401e04c3fSmrg	}
42501e04c3fSmrg}
42601e04c3fSmrg
427af69d88dSmrg/* Helper for decompressing a portion of a color or depth resource before
428af69d88dSmrg * blitting if any decompression is needed.
429af69d88dSmrg * The driver doesn't decompress resources automatically while u_blitter is
430af69d88dSmrg * rendering. */
431af69d88dSmrgstatic bool r600_decompress_subresource(struct pipe_context *ctx,
432af69d88dSmrg					struct pipe_resource *tex,
433af69d88dSmrg					unsigned level,
434af69d88dSmrg					unsigned first_layer, unsigned last_layer)
435af69d88dSmrg{
436af69d88dSmrg	struct r600_context *rctx = (struct r600_context *)ctx;
437af69d88dSmrg	struct r600_texture *rtex = (struct r600_texture*)tex;
438af69d88dSmrg
43901e04c3fSmrg	if (rtex->db_compatible) {
44001e04c3fSmrg		if (r600_can_sample_zs(rtex, false)) {
44101e04c3fSmrg			r600_blit_decompress_depth_in_place(rctx, rtex, false,
442af69d88dSmrg						   level, level,
443af69d88dSmrg						   first_layer, last_layer);
44401e04c3fSmrg			if (rtex->surface.has_stencil) {
44501e04c3fSmrg				r600_blit_decompress_depth_in_place(rctx, rtex, true,
44601e04c3fSmrg							   level, level,
44701e04c3fSmrg							   first_layer, last_layer);
44801e04c3fSmrg			}
449af69d88dSmrg		} else {
450af69d88dSmrg			if (!r600_init_flushed_depth_texture(ctx, tex, NULL))
451af69d88dSmrg				return false; /* error */
452af69d88dSmrg
453af69d88dSmrg			r600_blit_decompress_depth(ctx, rtex, NULL,
454af69d88dSmrg						   level, level,
455af69d88dSmrg						   first_layer, last_layer,
456af69d88dSmrg						   0, u_max_sample(tex));
457af69d88dSmrg		}
458af69d88dSmrg	} else if (rtex->cmask.size) {
459af69d88dSmrg		r600_blit_decompress_color(ctx, rtex, level, level,
460af69d88dSmrg					   first_layer, last_layer);
461af69d88dSmrg	}
462af69d88dSmrg	return true;
463af69d88dSmrg}
464af69d88dSmrg
4653464ebd5Sriastradhstatic void r600_clear(struct pipe_context *ctx, unsigned buffers,
4667ec681f3Smrg		       const struct pipe_scissor_state *scissor_state,
467af69d88dSmrg		       const union pipe_color_union *color,
468af69d88dSmrg		       double depth, unsigned stencil)
4693464ebd5Sriastradh{
470af69d88dSmrg	struct r600_context *rctx = (struct r600_context *)ctx;
471af69d88dSmrg	struct pipe_framebuffer_state *fb = &rctx->framebuffer.state;
472af69d88dSmrg
473af69d88dSmrg	if (buffers & PIPE_CLEAR_COLOR && rctx->b.chip_class >= EVERGREEN) {
474af69d88dSmrg		evergreen_do_fast_color_clear(&rctx->b, fb, &rctx->framebuffer.atom,
47501e04c3fSmrg					      &buffers, NULL, color);
47601e04c3fSmrg		if (!buffers)
47701e04c3fSmrg			return; /* all buffers have been fast cleared */
478af69d88dSmrg	}
479af69d88dSmrg
480af69d88dSmrg	if (buffers & PIPE_CLEAR_COLOR) {
481af69d88dSmrg		int i;
482af69d88dSmrg
483af69d88dSmrg		/* These buffers cannot use fast clear, make sure to disable expansion. */
484af69d88dSmrg		for (i = 0; i < fb->nr_cbufs; i++) {
485af69d88dSmrg			struct r600_texture *tex;
486af69d88dSmrg
487af69d88dSmrg			/* If not clearing this buffer, skip. */
488af69d88dSmrg			if (!(buffers & (PIPE_CLEAR_COLOR0 << i)))
489af69d88dSmrg				continue;
490af69d88dSmrg
491af69d88dSmrg			if (!fb->cbufs[i])
492af69d88dSmrg				continue;
493af69d88dSmrg
494af69d88dSmrg			tex = (struct r600_texture *)fb->cbufs[i]->texture;
495af69d88dSmrg			if (tex->fmask.size == 0)
496af69d88dSmrg				tex->dirty_level_mask &= ~(1 << fb->cbufs[i]->u.tex.level);
497af69d88dSmrg		}
498af69d88dSmrg	}
499af69d88dSmrg
500af69d88dSmrg	/* if hyperz enabled just clear hyperz */
501af69d88dSmrg	if (fb->zsbuf && (buffers & PIPE_CLEAR_DEPTH)) {
502af69d88dSmrg		struct r600_texture *rtex;
503af69d88dSmrg		unsigned level = fb->zsbuf->u.tex.level;
504af69d88dSmrg
505af69d88dSmrg		rtex = (struct r600_texture*)fb->zsbuf->texture;
506af69d88dSmrg
507af69d88dSmrg		/* We can't use hyperz fast clear if each slice of a texture
508af69d88dSmrg		 * array are clear to different value. To simplify code just
509af69d88dSmrg		 * disable fast clear for texture array.
510af69d88dSmrg		 */
51101e04c3fSmrg		if (r600_htile_enabled(rtex, level) &&
51201e04c3fSmrg                   fb->zsbuf->u.tex.first_layer == 0 &&
51301e04c3fSmrg                   fb->zsbuf->u.tex.last_layer == util_max_layer(&rtex->resource.b.b, level)) {
514af69d88dSmrg			if (rtex->depth_clear_value != depth) {
515af69d88dSmrg				rtex->depth_clear_value = depth;
51601e04c3fSmrg				r600_mark_atom_dirty(rctx, &rctx->db_state.atom);
517af69d88dSmrg			}
518af69d88dSmrg			rctx->db_misc_state.htile_clear = true;
51901e04c3fSmrg			r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
520af69d88dSmrg		}
521af69d88dSmrg	}
5223464ebd5Sriastradh
5233464ebd5Sriastradh	r600_blitter_begin(ctx, R600_CLEAR);
5243464ebd5Sriastradh	util_blitter_clear(rctx->blitter, fb->width, fb->height,
525af69d88dSmrg			   util_framebuffer_get_num_layers(fb),
5267ec681f3Smrg			   buffers, color, depth, stencil,
5277ec681f3Smrg			   util_framebuffer_get_num_samples(fb) > 1);
5283464ebd5Sriastradh	r600_blitter_end(ctx);
529af69d88dSmrg
530af69d88dSmrg	/* disable fast clear */
531af69d88dSmrg	if (rctx->db_misc_state.htile_clear) {
532af69d88dSmrg		rctx->db_misc_state.htile_clear = false;
53301e04c3fSmrg		r600_mark_atom_dirty(rctx, &rctx->db_misc_state.atom);
534af69d88dSmrg	}
5353464ebd5Sriastradh}
5363464ebd5Sriastradh
5373464ebd5Sriastradhstatic void r600_clear_render_target(struct pipe_context *ctx,
5383464ebd5Sriastradh				     struct pipe_surface *dst,
539af69d88dSmrg				     const union pipe_color_union *color,
5403464ebd5Sriastradh				     unsigned dstx, unsigned dsty,
54101e04c3fSmrg				     unsigned width, unsigned height,
54201e04c3fSmrg				     bool render_condition_enabled)
5433464ebd5Sriastradh{
544af69d88dSmrg	struct r600_context *rctx = (struct r600_context *)ctx;
5453464ebd5Sriastradh
54601e04c3fSmrg	r600_blitter_begin(ctx, R600_CLEAR_SURFACE |
54701e04c3fSmrg			   (render_condition_enabled ? 0 : R600_DISABLE_RENDER_COND));
548af69d88dSmrg	util_blitter_clear_render_target(rctx->blitter, dst, color,
5493464ebd5Sriastradh					 dstx, dsty, width, height);
5503464ebd5Sriastradh	r600_blitter_end(ctx);
5513464ebd5Sriastradh}
5523464ebd5Sriastradh
5533464ebd5Sriastradhstatic void r600_clear_depth_stencil(struct pipe_context *ctx,
5543464ebd5Sriastradh				     struct pipe_surface *dst,
5553464ebd5Sriastradh				     unsigned clear_flags,
5563464ebd5Sriastradh				     double depth,
5573464ebd5Sriastradh				     unsigned stencil,
5583464ebd5Sriastradh				     unsigned dstx, unsigned dsty,
55901e04c3fSmrg				     unsigned width, unsigned height,
56001e04c3fSmrg				     bool render_condition_enabled)
5613464ebd5Sriastradh{
562af69d88dSmrg	struct r600_context *rctx = (struct r600_context *)ctx;
5633464ebd5Sriastradh
56401e04c3fSmrg	r600_blitter_begin(ctx, R600_CLEAR_SURFACE |
56501e04c3fSmrg			   (render_condition_enabled ? 0 : R600_DISABLE_RENDER_COND));
5663464ebd5Sriastradh	util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
5673464ebd5Sriastradh					 dstx, dsty, width, height);
5683464ebd5Sriastradh	r600_blitter_end(ctx);
5693464ebd5Sriastradh}
5703464ebd5Sriastradh
571af69d88dSmrgstatic void r600_copy_buffer(struct pipe_context *ctx, struct pipe_resource *dst, unsigned dstx,
572af69d88dSmrg			     struct pipe_resource *src, const struct pipe_box *src_box)
5733464ebd5Sriastradh{
574af69d88dSmrg	struct r600_context *rctx = (struct r600_context*)ctx;
5753464ebd5Sriastradh
576af69d88dSmrg	if (rctx->screen->b.has_cp_dma) {
577af69d88dSmrg		r600_cp_dma_copy_buffer(rctx, dst, dstx, src, src_box->x, src_box->width);
578af69d88dSmrg	}
579af69d88dSmrg	else if (rctx->screen->b.has_streamout &&
580af69d88dSmrg		 /* Require 4-byte alignment. */
581af69d88dSmrg		 dstx % 4 == 0 && src_box->x % 4 == 0 && src_box->width % 4 == 0) {
582af69d88dSmrg
583af69d88dSmrg		r600_blitter_begin(ctx, R600_COPY_BUFFER);
584af69d88dSmrg		util_blitter_copy_buffer(rctx->blitter, dst, dstx, src, src_box->x, src_box->width);
585af69d88dSmrg		r600_blitter_end(ctx);
586af69d88dSmrg	} else {
587af69d88dSmrg		util_resource_copy_region(ctx, dst, 0, dstx, 0, 0, src, 0, src_box);
588af69d88dSmrg	}
589af69d88dSmrg}
5903464ebd5Sriastradh
591af69d88dSmrg/**
592af69d88dSmrg * Global buffers are not really resources, they are are actually offsets
593af69d88dSmrg * into a single global resource (r600_screen::global_pool).  The means
59401e04c3fSmrg * they don't have their own buf handle, so they cannot be passed
595af69d88dSmrg * to r600_copy_buffer() and must be handled separately.
596af69d88dSmrg */
597af69d88dSmrgstatic void r600_copy_global_buffer(struct pipe_context *ctx,
598af69d88dSmrg				    struct pipe_resource *dst, unsigned
599af69d88dSmrg				    dstx, struct pipe_resource *src,
600af69d88dSmrg				    const struct pipe_box *src_box)
6013464ebd5Sriastradh{
60201e04c3fSmrg	struct r600_context *rctx = (struct r600_context*)ctx;
60301e04c3fSmrg	struct compute_memory_pool *pool = rctx->screen->global_pool;
60401e04c3fSmrg	struct pipe_box new_src_box = *src_box;
60501e04c3fSmrg
60601e04c3fSmrg	if (src->bind & PIPE_BIND_GLOBAL) {
60701e04c3fSmrg		struct r600_resource_global *rsrc =
60801e04c3fSmrg			(struct r600_resource_global *)src;
60901e04c3fSmrg		struct compute_memory_item *item = rsrc->chunk;
61001e04c3fSmrg
61101e04c3fSmrg		if (is_item_in_pool(item)) {
61201e04c3fSmrg			new_src_box.x += 4 * item->start_in_dw;
61301e04c3fSmrg			src = (struct pipe_resource *)pool->bo;
61401e04c3fSmrg		} else {
61501e04c3fSmrg			if (item->real_buffer == NULL) {
61601e04c3fSmrg				item->real_buffer =
61701e04c3fSmrg					r600_compute_buffer_alloc_vram(pool->screen,
61801e04c3fSmrg								       item->size_in_dw * 4);
61901e04c3fSmrg			}
62001e04c3fSmrg			src = (struct pipe_resource*)item->real_buffer;
62101e04c3fSmrg		}
62201e04c3fSmrg	}
62301e04c3fSmrg	if (dst->bind & PIPE_BIND_GLOBAL) {
62401e04c3fSmrg		struct r600_resource_global *rdst =
62501e04c3fSmrg			(struct r600_resource_global *)dst;
62601e04c3fSmrg		struct compute_memory_item *item = rdst->chunk;
62701e04c3fSmrg
62801e04c3fSmrg		if (is_item_in_pool(item)) {
62901e04c3fSmrg			dstx += 4 * item->start_in_dw;
63001e04c3fSmrg			dst = (struct pipe_resource *)pool->bo;
63101e04c3fSmrg		} else {
63201e04c3fSmrg			if (item->real_buffer == NULL) {
63301e04c3fSmrg				item->real_buffer =
63401e04c3fSmrg					r600_compute_buffer_alloc_vram(pool->screen,
63501e04c3fSmrg								       item->size_in_dw * 4);
63601e04c3fSmrg			}
63701e04c3fSmrg			dst = (struct pipe_resource*)item->real_buffer;
63801e04c3fSmrg		}
63901e04c3fSmrg	}
64001e04c3fSmrg
64101e04c3fSmrg	r600_copy_buffer(ctx, dst, dstx, src, &new_src_box);
6423464ebd5Sriastradh}
6433464ebd5Sriastradh
644af69d88dSmrgstatic void r600_clear_buffer(struct pipe_context *ctx, struct pipe_resource *dst,
64501e04c3fSmrg			      uint64_t offset, uint64_t size, unsigned value,
64601e04c3fSmrg			      enum r600_coherency coher)
6473464ebd5Sriastradh{
648af69d88dSmrg	struct r600_context *rctx = (struct r600_context*)ctx;
649af69d88dSmrg
650af69d88dSmrg	if (rctx->screen->b.has_cp_dma &&
651af69d88dSmrg	    rctx->b.chip_class >= EVERGREEN &&
652af69d88dSmrg	    offset % 4 == 0 && size % 4 == 0) {
65301e04c3fSmrg		evergreen_cp_dma_clear_buffer(rctx, dst, offset, size, value, coher);
654af69d88dSmrg	} else if (rctx->screen->b.has_streamout && offset % 4 == 0 && size % 4 == 0) {
655af69d88dSmrg		union pipe_color_union clear_value;
656af69d88dSmrg		clear_value.ui[0] = value;
657af69d88dSmrg
658af69d88dSmrg		r600_blitter_begin(ctx, R600_DISABLE_RENDER_COND);
659af69d88dSmrg		util_blitter_clear_buffer(rctx->blitter, dst, offset, size,
660af69d88dSmrg					  1, &clear_value);
661af69d88dSmrg		r600_blitter_end(ctx);
662af69d88dSmrg	} else {
663af69d88dSmrg		uint32_t *map = r600_buffer_map_sync_with_rings(&rctx->b, r600_resource(dst),
6647ec681f3Smrg								 PIPE_MAP_WRITE);
66501e04c3fSmrg		map += offset / 4;
666af69d88dSmrg		size /= 4;
667af69d88dSmrg		for (unsigned i = 0; i < size; i++)
668af69d88dSmrg			*map++ = value;
669af69d88dSmrg	}
6703464ebd5Sriastradh}
6713464ebd5Sriastradh
67201e04c3fSmrgvoid r600_resource_copy_region(struct pipe_context *ctx,
67301e04c3fSmrg			       struct pipe_resource *dst,
67401e04c3fSmrg			       unsigned dst_level,
67501e04c3fSmrg			       unsigned dstx, unsigned dsty, unsigned dstz,
67601e04c3fSmrg			       struct pipe_resource *src,
67701e04c3fSmrg			       unsigned src_level,
67801e04c3fSmrg			       const struct pipe_box *src_box)
6793464ebd5Sriastradh{
680af69d88dSmrg	struct r600_context *rctx = (struct r600_context *)ctx;
681af69d88dSmrg	struct pipe_surface *dst_view, dst_templ;
682af69d88dSmrg	struct pipe_sampler_view src_templ, *src_view;
683af69d88dSmrg	unsigned dst_width, dst_height, src_width0, src_height0, src_widthFL, src_heightFL;
684af69d88dSmrg	unsigned src_force_level = 0;
685af69d88dSmrg	struct pipe_box sbox, dstbox;
686af69d88dSmrg
687af69d88dSmrg	/* Handle buffers first. */
6883464ebd5Sriastradh	if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
689af69d88dSmrg		if ((src->bind & PIPE_BIND_GLOBAL) ||
690af69d88dSmrg					(dst->bind & PIPE_BIND_GLOBAL)) {
691af69d88dSmrg			r600_copy_global_buffer(ctx, dst, dstx, src, src_box);
692af69d88dSmrg		} else {
693af69d88dSmrg			r600_copy_buffer(ctx, dst, dstx, src, src_box);
694af69d88dSmrg		}
6953464ebd5Sriastradh		return;
6963464ebd5Sriastradh	}
6973464ebd5Sriastradh
698af69d88dSmrg	assert(u_max_sample(dst) == u_max_sample(src));
699af69d88dSmrg
700af69d88dSmrg	/* The driver doesn't decompress resources automatically while
701af69d88dSmrg	 * u_blitter is rendering. */
702af69d88dSmrg	if (!r600_decompress_subresource(ctx, src, src_level,
703af69d88dSmrg					 src_box->z, src_box->z + src_box->depth - 1)) {
704af69d88dSmrg		return; /* error */
705af69d88dSmrg	}
706af69d88dSmrg
707af69d88dSmrg	dst_width = u_minify(dst->width0, dst_level);
708af69d88dSmrg        dst_height = u_minify(dst->height0, dst_level);
709af69d88dSmrg	src_width0 = src->width0;
710af69d88dSmrg	src_height0 = src->height0;
711af69d88dSmrg        src_widthFL = u_minify(src->width0, src_level);
712af69d88dSmrg        src_heightFL = u_minify(src->height0, src_level);
7133464ebd5Sriastradh
714af69d88dSmrg	util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
71501e04c3fSmrg	util_blitter_default_src_texture(rctx->blitter, &src_templ, src, src_level);
7163464ebd5Sriastradh
71701e04c3fSmrg	if (util_format_is_compressed(src->format) ||
71801e04c3fSmrg	    util_format_is_compressed(dst->format)) {
719af69d88dSmrg		unsigned blocksize = util_format_get_blocksize(src->format);
720af69d88dSmrg
721af69d88dSmrg		if (blocksize == 8)
722af69d88dSmrg			src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
723af69d88dSmrg		else
724af69d88dSmrg			src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
725af69d88dSmrg		dst_templ.format = src_templ.format;
726af69d88dSmrg
727af69d88dSmrg		dst_width = util_format_get_nblocksx(dst->format, dst_width);
728af69d88dSmrg		dst_height = util_format_get_nblocksy(dst->format, dst_height);
729af69d88dSmrg		src_width0 = util_format_get_nblocksx(src->format, src_width0);
730af69d88dSmrg		src_height0 = util_format_get_nblocksy(src->format, src_height0);
731af69d88dSmrg		src_widthFL = util_format_get_nblocksx(src->format, src_widthFL);
732af69d88dSmrg		src_heightFL = util_format_get_nblocksy(src->format, src_heightFL);
733af69d88dSmrg
734af69d88dSmrg		dstx = util_format_get_nblocksx(dst->format, dstx);
735af69d88dSmrg		dsty = util_format_get_nblocksy(dst->format, dsty);
736af69d88dSmrg
737af69d88dSmrg		sbox.x = util_format_get_nblocksx(src->format, src_box->x);
738af69d88dSmrg		sbox.y = util_format_get_nblocksy(src->format, src_box->y);
7393464ebd5Sriastradh		sbox.z = src_box->z;
740af69d88dSmrg		sbox.width = util_format_get_nblocksx(src->format, src_box->width);
741af69d88dSmrg		sbox.height = util_format_get_nblocksy(src->format, src_box->height);
7423464ebd5Sriastradh		sbox.depth = src_box->depth;
743af69d88dSmrg		src_box = &sbox;
744af69d88dSmrg
745af69d88dSmrg		src_force_level = src_level;
746af69d88dSmrg	} else if (!util_blitter_is_copy_supported(rctx->blitter, dst, src)) {
747af69d88dSmrg		if (util_format_is_subsampled_422(src->format)) {
748af69d88dSmrg
749af69d88dSmrg			src_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;
750af69d88dSmrg			dst_templ.format = PIPE_FORMAT_R8G8B8A8_UINT;
751af69d88dSmrg
752af69d88dSmrg			dst_width = util_format_get_nblocksx(dst->format, dst_width);
753af69d88dSmrg			src_width0 = util_format_get_nblocksx(src->format, src_width0);
754af69d88dSmrg			src_widthFL = util_format_get_nblocksx(src->format, src_widthFL);
755af69d88dSmrg
756af69d88dSmrg			dstx = util_format_get_nblocksx(dst->format, dstx);
757af69d88dSmrg
758af69d88dSmrg			sbox = *src_box;
759af69d88dSmrg			sbox.x = util_format_get_nblocksx(src->format, src_box->x);
760af69d88dSmrg			sbox.width = util_format_get_nblocksx(src->format, src_box->width);
761af69d88dSmrg			src_box = &sbox;
762af69d88dSmrg		} else {
763af69d88dSmrg			unsigned blocksize = util_format_get_blocksize(src->format);
764af69d88dSmrg
765af69d88dSmrg			switch (blocksize) {
766af69d88dSmrg			case 1:
767af69d88dSmrg				dst_templ.format = PIPE_FORMAT_R8_UNORM;
768af69d88dSmrg				src_templ.format = PIPE_FORMAT_R8_UNORM;
769af69d88dSmrg				break;
77001e04c3fSmrg			case 2:
771af69d88dSmrg				dst_templ.format = PIPE_FORMAT_R8G8_UNORM;
772af69d88dSmrg				src_templ.format = PIPE_FORMAT_R8G8_UNORM;
773af69d88dSmrg				break;
774af69d88dSmrg			case 4:
775af69d88dSmrg				dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
776af69d88dSmrg				src_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
777af69d88dSmrg				break;
77801e04c3fSmrg			case 8:
77901e04c3fSmrg				dst_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
78001e04c3fSmrg				src_templ.format = PIPE_FORMAT_R16G16B16A16_UINT;
78101e04c3fSmrg				break;
78201e04c3fSmrg			case 16:
78301e04c3fSmrg				dst_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;
78401e04c3fSmrg				src_templ.format = PIPE_FORMAT_R32G32B32A32_UINT;
78501e04c3fSmrg				break;
786af69d88dSmrg			default:
787af69d88dSmrg				fprintf(stderr, "Unhandled format %s with blocksize %u\n",
788af69d88dSmrg					util_format_short_name(src->format), blocksize);
789af69d88dSmrg				assert(0);
790af69d88dSmrg			}
791af69d88dSmrg		}
7923464ebd5Sriastradh	}
7933464ebd5Sriastradh
79401e04c3fSmrg	dst_view = r600_create_surface_custom(ctx, dst, &dst_templ,
79501e04c3fSmrg					      /* we don't care about these two for r600g */
79601e04c3fSmrg					      dst->width0, dst->height0,
79701e04c3fSmrg					      dst_width, dst_height);
7983464ebd5Sriastradh
799af69d88dSmrg	if (rctx->b.chip_class >= EVERGREEN) {
800af69d88dSmrg		src_view = evergreen_create_sampler_view_custom(ctx, src, &src_templ,
801af69d88dSmrg								src_width0, src_height0,
802af69d88dSmrg								src_force_level);
803af69d88dSmrg	} else {
804af69d88dSmrg		src_view = r600_create_sampler_view_custom(ctx, src, &src_templ,
805af69d88dSmrg							   src_widthFL, src_heightFL);
806af69d88dSmrg	}
807af69d88dSmrg
808af69d88dSmrg        u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
809af69d88dSmrg                 abs(src_box->depth), &dstbox);
810af69d88dSmrg
811af69d88dSmrg	/* Copy. */
812af69d88dSmrg	r600_blitter_begin(ctx, R600_COPY_TEXTURE);
813af69d88dSmrg	util_blitter_blit_generic(rctx->blitter, dst_view, &dstbox,
814af69d88dSmrg				  src_view, src_box, src_width0, src_height0,
81501e04c3fSmrg				  PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
8167ec681f3Smrg				  FALSE, FALSE);
817af69d88dSmrg	r600_blitter_end(ctx);
818af69d88dSmrg
819af69d88dSmrg	pipe_surface_reference(&dst_view, NULL);
820af69d88dSmrg	pipe_sampler_view_reference(&src_view, NULL);
821af69d88dSmrg}
822af69d88dSmrg
823af69d88dSmrgstatic bool do_hardware_msaa_resolve(struct pipe_context *ctx,
824af69d88dSmrg				     const struct pipe_blit_info *info)
825af69d88dSmrg{
826af69d88dSmrg	struct r600_context *rctx = (struct r600_context*)ctx;
827af69d88dSmrg	struct r600_texture *dst = (struct r600_texture*)info->dst.resource;
828af69d88dSmrg	unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
829af69d88dSmrg	unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
83001e04c3fSmrg	enum pipe_format format = info->src.format;
831af69d88dSmrg	unsigned sample_mask =
832af69d88dSmrg		rctx->b.chip_class == CAYMAN ? ~0 :
833af69d88dSmrg		((1ull << MAX2(1, info->src.resource->nr_samples)) - 1);
83401e04c3fSmrg	struct pipe_resource *tmp, templ;
83501e04c3fSmrg	struct pipe_blit_info blit;
83601e04c3fSmrg
83701e04c3fSmrg	/* Check basic requirements for hw resolve. */
83801e04c3fSmrg	if (!(info->src.resource->nr_samples > 1 &&
83901e04c3fSmrg	      info->dst.resource->nr_samples <= 1 &&
84001e04c3fSmrg	      !util_format_is_pure_integer(format) &&
84101e04c3fSmrg	      !util_format_is_depth_or_stencil(format) &&
84201e04c3fSmrg	      util_max_layer(info->src.resource, 0) == 0))
84301e04c3fSmrg		return false;
84401e04c3fSmrg
84501e04c3fSmrg	/* Check the remaining requirements for hw resolve. */
84601e04c3fSmrg	if (util_max_layer(info->dst.resource, info->dst.level) == 0 &&
84701e04c3fSmrg	    util_is_format_compatible(util_format_description(info->src.format),
84801e04c3fSmrg				      util_format_description(info->dst.format)) &&
849af69d88dSmrg	    !info->scissor_enable &&
850af69d88dSmrg	    (info->mask & PIPE_MASK_RGBA) == PIPE_MASK_RGBA &&
851af69d88dSmrg	    dst_width == info->src.resource->width0 &&
852af69d88dSmrg	    dst_height == info->src.resource->height0 &&
853af69d88dSmrg	    info->dst.box.x == 0 &&
854af69d88dSmrg	    info->dst.box.y == 0 &&
855af69d88dSmrg	    info->dst.box.width == dst_width &&
856af69d88dSmrg	    info->dst.box.height == dst_height &&
857af69d88dSmrg	    info->dst.box.depth == 1 &&
858af69d88dSmrg	    info->src.box.x == 0 &&
859af69d88dSmrg	    info->src.box.y == 0 &&
860af69d88dSmrg	    info->src.box.width == dst_width &&
861af69d88dSmrg	    info->src.box.height == dst_height &&
862af69d88dSmrg	    info->src.box.depth == 1 &&
86301e04c3fSmrg	    dst->surface.u.legacy.level[info->dst.level].mode >= RADEON_SURF_MODE_1D &&
864af69d88dSmrg	    (!dst->cmask.size || !dst->dirty_level_mask) /* dst cannot be fast-cleared */) {
865af69d88dSmrg		r600_blitter_begin(ctx, R600_COLOR_RESOLVE |
866af69d88dSmrg				   (info->render_condition_enable ? 0 : R600_DISABLE_RENDER_COND));
867af69d88dSmrg		util_blitter_custom_resolve_color(rctx->blitter,
868af69d88dSmrg						  info->dst.resource, info->dst.level,
869af69d88dSmrg						  info->dst.box.z,
870af69d88dSmrg						  info->src.resource, info->src.box.z,
871af69d88dSmrg						  sample_mask, rctx->custom_blend_resolve,
872af69d88dSmrg						  format);
873af69d88dSmrg		r600_blitter_end(ctx);
874af69d88dSmrg		return true;
875af69d88dSmrg	}
87601e04c3fSmrg
87701e04c3fSmrg	/* Shader-based resolve is VERY SLOW. Instead, resolve into
87801e04c3fSmrg	 * a temporary texture and blit.
87901e04c3fSmrg	 */
88001e04c3fSmrg	memset(&templ, 0, sizeof(templ));
88101e04c3fSmrg	templ.target = PIPE_TEXTURE_2D;
88201e04c3fSmrg	templ.format = info->src.resource->format;
88301e04c3fSmrg	templ.width0 = info->src.resource->width0;
88401e04c3fSmrg	templ.height0 = info->src.resource->height0;
88501e04c3fSmrg	templ.depth0 = 1;
88601e04c3fSmrg	templ.array_size = 1;
88701e04c3fSmrg	templ.usage = PIPE_USAGE_DEFAULT;
88801e04c3fSmrg	templ.flags = R600_RESOURCE_FLAG_FORCE_TILING;
88901e04c3fSmrg
89001e04c3fSmrg	tmp = ctx->screen->resource_create(ctx->screen, &templ);
89101e04c3fSmrg	if (!tmp)
89201e04c3fSmrg		return false;
89301e04c3fSmrg
89401e04c3fSmrg	/* resolve */
89501e04c3fSmrg	r600_blitter_begin(ctx, R600_COLOR_RESOLVE |
89601e04c3fSmrg			   (info->render_condition_enable ? 0 : R600_DISABLE_RENDER_COND));
89701e04c3fSmrg	util_blitter_custom_resolve_color(rctx->blitter, tmp, 0, 0,
89801e04c3fSmrg					  info->src.resource, info->src.box.z,
89901e04c3fSmrg					  sample_mask, rctx->custom_blend_resolve,
90001e04c3fSmrg					  format);
90101e04c3fSmrg	r600_blitter_end(ctx);
90201e04c3fSmrg
90301e04c3fSmrg	/* blit */
90401e04c3fSmrg	blit = *info;
90501e04c3fSmrg	blit.src.resource = tmp;
90601e04c3fSmrg	blit.src.box.z = 0;
90701e04c3fSmrg
90801e04c3fSmrg	r600_blitter_begin(ctx, R600_BLIT |
90901e04c3fSmrg			   (info->render_condition_enable ? 0 : R600_DISABLE_RENDER_COND));
91001e04c3fSmrg	util_blitter_blit(rctx->blitter, &blit);
91101e04c3fSmrg	r600_blitter_end(ctx);
91201e04c3fSmrg
91301e04c3fSmrg	pipe_resource_reference(&tmp, NULL);
91401e04c3fSmrg	return true;
915af69d88dSmrg}
916af69d88dSmrg
917af69d88dSmrgstatic void r600_blit(struct pipe_context *ctx,
918af69d88dSmrg                      const struct pipe_blit_info *info)
919af69d88dSmrg{
920af69d88dSmrg	struct r600_context *rctx = (struct r600_context*)ctx;
92101e04c3fSmrg	struct r600_texture *rdst = (struct r600_texture *)info->dst.resource;
922af69d88dSmrg
923af69d88dSmrg	if (do_hardware_msaa_resolve(ctx, info)) {
924af69d88dSmrg		return;
925af69d88dSmrg	}
926af69d88dSmrg
92701e04c3fSmrg	/* Using SDMA for copying to a linear texture in GTT is much faster.
92801e04c3fSmrg	 * This improves DRI PRIME performance.
92901e04c3fSmrg	 *
93001e04c3fSmrg	 * resource_copy_region can't do this yet, because dma_copy calls it
93101e04c3fSmrg	 * on failure (recursion).
93201e04c3fSmrg	 */
93301e04c3fSmrg	if (rdst->surface.u.legacy.level[info->dst.level].mode ==
93401e04c3fSmrg	    RADEON_SURF_MODE_LINEAR_ALIGNED &&
93501e04c3fSmrg	    rctx->b.dma_copy &&
93601e04c3fSmrg	    util_can_blit_via_copy_region(info, false)) {
93701e04c3fSmrg		rctx->b.dma_copy(ctx, info->dst.resource, info->dst.level,
93801e04c3fSmrg				 info->dst.box.x, info->dst.box.y,
93901e04c3fSmrg				 info->dst.box.z,
94001e04c3fSmrg				 info->src.resource, info->src.level,
94101e04c3fSmrg				 &info->src.box);
94201e04c3fSmrg		return;
94301e04c3fSmrg	}
94401e04c3fSmrg
945af69d88dSmrg	assert(util_blitter_is_blit_supported(rctx->blitter, info));
9463464ebd5Sriastradh
947af69d88dSmrg	/* The driver doesn't decompress resources automatically while
948af69d88dSmrg	 * u_blitter is rendering. */
949af69d88dSmrg	if (!r600_decompress_subresource(ctx, info->src.resource, info->src.level,
950af69d88dSmrg					 info->src.box.z,
951af69d88dSmrg					 info->src.box.z + info->src.box.depth - 1)) {
952af69d88dSmrg		return; /* error */
953af69d88dSmrg	}
954af69d88dSmrg
95501e04c3fSmrg	if (rctx->screen->b.debug_flags & DBG_FORCE_DMA &&
95601e04c3fSmrg	    util_try_blit_via_copy_region(ctx, info))
95701e04c3fSmrg		return;
95801e04c3fSmrg
959af69d88dSmrg	r600_blitter_begin(ctx, R600_BLIT |
960af69d88dSmrg			   (info->render_condition_enable ? 0 : R600_DISABLE_RENDER_COND));
961af69d88dSmrg	util_blitter_blit(rctx->blitter, info);
962af69d88dSmrg	r600_blitter_end(ctx);
9633464ebd5Sriastradh}
9643464ebd5Sriastradh
965af69d88dSmrgstatic void r600_flush_resource(struct pipe_context *ctx,
966af69d88dSmrg				struct pipe_resource *res)
9673464ebd5Sriastradh{
968af69d88dSmrg	struct r600_texture *rtex = (struct r600_texture*)res;
969af69d88dSmrg
970af69d88dSmrg	assert(res->target != PIPE_BUFFER);
971af69d88dSmrg
972af69d88dSmrg	if (!rtex->is_depth && rtex->cmask.size) {
973af69d88dSmrg		r600_blit_decompress_color(ctx, rtex, 0, res->last_level,
974af69d88dSmrg					   0, util_max_layer(res, 0));
975af69d88dSmrg	}
9763464ebd5Sriastradh}
9773464ebd5Sriastradh
978af69d88dSmrgvoid r600_init_blit_functions(struct r600_context *rctx)
9793464ebd5Sriastradh{
980af69d88dSmrg	rctx->b.b.clear = r600_clear;
981af69d88dSmrg	rctx->b.b.clear_render_target = r600_clear_render_target;
982af69d88dSmrg	rctx->b.b.clear_depth_stencil = r600_clear_depth_stencil;
983af69d88dSmrg	rctx->b.b.resource_copy_region = r600_resource_copy_region;
984af69d88dSmrg	rctx->b.b.blit = r600_blit;
985af69d88dSmrg	rctx->b.b.flush_resource = r600_flush_resource;
986af69d88dSmrg	rctx->b.clear_buffer = r600_clear_buffer;
987af69d88dSmrg	rctx->b.blit_decompress_depth = r600_blit_decompress_depth;
9883464ebd5Sriastradh}
989