Subversion Repositories Kolibri OS

Rev

Rev 4358 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4358 Serge 1
/*
2
 * Copyright 2010 Jerome Glisse 
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 "util/u_surface.h"
24
#include "util/u_blitter.h"
25
#include "util/u_format.h"
26
#include "radeonsi_pipe.h"
27
#include "si_state.h"
28
 
29
enum r600_blitter_op /* bitmask */
30
{
31
	R600_SAVE_TEXTURES      = 1,
32
	R600_SAVE_FRAMEBUFFER   = 2,
33
	R600_DISABLE_RENDER_COND = 4,
34
 
35
	R600_CLEAR         = 0,
36
 
37
	R600_CLEAR_SURFACE = R600_SAVE_FRAMEBUFFER,
38
 
39
	R600_COPY          = R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |
40
			     R600_DISABLE_RENDER_COND,
41
 
42
	R600_BLIT          = R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |
43
			     R600_DISABLE_RENDER_COND,
44
 
45
	R600_DECOMPRESS    = R600_SAVE_FRAMEBUFFER | R600_DISABLE_RENDER_COND,
46
};
47
 
48
static void r600_blitter_begin(struct pipe_context *ctx, enum r600_blitter_op op)
49
{
50
	struct r600_context *rctx = (struct r600_context *)ctx;
51
 
52
	r600_context_queries_suspend(rctx);
53
 
54
	util_blitter_save_blend(rctx->blitter, rctx->queued.named.blend);
55
	util_blitter_save_depth_stencil_alpha(rctx->blitter, rctx->queued.named.dsa);
56
	util_blitter_save_stencil_ref(rctx->blitter, &rctx->stencil_ref);
57
	util_blitter_save_rasterizer(rctx->blitter, rctx->queued.named.rasterizer);
58
	util_blitter_save_fragment_shader(rctx->blitter, rctx->ps_shader);
59
	util_blitter_save_vertex_shader(rctx->blitter, rctx->vs_shader);
60
	util_blitter_save_vertex_elements(rctx->blitter, rctx->vertex_elements);
61
	if (rctx->queued.named.viewport) {
62
		util_blitter_save_viewport(rctx->blitter, &rctx->queued.named.viewport->viewport);
63
	}
64
	util_blitter_save_vertex_buffer_slot(rctx->blitter, rctx->vertex_buffer);
65
	util_blitter_save_so_targets(rctx->blitter, rctx->num_so_targets,
66
				     (struct pipe_stream_output_target**)rctx->so_targets);
67
 
68
	if (op & R600_SAVE_FRAMEBUFFER)
69
		util_blitter_save_framebuffer(rctx->blitter, &rctx->framebuffer);
70
 
71
	if (op & R600_SAVE_TEXTURES) {
72
		util_blitter_save_fragment_sampler_states(
73
			rctx->blitter, rctx->ps_samplers.n_samplers,
74
			(void**)rctx->ps_samplers.samplers);
75
 
76
		util_blitter_save_fragment_sampler_views(
77
			rctx->blitter, rctx->ps_samplers.n_views,
78
			(struct pipe_sampler_view**)rctx->ps_samplers.views);
79
	}
80
 
81
	if ((op & R600_DISABLE_RENDER_COND) && rctx->current_render_cond) {
82
		rctx->saved_render_cond = rctx->current_render_cond;
83
		rctx->saved_render_cond_cond = rctx->current_render_cond_cond;
84
		rctx->saved_render_cond_mode = rctx->current_render_cond_mode;
85
		rctx->context.render_condition(&rctx->context, NULL, FALSE, 0);
86
	}
87
 
88
}
89
 
90
static void r600_blitter_end(struct pipe_context *ctx)
91
{
92
	struct r600_context *rctx = (struct r600_context *)ctx;
93
	if (rctx->saved_render_cond) {
94
		rctx->context.render_condition(&rctx->context,
95
					       rctx->saved_render_cond,
96
					       rctx->saved_render_cond_cond,
97
					       rctx->saved_render_cond_mode);
98
		rctx->saved_render_cond = NULL;
99
	}
100
	r600_context_queries_resume(rctx);
101
}
102
 
103
void si_blit_uncompress_depth(struct pipe_context *ctx,
104
		struct r600_resource_texture *texture,
105
		struct r600_resource_texture *staging,
106
		unsigned first_level, unsigned last_level,
107
		unsigned first_layer, unsigned last_layer)
