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