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