108
{
109
	struct r600_context *rctx = (struct r600_context *)ctx;
110
	unsigned layer, level, checked_last_layer, max_layer;
111
	float depth = 1.0f;
112
	const struct util_format_description *desc;
113
	void *custom_dsa;
114
	struct r600_resource_texture *flushed_depth_texture = staging ?
115
			staging : texture->flushed_depth_texture;
116
 
117
	if (!staging && !texture->dirty_db_mask)
118
		return;
119
 
120
	desc = util_format_description(flushed_depth_texture->resource.b.b.format);
121
	switch (util_format_has_depth(desc) | util_format_has_stencil(desc) << 1) {
122
	default:
123
		assert(!"No depth or stencil to uncompress");
124
	case 3:
125
		custom_dsa = rctx->custom_dsa_flush_depth_stencil;
126
		break;
127
	case 2:
128
		custom_dsa = rctx->custom_dsa_flush_stencil;
129
		break;
130
	case 1:
131
		custom_dsa = rctx->custom_dsa_flush_depth;
132
		break;
133
	}
134
 
135
	for (level = first_level; level <= last_level; level++) {
136
		if (!staging && !(texture->dirty_db_mask & (1 << level)))
137
			continue;
138
 
139
		/* The smaller the mipmap level, the less layers there are
140
		 * as far as 3D textures are concerned. */
141
		max_layer = util_max_layer(&texture->resource.b.b, level);
142
		checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
143
 
144
		for (layer = first_layer; layer <= checked_last_layer; layer++) {
145
			struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
146
 
147
			surf_tmpl.format = texture->real_format;
148
			surf_tmpl.u.tex.level = level;
149
			surf_tmpl.u.tex.first_layer = layer;
150
			surf_tmpl.u.tex.last_layer = layer;
151
 
152
			zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
153
 
154
			surf_tmpl.format = flushed_depth_texture->real_format;
155
			cbsurf = ctx->create_surface(ctx,
156
					(struct pipe_resource*)flushed_depth_texture, &surf_tmpl);
157
 
158
			r600_blitter_begin(ctx, R600_DECOMPRESS);
159
			util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, ~0, custom_dsa, depth);
160
			r600_blitter_end(ctx);
161
 
162
			pipe_surface_reference(&zsurf, NULL);
163
			pipe_surface_reference(&cbsurf, NULL);
164
		}
165
 
166
		/* The texture will always be dirty if some layers aren't flushed.
167
		 * I don't think this case can occur though. */
168
		if (!staging && first_layer == 0 && last_layer == max_layer) {
169
			texture->dirty_db_mask &= ~(1 << level);
170
		}
171
	}
172
}
173
 
174
static void si_blit_decompress_depth_in_place(struct r600_context *rctx,
175
                                              struct r600_resource_texture *texture,
176
                                              unsigned first_level, unsigned last_level,
177
                                              unsigned first_layer, unsigned last_layer)
178
{
179
	struct pipe_surface *zsurf, surf_tmpl = {{0}};
180
	unsigned layer, max_layer, checked_last_layer, level;
181
 
182
	surf_tmpl.format = texture->resource.b.b.format;
183
 
184
	for (level = first_level; level <= last_level; level++) {
185
		if (!(texture->dirty_db_mask & (1 << level)))
186
			continue;
187
 
188
		surf_tmpl.u.tex.level = level;
189
 
190
		/* The smaller the mipmap level, the less layers there are
191
		 * as far as 3D textures are concerned. */
192
		max_layer = util_max_layer(&texture->resource.b.b, level);
193
		checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
194
 
195
		for (layer = first_layer; layer <= checked_last_layer; layer++) {
196
			surf_tmpl.u.tex.first_layer = layer;
197
			surf_tmpl.u.tex.last_layer = layer;
198
 
199
			zsurf = rctx->context.create_surface(&rctx->context, &texture->resource.b.b, &surf_tmpl);
200
 
201
			r600_blitter_begin(&rctx->context, R600_DECOMPRESS);
202
			util_blitter_custom_depth_stencil(rctx->blitter, zsurf, NULL, ~0,
203
							  rctx->custom_dsa_flush_inplace,
204
							  1.0f);
205
			r600_blitter_end(&rctx->context);
206
 
207
			pipe_surface_reference(&zsurf, NULL);
208
		}
209
 
210
		/* The texture will always be dirty if some layers aren't flushed.
211
		 * I don't think this case occurs often though. */
212
		if (first_layer == 0 && last_layer == max_layer) {
213
			texture->dirty_db_mask &= ~(1 << level);
214
		}
215
	}
216
}
217
 
218
void si_flush_depth_textures(struct r600_context *rctx,
219
			     struct r600_textures_info *textures)
220
{
221
	unsigned i;
222
 
223
	for (i = 0; i < textures->n_views; ++i) {
224
		struct pipe_sampler_view *view;
225
		struct r600_resource_texture *tex;
226
 
227
		view = &textures->views[i]->base;
228
		if (!view) continue;
229
 
230
		tex = (struct r600_resource_texture *)view->texture;
231
		if (!tex->is_depth || tex->is_flushing_texture)
232
			continue;
233
 
234
		si_blit_decompress_depth_in_place(rctx, tex,
235
						  view->u.tex.first_level, view->u.tex.last_level,
236
						  0, util_max_layer(&tex->resource.b.b, view->u.tex.first_level));
237
	}
238
}
239
 
240
static void r600_clear(struct pipe_context *ctx, unsigned buffers,
241
		       const union pipe_color_union *color,
242
		       double depth, unsigned stencil)
243
{
244
	struct r600_context *rctx = (struct r600_context *)ctx;
245
	struct pipe_framebuffer_state *fb = &rctx->framebuffer;
246
 
247
	r600_blitter_begin(ctx, R600_CLEAR);
248
	util_blitter_clear(rctx->blitter, fb->width, fb->height,
249
			   buffers, color, depth, stencil);
250
	r600_blitter_end(ctx);
251
}
252
 
253
static void r600_clear_render_target(struct pipe_context *ctx,
254
				     struct pipe_surface *dst,
255
				     const union pipe_color_union *color,
256
				     unsigned dstx, unsigned dsty,
257
				     unsigned width, unsigned height)
258
{
259
	struct r600_context *rctx = (struct r600_context *)ctx;
260
 
261
	r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
262
	util_blitter_clear_render_target(rctx->blitter, dst, color,
263
					 dstx, dsty, width, height);
264
	r600_blitter_end(ctx);
265
}
266
 
267
static void r600_clear_depth_stencil(struct pipe_context *ctx,
268
				     struct pipe_surface *dst,
269
				     unsigned clear_flags,
270
				     double depth,
271
				     unsigned stencil,
272
				     unsigned dstx, unsigned dsty,
273
				     unsigned width, unsigned height)
274
{
275
	struct r600_context *rctx = (struct r600_context *)ctx;
276
 
277
	r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
278
	util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
279
					 dstx, dsty, width, height);
280
	r600_blitter_end(ctx);
281
}
282
 
283
struct texture_orig_info {
284
	unsigned format;
285
	unsigned width0;
286
	unsigned height0;
287
	unsigned npix_x;
288
	unsigned npix_y;
289
	unsigned npix0_x;
290
	unsigned npix0_y;
291
};
292
 
293
static void r600_compressed_to_blittable(struct pipe_resource *tex,
294
				   unsigned level,
295
				   struct texture_orig_info *orig)
296
{
297
	struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
298
	unsigned pixsize = util_format_get_blocksize(rtex->real_format);
299
	int new_format;
300
	int new_height, new_width;
301
 
302
	orig->format = tex->format;
303
	orig->width0 = tex->width0;
304
	orig->height0 = tex->height0;
305
	orig->npix0_x = rtex->surface.level[0].npix_x;
306
	orig->npix0_y = rtex->surface.level[0].npix_y;
307
	orig->npix_x = rtex->surface.level[level].npix_x;
308
	orig->npix_y = rtex->surface.level[level].npix_y;
309
 
310
	if (pixsize == 8)
311
		new_format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
312
	else
313
		new_format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
314
 
315
	new_width = util_format_get_nblocksx(tex->format, orig->width0);
316
	new_height = util_format_get_nblocksy(tex->format, orig->height0);
317
 
318
	tex->width0 = new_width;
319
	tex->height0 = new_height;
320
	tex->format = new_format;
321
	rtex->surface.level[0].npix_x = util_format_get_nblocksx(orig->format, orig->npix0_x);
322
	rtex->surface.level[0].npix_y = util_format_get_nblocksy(orig->format, orig->npix0_y);
323
	rtex->surface.level[level].npix_x = util_format_get_nblocksx(orig->format, orig->npix_x);
324
	rtex->surface.level[level].npix_y = util_format_get_nblocksy(orig->format, orig->npix_y);
4401 Serge 325
 
326
	/* By dividing the dimensions by 4, we effectively decrement
327
	 * last_level by 2, therefore the last 2 mipmap levels disappear and
328
	 * aren't blittable. Note that the last 3 mipmap levels (4x4, 2x2,
329
	 * 1x1) have equal slice sizes, which is an important assumption
330
	 * for this to work.
331
	 *
332
	 * In order to make the last 2 mipmap levels blittable, we have to
333
	 * add the slice size of the last mipmap level to the texture
334
	 * address, so that even though the hw thinks it reads last_level-2,
335
	 * it will actually read last_level-1, and if we add the slice size*2,
336
	 * it will read last_level. That's how this workaround works.
337
	 */
338
	if (level > rtex->resource.b.b.last_level-2)
339
		rtex->mipmap_shift = level - (rtex->resource.b.b.last_level-2);
4358 Serge 340
}
341
 
342
static void r600_change_format(struct pipe_resource *tex,
343
			       unsigned level,
344
			       struct texture_orig_info *orig,
345
			       enum pipe_format format)
346
{
347
	struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
348
 
349
	orig->format = tex->format;
350
	orig->width0 = tex->width0;
351
	orig->height0 = tex->height0;
352
	orig->npix0_x = rtex->surface.level[0].npix_x;
353
	orig->npix0_y = rtex->surface.level[0].npix_y;
354
	orig->npix_x = rtex->surface.level[level].npix_x;
355
	orig->npix_y = rtex->surface.level[level].npix_y;
356
 
357
	tex->format = format;
358
}
359
 
360
static void r600_reset_blittable_to_orig(struct pipe_resource *tex,
361
					 unsigned level,
362
					 struct texture_orig_info *orig)
363
{
364
	struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
365
 
366
	tex->format = orig->format;
367
	tex->width0 = orig->width0;
368
	tex->height0 = orig->height0;
369
	rtex->surface.level[0].npix_x = orig->npix0_x;
370
	rtex->surface.level[0].npix_y = orig->npix0_y;
371
	rtex->surface.level[level].npix_x = orig->npix_x;
372
	rtex->surface.level[level].npix_y = orig->npix_y;
4401 Serge 373
	rtex->mipmap_shift = 0;
4358 Serge 374
}
375
 
376
static void r600_resource_copy_region(struct pipe_context *ctx,
377
				      struct pipe_resource *dst,
378
				      unsigned dst_level,
379
				      unsigned dstx, unsigned dsty, unsigned dstz,
380
				      struct pipe_resource *src,
381
				      unsigned src_level,
382
				      const struct pipe_box *src_box)
383
{
384
	struct r600_context *rctx = (struct r600_context *)ctx;
385
	struct r600_resource_texture *rsrc = (struct r600_resource_texture*)src;
386
	struct texture_orig_info orig_info[2];
387
	struct pipe_box sbox;
388
	const struct pipe_box *psbox = src_box;
389
	boolean restore_orig[2];
390
 
391
	memset(orig_info, 0, sizeof(orig_info));
392
 
393
	/* Fallback for buffers. */
394
	if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
395
		util_resource_copy_region(ctx, dst, dst_level, dstx, dsty, dstz,
396
                                          src, src_level, src_box);
397
		return;
398
	}
399
 
400
	/* This must be done before entering u_blitter to avoid recursion. */
401
	if (rsrc->is_depth && !rsrc->is_flushing_texture) {
402
		si_blit_decompress_depth_in_place(rctx, rsrc,
403
						  src_level, src_level,
404
						  src_box->z, src_box->z + src_box->depth - 1);
405
	}
406
 
407
	restore_orig[0] = restore_orig[1] = FALSE;
408
 
409
	if (util_format_is_compressed(src->format) &&
410
	    util_format_is_compressed(dst->format)) {
411
		r600_compressed_to_blittable(src, src_level, &orig_info[0]);
412
		restore_orig[0] = TRUE;
413
		sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
414
		sbox.y = util_format_get_nblocksy(orig_info[0].format, src_box->y);
415
		sbox.z = src_box->z;
416
		sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
417
		sbox.height = util_format_get_nblocksy(orig_info[0].format, src_box->height);
418
		sbox.depth = src_box->depth;
419
		psbox=&sbox;
420
 
421
		r600_compressed_to_blittable(dst, dst_level, &orig_info[1]);
422
		restore_orig[1] = TRUE;
423
		/* translate the dst box as well */
424
		dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
425
		dsty = util_format_get_nblocksy(orig_info[1].format, dsty);
426
	} else if (!util_blitter_is_copy_supported(rctx->blitter, dst, src,
427
						   PIPE_MASK_RGBAZS)) {
428
		unsigned blocksize = util_format_get_blocksize(src->format);
429
 
430
		switch (blocksize) {
431
		case 1:
432
			r600_change_format(src, src_level, &orig_info[0],
433
					   PIPE_FORMAT_R8_UNORM);
434
			r600_change_format(dst, dst_level, &orig_info[1],
435
					   PIPE_FORMAT_R8_UNORM);
436
			break;
437
		case 2:
438
			r600_change_format(src, src_level, &orig_info[0],
439
					   PIPE_FORMAT_R8G8_UNORM);
440
			r600_change_format(dst, dst_level, &orig_info[1],
441
					   PIPE_FORMAT_R8G8_UNORM);
442
			break;
443
		case 4:
444
			r600_change_format(src, src_level, &orig_info[0],
445
					   PIPE_FORMAT_R8G8B8A8_UNORM);
446
			r600_change_format(dst, dst_level, &orig_info[1],
447
					   PIPE_FORMAT_R8G8B8A8_UNORM);
448
			break;
449
		case 8:
450
			r600_change_format(src, src_level, &orig_info[0],
451
					   PIPE_FORMAT_R16G16B16A16_UINT);
452
			r600_change_format(dst, dst_level, &orig_info[1],
453
					   PIPE_FORMAT_R16G16B16A16_UINT);
454
			break;
455
		case 16:
456
			r600_change_format(src, src_level, &orig_info[0],
457
					   PIPE_FORMAT_R32G32B32A32_UINT);
458
			r600_change_format(dst, dst_level, &orig_info[1],
459
					   PIPE_FORMAT_R32G32B32A32_UINT);
460
			break;
461
		default:
462
			fprintf(stderr, "Unhandled format %s with blocksize %u\n",
463
				util_format_short_name(src->format), blocksize);
464
			assert(0);
465
		}
466
		restore_orig[0] = TRUE;
467
		restore_orig[1] = TRUE;
468
	}
469
 
470
	r600_blitter_begin(ctx, R600_COPY);
471
	util_blitter_copy_texture(rctx->blitter, dst, dst_level, dstx, dsty, dstz,
472
				  src, src_level, psbox, PIPE_MASK_RGBAZS, TRUE);
473
	r600_blitter_end(ctx);
474
 
475
	if (restore_orig[0])
476
		r600_reset_blittable_to_orig(src, src_level, &orig_info[0]);
477
 
478
	if (restore_orig[1])
479
		r600_reset_blittable_to_orig(dst, dst_level, &orig_info[1]);
480
}
481
 
482
static void si_blit(struct pipe_context *ctx,
483
                      const struct pipe_blit_info *info)
484
{
485
	struct r600_context *rctx = (struct r600_context*)ctx;
486
	struct r600_resource_texture *rsrc = (struct r600_resource_texture*)info->src.resource;
487
 
488
	assert(util_blitter_is_blit_supported(rctx->blitter, info));
489
 
490
	if (info->src.resource->nr_samples > 1 &&
491
	    info->dst.resource->nr_samples <= 1 &&
492
	    !util_format_is_depth_or_stencil(info->src.resource->format) &&
493
	    !util_format_is_pure_integer(info->src.resource->format)) {
494
		debug_printf("radeonsi: color resolve is unimplemented\n");
495
		return;
496
	}
497
 
498
	if (rsrc->is_depth && !rsrc->is_flushing_texture) {
499
		si_blit_decompress_depth_in_place(rctx, rsrc,
500
						  info->src.level, info->src.level,
501
						  info->src.box.z,
502
						  info->src.box.z + info->src.box.depth - 1);
503
	}
504
 
505
	r600_blitter_begin(ctx, R600_BLIT);
506
	util_blitter_blit(rctx->blitter, info);
507
	r600_blitter_end(ctx);
508
}
509
 
510
void si_init_blit_functions(struct r600_context *rctx)
511
{
512
	rctx->context.clear = r600_clear;
513
	rctx->context.clear_render_target = r600_clear_render_target;
514
	rctx->context.clear_depth_stencil = r600_clear_depth_stencil;
515
	rctx->context.resource_copy_region = r600_resource_copy_region;
516
	rctx->context.blit = si_blit;
517
}